3f8dff31feda7b51e2d0428361dac48e18834ed8.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, NetBase, NetPlayer, NetRoom, _crd;
  4. function _reportPossibleCrUseOfNetBase(extras) {
  5. _reporterNs.report("NetBase", "./NetBase", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfNetPlayer(extras) {
  8. _reporterNs.report("NetPlayer", "./NetPlayer", _context.meta, extras);
  9. }
  10. _export("NetRoom", void 0);
  11. return {
  12. setters: [function (_unresolved_) {
  13. _reporterNs = _unresolved_;
  14. }, function (_cc) {
  15. _cclegacy = _cc.cclegacy;
  16. }, function (_unresolved_2) {
  17. NetBase = _unresolved_2.NetBase;
  18. }, function (_unresolved_3) {
  19. NetPlayer = _unresolved_3.NetPlayer;
  20. }],
  21. execute: function () {
  22. _crd = true;
  23. _cclegacy._RF.push({}, "577dcgqDHFNgZms65V5Rprz", "NetRoom", undefined);
  24. /**网络房间*/
  25. _export("NetRoom", NetRoom = class NetRoom extends (_crd && NetBase === void 0 ? (_reportPossibleCrUseOfNetBase({
  26. error: Error()
  27. }), NetBase) : NetBase) {
  28. /**游戏主机ID*/
  29. get HostId() {
  30. return this._hostId;
  31. }
  32. /**游戏是否关闭*/
  33. get cloosed() {
  34. return !this._status;
  35. }
  36. //
  37. constructor(roomData, playerData) {
  38. super(roomData.roomId);
  39. this.evt = chsdk.get_new_event();
  40. this._hostId = void 0;
  41. this._status = true;
  42. this._own_id = void 0;
  43. this._players = new Map();
  44. //
  45. this._waitSends_mode0 = [];
  46. this._waitSends_mode1 = [];
  47. this._waitSends_mode2 = [];
  48. this._results = [];
  49. this._chat_msg = null;
  50. this._last_chat_time = 0;
  51. this._status = roomData.status;
  52. this._hostId = roomData.hostInfo;
  53. let ps = Object.keys(playerData).map(key => ({
  54. key,
  55. data: playerData[key]
  56. }));
  57. for (let i = 0; i < ps.length; i++) this.addPlayer(ps[i]);
  58. this._players.forEach((v, _) => {
  59. v.set_host(v.Id === this._hostId, this.isHost);
  60. });
  61. this.initValue(roomData.gameData);
  62. }
  63. addPlayer(p) {
  64. const id = p.key;
  65. const pd = p.data;
  66. let player = this._players.get(id);
  67. if (!player) {
  68. player = new (_crd && NetPlayer === void 0 ? (_reportPossibleCrUseOfNetPlayer({
  69. error: Error()
  70. }), NetPlayer) : NetPlayer)(id);
  71. this._players.set(id, player);
  72. }
  73. player.init(pd);
  74. if (player.isOwn) this._own_id = id;
  75. }
  76. updatePlayerStatus(id, online) {
  77. const p = this.getPlayer(id);
  78. if (p) {
  79. p.change_online(online);
  80. const location = p.location;
  81. const nickName = p.nickName;
  82. this.evt._emit('r_online', id, online, location, nickName);
  83. }
  84. }
  85. changeHost(id) {
  86. this._hostId = id;
  87. this._players.forEach((v, _) => {
  88. v.set_host(v.Id === this._hostId, this.isHost);
  89. });
  90. const p = this.getPlayer(this._hostId);
  91. this.evt._emit('r_host', id, p.location, p.nickName);
  92. }
  93. /**自己是否是主机*/
  94. get isHost() {
  95. return this._own_id === this._hostId;
  96. }
  97. /**自己的所有信息*/
  98. get own() {
  99. return this._players.get(this._own_id);
  100. }
  101. /**其它玩家信息*/
  102. get others() {
  103. return Array.from(this._players.values()).filter(player => !player.isOwn);
  104. }
  105. /**所有ai信息*/
  106. get ais() {
  107. return Array.from(this._players.values()).filter(player => player.isAI);
  108. }
  109. /**所有玩家*/
  110. get all() {
  111. return this.getAllPlayer();
  112. }
  113. /**在线玩家信息*/
  114. get onlines() {
  115. return Array.from(this._players.values()).filter(player => player.online);
  116. }
  117. /**不在线玩家信息*/
  118. get outlines() {
  119. return Array.from(this._players.values()).filter(player => !player.online);
  120. }
  121. /**所有玩家*/
  122. getAllPlayer() {
  123. return Array.from(this._players.values());
  124. }
  125. /**将玩家按 location 放到一个数组中,并自定义数组长度*/
  126. getAllPlayersAtLocations(customLength) {
  127. const locationArray = Array(customLength).fill(null);
  128. this._players.forEach(player => {
  129. if (player.location >= 0 && player.location < customLength) locationArray[player.location] = player;
  130. });
  131. return locationArray;
  132. }
  133. /**中某个玩家的所有信息*/
  134. getPlayer(id) {
  135. return this._players.get(id);
  136. }
  137. /**获取除了某个id的所有玩家*/
  138. getExPlayer(id) {
  139. return Array.from(this._players.values()).filter(player => player.Id != id);
  140. }
  141. /**获在线或不在线的所有玩家*/
  142. getOnlinePlayer(isOnline) {
  143. return isOnline ? this.onlines : this.outlines;
  144. }
  145. /**是否有AI权限*/
  146. canAiPlayer(player) {
  147. if (!this._status) return false;
  148. if (!this.isHost) return false;
  149. if (!player || !player.isAI) return false;
  150. return true;
  151. }
  152. /**是否有AI权限Id,返回可以控制的player*/
  153. canAiPlayerId(id) {
  154. if (!this._status) return null;
  155. if (!this.isHost) return null;
  156. const player = this._players.get(id);
  157. if (!player) return null;
  158. if (!player.isAI) return null;
  159. return player;
  160. }
  161. /**是否拥有权限(包括,自己和自己是主机时的ai)*/
  162. hasPermission(player) {
  163. return player.isOwn || this.canAiPlayer(player);
  164. }
  165. /**创建obj数据*/
  166. creatObj(data) {
  167. var _super$getValue;
  168. if (!this._status) return;
  169. if (!this.isHost) return;
  170. let oid = (_super$getValue = super.getValue('oid')) != null ? _super$getValue : 0;
  171. oid++;
  172. const key = 'obj_' + oid;
  173. super.setValue('oid', oid);
  174. super.setValueDirty('oid', oid);
  175. super.setValue(key, data);
  176. super.setValueDirty(key, data);
  177. this.evt._emit('r_obj', key, data);
  178. return key;
  179. }
  180. /**删除obj数据*/
  181. getObj(key) {
  182. return super.getValue(key);
  183. }
  184. /**不建议经常更改obj */
  185. changeObj(key, data) {
  186. if (!this.isHost) return;
  187. let old = super.getValue(key);
  188. if (old) {
  189. super.setValue(key, data);
  190. super.setValueDirty(key, data);
  191. this.evt._emit('r_obj', key, data, old);
  192. }
  193. } //
  194. deleteObj(key) {
  195. if (!this.isHost) return;
  196. super.setValue(key, null);
  197. super.setValueDirty(key, null);
  198. }
  199. /**修改某个键的值*/
  200. setValue(key, data) {
  201. if (!this._status) return;
  202. if (!this.isHost) return; //不是主机没有权限
  203. let old = super.getValue(key);
  204. if (old) {
  205. if (typeof data === "object") {//old = JSON.parse(JSON.stringify(old));
  206. } else if (data === old) {
  207. return;
  208. }
  209. }
  210. super.setValue(key, data);
  211. super.setValueDirty(key, data);
  212. this.evt._emit(key, data, old);
  213. }
  214. /**获取数据的值*/
  215. getValue(key) {
  216. return super.getValue(key);
  217. }
  218. server_change(data) {
  219. if (!this._status) return;
  220. if (this.isHost) return;
  221. Object.keys(data).forEach(key => {
  222. this.change(key, data[key]);
  223. });
  224. }
  225. change(key, data) {
  226. const old = super.getValue(key);
  227. super.setValue(key, data);
  228. if (typeof data !== "object" && data === old) return;
  229. if (key === 'oid') {
  230. return;
  231. } else if (key.startsWith('obj_')) {
  232. this.evt._emit('r_obj', key, data, old);
  233. } else {
  234. this.evt._emit(key, data, old);
  235. }
  236. }
  237. /**
  238. * 游戏结算数据
  239. *
  240. * @returns {Array} results - 包含游戏结算信息的数组。
  241. * 每个元素表示一个玩家的结算数据,结构如下:
  242. *
  243. * - Id: {string} 玩家唯一标识符
  244. * - AddScore: {number} 本局游戏中增加的星星数
  245. * - IsFinish: {boolean} 游戏是否已完成
  246. * - Rank: {number} 玩家在当前游戏中的排名
  247. * - Score: {number} 玩家在最后星星数
  248. * - TotalRank: {number} 玩家的总排名
  249. * - Level: {Object} 玩家当前等级的信息
  250. * - LowerRank: {number} 当前段位的等级
  251. * - Rank: {number} 段位
  252. * - Star: {number} 当前段位的等级的星级
  253. * - UserData: {Object} 玩家个人信息
  254. * - gid: {string} 游戏全局唯一标识符
  255. * - head: {string} 玩家头像的 URL
  256. * - hid: {number} 玩家省份id
  257. * - ip: {string} 玩家 IP 地址
  258. * - loginTime: {number} 玩家登录时间的时间戳
  259. * - nickName: {string} 玩家昵称
  260. * - openId: {string} 玩家在平台上的唯一标识符
  261. * - option: {string} 玩家选择的选项或设置
  262. * - pf: {string} 平台信息
  263. * - registerTime: {number} 玩家注册时间的时间戳
  264. * - userId: {number} 玩家在系统中的用户 ID
  265. * - province: {string} 玩家所在的省份
  266. * - Elements: {Array} 包含其他玩家的结算数据
  267. * - Rank: {number} 其他玩家的排名
  268. * - Level: {Object} 其他玩家的等级信息
  269. * - LowerRank: {number} 其他玩家当前段位的等级
  270. * - Rank: {number} 其他玩家的段位
  271. * - Star: {number} 其他玩家段位的等级的星级
  272. * - UserData: {Object} 其他玩家的个人信息(与上面的 UserData 结构相同)
  273. */
  274. get results() {
  275. return this._results;
  276. }
  277. /**获取自己本局获得星星*/
  278. get own_results_addScore() {
  279. const rs = this.results;
  280. if (!rs) return 0;
  281. for (let i = 0; i < rs.length; i++) {
  282. if (rs[i].Id === this._own_id) {
  283. return rs[i].AddScore;
  284. }
  285. }
  286. return 0;
  287. }
  288. closed(data) {
  289. this._status = false;
  290. this._results.length = 0;
  291. const ps = Object.keys(data).map(key => ({
  292. key,
  293. data: data[key]
  294. }));
  295. for (let i = 0; i < ps.length; i++) {
  296. const key = ps[i].key;
  297. const user = ps[i].data;
  298. user.Id = key;
  299. const player = this.getPlayer(key);
  300. player.set_level(user.Level, user.Rank, user.Score, user.TotalRank);
  301. user.UserData = {
  302. gid: player.gid,
  303. head: player.head,
  304. hid: player.hid,
  305. ip: player.ip,
  306. loginTime: player.loginTime,
  307. nickName: player.nickName,
  308. openId: player.openId,
  309. option: player.option,
  310. pf: player.pf,
  311. registerTime: player.registerTime,
  312. userId: player.userId,
  313. province: player.province
  314. };
  315. const elements = user.Elements;
  316. if (elements) {
  317. for (let i = 0; i < elements.length; i++) {
  318. var _chsdk$provinceCode2N;
  319. const o = elements[i];
  320. o.nickName = o.nickName || '玩家' + o.userId;
  321. o.hid = Number.parseInt(o.hid);
  322. o.loginTime = Number.parseInt(o.loginTime);
  323. o.registerTime = Number.parseInt(o.registerTime);
  324. o.userId = Number.parseInt(o.userId);
  325. o.province = (_chsdk$provinceCode2N = chsdk.provinceCode2Name(o.hid)) != null ? _chsdk$provinceCode2N : '其它';
  326. }
  327. }
  328. this._results.push(user);
  329. }
  330. ;
  331. if (this._results.length > 1) {
  332. this._results.sort((a, b) => {
  333. if (a.Rank === 0 && b.Rank === 0) {
  334. return 0;
  335. } else if (a.Rank === 0) {
  336. return 1;
  337. } else if (b.Rank === 0) {
  338. return -1;
  339. } else {
  340. return a.Rank - b.Rank;
  341. }
  342. });
  343. } //
  344. this.evt._emit('r_closed');
  345. }
  346. finish(pid, rank) {
  347. const player = this.getPlayer(pid);
  348. if (!player) return;
  349. player.setFinish(rank);
  350. this.evt._emit('r_finish', pid, rank);
  351. }
  352. /**向房间所有玩家发消息*/
  353. sendEvt(key, ...data) {
  354. if (!this._status) return;
  355. this._waitSends_mode0.push({
  356. type: key,
  357. data: data
  358. });
  359. }
  360. /**向房间主机发消息*/
  361. sendToHost(key, ...data) {
  362. if (!this._status) return;
  363. this._waitSends_mode1.push({
  364. type: key,
  365. data: data
  366. });
  367. }
  368. /**向房间其它玩家*/
  369. sendToOther(key, ...data) {
  370. if (!this._status) return;
  371. this._waitSends_mode2.push({
  372. type: key,
  373. data: data
  374. });
  375. }
  376. /**处理发送事件*/
  377. doSendMode(f) {
  378. f(this._waitSends_mode0, this._waitSends_mode1, this._waitSends_mode2);
  379. this._waitSends_mode0.length = 0;
  380. this._waitSends_mode1.length = 0;
  381. this._waitSends_mode2.length = 0;
  382. }
  383. /**房间里聊天 ,成功返回true,发送频率过快返回false*/
  384. chat(msg) {
  385. const now = chsdk.date.now();
  386. if (now - this._last_chat_time < 1000) return false;
  387. this._last_chat_time = now;
  388. this._chat_msg = msg;
  389. return true;
  390. }
  391. onChat(id, msg) {
  392. const p = this.getPlayer(id);
  393. this.evt._emit('r_chat', id, msg, p.location, p.nickName);
  394. }
  395. doSendChat(f) {
  396. f(this._chat_msg);
  397. this._chat_msg = null;
  398. } //
  399. _onEvt(key, data) {
  400. this.evt._emit(key, ...data);
  401. } //
  402. dispose() {
  403. super.dispose();
  404. this._players.forEach((v, _) => {
  405. v.dispose();
  406. });
  407. this._players.clear();
  408. this._status = false;
  409. this.evt.clearAll();
  410. }
  411. });
  412. _cclegacy._RF.pop();
  413. _crd = false;
  414. }
  415. };
  416. });
  417. //# sourceMappingURL=3f8dff31feda7b51e2d0428361dac48e18834ed8.js.map