Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pausing #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 82 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ var GAMERULES = {
TRANSFORM_TIME: 2000,
MATCH_WAIT_TIME: 10000,
GAME_START_DELAY: 2125,
TIME_UNTIL_BOT: 30000
TIME_UNTIL_BOT: 30000,
MAX_PAUSE_TIME: 120000
};

var applyDefault = function(shown, def) {
Expand Down Expand Up @@ -412,6 +413,7 @@ var startGame = function(name) {
}
games[name].data = {nodes: nodes, removed: []};
broadcast("gameinfo:"+JSON.stringify(games[name].data), games[name]);
games[name].data.paused = 0;
setTimeout(function() {
// make sure that game still exists before starting it
if (name in games) {
Expand All @@ -421,6 +423,15 @@ var startGame = function(name) {
}, GAMERULES.GAME_START_DELAY);
};

var unpaused = function(gd) {
var groups = gd.data.unitgroups;
var time = new Date().getTime();
debugMsg(gd, "pausing", "Adding " + (time - gd.data.paused) + " ms to all UnitGroup durations to adjust for pausing");
for (var i = 0; groups.length; i++) {
groups[i].duration += (time - gd.data.paused);
}
};

var lastGroupID = 0;
var nextGroupID = function() {
return ++lastGroupID;
Expand Down Expand Up @@ -552,32 +563,37 @@ var commands = {
var ind = gd.users.indexOf(d.user);
if(ind>-1) {
if("data" in gd && gd.data.gameStarted) {
var owner = adjustForRemoved(gd, ind);
if(src >= 0 && src < gd.data.nodes.length && dst >= 0 && dst < gd.data.nodes.length) {
if(owner === gd.data.nodes[src].owner || gd.data.nodes[src].units[owner] > 0) {
var size = Math.floor(gd.data.nodes[src].units[owner]);
gd.data.nodes[src].units[ind] -= size;
var group = {
source: src,
dest: dst,
duration: Math.round(distance(gd.data.nodes[src], gd.data.nodes[dst])/gd.data.nodes[src].unitSpeed),
size: size,
owner: owner,
id: nextGroupID()
};
broadcast("send:"+JSON.stringify(group), gd);
group.start = new Date().getTime();
if(!("unitgroups" in gd.data)) {
gd.data.unitgroups = [];
if (gd.data.paused === 0) {
var owner = adjustForRemoved(gd, ind);
if(src >= 0 && src < gd.data.nodes.length && dst >= 0 && dst < gd.data.nodes.length) {
if(owner === gd.data.nodes[src].owner || gd.data.nodes[src].units[owner] > 0) {
var size = Math.floor(gd.data.nodes[src].units[owner]);
gd.data.nodes[src].units[ind] -= size;
var group = {
source: src,
dest: dst,
duration: Math.round(distance(gd.data.nodes[src], gd.data.nodes[dst])/gd.data.nodes[src].unitSpeed),
size: size,
owner: owner,
id: nextGroupID()
};
broadcast("send:"+JSON.stringify(group), gd);
group.start = new Date().getTime();
if(!("unitgroups" in gd.data)) {
gd.data.unitgroups = [];
}
gd.data.unitgroups.push(group);
}
else {
d.conn.send("error:You don't own that node.");
}
gd.data.unitgroups.push(group);
}
else {
d.conn.send("error:You don't own that node.");
d.conn.send("error:That node doesn't exist.");
}
}
else {
d.conn.send("error:That node doesn't exist.");
d.conn.send("error:Game is paused.");
}
}
else {
Expand Down Expand Up @@ -637,6 +653,38 @@ var commands = {
d.conn.send("error:You're not in a room.");
}
}
},
pause: {
data: true,
handler: function(d) {
for(var id in games) {
var gd = games[id];
var ind = gd.users.indexOf(d.user);
if(ind>-1) {
if("data" in gd && gd.data.gameStarted) {
if (gd.data.paused === 0) {
// pause game
if (gd.data.pauses[adjustForRemoved(ind)] > 0) {
debugMsg(gd, "pausing", "Pausing the game at the request of " + d.user);
gd.data.pauses[adjustForRemoved(gd, ind)]--;
gd.data.paused = new Date().getTime();
broadcast("pause:" + d.user + "," + gd.data.pauses[adjustForRemoved(ind)], gd);
}
} else {
// unpause game
debugMsg(gd, "pausing", "Unpausing the game at the request of " + d.user);
gd.data.paused = 0;
broadcast("unpause:" + d.user + ",", gd);
unpaused(gd);
}
} else {
d.conn.send("error:Game not started");
}
return;
}
}
d.conn.send("error:You're not in a room.");
}
}
};

Expand Down Expand Up @@ -730,6 +778,19 @@ var tick = function() {
for(var id in games) {
var gd = games[id];
if("data" in gd) {
if (gd.data.paused !== 0) {
// game is paused, don't do a normal tick
if (time - gd.data.paused > GAMERULES.MAX_PAUSE_TIME) {
// game has been paused too long, unpause it
debugMsg(gd, "pausing", "Game has been paused for too long, unpausing");
gd.data.paused = 0;
broadcast("unpause", gd);
unpaused(gd);
} else {
debugMsg(gd, "pausing", "Skipping a tick because the game is paused");
continue;
}
}
var groupsUncontested = true;
var unitsUncontested = true;
if("unitgroups" in gd.data && gd.data.unitgroups.length > 0) {
Expand Down