From 122a4d9a6eaaf35e201dc3ab04601788145f1de7 Mon Sep 17 00:00:00 2001 From: ascriptmaster Date: Sun, 12 Oct 2014 19:11:38 -0700 Subject: [PATCH] BattleRoom refactor to support tournaments better Generalized the BattleRoom constructor function (and the functions that call it) to pass an arbitrary `options` object instead of a boolean for `rated` only. Also, removed mention of `parent` or `parentid` in the constructor and functions because it's no longer needed or used. Added support to the BattleRoom constructor to let it process tournaments properly. Now, instead of having the tournament forcefully override the BattleRoom.win function, the BattleRoom itself can handle the callback for when a tournament participant wins the battle. Added a check for tournament battles to prevent users who aren't part of that specific tournament battle from entering the battle. --- rooms.js | 53 +++++++++++++++++++++++++++++++++++--------- tournaments/index.js | 8 +------ users.js | 2 +- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/rooms.js b/rooms.js index c5ecd95f68..4568bb7a66 100644 --- a/rooms.js +++ b/rooms.js @@ -395,7 +395,7 @@ var GlobalRoom = (function () { this.cancelSearch(user, true); this.cancelSearch(searchUser, true); user.send('|updatesearch|' + JSON.stringify({searching: false})); - this.startBattle(searchUser, user, search.formatid, true, search.team, newSearch.team); + this.startBattle(searchUser, user, search.formatid, search.team, newSearch.team, {rated: true}); return; } } @@ -520,7 +520,7 @@ var GlobalRoom = (function () { --this.userCount; this.cancelSearch(user, true); }; - GlobalRoom.prototype.startBattle = function (p1, p2, format, rated, p1team, p2team) { + GlobalRoom.prototype.startBattle = function (p1, p2, format, p1team, p2team, options) { var newRoom; p1 = Users.get(p1); p2 = Users.get(p2); @@ -554,7 +554,7 @@ var GlobalRoom = (function () { } this.lastBattle = i; rooms.global.writeNumRooms(); - newRoom = this.addRoom('battle-' + formaturlid + '-' + i, format, p1, p2, this.id, rated); + newRoom = this.addRoom('battle-' + formaturlid + '-' + i, format, p1, p2, options); p1.joinRoom(newRoom); p2.joinRoom(newRoom); newRoom.joinBattle(p1, p1team); @@ -564,7 +564,7 @@ var GlobalRoom = (function () { if (Config.reportbattles && rooms.lobby) { rooms.lobby.add('|b|' + newRoom.id + '|' + p1.getIdentity() + '|' + p2.getIdentity()); } - if (Config.logladderip && rated) { + if (Config.logladderip && options.rated) { if (!this.ladderIpLog) { this.ladderIpLog = fs.createWriteStream('logs/ladderip/ladderip.txt', {flags: 'a'}); } @@ -573,8 +573,8 @@ var GlobalRoom = (function () { } return newRoom; }; - GlobalRoom.prototype.addRoom = function (room, format, p1, p2, parent, rated) { - room = Rooms.createBattle(room, format, p1, p2, parent, rated); + GlobalRoom.prototype.addRoom = function (room, format, p1, p2, options) { + room = Rooms.createBattle(room, format, p1, p2, options); return room; }; GlobalRoom.prototype.chat = function (user, message, connection) { @@ -588,7 +588,7 @@ var GlobalRoom = (function () { })(); var BattleRoom = (function () { - function BattleRoom(roomid, format, p1, p2, parentid, rated) { + function BattleRoom(roomid, format, p1, p2, options) { Room.call(this, roomid, "" + p1.name + " vs. " + p2.name); this.modchat = (Config.battlemodchat || false); @@ -600,7 +600,13 @@ var BattleRoom = (function () { var formatid = toId(format); - if (rated && Tools.getFormat(formatid).rated !== false) { + // Sometimes we might allow BattleRooms to have no options + if (!options) { + options = {}; + } + + var rated; + if (options.rated && Tools.getFormat(formatid).rated !== false) { rated = { p1: p1.userid, p2: p2.userid, @@ -610,10 +616,20 @@ var BattleRoom = (function () { rated = false; } + if (options.tour) { + this.tour = { + p1: p1.userid, + p2: p2.userid, + format: format, + tour: options.tour + }; + } else { + this.tour = false; + } + this.rated = rated; this.battle = Simulator.create(this.id, format, rated, this); - this.parentid = parentid || ''; this.p1 = p1 || ''; this.p2 = p2 || ''; @@ -724,6 +740,10 @@ var BattleRoom = (function () { }); } } + if (this.tour) { + var tour = this.tour.tour; + tour.onBattleWin(this, winner); + } rooms.global.battleCount += 0 - (this.active ? 1 : 0); this.active = false; this.update(); @@ -1098,6 +1118,17 @@ var BattleRoom = (function () { } } + if (this.tour) { + if (this.tour.p1 === user.userid) { + slot = 0; + } else if (this.tour.p2 === user.userid) { + slot = 1; + } else { + user.popup("This is a tournament battle; your username must be " + this.tour.p1 + " or " + this.tour.p2 + " to join."); + return false; + } + } + if (this.battle.active) { user.popup("This battle already has two players."); return false; @@ -1464,7 +1495,7 @@ Rooms.search = function (name, fallback) { return getRoom(name) || getRoom(toId(name)) || Rooms.aliases[toId(name)] || (fallback ? rooms.global : undefined); }; -Rooms.createBattle = function (roomid, format, p1, p2, parent, rated) { +Rooms.createBattle = function (roomid, format, p1, p2, options) { if (roomid && roomid.id) return roomid; if (!p1 || !p2) return false; if (!roomid) roomid = 'default'; @@ -1472,7 +1503,7 @@ Rooms.createBattle = function (roomid, format, p1, p2, parent, rated) { // console.log("NEW BATTLE ROOM: " + roomid); ResourceMonitor.countBattle(p1.latestIp, p1.name); ResourceMonitor.countBattle(p2.latestIp, p2.name); - rooms[roomid] = new BattleRoom(roomid, format, p1, p2, parent, rated); + rooms[roomid] = new BattleRoom(roomid, format, p1, p2, options); } return rooms[roomid]; }; diff --git a/tournaments/index.js b/tournaments/index.js index 9a5cbe4a6d..c420a01614 100644 --- a/tournaments/index.js +++ b/tournaments/index.js @@ -630,7 +630,7 @@ Tournament = (function () { if (!this.pendingChallenges.get(challenge.from)) return; if (!this.pendingChallenges.get(user)) return; - var room = Rooms.global.startBattle(challenge.from, user, this.format, this.isRated, challenge.team, user.team); + var room = Rooms.global.startBattle(challenge.from, user, this.format, challenge.team, user.team, {rated: this.isRated, tour: this}); if (!room) return; this.pendingChallenges.set(challenge.from, null); @@ -644,12 +644,6 @@ Tournament = (function () { this.isBracketInvalidated = true; this.runAutoDisqualify(); this.update(); - - var self = this; - room.win = function (winner) { - self.onBattleWin(this, Users.get(winner)); - return Object.getPrototypeOf(this).win.call(this, winner); - }; }; Tournament.prototype.onBattleWin = function (room, winner) { var from = Users.get(room.p1); diff --git a/users.js b/users.js index c60927c0bd..3fc867b06e 100644 --- a/users.js +++ b/users.js @@ -1460,7 +1460,7 @@ User = (function () { } return false; } - Rooms.global.startBattle(this, user, user.challengeTo.format, false, this.team, user.challengeTo.team); + Rooms.global.startBattle(this, user, user.challengeTo.format, this.team, user.challengeTo.team, {rated: false}); delete this.challengesFrom[user.userid]; user.challengeTo = null; this.updateChallenges();