aboutsummaryrefslogtreecommitdiff
path: root/src/trivia/static/main.js
blob: acb649c8bc54ac71b334a53c964febe2ae3d7ae6 (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
window.onload = function () {
    var transactionData = new Vue({
        el: '#data',
        data: {
            gameCode: "",
            username: "",
            game: undefined,
            submitText: "",
            STATES: {
                GUESSING: 0,
                WAITING: 1,
                OVER: 2
            },
            interval: undefined
        },
        methods: {
            nonReady: function (players) {
                return players.filter(player => !player.ready)
              },
            startStatusLoop: function(){
                // event loop that runs while waiting for host to start
                var loadStatus = function (vue_object) {
                    fetch(new Request(`/trivia/lobby-status`))
                        .then(response => response.json())
                        .then(response => {
                            if (response.message) {
                                console.log(response.message)
                            } else if(response.gameStarted){
                                clearInterval(vue_object.interval)
                                vue_object.startGameLoop()

                            } else { // Just update game object with new players
                                vue_object.game = response
                            }
                        });
                    
                }
                this.interval = window.setInterval(loadStatus, 1000, this)
            },
            startGameLoop: function(){
                var loadStatus = function (vue_object) {
                    fetch(new Request(`/trivia/game-status`))
                        .then(response => response.json())
                        .then(response => {
                            if (response.message) {
                                console.log(response.message)
                            } else { 
                                vue_object.game = response
                            }
                        });
                    
                }
                this.interval = window.setInterval(loadStatus, 1000, this)
            },
            hostGame: function () {
                fetch(new Request(`/trivia/host-game?name=${this.username}`))
                    .then(response => response.json())
                    .then(response => {
                        if (response.message) {
                            console.log(response.message)
                        } else {
                            this.game = response
                            this.startStatusLoop()
                        }
                    })
            },
            joinGame: function () {
                fetch(new Request(`/trivia/join-game?name=${this.username}&code=${this.gameCode}`))
                    .then(response => response.json())
                    .then(response => {
                        if (response.message) {
                            console.log(response.message)
                        } else {
                            this.game = response
                            this.startStatusLoop()
                        }
                    })
            },
            startGame: function () {
                fetch(new Request(`/trivia/start-game`))
            }, 
            submitQuestion: function(){
                fetch(new Request(`/trivia/submit?text=${this.submitText}`))
                this.submitText = ""
            },
            buzzIn: function(){
                console.log("buz")
                fetch(new Request(`/trivia/buzz`))
            },
            endRound: function(){
                console.log("endround")
                fetch(new Request(`/trivia/endRound`))
            },
            endQuestion: function(){
                console.log("endquestion")
                fetch(new Request(`/trivia/endQuestion`))
            },
            giveScore: function(index, score){
                fetch(new Request(`/trivia/giveScore?index=${index}&points=${score}`))
            }
        },
        created() {

        },
        computed: {

        }
    });
}