RoomMgr.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { SceneDef } from "../../scripts/SceneDef";
  2. import { UserMgr } from "./UserMgr";
  3. import { director } from "cc";
  4. import { MsgUserDataChangedPush } from "../shared/protocols/public/room/MsgUserDataChangedPush";
  5. import { IUserData, IRoomData } from "../shared/types/RoomData";
  6. import { MsgRoomDataChangedPush } from "../shared/protocols/public/room/MsgRoomDataChangedPush";
  7. import { MsgRoomDataSyncPush } from "../shared/protocols/public/room/MsgRoomDataSyncPush";
  8. import { MsgRoomDismissedPush } from "../shared/protocols/public/room/MsgRoomDismissedPush";
  9. import { MsgUserComesToRoomPush } from "../shared/protocols/public/room/MsgUserComesToRoomPush";
  10. import { MsgUserLeavesFromRoomPush } from "../shared/protocols/public/room/MsgUserLeavesFromRoomPush";
  11. import { gameNet } from "./NetGameServer";
  12. import { GameServerAuthParams } from "../shared/types/GameServerAuthParams";
  13. import { MsgRoomClosed } from "../shared/protocols/public/room/MsgRoomClosed";
  14. import { LobbyMgr } from "./LobbyMgr";
  15. export class GameMode {
  16. public static SOLO: string = 'solo';
  17. public static AI: string = 'ai';
  18. public static ONLINE: string = 'online';
  19. }
  20. export class RoomEvent {
  21. /**
  22. * @en PN is used for notifying data changed, then the client can get the new data from the master server.
  23. * @zh 用于通知客户端哪些数据变动,方便从 Master 获取最新数据
  24. *
  25. */
  26. public static PN: string = 'RoomEvent.PN';
  27. public static NEW_USER_COMES: string = 'RoomEvent.NEW_USER_IN_TABLE';
  28. public static WATCHER_LIST_CHANGED: string = 'RoomEvent.WATCHER_LIST_CHANGED';
  29. public static USER_LEAVES: string = 'RoomEvent.USER_LEAVES';
  30. public static USER_DATA_CHANGED: string = 'RoomEvent.USER_DATA_CHANGED';
  31. public static ROOM_DATA_CHANGED: string = 'RoomEvent.ROOM_DATA_CHANGED';
  32. }
  33. export class RoomMgr {
  34. private static _inst: RoomMgr;
  35. public static get inst(): RoomMgr {
  36. if (!this._inst) {
  37. this._inst = new RoomMgr();
  38. }
  39. return this._inst;
  40. }
  41. protected _data: IRoomData
  42. public get data(): IRoomData {
  43. return this._data;
  44. }
  45. protected _gameMode: string = GameMode.SOLO;
  46. public get gameMode(): string {
  47. return this._gameMode;
  48. }
  49. public set gameMode(v: string) {
  50. this._gameMode = v;
  51. }
  52. public get isOnline(): boolean {
  53. return this.gameMode == GameMode.ONLINE;
  54. }
  55. constructor() {
  56. this.initNetMsgHandlers();
  57. }
  58. public init() {
  59. }
  60. public reset() {
  61. console.log('--- RoomMgr.reset ---');
  62. this._gameMode = GameMode.SOLO;
  63. this._data = null;
  64. }
  65. async rpc_LeaveRoom() {
  66. let ret = await gameNet.callApi("room/ExitRoom", {});
  67. return ret;
  68. }
  69. async backToLobby(silence: boolean = false) {
  70. if (this.isOnline) {
  71. let ret = await this.rpc_LeaveRoom();
  72. if (ret.isSucc) {
  73. gameNet.disconnect(3000, 'normal');
  74. this.reset();
  75. if (!silence) {
  76. tgx.UIWaiting.show('正在返回大厅');
  77. }
  78. LobbyMgr.inst.backToLobby();
  79. }
  80. else {
  81. tgx.UIAlert.show(ret.err.message);
  82. }
  83. }
  84. else {
  85. if (!silence) {
  86. tgx.UIWaiting.show('正在返回大厅');
  87. }
  88. LobbyMgr.inst.backToLobby();
  89. }
  90. }
  91. backToLogin() {
  92. tgx.UIAlert.show('网络链接断开,请重新登录').onClick(() => {
  93. this.reset();
  94. tgx.SceneUtil.loadScene(SceneDef.LOGIN);
  95. });
  96. }
  97. public get worldId(): string {
  98. if (!this._data) {
  99. return '';
  100. }
  101. return this._data.id;
  102. }
  103. public get isPlayer(): boolean {
  104. if (!this._data) {
  105. return false;
  106. }
  107. let user = this.getUser(UserMgr.inst.uid);
  108. return user ? !!user.playerId : false;
  109. }
  110. public get isPlaying(): boolean {
  111. if (!this._data) {
  112. return false;
  113. }
  114. return this._data.isPlaying;
  115. }
  116. public get isWatcher(): boolean {
  117. if (!this._data) {
  118. return false;
  119. }
  120. let user = this.getUser(UserMgr.inst.uid);
  121. return user ? !user.playerId : false;
  122. }
  123. public get selfUser() {
  124. return this.getUser(UserMgr.inst.uid);
  125. }
  126. public getUser(uid: string) {
  127. for (let i = 0; i < this._data.userList.length; ++i) {
  128. let u = this._data.userList[i];
  129. if (u.uid == uid) {
  130. return u;
  131. }
  132. }
  133. }
  134. async enterRoom(params: GameServerAuthParams) {
  135. let ret = await gameNet.ensureConnected();
  136. if (!ret.isSucc) {
  137. return ret;
  138. }
  139. let ret2 = await gameNet.joinRoomServer(UserMgr.inst.uid);
  140. if (ret2.isSucc) {
  141. this.gameMode = GameMode.ONLINE;
  142. }
  143. return ret2;
  144. }
  145. initNetMsgHandlers() {
  146. gameNet.listenMsg('room/RoomDataSyncPush', this.onNet_RoomDataSyncPush, this);
  147. gameNet.listenMsg('room/RoomDataChangedPush', this.onNet_RoomDataChangedPush, this);
  148. gameNet.listenMsg('room/UserComesToRoomPush', this.onNet_UserComesToRoomPush, this);
  149. gameNet.listenMsg('room/UserDataChangedPush', this.onNet_UserDataChangedPush, this);
  150. gameNet.listenMsg('room/UserLeavesFromRoomPush', this.onNet_UserLeavesFromRoomPush, this);
  151. gameNet.listenMsg('room/RoomClosed', this.onNet_RoomClosed, this);
  152. }
  153. async rpc_Ready() {
  154. let ret = await gameNet.callApi("room/Ready", {});
  155. return ret;
  156. }
  157. //==========================
  158. onNet_RoomDismissedPush(msg: MsgRoomDismissedPush) {
  159. }
  160. onNet_RoomDataSyncPush(msg: MsgRoomDataSyncPush) {
  161. this._data = msg.data;
  162. console.log('room data sync');
  163. }
  164. onNet_RoomDataChangedPush(msg: MsgRoomDataChangedPush) {
  165. if (!this._data) {
  166. return;
  167. }
  168. if (msg.isPlaying !== undefined) {
  169. this._data.isPlaying = msg.isPlaying;
  170. }
  171. if (msg.numPlayer !== undefined) {
  172. this._data.playerNum = msg.numPlayer;
  173. }
  174. //使用 director.getScene 发送 ROOM_DATA_CHANGED 事件
  175. director.emit(RoomEvent.ROOM_DATA_CHANGED, msg);
  176. }
  177. onNet_UserComesToRoomPush(msg: MsgUserComesToRoomPush) {
  178. if (!this._data) {
  179. return;
  180. }
  181. let user = this.getUser(msg.uid);
  182. if (!user) {
  183. this._data.userList.push(msg);
  184. }
  185. director.emit(RoomEvent.NEW_USER_COMES, msg);
  186. }
  187. onNet_UserDataChangedPush(msg: MsgUserDataChangedPush) {
  188. if (!this._data) {
  189. return;
  190. }
  191. let userId = msg.uid;
  192. let u = this.getUser(userId);
  193. if (!u) {
  194. return;
  195. }
  196. if (msg.ready !== undefined) {
  197. u.ready = msg.ready;
  198. }
  199. if (msg.playerId !== undefined) {
  200. u.playerId = msg.playerId;
  201. }
  202. if (msg.isOnline !== undefined) {
  203. u.isOnline = msg.isOnline;
  204. }
  205. director.emit(RoomEvent.USER_DATA_CHANGED, msg);
  206. }
  207. onNet_UserLeavesFromRoomPush(msg: MsgUserLeavesFromRoomPush) {
  208. if (!this._data) {
  209. return;
  210. }
  211. let isPlayer = false;
  212. for (let i = 0; i < this._data.userList.length; ++i) {
  213. let p = this._data.userList[i];
  214. if (p.uid == msg.uid) {
  215. this._data.userList.splice(i, 1);
  216. isPlayer = !!p.playerId;
  217. break;
  218. }
  219. }
  220. director.emit(RoomEvent.USER_LEAVES, { uid: msg.uid, isPlayer: isPlayer });
  221. }
  222. onNet_RoomClosed(msg: MsgRoomClosed) {
  223. if (!this._data || this._data.id != msg.roomId || this._data.gameType != msg.gameType) {
  224. return;
  225. }
  226. //tgx.UIAlert.show('房间已关闭').onClick(()=>{
  227. // this.backToLobby();
  228. //});
  229. }
  230. }
  231. RoomMgr.inst;