pass_db.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const date_fns = require("date-fns");
  2. let users = {}
  3. let votes = {};
  4. function users_init_fixed() {
  5. users = {
  6. 'fry': {
  7. username: 'fry',
  8. password: 'f'
  9. },
  10. 'leela': {
  11. username: 'leela',
  12. password: 'l'
  13. },
  14. 'bender': {
  15. username: 'bender',
  16. password: 'b'
  17. },
  18. 'farnsworth': {
  19. username: 'farnsworth',
  20. password: 'f'
  21. },
  22. }
  23. }
  24. function vote_init_fixed() {
  25. votes = {
  26. "2024-04-01": {
  27. "fry": "no",
  28. "leela": "no",
  29. "bender": "no",
  30. "farnsworth": "yes"
  31. },
  32. "2024-04-02": {},
  33. "2024-04-03": {
  34. "fry": "yes"
  35. },
  36. "2024-04-04": {
  37. "fry": "yes",
  38. "bender": "yes",
  39. "farnsworth": "no"
  40. },
  41. }
  42. }
  43. function vote_init_empty() {
  44. votes = {};
  45. }
  46. function vote_init_random(num_days) {
  47. let today = new Date();
  48. console.log("seeidng a bunch of false votes for", today);
  49. for (let i = 0; i < num_days; i++) {
  50. let d = date_fns.add(today, { days: i });
  51. let d_str = date_fns.format(d, "yyyy-MM-dd")
  52. if (votes[d_str] === undefined) {
  53. votes[d_str] = {}
  54. }
  55. for (let user in users) {
  56. let valence = Math.random();
  57. if (valence < 0.25) {
  58. placeVote(user, d_str, "no");
  59. } else if (valence > 0.75) {
  60. placeVote(user, d_str, "yes");
  61. }
  62. }
  63. }
  64. console.log(JSON.stringify(votes, null, 2))
  65. }
  66. function isUsernameTaken(username) {
  67. return !!users[username];
  68. }
  69. function createUser(username, password) {
  70. if (!isUsernameTaken(username)) {
  71. users[username] = {
  72. username,
  73. password,
  74. }
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. function authenticateUser(username, password) {
  81. if (users[username]) {
  82. let user = users[username];
  83. if (user.password === password) {
  84. return { username: user.username }
  85. }
  86. } else {
  87. return null;
  88. }
  89. }
  90. function getVotesAllDays() {
  91. return JSON.parse(JSON.stringify(votes))
  92. }
  93. function getVotesOneDay(date) {
  94. return JSON.parse(JSON.stringify(votes[date]))
  95. }
  96. function placeVote(username, date, vote) {
  97. if (votes[date] === undefined) {
  98. votes[date] = {};
  99. }
  100. if (!vote || vote === "maybe") {
  101. delete votes[date][username];
  102. } else {
  103. votes[date][username] = vote;
  104. }
  105. }
  106. module.exports = {
  107. // these are the actual useful methods
  108. isUsernameTaken,
  109. createUser,
  110. authenticateUser,
  111. getVotesAllDays,
  112. getVotesOneDay,
  113. placeVote,
  114. // I thought I'd try organizing my seed-data like this instead, is this helpful?
  115. users_init_fixed,
  116. vote_init_empty,
  117. vote_init_fixed,
  118. vote_init_random,
  119. }