mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2024-11-22 12:41:11 +00:00
Added gather model methods
This commit is contained in:
parent
9d17bf96d9
commit
11b2ad7bd3
4 changed files with 183 additions and 7 deletions
|
@ -21,20 +21,78 @@ function Gather () {
|
|||
this.gatherers = [];
|
||||
}
|
||||
|
||||
Gather.prototype.addGatherer = function (user) {
|
||||
Gather.prototype.containsUser = function (user) {
|
||||
return this.gatherers.some(function (gatherer) {
|
||||
return gatherer.id === user.id;
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.addUser = function (user) {
|
||||
if (this.containsUser(user)) {
|
||||
return null;
|
||||
}
|
||||
var gatherer = new Gatherer(user);
|
||||
this.gatherers.push(gatherer);
|
||||
return gatherer;
|
||||
};
|
||||
|
||||
Gather.prototype.removeGatherer = function (user) {
|
||||
|
||||
Gather.prototype.removeUser = function (user) {
|
||||
this.gatherers = this.gatherers.filter(function (gatherer) {
|
||||
return user.id !== gatherer.id;
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.moveToMarine = function (user) {
|
||||
|
||||
this.gatherers.forEach(function (gatherer, index, array) {
|
||||
if (gatherer.id === user.id) {
|
||||
gatherer.team = "marine";
|
||||
array[index] = gatherer;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.moveToAlien = function (user) {
|
||||
|
||||
this.gatherers.forEach(function (gatherer, index, array) {
|
||||
if (gatherer.id === user.id) {
|
||||
gatherer.team = "alien";
|
||||
array[index] = gatherer;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.moveToLobby = function (user) {
|
||||
this.gatherers.forEach(function (gatherer, index, array) {
|
||||
if (gatherer.id === user.id) {
|
||||
gatherer.team = "lobby";
|
||||
array[index] = gatherer;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Gather.prototype.retrieveGroup = function (team) {
|
||||
return this.gatherers.filter(function (gatherer) {
|
||||
return gatherer.team === team;
|
||||
});
|
||||
}
|
||||
|
||||
Gather.prototype.lobby = function () {
|
||||
return this.retrieveGroup("lobby");
|
||||
};
|
||||
|
||||
Gather.prototype.aliens = function () {
|
||||
return this.retrieveGroup("alien");
|
||||
};
|
||||
|
||||
Gather.prototype.marines = function () {
|
||||
return this.retrieveGroup("marine");
|
||||
};
|
||||
|
||||
Gather.prototype.toJson = function () {
|
||||
return {
|
||||
lobby: this.lobby(),
|
||||
marines: this.marines(),
|
||||
aliens: this.aliens()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Gather;
|
|
@ -7,7 +7,7 @@
|
|||
* - user data
|
||||
* - voting preferences
|
||||
* - leader status
|
||||
*
|
||||
* - Team: "lobby" "alien" "marine"
|
||||
*/
|
||||
|
||||
var User = require("../user/user");
|
||||
|
@ -15,8 +15,9 @@ var User = require("../user/user");
|
|||
function Gatherer (user) {
|
||||
this.votes = {};
|
||||
this.id = user.id;
|
||||
this.user = user;
|
||||
this.captain = false;
|
||||
this.team = "lobby";
|
||||
this.team = "lobby";
|
||||
}
|
||||
|
||||
module.exports = Gatherer;
|
85
spec/gather.js
Normal file
85
spec/gather.js
Normal file
|
@ -0,0 +1,85 @@
|
|||
"use strict";
|
||||
|
||||
var helper = require("./helpers/index.js");
|
||||
var User = helper.User;
|
||||
var Gather = helper.Gather;
|
||||
var Gatherer = helper.Gatherer;
|
||||
var assert = require("chai").assert;
|
||||
|
||||
describe("Gather model", function () {
|
||||
var user, gather;
|
||||
beforeEach(function () {
|
||||
user = helper.createUser();
|
||||
gather = Gather();
|
||||
});
|
||||
describe("addUser", function () {
|
||||
it ("adds gatherer to lobby", function () {
|
||||
gather.addUser(user);
|
||||
assert.equal(gather.gatherers.length, 1);
|
||||
assert.equal(gather.gatherers[0].id, user.id);
|
||||
});
|
||||
it ("does not add duplicate users", function () {
|
||||
gather.addUser(user);
|
||||
gather.addUser(user);
|
||||
assert.equal(gather.gatherers.length, 1);
|
||||
});
|
||||
});
|
||||
describe("removeUser", function () {
|
||||
it ("removes gatherer altogether", function () {
|
||||
gather.addUser(user);
|
||||
assert.equal(gather.gatherers.length, 1);
|
||||
assert.equal(gather.gatherers[0].id, user.id);
|
||||
gather.removeUser(user);
|
||||
assert.equal(gather.gatherers.length, 0);
|
||||
});
|
||||
});
|
||||
describe("moveToMarine", function () {
|
||||
it ("moves a player to marine", function () {
|
||||
gather.addUser(user);
|
||||
gather.moveToMarine(user);
|
||||
assert.equal(gather.marines().length, 1);
|
||||
assert.equal(gather.marines()[0].id, user.id);
|
||||
});
|
||||
});
|
||||
describe("moveToAlien", function () {
|
||||
it ("moves a player to alien", function () {
|
||||
gather.addUser(user);
|
||||
gather.moveToAlien(user);
|
||||
assert.equal(gather.aliens().length, 1);
|
||||
assert.equal(gather.aliens()[0].id, user.id);
|
||||
});
|
||||
});
|
||||
describe("moveToLobby", function () {
|
||||
it ("moves a player to lobby", function () {
|
||||
gather.addUser(user);
|
||||
gather.moveToAlien(user);
|
||||
assert.equal(gather.aliens().length, 1);
|
||||
gather.moveToLobby(user);
|
||||
assert.equal(gather.lobby().length, 1);
|
||||
assert.equal(gather.lobby()[0].id, user.id);
|
||||
});
|
||||
});
|
||||
describe("aliens", function () {
|
||||
it ("returns all gatherers in aliens", function () {
|
||||
gather.addUser(user);
|
||||
gather.moveToAlien(user);
|
||||
assert.equal(gather.aliens().length, 1);
|
||||
});
|
||||
});
|
||||
describe("marines", function () {
|
||||
it ("returns all gatherers in marines", function () {
|
||||
gather.addUser(user);
|
||||
gather.moveToMarine(user);
|
||||
assert.equal(gather.marines().length, 1);
|
||||
});
|
||||
});
|
||||
describe("lobby", function () {
|
||||
it ("returns all gatherers in lobby", function () {
|
||||
gather.addUser(user);
|
||||
assert.equal(gather.lobby().length, 1);
|
||||
});
|
||||
});
|
||||
describe("toJson", function () {
|
||||
it ("returns a json representation of the gather instance");
|
||||
});
|
||||
});
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var extend = require("extend");
|
||||
|
||||
var helpers = {}
|
||||
|
||||
|
@ -9,4 +10,35 @@ helpers.server = require(path.join(__dirname, "../../index.js"));
|
|||
|
||||
helpers.app = helpers.server.app;
|
||||
|
||||
// Models
|
||||
var User = helpers.User = require(path.join(__dirname, "../../lib/user/user"));
|
||||
var Gather = helpers.Gather = require(path.join(__dirname, "../../lib/gather/gather"));
|
||||
var Gatherer = helpers.Gatherer = require(path.join(__dirname, "../../lib/gather/gatherer"));
|
||||
|
||||
|
||||
// Create User Method
|
||||
// Each user will have unique ID, username and steam attributes
|
||||
var createUser = helpers.createUser = (function () {
|
||||
var counter = 0;
|
||||
return function (o) {
|
||||
counter++
|
||||
var defaultUser = {
|
||||
id: counter,
|
||||
username: "User" + counter,
|
||||
country: "CA",
|
||||
time_zone: "Eastern Time (US & Canada)",
|
||||
avatar: "/images/icons/noavatar.png",
|
||||
admin: false,
|
||||
steam: {
|
||||
url: "http://steamcommunity.com/profiles/7656119806792633" + counter,
|
||||
nickname: "SteamUser" + counter
|
||||
}
|
||||
};
|
||||
if (o && typeof o === 'object') {
|
||||
defaultUser = extend(defaultUser, o);
|
||||
}
|
||||
return new User(defaultUser);
|
||||
}
|
||||
})()
|
||||
|
||||
module.exports = helpers;
|
Loading…
Reference in a new issue