NetTeam.ts 9.6 KB

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