mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2025-05-31 01:11:07 +00:00
Clean up
This commit is contained in:
parent
c7c7c4eb24
commit
d850a3e34c
1 changed files with 128 additions and 160 deletions
|
@ -40,160 +40,6 @@ function Gather (options) {
|
||||||
this.initState();
|
this.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
Gather.prototype.alienLeader = () => {
|
|
||||||
return this.gatherers.reduce((acc, gatherer) => {
|
|
||||||
if (gatherer.team === "alien" && gatherer.leader) acc.push(gatherer);
|
|
||||||
return acc;
|
|
||||||
}, []).pop();
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.marineLeader = () => {
|
|
||||||
return this.gatherers.reduce((acc, gatherer) => {
|
|
||||||
if (gatherer.team === "marine" && gatherer.leader) acc.push(gatherer);
|
|
||||||
return acc;
|
|
||||||
}, []).pop();
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.assignMarineLeader = id => {
|
|
||||||
this.modifyGatherer({id: id}, gatherer => {
|
|
||||||
gatherer.leader = true;
|
|
||||||
gatherer.team = "marine";
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.assignAlienLeader = id => {
|
|
||||||
this.modifyGatherer({id: id}, gatherer => {
|
|
||||||
gatherer.leader = true;
|
|
||||||
gatherer.team = "alien";
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.containsUser = user => {
|
|
||||||
return this.gatherers.some(gatherer => {
|
|
||||||
return gatherer.id === user.id;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.addUser = user => {
|
|
||||||
if (this.containsUser(user)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
var gatherer = new Gatherer(user);
|
|
||||||
this.gatherers.push(gatherer);
|
|
||||||
return gatherer;
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.removeUser = user => {
|
|
||||||
this.gatherers = this.gatherers.filter(gatherer => user.id !== gatherer.id);
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.modifyGatherer = (user, callback) => {
|
|
||||||
this.gatherers.forEach(gatherer => {
|
|
||||||
if (gatherer.id === user.id) callback(gatherer);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Gather.prototype.moveToMarine = user => {
|
|
||||||
if (this.marines().length >= this.TEAM_SIZE) return;
|
|
||||||
this.modifyGatherer(user, gatherer => gatherer.team = "marine");
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.moveToAlien = user => {
|
|
||||||
if (this.aliens().length >= this.TEAM_SIZE) return;
|
|
||||||
this.modifyGatherer(user, gatherer => gatherer.team = "alien");
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.moveToLobby = user => {
|
|
||||||
this.gatherers.forEach((gatherer, index, array) => {
|
|
||||||
if (gatherer.id === user.id) {
|
|
||||||
gatherer.team = "lobby";
|
|
||||||
array[index] = gatherer;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.retrieveGroup = team => {
|
|
||||||
return this.gatherers.filter(gatherer => gatherer.team === team);
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.lobby = () => {
|
|
||||||
return this.retrieveGroup("lobby");
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.aliens = () => {
|
|
||||||
return this.retrieveGroup("alien");
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.marines = () => {
|
|
||||||
return this.retrieveGroup("marine");
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.toJson = () => {
|
|
||||||
return {
|
|
||||||
gatherers: this.gatherers,
|
|
||||||
state: this.current,
|
|
||||||
election: {
|
|
||||||
startTime: (this.electionStartTime === null) ? null : this.electionStartTime.toISOString(),
|
|
||||||
interval: this.ELECTION_INTERVAL
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.voteForMap = (voter, mapId) => {
|
|
||||||
this.gatherers.forEach((gatherer, index, array) => {
|
|
||||||
if (gatherer.id === voter.id) {
|
|
||||||
array[index].mapVote = mapId;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.voteForServer = (voter, serverId) => {
|
|
||||||
this.gatherers.forEach((gatherer, index, array) => {
|
|
||||||
if (gatherer.id === voter.id) {
|
|
||||||
array[index].serverVote = serverId;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Returns an array of IDs representing votes for leaders
|
|
||||||
Gather.prototype.leaderVotes = () => {
|
|
||||||
var self = this;
|
|
||||||
return self.gatherers.map(gatherer => gatherer.leaderVote)
|
|
||||||
.filter(leaderId => typeof leaderId === 'number')
|
|
||||||
.filter(candidate => {
|
|
||||||
return self.gatherers.some(gatherer => gatherer.id === candidate);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.voteForLeader = (voter, candidate) => {
|
|
||||||
// Find voter and then assign their vote to candidate id
|
|
||||||
this.gatherers.forEach((gatherer, index, array) => {
|
|
||||||
if (gatherer.id === voter.id) {
|
|
||||||
array[index].voteForLeader(candidate);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Gather.prototype.getGatherer = (user) => {
|
|
||||||
var matchingGatherer = null;
|
|
||||||
this.gatherers.forEach((gatherer) => {
|
|
||||||
if (gatherer.id === user.id) matchingGatherer = gatherer;
|
|
||||||
});
|
|
||||||
return matchingGatherer;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Gather States
|
|
||||||
// - Gathering
|
|
||||||
// - Election
|
|
||||||
// - Selection
|
|
||||||
// - Done
|
|
||||||
|
|
||||||
// gather.current - contains the current state
|
|
||||||
// gather.is(s) - return true if state s is the current state
|
|
||||||
// gather.can(e) - return true if event e can be fired in the current state
|
|
||||||
// gather.cannot(e) - return true if event e cannot be fired in the current state
|
|
||||||
// gather.transitions() - return list of events that are allowed from the current state
|
|
||||||
|
|
||||||
StateMachine.create({
|
StateMachine.create({
|
||||||
target: Gather.prototype,
|
target: Gather.prototype,
|
||||||
events: [
|
events: [
|
||||||
|
@ -213,17 +59,13 @@ StateMachine.create({
|
||||||
// Gathering State
|
// Gathering State
|
||||||
onbeforeaddGatherer: (event, from, to, user) => {
|
onbeforeaddGatherer: (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
|
// Election State
|
||||||
onbeforeselectLeader: (event, from, to, voter, candidate) => {
|
onbeforeselectLeader: (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;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onenterelection: () => {
|
onenterelection: () => {
|
||||||
|
@ -286,4 +128,130 @@ StateMachine.create({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Gather.prototype.alienLeader = () => {
|
||||||
|
return this.gatherers.reduce((acc, gatherer) => {
|
||||||
|
if (gatherer.team === "alien" && gatherer.leader) acc.push(gatherer);
|
||||||
|
return acc;
|
||||||
|
}, []).pop();
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.marineLeader = () => {
|
||||||
|
return this.gatherers.reduce((acc, gatherer) => {
|
||||||
|
if (gatherer.team === "marine" && gatherer.leader) acc.push(gatherer);
|
||||||
|
return acc;
|
||||||
|
}, []).pop();
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.assignMarineLeader = id => {
|
||||||
|
this.modifyGatherer({id: id}, gatherer => {
|
||||||
|
gatherer.leader = true;
|
||||||
|
gatherer.team = "marine";
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.assignAlienLeader = id => {
|
||||||
|
this.modifyGatherer({id: id}, gatherer => {
|
||||||
|
gatherer.leader = true;
|
||||||
|
gatherer.team = "alien";
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.containsUser = user => {
|
||||||
|
return this.gatherers.some(gatherer => {
|
||||||
|
return gatherer.id === user.id;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.addUser = user => {
|
||||||
|
if (this.containsUser(user)) return null;
|
||||||
|
var gatherer = new Gatherer(user);
|
||||||
|
this.gatherers.push(gatherer);
|
||||||
|
return gatherer;
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.removeUser = user => {
|
||||||
|
this.gatherers = this.gatherers.filter(gatherer => user.id !== gatherer.id);
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.modifyGatherer = (user, callback) => {
|
||||||
|
return this.gatherers
|
||||||
|
.filter(gatherer => gatherer.id === user.id)
|
||||||
|
.forEach(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
Gather.prototype.moveToMarine = user => {
|
||||||
|
if (this.marines().length >= this.TEAM_SIZE) return;
|
||||||
|
this.modifyGatherer(user, gatherer => gatherer.team = "marine");
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.moveToAlien = user => {
|
||||||
|
if (this.aliens().length >= this.TEAM_SIZE) return;
|
||||||
|
this.modifyGatherer(user, gatherer => gatherer.team = "alien");
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.moveToLobby = user => {
|
||||||
|
this.modifyGatherer(user, gatherer => gatherer.team = "lobby");
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.retrieveGroup = team => {
|
||||||
|
return this.gatherers.filter(gatherer => gatherer.team === team);
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.lobby = () => {
|
||||||
|
return this.retrieveGroup("lobby");
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.aliens = () => {
|
||||||
|
return this.retrieveGroup("alien");
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.marines = () => {
|
||||||
|
return this.retrieveGroup("marine");
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.electionTimer = () => {
|
||||||
|
return (this.electionStartTime === null) ? null : this.electionStartTime.toISOString();
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.toJson = () => {
|
||||||
|
return {
|
||||||
|
gatherers: this.gatherers,
|
||||||
|
state: this.current,
|
||||||
|
election: {
|
||||||
|
startTime: this.electionTimer(),
|
||||||
|
interval: this.ELECTION_INTERVAL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.voteForMap = (voter, mapId) => {
|
||||||
|
this.modifyGatherer(voter, gatherer => gatherer.mapVote = mapId);
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.voteForServer = (voter, serverId) => {
|
||||||
|
this.modifyGatherer(voter, gatherer => gatherer.serverVote = serverId);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns an array of IDs representing votes for leaders
|
||||||
|
Gather.prototype.leaderVotes = () => {
|
||||||
|
var self = this;
|
||||||
|
return self.gatherers.map(gatherer => gatherer.leaderVote)
|
||||||
|
.filter(leaderId => typeof leaderId === 'number')
|
||||||
|
.filter(candidate => {
|
||||||
|
return self.gatherers.some(gatherer => gatherer.id === candidate);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.voteForLeader = (voter, candidate) => {
|
||||||
|
this.modifyGatherer(voter, gatherer => gatherer.voteForLeader(candidate));
|
||||||
|
};
|
||||||
|
|
||||||
|
Gather.prototype.getGatherer = (user) => {
|
||||||
|
return this.gatherers
|
||||||
|
.filter(gatherer => gatherer.id === user.id)
|
||||||
|
.pop() || null;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = Gather;
|
module.exports = Gather;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue