From 4b44cf1801065cbfe7dc665f4631f3108cffbea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=A4=AA?= Date: Sun, 22 Dec 2013 09:24:05 +1100 Subject: [PATCH] Only run updates sent by the server --- js/client-chat-tournament.js | 111 ++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 47 deletions(-) diff --git a/js/client-chat-tournament.js b/js/client-chat-tournament.js index ce25e2a38..63010371a 100644 --- a/js/client-chat-tournament.js +++ b/js/client-chat-tournament.js @@ -127,6 +127,7 @@ this.$challengeAccept = $wrapper.find('.tournament-challenge-accept'); this.info = {}; + this.updates = {}; this.savedBracketPosition = {}; var self = this; @@ -225,69 +226,85 @@ break; case 'update': - $.extend(this.info, JSON.parse(data.join('|'))); + $.extend(this.updates, JSON.parse(data.join('|'))); break; case 'updateend': if (!this.info.isActive) { this.$wrapper.addClass("active"); - this.$box.addClass("active"); + if (this.info.isJoined) + this.$box.addClass("active"); this.info.isActive = true; } - - this.$format.text(BattleFormats[this.info.format].name); - this.$generator.text(this.info.generator); + + this.$format.text(BattleFormats[this.updates.format].name); + this.$generator.text(this.updates.generator); // Update the toolbox - this.$join.toggleClass('active', !this.info.isStarted && !this.info.isJoined); - this.$leave.toggleClass('active', !this.info.isStarted && this.info.isJoined); - this.$tools.toggleClass('active', !this.info.isStarted || this.info.isJoined); + this.$join.toggleClass('active', !this.updates.isStarted && !this.updates.isJoined); + this.$leave.toggleClass('active', !this.updates.isStarted && this.updates.isJoined); + this.$tools.toggleClass('active', !this.updates.isStarted || this.updates.isJoined); // Update the bracket - var $bracket = this.generateBracket(this.info.bracketData); - this.$bracket.empty(); - this.$bracket.removeClass('tournament-bracket-overflowing'); - if ($bracket) { - this.$bracket.append($bracket); - this.updateLayout(); + if ('bracketData' in this.updates) { + var $bracket = this.generateBracket(this.updates.bracketData); + this.$bracket.empty(); + this.$bracket.removeClass('tournament-bracket-overflowing'); + if ($bracket) { + this.$bracket.append($bracket); + this.updateLayout(); + } } - // Update the challenges + if (this.updates.isStarted) { + // Update the challenges + if ('challenges' in this.updates) { + this.$challenge.toggleClass('active', this.updates.challenges.length > 0); + if (this.updates.challenges.length > 0) { + this.$challengeUser.html(this.renderChallengeUsers()); + this.$challengeTeam.html(app.rooms[''].renderTeams(this.updates.format)); + this.$challengeTeam.children().data('type', 'challengeTeam'); + this.$challengeTeam.children().attr('name', 'tournamentButton'); + + this.setBoxVisibility(true); + this.room.notifyOnce("Tournament challenges available", "Room: " + this.room.title, 'tournament-challenges'); + } + } + + if ('challengeBys' in this.updates) { + this.$challengeBy.toggleClass('active', this.updates.challengeBys.length > 0); + if (this.updates.challengeBys.length > 0) + this.$challengeBy.text((this.updates.challenges.length > 0 ? "Or" : "Please") + " wait for " + arrayToPhrase(this.updates.challengeBys, "or") + " to challenge you."); + } + + if ('challenging' in this.updates) { + this.$challenging.toggleClass('active', !!this.updates.challenging); + if (this.updates.challenging) { + this.$challenging.text("Challenging " + this.updates.challenging + "..."); + // TODO: Add cancel button + } + } + + if ('challenged' in this.updates) { + this.$challenged.toggleClass('active', !!this.updates.challenged); + if (this.updates.challenged) { + this.$challengedMessage.text(this.updates.challenged + " has challenged you."); + this.$challengeTeam.html(app.rooms[''].renderTeams(this.updates.format)); + this.$challengeTeam.children().data('type', 'challengeTeam'); + this.$challengeTeam.children().attr('name', 'tournamentButton'); + + this.setBoxVisibility(true); + this.room.notifyOnce("Tournament challenge from " + this.updates.challenged, "Room: " + this.room.title, 'tournament-challenged'); + } + } + } + + $.extend(this.info, this.updates); + this.updates = {}; + this.$noMatches.toggleClass('active', this.info.isStarted && this.info.challenges.length === 0 && this.info.challengeBys.length === 0 && !this.info.challenging && !this.info.challenged); - this.$challenge.toggleClass('active', this.info.challenges.length > 0); - this.$challengeBy.toggleClass('active', this.info.challengeBys.length > 0); - this.$challenging.toggleClass('active', !!this.info.challenging); - this.$challenged.toggleClass('active', !!this.info.challenged); - - if (this.info.challenges.length > 0) { - this.$challengeUser.html(this.renderChallengeUsers()); - this.$challengeTeam.html(app.rooms[''].renderTeams(this.info.format)); - this.$challengeTeam.children().data('type', 'challengeTeam'); - this.$challengeTeam.children().attr('name', 'tournamentButton'); - - this.setBoxVisibility(true); - this.room.notifyOnce("Tournament challenges available", "Room: " + this.room.title, 'tournament-challenges'); - } - - if (this.info.challengeBys.length > 0) - this.$challengeBy.text((this.info.challenges.length > 0 ? "Or" : "Please") + " wait for " + arrayToPhrase(this.info.challengeBys, "or") + " to challenge you."); - - if (this.info.challenging) { - this.$challenging.text("Challenging " + this.info.challenging + "..."); - // TODO: Add cancel button - } - - if (this.info.challenged) { - this.$challengedMessage.text(this.info.challenged + " has challenged you."); - this.$challengeTeam.html(app.rooms[''].renderTeams(this.info.format)); - this.$challengeTeam.children().data('type', 'challengeTeam'); - this.$challengeTeam.children().attr('name', 'tournamentButton'); - - this.setBoxVisibility(true); - this.room.notifyOnce("Tournament challenge from " + this.info.challenged, "Room: " + this.room.title, 'tournament-challenged'); - } break;