Skip to content

Commit

Permalink
Added a way to stop spectating battles
Browse files Browse the repository at this point in the history
Requires a little effort on the side of the person maintaining the
server to let “leave” Relay messages through.
  • Loading branch information
varkor committed Jan 7, 2016
1 parent 04ca225 commit 2d2147d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
45 changes: 45 additions & 0 deletions battle/scripts/objects/general/BattleContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ function BattleContext (client) {
originalContext.font = Font.load(16 * Game.zoom, "bold");
originalContext.fillTextHD(Math.ceil(Math.max(0, timeLeft / 1000)), centre.x, centre.y);
}
if (battleContext.playerIsSpectating()) {
var position = battleContext.drawing.spectatingBar();
originalContext.fillStyle = "hsla(0, 0%, 0%, 0.8)";
originalContext.fillRectHD(position.x, position.y, position.width, position.height);
originalContext.fillStyle = "hsla(0, 0%, 100%, 1)";
originalContext.textAlign = "center";
originalContext.textBaseline = "middle";
originalContext.font = Font.load(10 * Game.zoom);
if (!Cursor.inArea(originalCanvas, position.x, position.y, position.width, position.height)) {
originalContext.fillTextHD("Spectating", (originalCanvasWidth / 2 + 10) * Game.zoom, position.y + position.height / 2);
if (Math.floor(now / 1000) % 2) {
originalContext.fillStyle = "hsl(0, 100%, 50%)";
originalContext.fillCircleHD(position.x + 15 * Game.zoom, position.y + position.height / 2, 3 * Game.zoom);
}
originalCanvas.classList.remove("hover");
} else {
originalContext.fillTextHD("Leave battle?", position.x + position.width / 2, position.y + position.height / 2);
originalCanvas.classList.add("hover");
}
}
}
}
} : {});
Expand Down Expand Up @@ -541,6 +561,28 @@ function BattleContext (client) {
position.scale = 1;
}
return position;
},
spectatingBar : function () {
var width = 80 * Game.zoom;
return {
x : (battleContext.canvas.width / window.devicePixelRatio * Game.zoom - width) / 2,
y : 0,
width,
height : 14 * Game.zoom
};
},
respondToClick : function () {
if (battleContext.playerIsSpectating()) {
var position = battleContext.drawing.spectatingBar();
if (Cursor.inArea(battleContext.canvas, position.x, position.y, position.x + position.width, position.y + position.height)) {
Relay.pass("leave", null, battleContext.identifier);
battleContext.end({
"outcome" : "termination"
}, true);
battleContext.canvas.classList.remove("hover");
return true;
}
}
}
},
all : function (excludeNoPokemon) {
Expand Down Expand Up @@ -1192,6 +1234,9 @@ function BattleContext (client) {
playerIsParticipating : function () {
return !battleContext.process && Game.player !== null && battleContext.alliedTrainers.contains(Game.player);
},
playerIsSpectating : function () {
return !battleContext.playerIsParticipating();
},
flushInputs : function () {
// Sends any inputs the player has made since the inputs were last flushed, to the server
// This is done after every set of inputs has been made at the start of the turn, and whenever extra input is required, such as when a Pokémon faints and the player has to decide which one to send out next
Expand Down
10 changes: 9 additions & 1 deletion battle/scripts/objects/unique/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,15 @@ Cursor = {
},
click : function () {
Input.controlScheme = "mouse";
if (typeof Textbox === "object" && Textbox.canvas) {
var bubble = true;
if (bubble && typeof Battle === "object" && Battle.canvas) {
if (Cursor.inArea(Battle.canvas, 0, 0, Battle.canvas.width, Battle.canvas.height)) {
if (Battle.drawing.respondToClick()) {
bubble = false;
}
}
}
if (bubble && typeof Textbox === "object" && Textbox.canvas) {
if (Cursor.inArea(Textbox.canvas, 0, 0, Textbox.canvas.width, Textbox.canvas.height)) {
Textbox.draw(); // Redraws the textbox so that the hovered response is updated on touchscreen devices
Textbox.progress();
Expand Down
15 changes: 15 additions & 0 deletions battle/scripts/objects/unique/Supervisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ Supervisor = {
return {
success : true
};
case "leave":
// A party stops spectating a battle
// data: parties
if (!data.hasOwnProperty("parties"))
return unsuccessful("The parameter `data` should have had a `parties` property.");
if (!Array.isArray(data.parties))
return unsuccessful("The parameter `data.parties` should have been an array.");
var process = Supervisor.processes[identifier];
foreach(data.parties, function (party) {
process.parties.removeElementsOfValue(party);
Supervisor.send(party, "terminate", "stopped spectating", identifier);
});
return {
success: true
};
case "terminate":
// Terminates a battle that is in progress
// data: reason
Expand Down

0 comments on commit 2d2147d

Please sign in to comment.