72bc24ff288b2b4316b5ddb0abd45b840383d6f0.js 9.8 KB

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