Advertisement
Guest User

Untitled

a guest
Feb 4th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.58 KB | None | 0 0
  1. 'use strict';
  2.  
  3. class PassTheBomb extends Rooms.RoomGame {
  4. constructor(room, seconds) {
  5. super(room);
  6.  
  7. this.gameid = 'ptb';
  8. this.title = 'Pass The Bomb';
  9. this.players = new Map();
  10. this.round = 0;
  11. this.room = room;
  12. if (this.room.bombCount) {
  13. this.room.bombCount++;
  14. } else {
  15. this.room.bombCount = 1;
  16. }
  17. this.timeLeft = Date.now() + seconds * 1000;
  18.  
  19. this.room.add('|uhtml|bomb' + this.room.bombCount + this.round + '|<div class = "infobox"><center>A game of <strong>Pass the Bomb</strong> has been started!<br>' +
  20. 'The game will begin in <strong>' + seconds + '</strong> seconds!<br>' +
  21. '<button name = "send" value = "/passthebomb join">Join!</button></center></div>'
  22. );
  23. this.timer = setTimeout(() => {
  24. if (this.players.size < 3) {
  25. this.room.add('|uhtmlchange|bomb' + this.room.bombCount + this.round + '|<div class = "infobox"><center>This game of Pass the Bomb has been ended due to the lack of players.</center></div>').update();
  26. return this.end();
  27. }
  28. this.nextRound();
  29. }, seconds * 1000);
  30. }
  31. updateJoins() {
  32. let msg = 'bomb' + this.room.bombCount + this.round + '|<div class = "infobox"><center>A game of <strong>Pass the Bomb</strong> has been started!<br>' +
  33. 'The game will begin in <strong>' + Math.round((this.timeLeft - Date.now()) / 1000) + '</strong> seconds<br>' +
  34. '<button name = "send" value = "/passthebomb join">Join!</button></center>';
  35. if (this.players.size > 0) {
  36. msg += '<center><strong>' + this.players.size + '</strong> ' + (this.players.size === 1 ? 'user has' : 'users have') + ' joined: ' + Array.from(this.players).map(player => hashColors(player[1].name)).join(', ') + '</center>';
  37. }
  38. this.room.add('|uhtmlchange|' + msg + '</div>');
  39. }
  40. join(user, self) {
  41. if (this.round > 0) return self.sendReply('You cannot join a game of Pass The Bomb after it has been started.');
  42. if (!user.named) return self.errorReply("You must choose a name before joining a game of Pass The Bomb.");
  43. if (this.players.has(user.userid)) return self.sendReply('You have already joined this game of Pass The Bomb.');
  44.  
  45. let players = Array.from(this.players).map(playerinfo => playerinfo[1]);
  46. let joined = players.filter(player => player.ip === user.latestIp);
  47. if (joined.length) return self.errorReply("You have already joined this game of Pass the Bomb under the name '" + joined[0].name + "'. Use that name/alt instead.");
  48.  
  49. this.players.set(user.userid, {'name': user.name, 'ip': user.latestIp, 'status': 'alive', 'warnings': 0});
  50. this.updateJoins();
  51. }
  52. leave(userid, self) {
  53. if (!this.players.has(userid)) return self.sendReply('You haven\'t joined this game of Pass The Bomb yet.');
  54.  
  55. if (!this.round) {
  56. this.players.delete(userid);
  57. this.updateJoins();
  58. self.sendReply("You have left this game of Pass The Bomb.");
  59. } else {
  60. this.removeUser(userid, true);
  61. }
  62. }
  63. getSurvivors() {
  64. return Array.from(this.players).filter(player => player[1].status === 'alive');
  65. }
  66. setBomb(userid) {
  67. if (!userid) {
  68. let players = this.getSurvivors();
  69. this.holder = players[Math.floor(Math.random() * players.length)][0];
  70. } else {
  71. this.holder = userid;
  72. }
  73. }
  74. getMsg() {
  75. let msg = 'bomb' + this.room.bombCount + this.round + '|<div class = "infobox"><center><strong>Round ' + this.round + '</strong><br>' +
  76. 'Players: ' + this.getSurvivors().map(player => hashColors(player[1].name)).join(', ') +
  77. '<br><small>Use /pb or /passbomb [player] to Pass The Bomb to another player!</small>';
  78. return msg;
  79. }
  80. nextRound() {
  81. clearTimeout(this.timer);
  82. this.canPass = false;
  83. if (this.checkWinner()) return this.getWinner();
  84. this.players.forEach((details, user) => {
  85. if (this.players.get(user).status === 'alive') {
  86. this.players.get(user).warnings = 0;
  87. }
  88. });
  89.  
  90. this.round++;
  91. this.madeMove = false;
  92. this.room.add('|uhtml|' + this.getMsg() + '<br><i>Wait for it...</i></div>').update();
  93.  
  94. this.release = setTimeout(() => {
  95. this.setBomb();
  96. let player = this.players.get(this.holder).name;
  97. this.room.add('|uhtmlchange|' + this.getMsg() + '<br><strong style = "font-size: 10pt;">The bomb has been passed to </strong>' + hashColors(this.holder, true) + hashColors(player, true) + '</div>').update();
  98. this.canPass = true;
  99. this.resetTimer();
  100. }, (Math.floor(Math.random() * 12) + 3) * 1000);
  101. }
  102. pass(user, target, self) {
  103. let getUser = this.players.get(user.userid);
  104. if (!getUser) return self.sendReply("You aren't a player in this game of Pass the Bomb.");
  105. if (!this.round) return self.sendReply("The game hasn't started yet!");
  106.  
  107. if (getUser.status === 'dead') return self.sendReply("You've already been killed!");
  108.  
  109. if (!target || !target.trim()) return self.sendReply("You need to choose a player to Pass The Bomb to.");
  110.  
  111. let targetId = toId(target);
  112. let targetUser = Users.getExact(targetId) ? Users(targetId).name : target;
  113. if (!this.players.has(targetId)) return self.sendReply(targetUser + ' is not a player!');
  114. if (this.players.get(targetId).status === 'dead') return self.sendReply(this.players.get(targetId).name + ' has already been killed!');
  115. if (targetId === user.userid) return self.sendReply('You\'re already in possession of the bomb! You can\'t pass it to yourself!');
  116.  
  117. if (!this.canPass || this.holder !== user.userid) {
  118. if (getUser.warnings < 2) {
  119. this.players.get(user.userid).warnings++;
  120. return self.sendReply("You're not in posession of the bomb yet!");
  121. }
  122. this.removeUser(user.userid);
  123. self.sendReply("You have been disqualified for spamming /passbomb.");
  124. self.privateModCommand("(" + user.name + " was disqualified for spamming /passbomb.)");
  125. return;
  126. }
  127.  
  128. this.madeMove = true;
  129. this.setBomb(targetId);
  130. this.room.add('|html|' + hashColors(user.name) + ' passed the bomb to ' + hashColors(targetId, true) + this.players.get(targetId).name + '</strong>!');
  131.  
  132. if (this.checkWinner()) this.getWinner();
  133. }
  134. resetTimer() {
  135. this.timer = setTimeout(() => {
  136. let player = this.players.get(this.holder).name;
  137. this.room.add('|html|The bomb exploded and killed <span style = "' + hashColors(this.holder, true) + '">' + player + '</span>').update();
  138. this.players.get(this.holder).status = 'dead';
  139. this.canPass = false;
  140. setTimeout(() => {
  141. this.nextRound();
  142. this.room.update();
  143. }, 1200);
  144. }, (Math.floor(Math.random() * 26) + 5) * 1000);
  145. }
  146. dq(user, target, self) {
  147. if (!this.round) return self.sendReply('You can only disqualify a player after the first round has begun.');
  148. let targetId = toId(target);
  149.  
  150. let getUser = this.players.get(targetId);
  151. if (!getUser) return self.sendReply(target + ' is not a player!');
  152. if (getUser.status === 'dead') return self.sendReply(getUser.name + ' has already been killed!');
  153.  
  154. self.privateModCommand("(" + getUser.name + " was disqualified by " + user.name + ".)");
  155. this.removeUser(targetId);
  156. }
  157. removeUser(userid, left) {
  158. if (!this.players.has(userid)) return;
  159.  
  160. this.room.add('|html|' + hashColors(this.players.get(userid).name, true) + ' has ' + (left ? 'left' : 'been disqualified from') + ' the game.');
  161. this.players.delete(userid);
  162. this.madeMove = true;
  163. if (this.checkWinner()) {
  164. this.getWinner();
  165. } else if (!this.canPass) {
  166. this.room.add('|uhtmlchange|' + this.getMsg() + '<br><i>Wait for it...</i></div>').update();
  167. } else if (this.holder === userid) {
  168. this.setBomb();
  169. let player = this.players.get(this.holder).name;
  170. this.room.add('|html|The bomb has been passed to ' + hashColors(player, true) + '!').update();
  171. }
  172. }
  173. checkWinner() {
  174. if (this.getSurvivors().length === 1) return true;
  175. }
  176. getWinner() {
  177. let winner = this.getSurvivors()[0][1].name;
  178. let msg = `|html|<div class = "infobox"><center>The winner of this game of Pass the Bomb is ${hashColors(winner, true)}! Congratulations!</center>`;
  179. if (this.room.isOfficial) {
  180. Server.ExpControl.addExp(winner, this.room, 5);
  181. msg += `${hashColors(winner, true)} has also won 5 EXP for winning this game of Pass the Bomb.`;
  182. }
  183. this.room.add(msg).update();
  184. this.end();
  185. }
  186. end(user) {
  187. if (user) {
  188. let msg = '<div class = "infobox"><center>This game of Pass the Bomb has been forcibly ended by ' + hashColors(user.name, true) + '.</center></div>';
  189. if (!this.madeMove) {
  190. this.room.add('|uhtmlchange|bomb' + this.room.bombCount + this.round + '|' + msg).update();
  191. } else {
  192. this.room.add('|html|' + msg).update();
  193. }
  194. }
  195. if (this.release) clearTimeout(this.release);
  196. clearTimeout(this.timer);
  197. delete this.room.passthebomb;
  198. }
  199. }
  200.  
  201. let commands = {
  202. '': 'help',
  203. help: function () {
  204. this.parse('/help passthebomb');
  205. },
  206. 'new': 'start',
  207. begin: 'start',
  208. start: function (target, room, user) {
  209. if (room.passthebomb) return this.sendReply("There is already a game of Pass The Bomb going on in this room.");
  210. if (!this.canTalk()) return this.errorReply("You cannot use this while unable to speak.");
  211. if (!user.can('broadcast', null, room)) return this.sendReply("You must be ranked + or higher in this room to start a game of Pass The Bomb.");
  212.  
  213. if (!target || !target.trim()) target = '60';
  214. if (isNaN(target)) return this.sendReply('\'' + target + '\' is not a valid number.');
  215. if (target.includes('.') || target > 180 || target < 10) return this.sendReply('The number of seconds needs to be a non-decimal number between 10 and 180.');
  216.  
  217. room.passthebomb = new PassTheBomb(room, Number(target));
  218. },
  219. join: function (target, room, user) {
  220. if (!room.passthebomb) return this.sendReply("There is no game of Pass The Bomb going on in this room.");
  221. if (!this.canTalk()) return this.errorReply("You cannot use this while unable to speak.");
  222.  
  223. room.passthebomb.join(user, this);
  224. },
  225. leave: function (target, room, user) {
  226. if (!room.passthebomb) return this.sendReply("There is no game of Pass The Bomb going on in this room.");
  227.  
  228. room.passthebomb.leave(user.userid, this);
  229. },
  230. proceed: function (target, room, user) {
  231. if (!room.passthebomb) return this.sendReply("There is no game of Pass The Bomb going on in this room.");
  232. if (!this.canTalk()) return this.errorReply("You cannot use this while unable to speak.");
  233. if (!user.can('broadcast', null, room)) return this.sendReply("You must be ranked + or higher in this room to forcibly begin the first round of a game of Pass The Bomb.");
  234.  
  235. if (room.passthebomb.round) return this.sendReply('This game of Pass The Bomb has already begun!');
  236. if (room.passthebomb.players.size < 2) return this.sendReply('There aren\'t enough players yet. Wait for more to join!');
  237. room.add('(' + user.name + ' forcibly started round 1)');
  238. room.passthebomb.nextRound();
  239. },
  240. disqualify: 'dq',
  241. dq: function (target, room, user) {
  242. if (!room.passthebomb) return this.sendReply("There is no game of Pass The Bomb going on in this room.");
  243. if (!this.canTalk()) return this.errorReply("You cannot use this while unable to speak.");
  244. if (!user.can('mute', null, room)) return this.sendReply("You must be ranked % or higher in this room to disqualify a user from a game of Pass The Bomb.");
  245.  
  246. room.passthebomb.dq(user, target, this);
  247. },
  248. passbomb: 'pass',
  249. pass: function (target, room, user) {
  250. if (!room.passthebomb) return this.sendReply("There is no game of Pass The Bomb going on in this room.");
  251. if (!this.canTalk()) return this.errorReply("You cannot use this while unable to speak.");
  252.  
  253. room.passthebomb.pass(user, target, this);
  254. },
  255. cancel: 'end',
  256. end: function (target, room, user) {
  257. if (!room.passthebomb) return this.sendReply("There is no game of Pass The Bomb going on in this room.");
  258. if (!user.can('mute', null, room)) return this.sendReply("You must be ranked % or higher in this room to end a game of Pass The Bomb.");
  259.  
  260. room.passthebomb.end(user);
  261. },
  262. };
  263.  
  264. exports.commands = {
  265. ptb: 'passthebomb',
  266. passthebomb: commands,
  267. pb: 'passbomb',
  268. passbomb: commands.pass,
  269. passthebombhelp: [
  270. '/passthebomb start [seconds] - Starts a game of Pass The Bomb in the room. The first round will begin after the mentioned number of seconds (1 minute by default). Requires + or higher to use.',
  271. '/passthebomb join/leave - Joins/Leaves a game of Pass The Bomb.',
  272. '/passthebomb proceed - Forcibly starts the first round of a game. Requires + or higher to use.',
  273. '/passthebomb dq [user] - Disqualifies a player from a game of Pass The Bomb. Requires % or higher to use.',
  274. '/passthebomb pass [user] - Passes the bomb to another player. (NOTE: Spamming this can get you disqualified)',
  275. '/passthebomb end - Forcibly ends a game of Pass The Bomb. Requires % or higher to use.',
  276. '(/ptb is a valid alias for /passthebomb)',
  277. ],
  278. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement