mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2025-05-11 07:50:55 +00:00
Added timeout to leadership election
This commit is contained in:
parent
3bdaaa9169
commit
6f5e927f70
2 changed files with 52 additions and 3 deletions
|
@ -20,6 +20,8 @@ function Gather () {
|
||||||
}
|
}
|
||||||
this.TEAM_SIZE = 6;
|
this.TEAM_SIZE = 6;
|
||||||
this.gatherers = [];
|
this.gatherers = [];
|
||||||
|
this.ELECTION_INTERVAL = 10000; // 10 Seconds (temporarily)
|
||||||
|
this.electionStartTime = null;
|
||||||
this.initState();
|
this.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +131,11 @@ Gather.prototype.marines = function () {
|
||||||
Gather.prototype.toJson = function () {
|
Gather.prototype.toJson = function () {
|
||||||
return {
|
return {
|
||||||
gatherers: this.gatherers,
|
gatherers: this.gatherers,
|
||||||
state: this.current
|
state: this.current,
|
||||||
|
election: {
|
||||||
|
startTime: this.electionStartTime,
|
||||||
|
interval: this.ELECTION_INTERVAL
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -198,25 +204,43 @@ StateMachine.create({
|
||||||
{ name: "initState", from: "none", to: "gathering" },
|
{ name: "initState", from: "none", to: "gathering" },
|
||||||
{ name: "addGatherer", from: "gathering", to: "election" },
|
{ name: "addGatherer", from: "gathering", to: "election" },
|
||||||
{ name: "selectLeader", from: "election", to: "selection" },
|
{ name: "selectLeader", from: "election", to: "selection" },
|
||||||
|
{ name: "electionTimeout", from: "election", to: "selection" },
|
||||||
{ name: "confirmSelection", from: "selection", to: "done" },
|
{ name: "confirmSelection", from: "selection", to: "done" },
|
||||||
{ name: "removeGatherer", from: ["gathering", "election", "selection"], to: "gathering" }
|
{ name: "removeGatherer", from: ["gathering", "election", "selection"], to: "gathering" }
|
||||||
],
|
],
|
||||||
callbacks: {
|
callbacks: {
|
||||||
|
// Gathering State
|
||||||
onbeforeaddGatherer: function (event, from, to, user) {
|
onbeforeaddGatherer: function (event, from, to, user) {
|
||||||
this.addUser(user);
|
this.addUser(user);
|
||||||
if (this.gatherers.length !== 12) {
|
if (this.gatherers.length !== 12) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Election State
|
||||||
onbeforeselectLeader: function (event, from, to, voter, candidate) {
|
onbeforeselectLeader: function (event, from, to, voter, candidate) {
|
||||||
this.voteForLeader(voter, candidate);
|
this.voteForLeader(voter, candidate);
|
||||||
if (this.leaderVotes().length !== 12) {
|
if (this.leaderVotes().length !== 12) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onbeforeremoveGatherer: function (event, from, to, user) {
|
|
||||||
this.removeUser(user);
|
onenterelection: function () {
|
||||||
|
// Setup timer for elections
|
||||||
|
var self = this;
|
||||||
|
self.electionStartTime = new Date();
|
||||||
|
setTimeout(function () {
|
||||||
|
if (self.can("electionTimeout")) {
|
||||||
|
self.electionTimeout();
|
||||||
|
}
|
||||||
|
}, self.ELECTION_INTERVAL);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onleaveelection: function () {
|
||||||
|
this.electionStartTime = null;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Selection State
|
||||||
onenterselection: function () {
|
onenterselection: function () {
|
||||||
// Remove all leaders and teams
|
// Remove all leaders and teams
|
||||||
this.gatherers.forEach(function (gatherer, index, array) {
|
this.gatherers.forEach(function (gatherer, index, array) {
|
||||||
|
@ -244,9 +268,14 @@ StateMachine.create({
|
||||||
this.assignAlienLeader(parseInt(rank.pop().candidate, 0));
|
this.assignAlienLeader(parseInt(rank.pop().candidate, 0));
|
||||||
this.assignMarineLeader(parseInt(rank.pop().candidate, 0));
|
this.assignMarineLeader(parseInt(rank.pop().candidate, 0));
|
||||||
},
|
},
|
||||||
|
|
||||||
onbeforeconfirmSelection: function (event, from, to, leader) {
|
onbeforeconfirmSelection: function (event, from, to, leader) {
|
||||||
return (this.aliens().length === this.TEAM_SIZE
|
return (this.aliens().length === this.TEAM_SIZE
|
||||||
&& this.marines().length === this.TEAM_SIZE);
|
&& this.marines().length === this.TEAM_SIZE);
|
||||||
|
},
|
||||||
|
|
||||||
|
onbeforeremoveGatherer: function (event, from, to, user) {
|
||||||
|
this.removeUser(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -43,6 +43,23 @@ describe("Gather Model:", function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Election Tmimeout", function () {
|
||||||
|
it ("starts a timer and transitions to next state when timer runs out", function (done) {
|
||||||
|
gather.ELECTION_INTERVAL = 100; // 1 second
|
||||||
|
assert.isNull(gather.electionStartTime);
|
||||||
|
gatherers.forEach(function (gatherer) {
|
||||||
|
gather.addGatherer(gatherer);
|
||||||
|
});
|
||||||
|
assert.equal(gather.current, "election");
|
||||||
|
assert.isNotNull(gather.electionStartTime);
|
||||||
|
setTimeout(function () {
|
||||||
|
assert.equal(gather.current, "selection");
|
||||||
|
assert.isNull(gather.electionStartTime);
|
||||||
|
done();
|
||||||
|
}, 200);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("Election", function () {
|
describe("Election", function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
gatherers.forEach(function (gatherer) {
|
gatherers.forEach(function (gatherer) {
|
||||||
|
@ -58,6 +75,7 @@ describe("Gather Model:", function () {
|
||||||
assert.equal(gather.leaderVotes().length, 1);
|
assert.equal(gather.leaderVotes().length, 1);
|
||||||
assert.equal(gather.leaderVotes()[0], candidate.id);
|
assert.equal(gather.leaderVotes()[0], candidate.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
it ("retains state of 'election' until all votes cast", function () {
|
it ("retains state of 'election' until all votes cast", function () {
|
||||||
var candidate = gatherers[0];
|
var candidate = gatherers[0];
|
||||||
gatherers.forEach(function (voter, index) {
|
gatherers.forEach(function (voter, index) {
|
||||||
|
@ -235,6 +253,8 @@ describe("Gather Model:", function () {
|
||||||
var output = gather.toJson();
|
var output = gather.toJson();
|
||||||
assert.isArray(output.gatherers);
|
assert.isArray(output.gatherers);
|
||||||
assert.isString(output.state);
|
assert.isString(output.state);
|
||||||
|
assert.isNull(output.election.startTime);
|
||||||
|
assert.equal(output.election.interval, gather.ELECTION_INTERVAL);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue