aboutsummaryrefslogtreecommitdiff
path: root/src/trivia/server.js
blob: 3c66050b8dad715febc5bb0984d3122c4b5abb09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const uuidv4 = require('uuid')["v4"];
const words = require('./words').words;
const prompts = require('./prompts').prompts;

function setUpRoutes(server, models, jwtFunctions, database) {
    // simple send files
    server.get('/trivia', (req, res) => res.sendFile(__dirname + "/index.html"))
    server.get('/trivia/main.js', (req, res) => res.sendFile(__dirname + "/static/main.js"))
    server.get('/trivia/styles.css', (req, res) => res.sendFile(__dirname + "/static/styles.css"))
    server.get('/trivia/sound-effects', (req, res) => res.sendFile(__dirname + "/sound-effects.html"))
    server.get('/trivia/', (req, res) => res.sendFile(__dirname + "/static/styles.css"))

    // a list of games
    var games = []
    let STATES = {
        GUESSING: 0,
        WAITING: 1,
        OVER: 2
    }

    // generates a game code
    function generateGameCode() {
        // let words = ["cat", "dog"];
        var index = Math.floor(Math.random() * words.length)
        // ensure no duplicate game code
        while (games.find(el => el.gameCode == words[index].toLowerCase())) {
            var index = Math.floor(Math.random() * words.length)
        }
        return words[index].toLowerCase()
    }
    // gets a new game object
    function getNewGame(hostCookie, hostName) {
        let gameCode = generateGameCode();
        return {
            host: hostCookie,
            players: [{ cookie: hostCookie, name: hostName }],
            questions: [],
            gameCode: gameCode,
            buzzes: []
        }
    }
    // adds a player to a game object
    function addToGame(gameCode, playerCookie, playerName) {
        let game = games.find(el => el.gameCode == gameCode)
        if (game) {
            game.players.push({ cookie: playerCookie, name: playerName })
            return true
        }
        return false
    }
    function findGameByCookie(cookie) {
        return games.find(el => el.players.some(player => player.cookie == cookie))
    }
    function findPlayerByCookie(game, cookie) {
        return game.players.find(player => player.cookie == cookie)
    }
    function getPlayerNames(players) {
        // return players.map(player => player.name)
        return players.map(player => {
            return { name: player.name, score: player.score }
        })
    }
    // Turn the game into a public game object (no cookies, etc.)
    function getPublicGame(cookie) {
        var game = findGameByCookie(cookie)
        if (!game) {
            return { message: "no game active" }
        } else {
            let isHost = cookie == game.host
            let username = game.players.find(player => player.cookie == cookie).name
            let players = getPlayerNames(game.players)
            // console.log(players)
            var newGame = {
                host: isHost,
                players: players,
                name: username,
                gameCode: game.gameCode,
                gameStarted: game.gameStarted,
                state: game.state,
                round: game.round,
                questions: game.questions,
                buzzes: game.buzzes
            }
            if(game.state == STATES.WAITING){
                newGame.players = getPlayerNames(game.players.filter(player => player.cookie != game.host))
            } else if(game.state == STATES.OVER){
                newGame.players = getPlayerNames(game.players.filter(player => player.cookie != game.host))
                var winningScore = Math.max.apply(Math, game.players.map(player => player.score))
                var winningPlayers = game.players.filter(player => player.score == winningScore)
                newGame.winner = winningPlayers.map(player => player.name).join(", ")
                // console.log(newGame)
            }

            return newGame
        }
    }
    // marks the game as started
    function startGame(cookie) {
        let game = games.find(el => el.host == cookie)
        if (game && game.players.length >= 2) {
            game.gameStarted = true
            game.round = 0;
            game.players.forEach(player => {
                player.score = 0
            })
            game.state = STATES.GUESSING
            return true
        } else {
            return false
        }
    }
    function endRound(game) {
        game.state = STATES.GUESSING
        game.buzzes = []
        game.round += 1
        if (game.round >= game.questions.length) {
            game.state = STATES.OVER
        }
    }
    function endQuestion(game){
        game.state = STATES.WAITING
    }
    // give points to player 
    function giveScore(game, index, score) {
        let player = game.players[index]
        player.score += score
    }
    // Requested by host once
    server.get('/trivia/host-game', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            cookie = uuidv4();
            res.cookie('session', cookie, { expires: new Date(Date.now() + (1000 * 60 * 60)) });
        }
        let username = req.query.name
        var game = getNewGame(cookie, username)
        games.push(game)
        res.status(200).send(getPublicGame(cookie))
    })
    // Requested by players joining from game code
    server.get('/trivia/join-game', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            cookie = uuidv4();
            res.cookie('session', cookie, { expires: new Date(Date.now() + (1000 * 60 * 60)) });
        }
        let username = req.query.name
        let code = req.query.code
        if (addToGame(code, cookie, username)) {
            res.status(200).send(getPublicGame(cookie))
        } else {
            res.status(200).send({ message: "Invalid game code" })
        }
    })
    // starts the game
    server.get('/trivia/start-game', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie || !startGame(cookie)) {
            res.status(400).send({ message: "you cannot start a game" });
        } else {
            res.status(200).send(getPublicGame(cookie))
        }
    })
    // constantly requested by client while in lobby
    server.get('/trivia/lobby-status', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            res.status(400).send({ message: "you are not in a game" });
        } else {
            res.status(200).send(getPublicGame(cookie))
        }
    })
    // constantly requested by client while game started
    server.get('/trivia/game-status', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            res.status(400).send({ message: "you are not in a game" });
        } else {
            var game = findGameByCookie(cookie)
            res.status(200).send(getPublicGame(cookie))
        }
    })
    server.get('/trivia/giveScore', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie || req.query.index == undefined || req.query.points == undefined) {
            res.status(400).send({ message: "you are not in a game" });
        } else {
            var game = findGameByCookie(cookie)
            giveScore(game, req.query.index, Number(req.query.points))
            res.status(200).send()
        }
    })
    server.get('/trivia/submit', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie || req.query.text == undefined) {
            res.status(400).send({ message: "you are not in a game" });
        } else {
            var game = findGameByCookie(cookie)
            console.log(game.hostCookie, cookie)
            if(game.host != cookie){
                res.status(400).send({ message: "you are not host" });
            }
            game.questions.push(req.query.text)
            res.status(200).send()
        }
    })
    server.get('/trivia/endRound', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            endRound()
            res.status(400).send({ message: "you are not in a game" });
        } else {
            var game = findGameByCookie(cookie)
            endRound(game)
            res.status(200).send()
        }
    })
    server.get('/trivia/endQuestion', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            endRound()
            res.status(400).send({ message: "you are not in a game" });
        } else {
            var game = findGameByCookie(cookie)
            endQuestion(game)
            res.status(200).send()
        }
    })
    server.get('/trivia/buzz', (req, res, next) => {
        let cookie = req.cookies.session;
        if (!cookie) {
            endRound()
            res.status(400).send({ message: "you are not in a game" });
        } else {
            var game = findGameByCookie(cookie)
            var player = findPlayerByCookie(game, cookie)
            game.buzzes.push(player.name)
            res.status(200).send()
        }
    })
}

module.exports = {
    setUpRoutes
};