LNGameMateManager.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. }
  137. // MARK: 陪玩师类别
  138. extension LNGameMateManager {
  139. func getGameTypeList(queue: DispatchQueue = .main,
  140. handler: @escaping ([LNGameTypeItemVO]?) -> Void) {
  141. LNHttpManager.shared.getGameCategories { [weak self] res, err in
  142. guard let self else { return }
  143. guard err == nil, let res else {
  144. queue.asyncIfNotGlobal {
  145. handler(nil)
  146. }
  147. return
  148. }
  149. reloadGameFilterConfig()
  150. curGameTypes = res.list
  151. queue.asyncIfNotGlobal {
  152. handler(res.list)
  153. }
  154. notifyGameTypesChanged()
  155. }
  156. }
  157. func reloadGameFilterConfig() {
  158. LNHttpManager.shared.getGameFilterConfig { [weak self] list, err in
  159. guard let self else { return }
  160. guard let list else { return }
  161. gameFilterMap.removeAll()
  162. for item in list.list {
  163. gameFilterMap[item.code] = item.filterFields
  164. }
  165. }
  166. }
  167. }
  168. // MARK: 陪玩师入驻
  169. extension LNGameMateManager {
  170. func getJoinGameMateInfo(queue: DispatchQueue = .main,
  171. handler: @escaping (LNJoinUsInputInfosResponse?) -> Void) {
  172. LNHttpManager.shared.getJoinGameMateInfo { res, err in
  173. queue.asyncIfNotGlobal {
  174. handler(res)
  175. }
  176. if let err {
  177. showToast(err.errorDesc)
  178. }
  179. }
  180. }
  181. func setJoinGameMateBaseInfo(info: LNJoinUsBaseInfoVO, queue: DispatchQueue = .main,
  182. handler: @escaping (Bool) -> Void) {
  183. LNHttpManager.shared.setJoinGameMateBaseInfo(info: info) { res, err in
  184. queue.asyncIfNotGlobal {
  185. handler(err == nil)
  186. }
  187. if let err {
  188. showToast(err.errorDesc)
  189. }
  190. }
  191. }
  192. func getCreateSkillFields(id: String, queue: DispatchQueue = .main, handler: @escaping (LNCreateSkillInputFieldsVO?) -> Void) {
  193. LNHttpManager.shared.getCreateSkillFields(id: id) { res, err in
  194. queue.asyncIfNotGlobal {
  195. if let res {
  196. guard !res.fields.isEmpty else {
  197. showToast(.init(key: "B00060"))
  198. handler(nil)
  199. return
  200. }
  201. guard res.isAvailable else {
  202. showToast(.init(key: "B00058"))
  203. handler(nil)
  204. return
  205. }
  206. }
  207. handler(res)
  208. }
  209. if let err {
  210. showToast(err.errorDesc)
  211. }
  212. }
  213. }
  214. func createSkill(info: LNCreateSkillFieldsInfo,
  215. queue: DispatchQueue = .main,
  216. handler: @escaping (Bool) -> Void)
  217. {
  218. LNHttpManager.shared.createSkill(info: info) { err in
  219. queue.asyncIfNotGlobal {
  220. handler(err == nil)
  221. }
  222. if let err {
  223. showToast(err.errorDesc)
  224. }
  225. }
  226. }
  227. }
  228. // MARK: 陪玩师管理
  229. extension LNGameMateManager {
  230. func getGameMateManagerInfo(
  231. queue: DispatchQueue = .main,
  232. handler: ((LNGameMateManagerInfo?) -> Void)? = nil)
  233. {
  234. LNHttpManager.shared.getGameMateManagerInfo { [weak self] info, err in
  235. queue.asyncIfNotGlobal {
  236. handler?(info)
  237. }
  238. if let err {
  239. showToast(err.errorDesc)
  240. } else {
  241. guard let self else { return }
  242. myGameMateInfo = info
  243. notifyUserGameMateManagerInfoChanged()
  244. }
  245. }
  246. }
  247. func enableGameMate(open: Bool,
  248. queue: DispatchQueue = .main,
  249. handler: @escaping (Bool) -> Void) {
  250. LNHttpManager.shared.enableGameMate(open: open) { [weak self] err in
  251. queue.asyncIfNotGlobal {
  252. handler(err == nil)
  253. }
  254. if let err {
  255. showToast(err.errorDesc)
  256. } else {
  257. guard let self else { return }
  258. getGameMateManagerInfo()
  259. }
  260. }
  261. }
  262. func getOrderAccetpConfig(
  263. queue: DispatchQueue = .main,
  264. handler: @escaping (LNOrderAcceptConfig?) -> Void)
  265. {
  266. LNHttpManager.shared.getOrderAccetpConfig { res, err in
  267. queue.asyncIfNotGlobal {
  268. handler(res)
  269. }
  270. if let err {
  271. showToast(err.errorDesc)
  272. }
  273. }
  274. }
  275. func setOrderAcceptConfig(
  276. config: LNOrderAcceptConfig,
  277. queue: DispatchQueue = .main,
  278. handler: @escaping (Bool) -> Void)
  279. {
  280. LNHttpManager.shared.setOrderAcceptConfig(config: config) { err in
  281. queue.asyncIfNotGlobal {
  282. handler(err == nil)
  283. }
  284. if let err {
  285. showToast(err.errorDesc)
  286. }
  287. }
  288. }
  289. func getMySkillList(
  290. queue: DispatchQueue = .main,
  291. handler: @escaping (LNMySkillListResponse?) -> Void)
  292. {
  293. LNHttpManager.shared.getMySkillList { res, err in
  294. queue.asyncIfNotGlobal {
  295. handler(res)
  296. }
  297. if let err {
  298. showToast(err.errorDesc)
  299. }
  300. }
  301. }
  302. func getSkillSwitchInfo(id: String,
  303. queue: DispatchQueue = .main,
  304. handler: @escaping (LNSkillSwitchResponse?) -> Void) {
  305. LNHttpManager.shared.getSkillSwitchInfo(id: id) { res, err in
  306. queue.asyncIfNotGlobal {
  307. handler(res)
  308. }
  309. if let err {
  310. showToast(err.errorDesc)
  311. }
  312. }
  313. }
  314. func enableSkill(skillId: String, open: Bool, mainSkill: Bool? = nil,
  315. queue: DispatchQueue = .main,
  316. handler: @escaping (Bool) -> Void) {
  317. LNHttpManager.shared.enableSkill(skillId: skillId, open: open, mainSkill: mainSkill) { err in
  318. queue.asyncIfNotGlobal {
  319. handler(err == nil)
  320. }
  321. if let err {
  322. showToast(err.errorDesc)
  323. } else {
  324. LNProfileManager.shared.reloadMyProfileDetail()
  325. }
  326. }
  327. }
  328. func getVisitorsList(next: String? = nil,
  329. queue: DispatchQueue = .main,
  330. handler: @escaping ([LNVisitorItemVO]?, String?) -> Void) {
  331. LNHttpManager.shared.getVisitorsList(next: next ?? "", size: 30) { res, err in
  332. queue.asyncIfNotGlobal {
  333. handler(res?.list, res?.next)
  334. }
  335. if let err {
  336. showToast(err.errorDesc)
  337. }
  338. }
  339. }
  340. func getSkillEditFields(id: String,
  341. queue: DispatchQueue = .main,
  342. handler: @escaping (LNSkillEditFieldsResponse?) -> Void) {
  343. LNHttpManager.shared.getSkillEditFields(id: id) { res, err in
  344. queue.asyncIfNotGlobal {
  345. handler(res)
  346. }
  347. if let err {
  348. showToast(err.errorDesc)
  349. }
  350. }
  351. }
  352. func commitSkillEdit(info: LNEditSkillFieldsInfo,
  353. queue: DispatchQueue = .main,
  354. handler: @escaping (Bool) -> Void) {
  355. LNHttpManager.shared.commitSkillEdit(info: info) { err in
  356. queue.asyncIfNotGlobal {
  357. handler(err == nil)
  358. }
  359. if let err {
  360. showToast(err.errorDesc)
  361. }
  362. }
  363. }
  364. }
  365. // MARK: 欢迎语(自动回复)
  366. extension LNGameMateManager {
  367. func enableAutoReplay(on: Bool, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  368. LNHttpManager.shared.enableAutoReplay(on: on) { err in
  369. queue.asyncIfNotGlobal {
  370. handler(err == nil)
  371. }
  372. if let err {
  373. showToast(err.errorDesc)
  374. }
  375. }
  376. }
  377. func getAutoReplayList(queue: DispatchQueue = .main, handler: @escaping (LNAutoReplyResponse?) -> Void) {
  378. LNHttpManager.shared.getAutoReplayList { list, err in
  379. queue.asyncIfNotGlobal {
  380. handler(list)
  381. }
  382. if let err {
  383. showToast(err.errorDesc)
  384. }
  385. }
  386. }
  387. func deleteAutoReplay(id: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  388. LNHttpManager.shared.deleteAutoReplay(id: id) { err in
  389. queue.asyncIfNotGlobal {
  390. handler(err == nil)
  391. }
  392. if let err {
  393. showToast(err.errorDesc)
  394. } else {
  395. Self.notifyAutoReplayDeleted(id: id)
  396. }
  397. }
  398. }
  399. func createAutoReplay(item: LNAutoReplyVO, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  400. LNHttpManager.shared.createAutoReplay(item: item) { err in
  401. queue.asyncIfNotGlobal {
  402. handler(err == nil)
  403. }
  404. if let err {
  405. showToast(err.errorDesc)
  406. } else {
  407. Self.notifyAutoReplayCreated(item: item)
  408. }
  409. }
  410. }
  411. func updateAutoReplay(item: LNAutoReplyVO, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  412. LNHttpManager.shared.updateAutoReplay(item: item) { err in
  413. queue.asyncIfNotGlobal {
  414. handler(err == nil)
  415. }
  416. if let err {
  417. showToast(err.errorDesc)
  418. } else {
  419. Self.notifyAutoReplayModified(item: item)
  420. }
  421. }
  422. }
  423. func triggerAutoReplayIfNeed(uid: String) {
  424. LNHttpManager.shared.triggerAutoReplayIfNeed(uid: uid) { _ in }
  425. }
  426. func markUseAutoReplay(uid: String) {
  427. LNHttpManager.shared.markUseAutoReplay(uid: uid) { _ in }
  428. }
  429. func fetchAutoReplyQuota(uid: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  430. LNHttpManager.shared.fetchAutoReplyQuota(uid: uid) { res, err in
  431. queue.asyncIfNotGlobal {
  432. if let err {
  433. showToast(err.errorDesc)
  434. handler(false)
  435. } else if let res {
  436. if res.playmateQuotaCount > 0 && res.userQuotaCount > 0 {
  437. handler(true)
  438. } else {
  439. showToast(.init(key: "A00350"))
  440. handler(false)
  441. }
  442. } else {
  443. handler(false)
  444. }
  445. }
  446. }
  447. }
  448. }
  449. // MARK: 潜在用户
  450. extension LNGameMateManager {
  451. func getPotentialUsers(queue: DispatchQueue = .main, handler: @escaping (LNPotentialUsersResponse?) -> Void) {
  452. LNHttpManager.shared.getPotentialUsers { res, err in
  453. queue.asyncIfNotGlobal {
  454. handler(res)
  455. }
  456. if let err {
  457. showToast(err.errorDesc)
  458. }
  459. }
  460. }
  461. }
  462. extension LNGameMateManager: LNProfileManagerNotify {
  463. func onUserInfoChanged(userInfo: LNUserProfileVO) {
  464. guard userInfo.userNo.isMyUid else { return }
  465. if userInfo.playmate {
  466. getGameMateManagerInfo()
  467. }
  468. }
  469. }
  470. extension LNGameMateManager {
  471. private func notifyUserGameMateManagerInfoChanged() {
  472. LNEventDeliver.notifyEvent { ($0 as? LNGameMateManagerNotify)?.onGameMateManagerInfoChanged() }
  473. }
  474. private func notifyGameTypesChanged() {
  475. LNEventDeliver.notifyEvent {
  476. ($0 as? LNGameMateManagerNotify)?.onGameTypesChanged()
  477. }
  478. }
  479. private static func notifyAutoReplayCreated(item: LNAutoReplyVO) {
  480. LNEventDeliver.notifyEvent {
  481. ($0 as? LNGameMateManagerNotify)?.onAutoReplyCreated(item: item)
  482. }
  483. }
  484. private static func notifyAutoReplayDeleted(id: String) {
  485. LNEventDeliver.notifyEvent {
  486. ($0 as? LNGameMateManagerNotify)?.onAutoReplyDeleted(id: id)
  487. }
  488. }
  489. private static func notifyAutoReplayModified(item: LNAutoReplyVO) {
  490. LNEventDeliver.notifyEvent {
  491. ($0 as? LNGameMateManagerNotify)?.onAutoReplyModified(item: item)
  492. }
  493. }
  494. }