Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.23 KB | None | 0 0
  1. var Packet = require('./packet');
  2. var Vec2 = require('./modules/Vec2');
  3. var BinaryWriter = require("./packet/BinaryWriter");
  4.  
  5. function PlayerTracker(gameServer, socket) {
  6. this.gameServer = gameServer;
  7. this.socket = socket;
  8. this.pID = -1;
  9. this.userAuth = null;
  10. this.isRemoved = false;
  11. this.isCloseRequested = false;
  12. this._name = "";
  13. this._skin = "";
  14. this._nameUtf8 = null;
  15. this._skinUtf8protocol11 = null;
  16. this._nameUnicode = null;
  17. this._skinUtf8 = null;
  18. this.color = { r: 0, g: 0, b: 0 };
  19. this.accountusername = this.pID;
  20. this.viewNodes = [];
  21. this.clientNodes = [];
  22. this.cells = [];
  23. this.mergeOverride = false; // Triggered by console command
  24. this._score = 0; // Needed for leaderboard
  25. this._scale = 1;
  26. this.borderCounter = 0;
  27. this.connectedTime = new Date();
  28.  
  29. this.tickLeaderboard = 0;
  30. this.team = 0;
  31. this.spectate = false;
  32. this.freeRoam = false; // Free-roam mode enables player to move in spectate mode
  33. this.spectateTarget = null; // Spectate target, null for largest player
  34. this.lastKeypressTick = 0;
  35.  
  36. this.centerPos = new Vec2(0, 0);
  37. this.mouse = new Vec2(0, 0);
  38. this.viewBox = {
  39. minx: 0,
  40. miny: 0,
  41. maxx: 0,
  42. maxy: 0
  43. };
  44.  
  45. // Scramble the coordinate system for anti-raga
  46. this.scrambleX = 0;
  47. this.scrambleY = 0;
  48. this.scrambleId = 0;
  49. this.isMinion = false;
  50. this.isMuted = false;
  51.  
  52. // Custom commands
  53. this.spawnmass = 0;
  54. this.frozen = false;
  55. this.customspeed = 0;
  56. this.rec = false;
  57. this.perfectpopsplit = false;
  58. this.beingpopsplited = false;
  59. // GameMode
  60. this.canShootPopsplitVirus = false;
  61. this.canShootVirus = false;
  62. this.doublespeed = false;
  63. this.timeuntilsplit = 0;
  64. this.antiteamstate = false;
  65. this.wcount = 0;
  66. this.viruspopcount = 0;
  67. this.lastwtick = 0;
  68. this.lastviruspoptick = 0;
  69. this.timeuntilsplit = 0;
  70.  
  71. // Minions
  72. this.miQ = 0;
  73. this.isMi = false;
  74. this.minionSplit = false;
  75. this.minionEject = false;
  76. this.minionFrozen = false;
  77. this.minionControl = false;
  78. this.collectPellets = false;
  79.  
  80. // Gamemode function
  81. if (gameServer) {
  82. // Player id
  83. this.pID = gameServer.lastPlayerId++ >> 0;
  84. // Gamemode function
  85. gameServer.gameMode.onPlayerInit(this);
  86. // Only scramble if enabled in config
  87. this.scramble();
  88. }
  89. // Account system
  90. this.level = 0;
  91. this.levelexps = [
  92. 50,
  93. 125,
  94. 250,
  95. 500,
  96. 1000,
  97. 1600,
  98. 2300,
  99. 3100,
  100. 4000,
  101. 5000,
  102. 6100,
  103. 7300,
  104. 8600,
  105. 10000,
  106. 11500,
  107. 13100,
  108. 14800,
  109. 16600,
  110. 18500,
  111. 20500,
  112. 22600,
  113. 24800,
  114. 27100,
  115. 29500,
  116. 32000,
  117. 34600,
  118. 37300,
  119. 40100,
  120. 43000,
  121. 46000,
  122. 49100,
  123. 52300,
  124. 55600,
  125. 59000,
  126. 62500,
  127. 66100,
  128. 69800,
  129. 73600,
  130. 77500,
  131. 81500,
  132. 85600,
  133. 89800,
  134. 94100,
  135. 98500,
  136. 103000,
  137. 107600,
  138. 112300,
  139. 127000,
  140. 132100,
  141. 137300,
  142. 142600,
  143. 148000,
  144. 153500,
  145. 159100,
  146. 164800,
  147. 170600,
  148. 176500,
  149. 182500,
  150. 188600,
  151. 194800,
  152. 201100,
  153. 207500,
  154. 214000,
  155. 220600,
  156. 227300,
  157. 234100,
  158. 241000,
  159. 248000,
  160. 255100,
  161. 262300,
  162. 269600,
  163. 277000,
  164. 284500,
  165. 292100,
  166. 299800,
  167. 307600,
  168. 315500,
  169. 323500,
  170. 331600,
  171. 339800,
  172. 348100,
  173. 356500,
  174. 365000,
  175. 373600,
  176. 382300,
  177. 391100,
  178. 400000,
  179. 409000,
  180. 418100,
  181. 427300,
  182. 436600,
  183. 446000,
  184. 455500,
  185. 465100,
  186. 474800,
  187. 484600,
  188. 494500,
  189. ];
  190. this.exp = 0;
  191. this.accountusername = this.pID.toString();
  192. this.accountpassword = "";
  193.  
  194. var UserRoleEnum = require("./enum/UserRoleEnum");
  195. this.userRole = UserRoleEnum.GUEST;
  196. }
  197.  
  198. module.exports = PlayerTracker;
  199.  
  200. // Setters/Getters
  201.  
  202. PlayerTracker.prototype.scramble = function() {
  203. if (!this.gameServer.config.serverScrambleLevel) {
  204. this.scrambleId = 0;
  205. this.scrambleX = 0;
  206. this.scrambleY = 0;
  207. } else {
  208. this.scrambleId = (Math.random() * 0xFFFFFFFF) >>> 0;
  209. // avoid mouse packet limitations
  210. var maxx = Math.max(0, 31767 - this.gameServer.border.width);
  211. var maxy = Math.max(0, 31767 - this.gameServer.border.height);
  212. var x = maxx * Math.random();
  213. var y = maxy * Math.random();
  214. if (Math.random() >= 0.5) x = -x;
  215. if (Math.random() >= 0.5) y = -y;
  216. this.scrambleX = x;
  217. this.scrambleY = y;
  218. }
  219. this.borderCounter = 0;
  220. };
  221.  
  222. PlayerTracker.prototype.setName = function(name) {
  223. this._name = name;
  224. var writer = new BinaryWriter()
  225. writer.writeStringZeroUnicode(name);
  226. this._nameUnicode = writer.toBuffer();
  227. writer = new BinaryWriter();
  228. writer.writeStringZeroUtf8(name);
  229. this._nameUtf8 = writer.toBuffer();
  230. };
  231.  
  232. PlayerTracker.prototype.onLevel = function () {
  233. var fs = require('fs');
  234. var newuser = {
  235. username: this.accountusername,
  236. password: this.accountpassword,
  237. role: this.userRole,
  238. name: this.userAuth,
  239. level: this.level,
  240. exp: this.exp
  241. }
  242. for (var i in this.gameServer.userList) {
  243. var user = this.gameServer.userList[i];
  244.  
  245. if (user.username == this.accountusername && user.password == this.accountpassword) {
  246. this.gameServer.userList[i] = newuser;
  247. json = JSON.stringify(this.gameServer.userList);
  248. var file = '../src/enum/UserRoles.json';
  249. fs.writeFileSync(file, json, 'utf-8');
  250. this.gameServer.loadFiles();
  251. }
  252. }
  253. this.spawnmass = (this.gameServer.config.playerStartSize + (2 * (Math.sqrt(this.level * 100))) < 500) ? this.gameServer.config.playerStartSize + (2 * (Math.sqrt(this.level * 100))) : 500; // 2500 Spawnmass is wayy too much
  254. };
  255.  
  256. PlayerTracker.prototype.setSkin = function(skin) {
  257. this._skin = skin;
  258. var writer = new BinaryWriter();
  259. writer.writeStringZeroUtf8(skin);
  260. this._skinUtf8 = writer.toBuffer();
  261. var writer1 = new BinaryWriter();
  262. writer1.writeStringZeroUtf8("%" + skin);
  263. this._skinUtf8protocol11 = writer1.toBuffer();
  264. };
  265.  
  266. PlayerTracker.prototype.setColor = function(color) {
  267. this.color.r = color.r;
  268. this.color.g = color.g;
  269. this.color.b = color.b;
  270. };
  271.  
  272. PlayerTracker.prototype.getScale = function() {
  273. this._score = 0; // reset to not cause bugs with leaderboard
  274. var scale = 0; // reset to not cause bugs with viewbox
  275. for (var i = 0; i < this.cells.length; i++) {
  276. scale += this.cells[i]._size;
  277. this._score += this.cells[i]._mass;
  278. }
  279. if (!scale) return scale = this._score = 0; // reset
  280. else return this._scale = Math.pow(Math.min(64 / scale, 1), 0.4);
  281. };
  282.  
  283. PlayerTracker.prototype.joinGame = function(name, skin, isMi) {
  284. if (this.cells.length) return;
  285.  
  286. if (skin) this.setSkin(skin);
  287. if (!name) name = "An unnamed cell";
  288. // 5 = DEV 4 = Admin 2 = Moder
  289. if (this.userRole == 4) name = name + "ᴬᴰᴹᴵᴺ";
  290. else if (this.userRole == 2) name = name + "ᴹᴼᴰᴱᴿ";
  291. // Perform check to see if someone that isn't admin has a check
  292. if (this.userRole != 4 && this.userRole != 2) {
  293. for (var i in name) {
  294. name = name.replace('ᴬᴰᴹᴵᴺ', '');
  295. name = name.replace('ᴹᴼᴰᴱᴿ', '');
  296. }
  297. }
  298. this.setName(name);
  299. this.spectate = false;
  300. this.freeRoam = false;
  301. this.spectateTarget = null;
  302. var packetHandler = this.socket.packetHandler;
  303.  
  304. if (!this.isMi && this.socket.isConnected != null) {
  305. // some old clients don't understand ClearAll message
  306. // so we will send update for them
  307. if (packetHandler.protocol < 6) {
  308. packetHandler.sendPacket(new Packet.UpdateNodes(this, [], [], [], this.clientNodes));
  309. }
  310. packetHandler.sendPacket(new Packet.ClearAll());
  311. this.clientNodes = [];
  312. this.scramble();
  313. if (this.gameServer.config.serverScrambleLevel < 2) {
  314. // no scramble / lightweight scramble
  315. packetHandler.sendPacket(new Packet.SetBorder(this, this.gameServer.border));
  316. }
  317. else if (this.gameServer.config.serverScrambleLevel == 3) {
  318. var ran = 10065536 * Math.random();
  319. // Ruins most known minimaps (no border)
  320. var border = {
  321. minx: this.gameServer.border.minx - ran,
  322. miny: this.gameServer.border.miny - ran,
  323. maxx: this.gameServer.border.maxx + ran,
  324. maxy: this.gameServer.border.maxy + ran
  325. };
  326. packetHandler.sendPacket(new Packet.SetBorder(this, border));
  327. }
  328. }
  329. if (!this.isMi || isMi)
  330. this.gameServer.gameMode.onPlayerSpawn(this.gameServer, this);
  331. };
  332.  
  333. PlayerTracker.prototype.checkConnection = function() {
  334. // Handle disconnection
  335. if (!this.socket.isConnected) {
  336. // Wait for playerDisconnectTime
  337. var pt = this.gameServer.config.playerDisconnectTime;
  338. var dt = (this.gameServer.stepDateTime - this.socket.closeTime) / 1e3;
  339. if (pt && (!this.cells.length || dt >= pt)) {
  340. // Remove all client cells
  341. while (this.cells.length) this.gameServer.removeNode(this.cells[0]);
  342. }
  343. this.cells = [];
  344. this.isRemoved = true;
  345. this.mouse = null;
  346. this.socket.packetHandler.pressSpace = false;
  347. this.socket.packetHandler.pressQ = false;
  348. this.socket.packetHandler.pressW = false;
  349. return;
  350. }
  351.  
  352. // Check timeout
  353. if (!this.isCloseRequested && this.gameServer.config.serverTimeout) {
  354. dt = (this.gameServer.stepDateTime - this.socket.lastAliveTime) / 1000;
  355. if (dt >= this.gameServer.config.serverTimeout) {
  356. this.socket.close(1000, "Connection timeout");
  357. this.isCloseRequested = true;
  358. }
  359. }
  360. };
  361.  
  362. PlayerTracker.prototype.updateTick = function() {
  363. if (this.isRemoved || this.isMinion)
  364. return; // do not update
  365. this.socket.packetHandler.process();
  366. if (this.isMi) return;
  367.  
  368. // update viewbox
  369. this.updateSpecView(this.cells.length);
  370. var scale = Math.max(this.getScale(), this.gameServer.config.serverMinScale);
  371. var halfWidth = (this.gameServer.config.serverViewBaseX + 100) / scale / 2;
  372. var halfHeight = (this.gameServer.config.serverViewBaseY + 100) / scale / 2;
  373. this.viewBox = {
  374. minx: this.centerPos.x - halfWidth,
  375. miny: this.centerPos.y - halfHeight,
  376. maxx: this.centerPos.x + halfWidth,
  377. maxy: this.centerPos.y + halfHeight
  378. };
  379.  
  380. // update visible nodes
  381. this.viewNodes = [];
  382. var self = this;
  383. this.gameServer.quadTree.find(this.viewBox, function(check) {
  384. if (check.owner != self)
  385. self.viewNodes.push(check);
  386. });
  387. this.viewNodes = this.viewNodes.concat(this.cells);
  388. this.viewNodes.sort(function(a, b) { return a.nodeId - b.nodeId; });
  389. };
  390.  
  391. PlayerTracker.prototype.sendUpdate = function() {
  392. if (this.isRemoved || !this.socket.packetHandler.protocol ||
  393. !this.socket.isConnected || this.isMi || this.isMinion ||
  394. (this.socket._socket.writable != null && !this.socket._socket.writable) ||
  395. this.socket.readyState != this.socket.OPEN) {
  396. // do not send update for disconnected clients
  397. // also do not send if initialization is not complete yet
  398. return;
  399. }
  400.  
  401. var packetHandler = this.socket.packetHandler;
  402. if (this.gameServer.config.serverScrambleLevel == 2) {
  403. // scramble (moving border)
  404. if (!this.borderCounter) {
  405. var b = this.gameServer.border, v = this.viewBox;
  406. var bound = {
  407. minx: Math.max(b.minx, v.minx - v.halfWidth),
  408. miny: Math.max(b.miny, v.miny - v.halfHeight),
  409. maxx: Math.min(b.maxx, v.maxx + v.halfWidth),
  410. maxy: Math.min(b.maxy, v.maxy + v.halfHeight)
  411. };
  412. packetHandler.sendPacket(new Packet.SetBorder(this, bound));
  413. }
  414. if (++this.borderCounter >= 20) this.borderCounter = 0;
  415. }
  416.  
  417. var delNodes = [];
  418. var eatNodes = [];
  419. var addNodes = [];
  420. var updNodes = [];
  421. var oldIndex = 0;
  422. var newIndex = 0;
  423. for (; newIndex < this.viewNodes.length && oldIndex < this.clientNodes.length;) {
  424. if (this.viewNodes[newIndex].nodeId < this.clientNodes[oldIndex].nodeId) {
  425. if (this.viewNodes[newIndex].isRemoved) continue;
  426. addNodes.push(this.viewNodes[newIndex]);
  427. newIndex++;
  428. continue;
  429. }
  430. if (this.viewNodes[newIndex].nodeId > this.clientNodes[oldIndex].nodeId) {
  431. var node = this.clientNodes[oldIndex];
  432. if (node.isRemoved) eatNodes.push(node);
  433. else delNodes.push(node);
  434. oldIndex++;
  435. continue;
  436. }
  437. var node = this.viewNodes[newIndex];
  438. if (node.isRemoved) continue;
  439. // only send update for moving or player nodes
  440. if (node.isMoving || node.cellType == 0)
  441. updNodes.push(node);
  442. addNodes.push(node);
  443. newIndex++;
  444. oldIndex++;
  445. }
  446. for (; oldIndex < this.clientNodes.length; oldIndex++) {
  447. var node = this.clientNodes[oldIndex];
  448. if (node.isRemoved) eatNodes.push(node);
  449. else delNodes.push(node);
  450. }
  451. this.clientNodes = this.viewNodes;
  452.  
  453. // Send update packet
  454. packetHandler.sendPacket(new Packet.UpdateNodes(this, addNodes, updNodes, eatNodes, delNodes));
  455.  
  456. // Update leaderboard
  457. if (++this.tickLeaderboard > 25) {
  458. // 1 / 0.040 = 25 (once per second)
  459. this.tickLeaderboard = 0;
  460. if (this.gameServer.leaderboardType >= 0)
  461. packetHandler.sendPacket(new Packet.UpdateLeaderboard(this, this.gameServer.leaderboard, this.gameServer.leaderboardType));
  462. }
  463. };
  464.  
  465. PlayerTracker.prototype.updateSpecView = function(len) {
  466. if (!this.spectate || len) {
  467. // in game
  468. var cx = 0, cy = 0;
  469. for (var i = 0; i < len; i++) {
  470. cx += this.cells[i].position.x / len;
  471. cy += this.cells[i].position.y / len;
  472. this.centerPos = new Vec2(cx, cy);
  473. }
  474. } else {
  475. if (this.freeRoam || this.getSpecTarget() == null) {
  476. // free roam
  477. var d = this.mouse.clone().sub(this.centerPos);
  478. var scale = this.gameServer.config.serverSpectatorScale;
  479. this.setCenterPos(this.centerPos.add(d, 32 / d.sqDist()));
  480. } else {
  481. // spectate target
  482. var player = this.getSpecTarget();
  483. if (player) {
  484. this.setCenterPos(player.centerPos);
  485. var scale = player.getScale();
  486. this.place = player.place;
  487. this.viewBox = player.viewBox;
  488. this.viewNodes = player.viewNodes;
  489. }
  490. }
  491. // sends camera packet
  492. this.socket.packetHandler.sendPacket(new Packet.UpdatePosition(
  493. this, this.centerPos.x, this.centerPos.y, scale
  494. ));
  495. }
  496. }
  497.  
  498. PlayerTracker.prototype.pressSpace = function() {
  499. if (this.spectate) {
  500. // Check for spam first (to prevent too many add/del updates)
  501. if (this.gameServer.tickCounter - this.lastKeypressTick < 40)
  502. return;
  503. this.lastKeypressTick = this.gameServer.tickCounter;
  504.  
  505. // Space doesn't work for freeRoam mode
  506. if (this.freeRoam || this.gameServer.largestClient == null)
  507. return;
  508. } else if (this.gameServer.run) {
  509. // Disable mergeOverride on the last merging cell
  510. if (this.cells.length <= 2)
  511. this.mergeOverride = false;
  512. // Cant split if merging or frozen
  513. if (this.mergeOverride || this.frozen)
  514. return;
  515. this.gameServer.splitCells(this);
  516. }
  517. };
  518.  
  519. PlayerTracker.prototype.pressW = function() {
  520. if (this.spectate || !this.gameServer.run) return;
  521. this.gameServer.ejectMass(this);
  522. };
  523.  
  524. PlayerTracker.prototype.pressQ = function() {
  525. if (this.spectate) {
  526. // Check for spam first (to prevent too many add/del updates)
  527. if (this.gameServer.tickCounter - this.lastKeypressTick < 40)
  528. return;
  529.  
  530. this.lastKeypressTick = this.gameServer.tickCounter;
  531. if (this.spectateTarget == null)
  532. this.freeRoam = !this.freeRoam;
  533. this.spectateTarget = null;
  534. }
  535. };
  536.  
  537. PlayerTracker.prototype.getSpecTarget = function() {
  538. if (this.spectateTarget == null || this.spectateTarget.isRemoved) {
  539. this.spectateTarget = null;
  540. return this.gameServer.largestClient;
  541. }
  542. return this.spectateTarget;
  543. };
  544.  
  545. PlayerTracker.prototype.setCenterPos = function(p) {
  546. p.x = Math.max(p.x, this.gameServer.border.minx);
  547. p.y = Math.max(p.y, this.gameServer.border.miny);
  548. p.x = Math.min(p.x, this.gameServer.border.maxx);
  549. p.y = Math.min(p.y, this.gameServer.border.maxy);
  550. this.centerPos = p;
  551. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement