| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- const date_fns = require("date-fns");
- let users = {}
- let votes = {};
- function users_init_fixed() {
- users = {
- 'fry': {
- username: 'fry',
- password: 'f'
- },
- 'leela': {
- username: 'leela',
- password: 'l'
- },
- 'bender': {
- username: 'bender',
- password: 'b'
- },
- 'farnsworth': {
- username: 'farnsworth',
- password: 'f'
- },
- }
- }
- function vote_init_fixed() {
- votes = {
- "2024-04-01": {
- "fry": "no",
- "leela": "no",
- "bender": "no",
- "farnsworth": "yes"
- },
- "2024-04-02": {},
- "2024-04-03": {
- "fry": "yes"
- },
- "2024-04-04": {
- "fry": "yes",
- "bender": "yes",
- "farnsworth": "no"
- },
- }
- }
- function vote_init_empty() {
- votes = {};
- }
- function vote_init_random(num_days) {
- let today = new Date();
- console.log("seeidng a bunch of false votes for", today);
- for (let i = 0; i < num_days; i++) {
- let d = date_fns.add(today, { days: i });
- let d_str = date_fns.format(d, "yyyy-MM-dd")
- if (votes[d_str] === undefined) {
- votes[d_str] = {}
- }
- for (let user in users) {
- let valence = Math.random();
- if (valence < 0.25) {
- placeVote(user, d_str, "no");
- } else if (valence > 0.75) {
- placeVote(user, d_str, "yes");
- }
- }
- }
- console.log(JSON.stringify(votes, null, 2))
- }
- function isUsernameTaken(username) {
- return !!users[username];
- }
- function createUser(username, password) {
- if (!isUsernameTaken(username)) {
- users[username] = {
- username,
- password,
- }
- return true;
- } else {
- return false;
- }
- }
- function authenticateUser(username, password) {
- if (users[username]) {
- let user = users[username];
- if (user.password === password) {
- return { username: user.username }
- }
- } else {
- return null;
- }
- }
- function getVotesAllDays() {
- return JSON.parse(JSON.stringify(votes))
- }
- function getVotesOneDay(date) {
- return JSON.parse(JSON.stringify(votes[date]))
- }
- function placeVote(username, date, vote) {
- if (votes[date] === undefined) {
- votes[date] = {};
- }
- if (!vote || vote === "maybe") {
- delete votes[date][username];
- } else {
- votes[date][username] = vote;
- }
- }
- module.exports = {
- // these are the actual useful methods
- isUsernameTaken,
- createUser,
- authenticateUser,
- getVotesAllDays,
- getVotesOneDay,
- placeVote,
- // I thought I'd try organizing my seed-data like this instead, is this helpful?
- users_init_fixed,
- vote_init_empty,
- vote_init_fixed,
- vote_init_random,
- }
|