HallClient.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { trackEvent } from '@scripts/tracking/TrackEvents';
  2. import { AppKernel } from '../../app/AppKernel';
  3. import { APP_SERVER } from '../../define/Define';
  4. import { ColyseusRoomClient } from '../../network/colyseus/ColyseusRoomClient';
  5. import { AppTracks } from '@scripts/app/AppTrack';
  6. const CASH_BASE = 100;
  7. export interface HallMessage {
  8. cmd?: string;
  9. data?: Record<string, unknown>;
  10. [key: string]: unknown;
  11. }
  12. export interface HallPayFinishMessage extends HallMessage {
  13. cmd: 'pay_finish';
  14. data?: {
  15. Golds?: number | string;
  16. PayNum?: number | string;
  17. event?: string;
  18. params?: unknown;
  19. [key: string]: unknown;
  20. };
  21. }
  22. export interface HallCallClientMessage extends HallMessage {
  23. cmd: 'call_client';
  24. data?: {
  25. type?: 'track' | 'refresh_mail' | 'start_change' | string;
  26. Golds?: number | string;
  27. event?: string;
  28. params?: unknown;
  29. [key: string]: unknown;
  30. };
  31. }
  32. class HallClient {
  33. private client: ColyseusRoomClient | null = null;
  34. private serverUrl = '';
  35. async connect(serverUrl?: string): Promise<boolean> {
  36. const mainStore = AppKernel.stores.mainStore;
  37. if (!mainStore.isLogin || mainStore.userID <= 0) return false;
  38. const nextUrl = serverUrl || mainStore.hallServerUrl || APP_SERVER.SERVER_URL;
  39. console.log('[HallClient] endpoint:', JSON.stringify(nextUrl));
  40. if (!nextUrl) {
  41. console.warn('[HallClient] Hall server URL is empty');
  42. return false;
  43. }
  44. if (!this.client || this.serverUrl !== nextUrl) {
  45. if (this.client) await this.client.disconnect();
  46. this.serverUrl = nextUrl;
  47. this.client = this.createClient(nextUrl);
  48. }
  49. return this.client.connect();
  50. }
  51. async disconnect(): Promise<void> {
  52. const client = this.client;
  53. this.client = null;
  54. this.serverUrl = '';
  55. if (client) await client.disconnect();
  56. }
  57. sendChat(message: unknown): boolean {
  58. return this.send('chat', message);
  59. }
  60. sendRegistration(): boolean {
  61. const user = AppKernel.stores.mainStore.userInfo;
  62. if (!user) return false;
  63. const { UserID, GlobalUID, FaceID, NickName } = user;
  64. return this.send('id', { UserID, GlobalUID, FaceID, NickName });
  65. }
  66. sendState(): boolean {
  67. return this.send('state', { in_game: AppKernel.stores.gameStore.gameID });
  68. }
  69. private createClient(serverUrl: string): ColyseusRoomClient {
  70. return new ColyseusRoomClient(APP_SERVER.ROOM_HALL, serverUrl, {
  71. onConnected: () => {
  72. AppKernel.appLobby.hallClientConnected();
  73. this.sendRegistration();
  74. },
  75. onDisconnected: (code) => AppKernel.events.emit('hall:disconnected', { code }),
  76. onTimeout: () => AppKernel.appLobby.hallClientTimeout(),
  77. onMessage: (type, message) => this.onMessage(type, message),
  78. });
  79. }
  80. private send(type: string, message: unknown = ''): boolean {
  81. return this.client?.send(type, message) ?? false;
  82. }
  83. private onMessage(type: string, rawMessage: unknown): void {
  84. const message = this.asMessage(rawMessage);
  85. console.log(`[HallClient] onMessage type:${type} cmd:${message?.cmd}`);
  86. switch (type) {
  87. case 'operate':
  88. this.handleOperate(message);
  89. break;
  90. case 'roger':
  91. this.handleRoger(message);
  92. break;
  93. }
  94. }
  95. private handleOperate(message: HallMessage): void {
  96. switch (message.cmd) {
  97. case 'id':
  98. this.sendState();
  99. break;
  100. case 'kick':
  101. AppKernel.appLobby.handleKickout();
  102. break;
  103. }
  104. }
  105. private handleRoger(message: HallMessage): void {
  106. switch (message.cmd) {
  107. case 'pay_finish': {
  108. // const payMessage = message as HallPayFinishMessage;
  109. const data = message.data;
  110. if (!!data && !!data.event) {
  111. trackEvent(AppTracks.Purchased, data.params)
  112. }
  113. AppKernel.appLobby.onPaymentFinished();
  114. break;
  115. }
  116. case 'call_client': {
  117. const callMessage = message as HallCallClientMessage;
  118. if (!!callMessage.data && !!callMessage.data.type) {
  119. const { data } = callMessage;
  120. switch (data.type) {
  121. case 'track':
  122. trackEvent(data.event as any, data.params);
  123. break;
  124. case 'refresh_mail':
  125. break;
  126. case 'start_change':
  127. break;
  128. }
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. private asMessage(message: unknown): HallMessage {
  135. if (message && typeof message === 'object') return message as HallMessage;
  136. return { data: { value: message } };
  137. }
  138. }
  139. export const hallClient = new HallClient();