server.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.name) {
  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.post("/win/:name", function(req, res, next) {
  63. const user = db.get(`/users/${req.body.name}`);
  64. if (user && req.body.password === user.password) {
  65. const dbEntry = db.get(`/wins/${req.params.name}`);
  66. db.set(`/wins/${req.params.name}`, {
  67. ...dbEntry,
  68. [req.body.game]: { ...dbEntry[req.body.game], [req.body.name]: 1 }
  69. });
  70. res.send(db.get(`/wins`));
  71. } else {
  72. res.send({ error: errors.vote.baduser });
  73. }
  74. return next();
  75. });
  76. server.get("/wins", function(req, res, next) {
  77. const dbEntry = db.get(`/wins`);
  78. if (dbEntry) {
  79. res.send(dbEntry);
  80. } else {
  81. res.send({});
  82. }
  83. return next();
  84. });
  85. server.put("/vote/:name", function(req, res, next) {
  86. const user = db.get(`/users/${req.params.name}`);
  87. if (user && req.body.password === user.password) {
  88. db.set(`/votes/${req.params.name}`, { factions: req.body.factions });
  89. const dbEntry = db.get(`/votes/${req.params.name}`);
  90. res.send(dbEntry);
  91. } else {
  92. res.send({ error: errors.vote.baduser });
  93. }
  94. return next();
  95. });
  96. server.get("/vote/:name", function(req, res, next) {
  97. const dbEntry = db.get(`/votes/${req.params.name}`);
  98. if (dbEntry) {
  99. res.send(dbEntry);
  100. } else {
  101. res.send({ error: errors.vote.novotes });
  102. }
  103. return next();
  104. });
  105. server.post("/user/:name", function(req, res, next) {
  106. if (req.params.name) {
  107. const userEntry = db.get(`/users/${req.params.name}`);
  108. if (userEntry) {
  109. if (req.body && userEntry.password === req.body.password) {
  110. const voteEntry = db.get(`/votes/${req.params.name}`) || [];
  111. res.send({ ...voteEntry });
  112. } else {
  113. res.send({ error: errors.user.badpassword });
  114. }
  115. } else {
  116. res.send({ error: errors.user.notfound });
  117. }
  118. } else {
  119. res.send({ error: errors.user.nouser });
  120. }
  121. return next();
  122. });
  123. function lockChoices() {
  124. const dbEntry = db.get(`/votes`);
  125. function customizer(objValue, srcValue) {
  126. if (_.isArray(objValue)) {
  127. return objValue.concat(srcValue);
  128. }
  129. }
  130. const allChoices = Object.keys(dbEntry).reduce((acc, player) => {
  131. const achoices = dbEntry[player].factions.reduce((acc, factionChoice) => {
  132. return _.mergeWith(
  133. acc,
  134. {
  135. [factionChoice.id]: { ...factionChoice, chosenBy: [player] }
  136. },
  137. customizer
  138. );
  139. }, {});
  140. return _.mergeWith(acc, achoices, customizer);
  141. }, {});
  142. let notPicked = Object.keys(allChoices).reduce((acc, factionId) => {
  143. return acc.filter(faction => {
  144. return Number(faction.id) !== Number(factionId);
  145. });
  146. }, factions);
  147. const resolved = Object.keys(dbEntry).reduce((acc, player) => {
  148. const resolvedChoices = dbEntry[player].factions.map(factionChoice => {
  149. return {
  150. ...factionChoice,
  151. unique: allChoices[factionChoice.id].chosenBy.length < 2
  152. };
  153. });
  154. let selected = resolvedChoices.find(choice => choice.unique);
  155. if (!selected) {
  156. const randomId = Math.floor(Math.random() * (notPicked.length - 1));
  157. selected = notPicked[Object.keys(notPicked)[randomId]];
  158. notPicked = notPicked.filter(faction => faction.id !== selected.id);
  159. }
  160. return { ...acc, [player]: { choices: resolvedChoices, selected } };
  161. }, {});
  162. db.set("/factionChoices", resolved);
  163. return resolved;
  164. }
  165. server.get("/votes", function(req, res, next) {
  166. const revealDate = 1618077600 * 1000;
  167. if (Date.now() >= revealDate) {
  168. const choices = db.get("/factionChoices") || lockChoices();
  169. res.send(choices);
  170. } else {
  171. const dbEntry = db.get(`/votes`);
  172. res.send({ error: errors.votes.toosoon, voted: Object.keys(dbEntry) });
  173. }
  174. return next();
  175. });
  176. // test comment
  177. server.listen(8082, function() {
  178. console.log("%s listening at %s", server.name, server.url);
  179. });