|
|
@@ -1,13 +1,41 @@
|
|
|
import restify from "restify";
|
|
|
+import _ from "lodash";
|
|
|
import loadDb from "./db.js";
|
|
|
import corsMiddleware from "cors";
|
|
|
-import {errors} from "common";
|
|
|
+import { errors } from "common";
|
|
|
+
|
|
|
+const factions = [
|
|
|
+ { name: "Arborec", id: 1 },
|
|
|
+ { name: "Barony of Letnev", id: 2 },
|
|
|
+ { name: "Clan of Saar", id: 3 },
|
|
|
+ { name: "Embers of Muaat", id: 4 },
|
|
|
+ { name: "Emirates of Hacan", id: 5 },
|
|
|
+ { name: "Federation of Sol", id: 6 },
|
|
|
+ { name: "Ghosts of Creuss", id: 7 },
|
|
|
+ { name: "L1Z1X Mindnet", id: 8 },
|
|
|
+ { name: "Mentak Coalition", id: 9 },
|
|
|
+ { name: "Naalu Collective", id: 10 },
|
|
|
+ { name: "Nekro Virus", id: 11 },
|
|
|
+ { name: "Sardakk N’orr", id: 12 },
|
|
|
+ { name: "Universities of Jol-Nar", id: 13 },
|
|
|
+ { name: "Winnu", id: 14 },
|
|
|
+ { name: "Xxcha Kingdom", id: 15 },
|
|
|
+ { name: "Yin Brotherhood", id: 16 },
|
|
|
+ { name: "Yssaril Tribes", id: 17 },
|
|
|
+ { name: "Argent Flight", id: 18 },
|
|
|
+ { name: "Empyrean", id: 19 },
|
|
|
+ { name: "Mahact Gene-Sorcerers", id: 20 },
|
|
|
+ { name: "Naaz-Rokha Alliance", id: 21 },
|
|
|
+ { name: "Nomad", id: 22 },
|
|
|
+ { name: "Titans of Ul", id: 23 },
|
|
|
+ { name: "Vuil'Raith Cabal", id: 24 }
|
|
|
+];
|
|
|
|
|
|
const db = loadDb("/data/ti.db");
|
|
|
|
|
|
const server = restify.createServer({
|
|
|
name: "spoll",
|
|
|
- version: "1.0.0"
|
|
|
+ version: "1.0.1"
|
|
|
});
|
|
|
|
|
|
server.use(restify.plugins.acceptParser(server.acceptable));
|
|
|
@@ -79,17 +107,62 @@ server.post("/user/:name", function(req, res, next) {
|
|
|
return next();
|
|
|
});
|
|
|
|
|
|
+function lockChoices() {
|
|
|
+ const dbEntry = db.get(`/votes`);
|
|
|
+ function customizer(objValue, srcValue) {
|
|
|
+ if (_.isArray(objValue)) {
|
|
|
+ return objValue.concat(srcValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const allChoices = Object.keys(dbEntry).reduce((acc, player) => {
|
|
|
+ const achoices = dbEntry[player].factions.reduce((acc, factionChoice) => {
|
|
|
+ return _.mergeWith(
|
|
|
+ acc,
|
|
|
+ {
|
|
|
+ [factionChoice.id]: { ...factionChoice, chosenBy: [player] }
|
|
|
+ },
|
|
|
+ customizer
|
|
|
+ );
|
|
|
+ }, {});
|
|
|
+ return _.mergeWith(acc, achoices, customizer);
|
|
|
+ }, {});
|
|
|
+
|
|
|
+ let notPicked = Object.keys(allChoices).reduce((acc, factionId) => {
|
|
|
+ return acc.filter(faction => {
|
|
|
+ return Number(faction.id) !== Number(factionId);
|
|
|
+ });
|
|
|
+ }, factions);
|
|
|
+ const resolved = Object.keys(dbEntry).reduce((acc, player) => {
|
|
|
+ const resolvedChoices = dbEntry[player].factions.map(factionChoice => {
|
|
|
+ return {
|
|
|
+ ...factionChoice,
|
|
|
+ unique: allChoices[factionChoice.id].chosenBy.length < 2
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ let selected = resolvedChoices.find(choice => choice.unique);
|
|
|
+ if (!selected) {
|
|
|
+ const randomId = Math.floor(Math.random() * (notPicked.length - 1));
|
|
|
+ selected = notPicked[Object.keys(notPicked)[randomId]];
|
|
|
+ notPicked = notPicked.filter(faction => faction.id !== selected.id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return { ...acc, [player]: { choices: resolvedChoices, selected } };
|
|
|
+ }, {});
|
|
|
+ db.set("/factionChoices", resolved);
|
|
|
+ return resolved;
|
|
|
+}
|
|
|
+
|
|
|
server.get("/votes", function(req, res, next) {
|
|
|
- const revealDate = 1616803200 * 1000;
|
|
|
+ const revealDate = 1617368400 * 1000;
|
|
|
if (Date.now() >= revealDate) {
|
|
|
- const dbEntry = db.get(`/votes`);
|
|
|
- if (dbEntry) {
|
|
|
- res.send(dbEntry);
|
|
|
- } else {
|
|
|
- res.send({ error: errors.votes.novotes });
|
|
|
- }
|
|
|
+ const choices = db.get("/factionChoices") || lockChoices();
|
|
|
+
|
|
|
+ res.send(choices);
|
|
|
} else {
|
|
|
- res.send({ error: errors.votes.toosoon });
|
|
|
+ const dbEntry = db.get(`/votes`);
|
|
|
+ res.send({ error: errors.votes.toosoon, voted: Object.keys(dbEntry) });
|
|
|
}
|
|
|
return next();
|
|
|
});
|