LNGameMateManager.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. //
  2. // LNGameMateManager.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/20.
  6. //
  7. import Foundation
  8. protocol LNGameMateManagerNotify {
  9. func onGameMateManagerInfoChanged()
  10. func onGameTypesChanged()
  11. func onAutoReplyCreated(item: LNAutoReplyVO)
  12. func onAutoReplyDeleted(id: String)
  13. func onAutoReplyModified(item: LNAutoReplyVO)
  14. }
  15. extension LNGameMateManagerNotify {
  16. func onGameMateManagerInfoChanged() { }
  17. func onGameTypesChanged() { }
  18. func onAutoReplyCreated(item: LNAutoReplyVO) { }
  19. func onAutoReplyDeleted(id: String) { }
  20. func onAutoReplyModified(item: LNAutoReplyVO) { }
  21. }
  22. var myGameMateInfo: LNGameMateManagerInfo? {
  23. LNGameMateManager.shared.myGameMateInfo
  24. }
  25. class LNGameMateManager {
  26. static let shared = LNGameMateManager()
  27. private(set) var curGameTypes: [LNGameTypeItemVO] = []
  28. private(set) var gameFilterMap: [String: [LNSkillFilterField]] = [:]
  29. private(set) var myGameMateInfo: LNGameMateManagerInfo?
  30. static let gameSelectionMaxCount = 5
  31. static let AutoReplayMaxInput = 500
  32. func gameCategory(for code: String) -> LNGameCategoryItemVO? {
  33. for type in curGameTypes {
  34. if let item = type.children.first(where: { $0.code == code }) {
  35. return item
  36. }
  37. }
  38. return nil
  39. }
  40. func gameFilter(for code: String) -> [LNSkillFilterField] {
  41. gameFilterMap[code] ?? []
  42. }
  43. private init() {
  44. LNEventDeliver.addObserver(self)
  45. }
  46. }
  47. // MARK: 陪玩师信息拉取
  48. extension LNGameMateManager {
  49. func getUserSkills(uid: String, queue: DispatchQueue = .main,
  50. handler: @escaping ([LNGameMateSkillVO]?) -> Void) {
  51. LNHttpManager.shared.getUserSkills(uid: uid) { res, err in
  52. guard err == nil, let res else {
  53. queue.asyncIfNotGlobal {
  54. handler(nil)
  55. }
  56. return
  57. }
  58. queue.asyncIfNotGlobal {
  59. handler(res.list)
  60. }
  61. }
  62. }
  63. func getSkillDetail(skillId: String, queue: DispatchQueue = .main,
  64. handler: @escaping (LNGameMateSkillDetailVO?) -> Void) {
  65. LNHttpManager.shared.getSkillDetail(skillId: skillId) { res, err in
  66. guard err == nil, let res else {
  67. queue.asyncIfNotGlobal {
  68. handler(nil)
  69. }
  70. return
  71. }
  72. queue.asyncIfNotGlobal {
  73. handler(res)
  74. }
  75. }
  76. }
  77. func scoreGameMate(uid: String, score: Int, queue: DispatchQueue = .main,
  78. handler: @escaping (Bool) -> Void) {
  79. LNHttpManager.shared.scoreGameMate(uid: uid, score: score) { err in
  80. queue.asyncIfNotGlobal {
  81. handler(err == nil)
  82. }
  83. }
  84. }
  85. func getSkillCommentList(id: String, next: String? = nil,
  86. queue: DispatchQueue = .main,
  87. handler: @escaping (LNSkillCommentListResponse?) -> Void) {
  88. LNHttpManager.shared.getSkillCommentList(id: id, size: 30, next: next ?? "") { res, err in
  89. queue.asyncIfNotGlobal {
  90. handler(res)
  91. }
  92. if let err {
  93. showToast(err.errorDesc)
  94. }
  95. }
  96. }
  97. func getGameMateList(
  98. topCategory: String, category: String?, filter: LNGameMateFilter, size: Int,
  99. next: String?, queue: DispatchQueue = .main,
  100. handler: @escaping ([LNGameMateListItemVO]?, String?) -> Void) {
  101. LNHttpManager.shared.getGameMateList(
  102. topCategory: topCategory, category: category,
  103. filter: filter, size: size, next: next ?? "")
  104. { res, err in
  105. queue.asyncIfNotGlobal {
  106. handler(res?.list, res?.next)
  107. }
  108. if let err {
  109. showToast(err.errorDesc)
  110. }
  111. }
  112. LNRoomManager.shared.reloadHallEntrance()
  113. }
  114. func searchGameMate(keyword: String, next: String, queue: DispatchQueue = .main,
  115. handler: @escaping ([LNGameMateSearchResultVO]?, String?) -> Void) {
  116. LNHttpManager.shared.searchGameMate(keyword: keyword, size: 30, next: next) { res, err in
  117. queue.asyncIfNotGlobal {
  118. handler(res?.list, res?.next)
  119. }
  120. if let err {
  121. showToast(err.errorDesc)
  122. }
  123. }
  124. }
  125. func mixSearch(keyword: String, queue: DispatchQueue = .main,
  126. handler: @escaping (LNMixSearchResponse?) -> Void) {
  127. LNHttpManager.shared.mixSearch(keyword: keyword) { res, err in
  128. queue.asyncIfNotGlobal {
  129. handler(res)
  130. }
  131. if let err {
  132. showToast(err.errorDesc)
  133. }
  134. }
  135. }
  136. func searchRoom(keyword: String, next: String?, queue: DispatchQueue = .main,
  137. handler: @escaping (LNSearchRoomResponse?) -> Void) {
  138. LNHttpManager.shared.searchRoom(keyword: keyword, size: 30, next: next ?? "") { res, err in
  139. queue.asyncIfNotGlobal {
  140. handler(res)
  141. }
  142. if let err {
  143. showToast(err.errorDesc)
  144. }
  145. }
  146. }
  147. }
  148. // MARK: 陪玩师类别
  149. extension LNGameMateManager {
  150. func getGameTypeList(queue: DispatchQueue = .main,
  151. handler: @escaping ([LNGameTypeItemVO]?) -> Void) {
  152. LNHttpManager.shared.getGameCategories { [weak self] res, err in
  153. guard let self else { return }
  154. guard err == nil, let res else {
  155. queue.asyncIfNotGlobal {
  156. handler(nil)
  157. }
  158. return
  159. }
  160. reloadGameFilterConfig()
  161. curGameTypes = res.list
  162. queue.asyncIfNotGlobal {
  163. handler(res.list)
  164. }
  165. notifyGameTypesChanged()
  166. }
  167. }
  168. func reloadGameFilterConfig() {
  169. LNHttpManager.shared.getGameFilterConfig { [weak self] list, err in
  170. guard let self else { return }
  171. guard let list else { return }
  172. gameFilterMap.removeAll()
  173. for item in list.list {
  174. gameFilterMap[item.code] = item.filterFields
  175. }
  176. }
  177. }
  178. }
  179. // MARK: 陪玩师入驻
  180. extension LNGameMateManager {
  181. func getJoinGameMateInfo(queue: DispatchQueue = .main,
  182. handler: @escaping (LNJoinUsInputInfosResponse?) -> Void) {
  183. LNHttpManager.shared.getJoinGameMateInfo { res, err in
  184. queue.asyncIfNotGlobal {
  185. handler(res)
  186. }
  187. if let err {
  188. showToast(err.errorDesc)
  189. }
  190. }
  191. }
  192. func setJoinGameMateBaseInfo(info: LNJoinUsBaseInfoVO, queue: DispatchQueue = .main,
  193. handler: @escaping (Bool) -> Void) {
  194. LNHttpManager.shared.setJoinGameMateBaseInfo(info: info) { res, err in
  195. queue.asyncIfNotGlobal {
  196. handler(err == nil)
  197. }
  198. if let err {
  199. showToast(err.errorDesc)
  200. }
  201. }
  202. }
  203. func getCreateSkillFields(id: String, queue: DispatchQueue = .main, handler: @escaping (LNCreateSkillInputFieldsVO?) -> Void) {
  204. LNHttpManager.shared.getCreateSkillFields(id: id) { res, err in
  205. queue.asyncIfNotGlobal {
  206. if let res {
  207. guard !res.fields.isEmpty else {
  208. showToast(.init(key: "B00060"))
  209. handler(nil)
  210. return
  211. }
  212. guard res.isAvailable else {
  213. showToast(.init(key: "B00058"))
  214. handler(nil)
  215. return
  216. }
  217. }
  218. handler(res)
  219. }
  220. if let err {
  221. showToast(err.errorDesc)
  222. }
  223. }
  224. }
  225. func createSkill(info: LNCreateSkillFieldsInfo,
  226. queue: DispatchQueue = .main,
  227. handler: @escaping (Bool) -> Void)
  228. {
  229. LNHttpManager.shared.createSkill(info: info) { err in
  230. queue.asyncIfNotGlobal {
  231. handler(err == nil)
  232. }
  233. if let err {
  234. showToast(err.errorDesc)
  235. }
  236. }
  237. }
  238. }
  239. // MARK: 陪玩师管理
  240. extension LNGameMateManager {
  241. func getGameMateManagerInfo(
  242. queue: DispatchQueue = .main,
  243. handler: ((LNGameMateManagerInfo?) -> Void)? = nil)
  244. {
  245. LNHttpManager.shared.getGameMateManagerInfo { [weak self] info, err in
  246. queue.asyncIfNotGlobal {
  247. handler?(info)
  248. }
  249. if let err {
  250. showToast(err.errorDesc)
  251. } else {
  252. guard let self else { return }
  253. myGameMateInfo = info
  254. notifyUserGameMateManagerInfoChanged()
  255. }
  256. }
  257. }
  258. func enableGameMate(open: Bool,
  259. queue: DispatchQueue = .main,
  260. handler: @escaping (Bool) -> Void) {
  261. LNHttpManager.shared.enableGameMate(open: open) { [weak self] err in
  262. queue.asyncIfNotGlobal {
  263. handler(err == nil)
  264. }
  265. if let err {
  266. showToast(err.errorDesc)
  267. } else {
  268. guard let self else { return }
  269. getGameMateManagerInfo()
  270. }
  271. }
  272. }
  273. func getOrderAccetpConfig(
  274. queue: DispatchQueue = .main,
  275. handler: @escaping (LNOrderAcceptConfig?) -> Void)
  276. {
  277. LNHttpManager.shared.getOrderAccetpConfig { res, err in
  278. queue.asyncIfNotGlobal {
  279. handler(res)
  280. }
  281. if let err {
  282. showToast(err.errorDesc)
  283. }
  284. }
  285. }
  286. func setOrderAcceptConfig(
  287. config: LNOrderAcceptConfig,
  288. queue: DispatchQueue = .main,
  289. handler: @escaping (Bool) -> Void)
  290. {
  291. LNHttpManager.shared.setOrderAcceptConfig(config: config) { err in
  292. queue.asyncIfNotGlobal {
  293. handler(err == nil)
  294. }
  295. if let err {
  296. showToast(err.errorDesc)
  297. }
  298. }
  299. }
  300. func getMySkillList(
  301. queue: DispatchQueue = .main,
  302. handler: @escaping (LNMySkillListResponse?) -> Void)
  303. {
  304. LNHttpManager.shared.getMySkillList { res, err in
  305. queue.asyncIfNotGlobal {
  306. handler(res)
  307. }
  308. if let err {
  309. showToast(err.errorDesc)
  310. }
  311. }
  312. }
  313. func getSkillSwitchInfo(id: String,
  314. queue: DispatchQueue = .main,
  315. handler: @escaping (LNSkillSwitchResponse?) -> Void) {
  316. LNHttpManager.shared.getSkillSwitchInfo(id: id) { res, err in
  317. queue.asyncIfNotGlobal {
  318. handler(res)
  319. }
  320. if let err {
  321. showToast(err.errorDesc)
  322. }
  323. }
  324. }
  325. func enableSkill(skillId: String, open: Bool, mainSkill: Bool? = nil,
  326. queue: DispatchQueue = .main,
  327. handler: @escaping (Bool) -> Void) {
  328. LNHttpManager.shared.enableSkill(skillId: skillId, open: open, mainSkill: mainSkill) { err in
  329. queue.asyncIfNotGlobal {
  330. handler(err == nil)
  331. }
  332. if let err {
  333. showToast(err.errorDesc)
  334. } else {
  335. LNProfileManager.shared.reloadMyProfileDetail()
  336. }
  337. }
  338. }
  339. func getVisitorsList(next: String? = nil,
  340. queue: DispatchQueue = .main,
  341. handler: @escaping ([LNVisitorItemVO]?, String?) -> Void) {
  342. LNHttpManager.shared.getVisitorsList(next: next ?? "", size: 30) { res, err in
  343. queue.asyncIfNotGlobal {
  344. handler(res?.list, res?.next)
  345. }
  346. if let err {
  347. showToast(err.errorDesc)
  348. }
  349. }
  350. }
  351. func getSkillEditFields(id: String,
  352. queue: DispatchQueue = .main,
  353. handler: @escaping (LNSkillEditFieldsResponse?) -> Void) {
  354. LNHttpManager.shared.getSkillEditFields(id: id) { res, err in
  355. queue.asyncIfNotGlobal {
  356. handler(res)
  357. }
  358. if let err {
  359. showToast(err.errorDesc)
  360. }
  361. }
  362. }
  363. func commitSkillEdit(info: LNEditSkillFieldsInfo,
  364. queue: DispatchQueue = .main,
  365. handler: @escaping (Bool) -> Void) {
  366. LNHttpManager.shared.commitSkillEdit(info: info) { err in
  367. queue.asyncIfNotGlobal {
  368. handler(err == nil)
  369. }
  370. if let err {
  371. showToast(err.errorDesc)
  372. }
  373. }
  374. }
  375. }
  376. // MARK: 欢迎语(自动回复)
  377. extension LNGameMateManager {
  378. func enableAutoReplay(on: Bool, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  379. LNHttpManager.shared.enableAutoReplay(on: on) { err in
  380. queue.asyncIfNotGlobal {
  381. handler(err == nil)
  382. }
  383. if let err {
  384. showToast(err.errorDesc)
  385. }
  386. }
  387. }
  388. func getAutoReplayList(queue: DispatchQueue = .main, handler: @escaping (LNAutoReplyResponse?) -> Void) {
  389. LNHttpManager.shared.getAutoReplayList { list, err in
  390. queue.asyncIfNotGlobal {
  391. handler(list)
  392. }
  393. if let err {
  394. showToast(err.errorDesc)
  395. }
  396. }
  397. }
  398. func deleteAutoReplay(id: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  399. LNHttpManager.shared.deleteAutoReplay(id: id) { err in
  400. queue.asyncIfNotGlobal {
  401. handler(err == nil)
  402. }
  403. if let err {
  404. showToast(err.errorDesc)
  405. } else {
  406. Self.notifyAutoReplayDeleted(id: id)
  407. }
  408. }
  409. }
  410. func createAutoReplay(item: LNAutoReplyVO, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  411. LNHttpManager.shared.createAutoReplay(item: item) { err in
  412. queue.asyncIfNotGlobal {
  413. handler(err == nil)
  414. }
  415. if let err {
  416. showToast(err.errorDesc)
  417. } else {
  418. Self.notifyAutoReplayCreated(item: item)
  419. }
  420. }
  421. }
  422. func updateAutoReplay(item: LNAutoReplyVO, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  423. LNHttpManager.shared.updateAutoReplay(item: item) { err in
  424. queue.asyncIfNotGlobal {
  425. handler(err == nil)
  426. }
  427. if let err {
  428. showToast(err.errorDesc)
  429. } else {
  430. Self.notifyAutoReplayModified(item: item)
  431. }
  432. }
  433. }
  434. func triggerAutoReplayIfNeed(uid: String) {
  435. LNHttpManager.shared.triggerAutoReplayIfNeed(uid: uid) { _ in }
  436. }
  437. func markUseAutoReplay(uid: String) {
  438. LNHttpManager.shared.markUseAutoReplay(uid: uid) { _ in }
  439. }
  440. func fetchAutoReplyQuota(uid: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  441. LNHttpManager.shared.fetchAutoReplyQuota(uid: uid) { res, err in
  442. queue.asyncIfNotGlobal {
  443. if let err {
  444. showToast(err.errorDesc)
  445. handler(false)
  446. } else if let res {
  447. if res.playmateQuotaCount > 0 && res.userQuotaCount > 0 {
  448. handler(true)
  449. } else {
  450. showToast(.init(key: "A00350"))
  451. handler(false)
  452. }
  453. } else {
  454. handler(false)
  455. }
  456. }
  457. }
  458. }
  459. }
  460. // MARK: 潜在用户
  461. extension LNGameMateManager {
  462. func getPotentialUsers(queue: DispatchQueue = .main, handler: @escaping (LNPotentialUsersResponse?) -> Void) {
  463. LNHttpManager.shared.getPotentialUsers { res, err in
  464. queue.asyncIfNotGlobal {
  465. handler(res)
  466. }
  467. if let err {
  468. showToast(err.errorDesc)
  469. }
  470. }
  471. }
  472. }
  473. extension LNGameMateManager: LNProfileManagerNotify {
  474. func onUserInfoChanged(userInfo: LNUserProfileVO) {
  475. guard userInfo.userNo.isMyUid else { return }
  476. if userInfo.playmate {
  477. getGameMateManagerInfo()
  478. }
  479. }
  480. }
  481. extension LNGameMateManager {
  482. private func notifyUserGameMateManagerInfoChanged() {
  483. LNEventDeliver.notifyEvent { ($0 as? LNGameMateManagerNotify)?.onGameMateManagerInfoChanged() }
  484. }
  485. private func notifyGameTypesChanged() {
  486. LNEventDeliver.notifyEvent {
  487. ($0 as? LNGameMateManagerNotify)?.onGameTypesChanged()
  488. }
  489. }
  490. private static func notifyAutoReplayCreated(item: LNAutoReplyVO) {
  491. LNEventDeliver.notifyEvent {
  492. ($0 as? LNGameMateManagerNotify)?.onAutoReplyCreated(item: item)
  493. }
  494. }
  495. private static func notifyAutoReplayDeleted(id: String) {
  496. LNEventDeliver.notifyEvent {
  497. ($0 as? LNGameMateManagerNotify)?.onAutoReplyDeleted(id: id)
  498. }
  499. }
  500. private static func notifyAutoReplayModified(item: LNAutoReplyVO) {
  501. LNEventDeliver.notifyEvent {
  502. ($0 as? LNGameMateManagerNotify)?.onAutoReplyModified(item: item)
  503. }
  504. }
  505. }