SceneTest.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { _decorator, assetManager, Component, Node, Vec3 } from 'cc';
  2. import { ModuleDef } from '../scripts/ModuleDef';
  3. import { NetGameServer } from '../module_basic/scripts/NetGameServer';
  4. import { GameServerAuthParams } from '../module_basic/shared/types/GameServerAuthParams';
  5. import { UserInfo } from '../module_basic/shared/types/UserInfo';
  6. const { ccclass, property } = _decorator;
  7. class MockClient{
  8. private net = new NetGameServer();
  9. private userInfo:UserInfo = {} as any;
  10. async startTest(index:number){
  11. let account = 'test_' + index;
  12. let password = '123456';
  13. this.net.startPing();
  14. this.net.createConnection(['ws://192.168.0.112:3001','ws://192.168.0.112:3002','ws://192.168.0.112:3003','ws://192.168.0.112:3004']);
  15. let ret = await this.net.ensureConnected();
  16. if(!ret.isSucc){
  17. console.log(ret.err.message);
  18. return;
  19. }
  20. await this.doLogin(account,password);
  21. }
  22. async doLogin(account,password){
  23. let net = this.net;
  24. let ret = await net.callApi('login/Login', { account: account, password: password });
  25. if(ret.isSucc){
  26. console.log('login success');
  27. this.userInfo = ret.res.userInfo;
  28. this.doAuth(ret.res.gameServerInfo);
  29. }
  30. else{
  31. this.enterRigster(account,password);
  32. }
  33. }
  34. async enterRoom(params: GameServerAuthParams) {
  35. this.net.authParams = params;
  36. let ret = await this.net.ensureConnected();
  37. if (!ret.isSucc) {
  38. return ret;
  39. }
  40. let ret2 = await this.net.joinRoomServer(this.userInfo.uid);
  41. if (ret2.isSucc) {
  42. }
  43. return ret2;
  44. }
  45. async doTryEnterRoom(id: string, password?: string) {
  46. let ret = await this.net.callApi('lobby/TryEnterRoom', { id: id, password: password }, { timeout: 10000 });
  47. if (ret.isSucc) {
  48. let params = ret.res;
  49. await this.net.connectToRoomServer(params);
  50. let ret2 = await this.enterRoom(params);
  51. if (ret2.isSucc) {
  52. console.log('is in game');
  53. }
  54. }
  55. else {
  56. console.log(ret.err.message);
  57. }
  58. return ret;
  59. }
  60. async doAuth(params: GameServerAuthParams) {
  61. if (params) {
  62. this.net.authParams = params;
  63. if (params.serverUrl != this.net.serverUrl) {
  64. this.net.createConnection([params.serverUrl]);
  65. let ret2 = await this.net.ensureConnected();
  66. if (!ret2.isSucc) {
  67. console.log(ret2.err.message);
  68. return ret2;
  69. }
  70. }
  71. let ret = await this.net.callApi('login/AuthClient', {
  72. uid: this.userInfo.uid,
  73. sign: params.token,
  74. time: params.time,
  75. roomId: params.roomId,
  76. gameType: params.gameType,
  77. });
  78. if (!ret.isSucc) {
  79. console.log(ret.err.message);
  80. return ret;
  81. }
  82. }
  83. //没有名字,表示还未创建角色,则进入角色创建流程
  84. if (!this.userInfo.name) {
  85. this.enterCreateRole();
  86. }
  87. //如果角色在房间中,则进入房间
  88. else if (params.roomId) {
  89. let ret2 = await this.doTryEnterRoom(params.roomId);
  90. if (!ret2.isSucc) {
  91. //进入大厅
  92. await this.enterLobby();
  93. }
  94. }
  95. else {
  96. //进入大厅
  97. await this.enterLobby();
  98. }
  99. return { isSucc: true };
  100. }
  101. async rpc_QuickPlay(type: string, immediate?: boolean) {
  102. let ret = await this.net.callApi("lobby/StartMatch", { type: type, immediate: immediate });
  103. return ret;
  104. }
  105. async enterLobby(){
  106. let ret = await this.rpc_QuickPlay('normal');
  107. if(ret.isSucc){
  108. this.enterRoom(ret.res);
  109. }
  110. }
  111. async enterRigster(account,password){
  112. let net = this.net;
  113. let ret = await net.callApi('login/Register', { account: account, password: password });
  114. if(!ret.isSucc){
  115. console.log('register failed');
  116. }
  117. else{
  118. this.doLogin(account,password);
  119. }
  120. }
  121. async enterCreateRole(){
  122. let name = '' + Math.random().toFixed(5);
  123. let visualId = 0;
  124. let ret = await this.net.callApi('login/CreateRole', { name: name, visualId: visualId });
  125. if (ret.isSucc) {
  126. this.userInfo.name = ret.res.name;
  127. this.userInfo.visualId = ret.res.visualId;
  128. this.enterLobby();
  129. }
  130. }
  131. }
  132. @ccclass('SceneTest')
  133. export class SceneTest extends Component {
  134. private _initCompleted = false;
  135. start() {
  136. assetManager.loadBundle(ModuleDef.BASIC,(err,bundle)=>{
  137. this._initCompleted = true;
  138. });
  139. }
  140. private startMocking = false;
  141. private count = 0;
  142. update(deltaTime: number) {
  143. }
  144. async onStartTest(){
  145. if(!this._initCompleted){
  146. return;
  147. }
  148. this.startMocking = true;
  149. setInterval(()=>{
  150. if(this.startMocking && this.count < 1000){
  151. let client = new MockClient();
  152. client.startTest(this.count);
  153. this.count++;
  154. }
  155. },100);
  156. }
  157. }