| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import restify from "restify";
- import _ from "lodash";
- import loadDb from "./db.js";
- import corsMiddleware from "cors";
- 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.1"
- });
- server.use(restify.plugins.acceptParser(server.acceptable));
- server.use(restify.plugins.queryParser());
- server.use(restify.plugins.bodyParser());
- const cors = corsMiddleware({
- credentials: true,
- preflightMaxAge: 5,
- origin: function(origin, callback) {
- callback(null, origin);
- }
- });
- server.pre(cors);
- server.put("/register/:name", function(req, res, next) {
- const existingRecord = db.get(`/users/${req.params.name}`);
- if (existingRecord) {
- res.send({ error: errors.register.exists });
- } else {
- db.update(`/users/${req.params.name}`, {
- password: req.body.password,
- factions: []
- });
- const dbEntry = db.get(`/users/${req.params.name}`);
- res.send(dbEntry);
- return next();
- }
- });
- server.put("/vote/:name", function(req, res, next) {
- const user = db.get(`/users/${req.params.name}`);
- if (user && req.body.password === user.password) {
- db.set(`/votes/${req.params.name}`, { factions: req.body.factions });
- const dbEntry = db.get(`/votes/${req.params.name}`);
- res.send(dbEntry);
- } else {
- res.send({ error: errors.vote.baduser });
- }
- return next();
- });
- server.get("/vote/:name", function(req, res, next) {
- const dbEntry = db.get(`/votes/${req.params.name}`);
- if (dbEntry) {
- res.send(dbEntry);
- } else {
- res.send({ error: errors.vote.novotes });
- }
- return next();
- });
- server.post("/user/:name", function(req, res, next) {
- if (req.params.name) {
- const userEntry = db.get(`/users/${req.params.name}`);
- if (userEntry) {
- if (req.body && userEntry.password === req.body.password) {
- const voteEntry = db.get(`/votes/${req.params.name}`) || [];
- res.send({ ...voteEntry });
- } else {
- res.send({ error: errors.user.badpassword });
- }
- } else {
- res.send({ error: errors.user.notfound });
- }
- } else {
- res.send({ error: errors.user.nouser });
- }
- 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 = 1617368400 * 1000;
- if (Date.now() >= revealDate) {
- const choices = db.get("/factionChoices") || lockChoices();
- res.send(choices);
- } else {
- const dbEntry = db.get(`/votes`);
- res.send({ error: errors.votes.toosoon, voted: Object.keys(dbEntry) });
- }
- return next();
- });
- server.listen(8082, function() {
- console.log("%s listening at %s", server.name, server.url);
- });
|