aboutsummaryrefslogtreecommitdiff
path: root/src/ur/static/main.js
blob: 8f29c6c57743f6f3a48343f4c961c95ba408bb1b (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
window.onload = function () {
    var transactionData = new Vue({
        el: '#data',
        data: {
            status: "Loading...",
            playing: false
        },
        methods: {
            newGame: function () {
                if (this.playing) {
                    fetch(new Request(`/ur/new`))
                } else {
                    this.startGame()
                }
            },
            startGame: function () {
                console.log("created")
                var mouseDown = function (e) {
                    var canvas = document.getElementById('canvas')
                    col = Math.floor((e.x - canvas.offsetLeft) / 50);
                    row = Math.floor((e.y - canvas.offsetTop) / 50);
                    console.log(row, col)
                    if (0 <= col && col <= 7 && 0 <= row && row <= 2) {
                        console.log("clicked board")
                        fetch(new Request(`/ur/game/${col}/${row}`))
                    } else if( 0 == col && 4 == row){
                        console.log("roll")
                    }
                }
                document.addEventListener("click", mouseDown);
                var playInterval;
                var playGame = function (t) {
                    fetch(new Request(`/ur/game`)).then(response => response.json())
                        .then(response => {
                            var c = document.getElementById("canvas");
                            var ctx = c.getContext("2d");
                            ctx.fillStyle = "#eee"
                            ctx.fillRect(0, 0, 401, 301)

                            if (response.restart) {
                                t.playing = false
                                t.status = "The game has been forfeited"
                                clearInterval(playInterval)
                                return
                            } else if (response.winner != undefined) {
                                clearInterval(playInterval)
                                if (response.winner) {
                                    t.status = "You won!"
                                } else {
                                    t.status = "You lose!"
                                }
                            } else if (response.turn) {
                                t.status = "It's your turn!"
                            } else {
                                t.status = "Waiting for other player..."
                            }

                            ctx.strokeStyle = "black"
                            for (var i = 0; i <= 4; i++) {
                                ctx.moveTo(50 * i, 0);
                                ctx.lineTo(50 * i, 150);
                                ctx.stroke();
                            }
                            for (var i = 6; i <= 8; i++) {
                                ctx.moveTo(50 * i, 0);
                                ctx.lineTo(50 * i, 150);
                                ctx.stroke();
                            }
                            ctx.moveTo(250, 50);
                            ctx.lineTo(250, 100);
                            ctx.moveTo(0, 0);
                            ctx.lineTo(200, 0);
                            ctx.moveTo(300, 0);
                            ctx.lineTo(400, 0);
                            ctx.moveTo(0, 50);
                            ctx.lineTo(400, 50);
                            ctx.moveTo(0, 100);
                            ctx.lineTo(400, 100);
                            ctx.moveTo(0, 150);
                            ctx.lineTo(200, 150);
                            ctx.moveTo(300, 150);
                            ctx.lineTo(400, 150);
                            ctx.stroke();

                            for (var col = 0; col < 7; col++) {
                                for (var row = 0; row < 6; row++) {
                                    if (response.game.board[col][row] == undefined) {
                                        continue
                                    } else if (response.game.board[col][row] == 0) {
                                        ctx.fillStyle = "#0000FF";
                                    } else {
                                        ctx.fillStyle = "#FF0000";
                                    }
                                    ctx.beginPath();
                                    ctx.arc(25 + 50 * col, 50 * (5 - row) + 25, 23, 0, 2 * Math.PI);
                                    ctx.fill();
                                }
                            }
                            ctx.fillStyle = "black"
                            ctx.fillRect(0, 200, 50, 50);
                        });
                }
                var loadInterval = undefined
                var loadStatus = function (recurse, t) {
                    fetch(new Request(`/ur/status`)).then(response => response.json())
                        .then(response => {
                            if (response.waiting) {
                                t.status = "Waiting..."
                                if (recurse) {
                                    loadInterval = window.setInterval(loadStatus, 1000, false, t)
                                }
                            } else {
                                t.status = "Playing..."
                                t.playing = true
                                clearInterval(loadInterval)
                                playInterval = window.setInterval(playGame, 1000, t)
                            }
                        });
                }
                loadStatus(true, this)
            }
        },
        created() {
            this.startGame();
        },
        computed: {

        }
    });
}