server.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import restify from "restify";
  2. import _ from "lodash";
  3. import loadDb from "./db.js";
  4. import corsMiddleware from "cors";
  5. import { errors } from "common";
  6. const factions = [
  7. { name: "Arborec", id: 1 },
  8. { name: "Barony of Letnev", id: 2 },
  9. { name: "Clan of Saar", id: 3 },
  10. { name: "Embers of Muaat", id: 4 },
  11. { name: "Emirates of Hacan", id: 5 },
  12. { name: "Federation of Sol", id: 6 },
  13. { name: "Ghosts of Creuss", id: 7 },
  14. { name: "L1Z1X Mindnet", id: 8 },
  15. { name: "Mentak Coalition", id: 9 },
  16. { name: "Naalu Collective", id: 10 },
  17. { name: "Nekro Virus", id: 11 },
  18. { name: "Sardakk N’orr", id: 12 },
  19. { name: "Universities of Jol-Nar", id: 13 },
  20. { name: "Winnu", id: 14 },
  21. { name: "Xxcha Kingdom", id: 15 },
  22. { name: "Yin Brotherhood", id: 16 },
  23. { name: "Yssaril Tribes", id: 17 },
  24. { name: "Argent Flight", id: 18 },
  25. { name: "Empyrean", id: 19 },
  26. { name: "Mahact Gene-Sorcerers", id: 20 },
  27. { name: "Naaz-Rokha Alliance", id: 21 },
  28. { name: "Nomad", id: 22 },
  29. { name: "Titans of Ul", id: 23 },
  30. { name: "Vuil'Raith Cabal", id: 24 }
  31. ];
  32. const db = loadDb("/data/ti.db");
  33. const server = restify.createServer({
  34. name: "spoll",
  35. version: "1.0.1"
  36. });
  37. server.use(restify.plugins.acceptParser(server.acceptable));
  38. server.use(restify.plugins.queryParser());
  39. server.use(restify.plugins.bodyParser());
  40. const cors = corsMiddleware({
  41. credentials: true,
  42. preflightMaxAge: 5,
  43. origin: function(origin, callback) {
  44. callback(null, origin);
  45. }
  46. });
  47. server.pre(cors);
  48. server.put("/register/:name", function(req, res, next) {
  49. const existingRecord = db.get(`/users/${req.params.name}`);
  50. if (existingRecord) {
  51. res.send({ error: errors.register.exists });
  52. } else {
  53. db.update(`/users/${req.params.name}`, {
  54. password: req.body.password,
  55. factions: []
  56. });
  57. const dbEntry = db.get(`/users/${req.params.name}`);
  58. res.send(dbEntry);
  59. return next();
  60. }
  61. });
  62. server.put("/vote/:name", function(req, res, next) {
  63. const user = db.get(`/users/${req.params.name}`);
  64. if (user && req.body.password === user.password) {
  65. db.set(`/votes/${req.params.name}`, { factions: req.body.factions });
  66. const dbEntry = db.get(`/votes/${req.params.name}`);
  67. res.send(dbEntry);
  68. } else {
  69. res.send({ error: errors.vote.baduser });
  70. }
  71. return next();
  72. });
  73. server.get("/vote/:name", function(req, res, next) {
  74. const dbEntry = db.get(`/votes/${req.params.name}`);
  75. if (dbEntry) {
  76. res.send(dbEntry);
  77. } else {
  78. res.send({ error: errors.vote.novotes });
  79. }
  80. return next();
  81. });
  82. server.post("/user/:name", function(req, res, next) {
  83. if (req.params.name) {
  84. const userEntry = db.get(`/users/${req.params.name}`);
  85. if (userEntry) {
  86. if (req.body && userEntry.password === req.body.password) {
  87. const voteEntry = db.get(`/votes/${req.params.name}`) || [];
  88. res.send({ ...voteEntry });
  89. } else {
  90. res.send({ error: errors.user.badpassword });
  91. }
  92. } else {
  93. res.send({ error: errors.user.notfound });
  94. }
  95. } else {
  96. res.send({ error: errors.user.nouser });
  97. }
  98. return next();
  99. });
  100. function lockChoices() {
  101. const dbEntry = db.get(`/votes`);
  102. function customizer(objValue, srcValue) {
  103. if (_.isArray(objValue)) {
  104. return objValue.concat(srcValue);
  105. }
  106. }
  107. const allChoices = Object.keys(dbEntry).reduce((acc, player) => {
  108. const achoices = dbEntry[player].factions.reduce((acc, factionChoice) => {
  109. return _.mergeWith(
  110. acc,
  111. {
  112. [factionChoice.id]: { ...factionChoice, chosenBy: [player] }
  113. },
  114. customizer
  115. );
  116. }, {});
  117. return _.mergeWith(acc, achoices, customizer);
  118. }, {});
  119. let notPicked = Object.keys(allChoices).reduce((acc, factionId) => {
  120. return acc.filter(faction => {
  121. return Number(faction.id) !== Number(factionId);
  122. });
  123. }, factions);
  124. const resolved = Object.keys(dbEntry).reduce((acc, player) => {
  125. const resolvedChoices = dbEntry[player].factions.map(factionChoice => {
  126. return {
  127. ...factionChoice,
  128. unique: allChoices[factionChoice.id].chosenBy.length < 2
  129. };
  130. });
  131. let selected = resolvedChoices.find(choice => choice.unique);
  132. if (!selected) {
  133. const randomId = Math.floor(Math.random() * (notPicked.length - 1));
  134. selected = notPicked[Object.keys(notPicked)[randomId]];
  135. notPicked = notPicked.filter(faction => faction.id !== selected.id);
  136. }
  137. return { ...acc, [player]: { choices: resolvedChoices, selected } };
  138. }, {});
  139. db.set("/factionChoices", resolved);
  140. return resolved;
  141. }
  142. server.get("/votes", function(req, res, next) {
  143. const revealDate = 1617368400 * 1000;
  144. if (Date.now() >= revealDate) {
  145. const choices = db.get("/factionChoices") || lockChoices();
  146. res.send(choices);
  147. } else {
  148. const dbEntry = db.get(`/votes`);
  149. res.send({ error: errors.votes.toosoon, voted: Object.keys(dbEntry) });
  150. }
  151. return next();
  152. });
  153. server.listen(8082, function() {
  154. console.log("%s listening at %s", server.name, server.url);
  155. });