NetTeam.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { NetPlayer } from "./NetPlayer";
  2. type PickEvent<T, Prefix extends string> = Pick<T, Extract<keyof T, `${Prefix}${string}`>>;
  3. const C2S_MATCH = 'c2s_match';
  4. const C2S_MATCH_CANCEL = 'c2s_match_cancel';
  5. const C2S_READY = 'c2s_ready';
  6. const C2S_KICK_OUT = 'c2s_kick_out';
  7. /**网络队伍*/
  8. export class NetTeam<GD> {
  9. /**队伍事件*/
  10. public readonly evt = chsdk.get_new_event<PickEvent<GD, 't_'>>();
  11. private _status: boolean = true;
  12. /**队伍是否能进*/public get status(): boolean { return this._status };
  13. private _hostId: string;
  14. /**队伍队长ID*/public get HostId(): string { return this._hostId };
  15. private _password: string;
  16. /**队伍进入口令*/public get Password(): string { return this._password };
  17. private _limit: number;
  18. /**队伍上限*/public get Limit(): number { return this._limit };
  19. private _id: string;
  20. /**队伍标识id*/public get Id(): string { return this._id };
  21. private _inCollect: boolean = false;
  22. /**是否在匹配中*/public get inCollect(): boolean { return this._inCollect };
  23. private _own_id: string;
  24. private _players: Map<string, NetPlayer<GD>> = new Map();
  25. private _send: (type: string, data?: any) => void;
  26. constructor(data: any, send: (type: string, data?: any) => void) {
  27. const team = data.teamData;
  28. const playerData = data.playerData;
  29. this._hostId = team.hostInfo;
  30. this._status = team.status;
  31. this._inCollect = team.inCollect;
  32. this._password = team.password;
  33. this._limit = team.limit;
  34. this._id = team.roomId;
  35. this._players.clear();
  36. let ps = Object.keys(playerData).map(key => ({ key, data: playerData[key] }));
  37. for (let i = 0; i < ps.length; i++) this.addPlayer(ps[i]);
  38. this._send = send;
  39. }
  40. /**主机开始匹配*/
  41. public match(): void {
  42. if (!this.isHost) return;
  43. this._send(C2S_MATCH);
  44. }
  45. /**主机退出匹配*/
  46. public exit_match(): void {
  47. if (!this.isHost) return;
  48. this._send(C2S_MATCH_CANCEL);
  49. }
  50. /**主机踢出某个玩家出小队 */
  51. public kick_out(location: number): void;
  52. public kick_out(id: string): void;
  53. public kick_out(arg: string | number): void {
  54. if (!this.isHost) return;
  55. if (typeof arg === 'string') {
  56. this._send(C2S_KICK_OUT, arg);
  57. } else if (typeof arg === 'number') {
  58. const playerToKick = this.all.find(player => player.location === arg);
  59. if (playerToKick) this._send(C2S_KICK_OUT, playerToKick.Id); // 踢出找到的玩家
  60. }
  61. }
  62. /**主机踢出所有没有准备的玩家 */
  63. public kick_out_no_readys(): void {
  64. if (!this.isHost) return;
  65. const list = this.notreadys;
  66. for (let i = 0; i < list.length; i++) this._send(C2S_KICK_OUT, list[i].Id);
  67. }
  68. /**主机踢出所有不在线玩家*/
  69. public kick_out_outlines(): void {
  70. if (!this.isHost) return;
  71. const list = this.outlines;
  72. for (let i = 0; i < list.length; i++) this._send(C2S_KICK_OUT, list[i].Id);
  73. }
  74. /**主机踢出所有不在线或没有准备的玩家*/
  75. public kick_out_badplayers(): void {
  76. if (!this.isHost) return;
  77. const list = this.badplayers;
  78. for (let i = 0; i < list.length; i++) this._send(C2S_KICK_OUT, list[i].Id);
  79. }
  80. /**准备*/
  81. public ready(): void {
  82. this._send(C2S_READY);
  83. }
  84. private addPlayer(p: any, isevt: boolean = false): void {
  85. const id = p.key;
  86. const pd = p.data;
  87. let player = this._players.get(id);
  88. if (!player) {
  89. player = new NetPlayer<GD>(id);
  90. this._players.set(id, player);
  91. }
  92. player.init(pd);
  93. (player as any).set_host(player.Id === this._hostId);
  94. if (player.isOwn) this._own_id = id;
  95. if (isevt) (this.evt as any)._emit('t_entry', id, player.location, player.nickName);
  96. }
  97. /**玩家离开*/
  98. private removePlayer(id: string): void {
  99. const p = this._players.get(id)
  100. if (p) {
  101. const location = p.location;
  102. const nickName = p.nickName;
  103. this._players.delete(id);
  104. (this.evt as any)._emit('t_exit', id, location, nickName);
  105. }
  106. }
  107. /**队伍解散*/
  108. private closed(): void {
  109. (this.evt as any)._emit('t_closed');
  110. }
  111. private updatePlayerStatus(id: string, online: boolean): void {
  112. const p = this.getPlayer(id);
  113. if (p) {
  114. (p as any).change_online(online);
  115. const location = p.location;
  116. const nickName = p.nickName;
  117. (this.evt as any)._emit('t_online', id, online, location, nickName);
  118. }
  119. }
  120. private updatePlayerReady(id: string, ready: boolean): void {
  121. const p = this.getPlayer(id);
  122. if (p) {
  123. (p as any).change_ready(ready);
  124. const location = p.location;
  125. const nickName = p.nickName;
  126. (this.evt as any)._emit('t_ready', id, ready, location, nickName);
  127. }
  128. }
  129. private match_start(): void {
  130. this._inCollect = true;
  131. (this.evt as any)._emit('t_matchStart');
  132. }
  133. private match_cancel(): void {
  134. this._inCollect = false;
  135. (this.evt as any)._emit('t_matchCancel');
  136. }
  137. private match_success(): void {
  138. this._inCollect = false;
  139. (this.evt as any)._emit('t_matchSuccess');
  140. const ps = this.getAllPlayer();
  141. for (let i = 0; i < ps.length; i++) {
  142. (ps[i] as any).change_ready(false);
  143. }
  144. }
  145. private not_ready(): void {
  146. (this.evt as any)._emit('t_no_ready');
  147. }
  148. private changeHost(id: string): void {
  149. this._hostId = id;
  150. this._players.forEach((v, _) => { (v as any).set_host(v.Id === this._hostId); });
  151. const p = this.getPlayer(this._hostId);
  152. (this.evt as any)._emit('t_host', id, p.location, p.nickName);
  153. }
  154. private _chat_msg: string | null = null;
  155. private _last_chat_time: number = 0;
  156. /*队伍里聊天,成功返回true,发送频率过快返回false*/
  157. public chat(msg: string): boolean {
  158. const now = chsdk.date.now();
  159. if (now - this._last_chat_time < 1000) return false;
  160. this._last_chat_time = now;
  161. this._chat_msg = msg;
  162. return true;
  163. }
  164. private onChat(id: string, msg: string): void {
  165. const p = this.getPlayer(id);
  166. (this.evt as any)._emit('t_chat', id, msg, p.location, p.nickName);
  167. }
  168. private doSendChat(f: (msg: string) => void) {
  169. f(this._chat_msg);
  170. this._chat_msg = null;
  171. }
  172. /**自己是否是主机*/
  173. get isHost(): boolean {
  174. return this._own_id === this._hostId;
  175. }
  176. /**自己的所有信息*/
  177. public get own(): NetPlayer<GD> {
  178. return this._players.get(this._own_id);
  179. }
  180. /**所有玩家*/
  181. public get all(): NetPlayer<GD>[] {
  182. return Array.from(this._players.values());
  183. }
  184. /**其它玩家信息*/
  185. public get others(): NetPlayer<GD>[] {
  186. return Array.from(this._players.values()).filter(player => !player.isOwn);;
  187. }
  188. /**在线玩家信息*/
  189. public get onlines(): NetPlayer<GD>[] {
  190. return Array.from(this._players.values()).filter(player => player.online);;
  191. }
  192. /**不在线玩家信息*/
  193. public get outlines(): NetPlayer<GD>[] {
  194. return Array.from(this._players.values()).filter(player => !player.online);;
  195. }
  196. /**没有准备或不在线玩家*/
  197. public get badplayers(): NetPlayer<GD>[] {
  198. return Array.from(this._players.values()).filter(player => !player.ready || !player.online);;
  199. }
  200. /**准备好的玩家信息*/
  201. public get readys(): NetPlayer<GD>[] {
  202. return Array.from(this._players.values()).filter(player => player.ready);;
  203. }
  204. /**没有准备的玩家信息*/
  205. public get notreadys(): NetPlayer<GD>[] {
  206. return Array.from(this._players.values()).filter(player => !player.ready);;
  207. }
  208. /**某个玩家的所有信息*/
  209. public getPlayer(id: string): NetPlayer<GD> {
  210. return this._players.get(id);
  211. }
  212. /**所有玩家*/
  213. public getAllPlayer(): NetPlayer<GD>[] {
  214. return this.all;
  215. }
  216. /**将玩家按 location 放到一个数组中,并自定义数组长度*/
  217. public getAllPlayersAtLocations(customLength: number): (NetPlayer<GD> | null)[] {
  218. const locationArray: (NetPlayer<GD> | null)[] = Array(customLength).fill(null);
  219. this._players.forEach((player) => {
  220. if (player.location >= 0 && player.location < customLength) locationArray[player.location] = player;
  221. });
  222. return locationArray;
  223. }
  224. /**获取除了某个id的所有玩家*/
  225. public getExPlayer(id: string): NetPlayer<GD>[] {
  226. return Array.from(this._players.values()).filter(player => player.Id != id);;
  227. }
  228. /**获在线或不在线的所有玩家*/
  229. public getOnlinePlayer(isOnline: boolean): NetPlayer<GD>[] {
  230. return isOnline ? this.onlines : this.outlines;
  231. }
  232. /**获准备或没有准备的所有玩家*/
  233. public getReadyPlayer(ready: boolean): NetPlayer<GD>[] {
  234. return ready ? this.readys : this.notreadys;
  235. }
  236. public dispose(): void {
  237. this._send = null;
  238. this._players.forEach((v, _) => {
  239. v.dispose();
  240. });
  241. this._players.clear();
  242. }
  243. }