server.js 5.8 KB

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