summaryrefslogtreecommitdiff
path: root/rps/morpion/base.js
blob: e42b1ffe4d4addd31b111e3353ef30638ab5df05 (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
trainingMain = true;
training = false;
playable = true;
gridBefore = [];

function train() {}

window.addEventListener("load", () => {
    if (!location.hash.startsWith("#online/")) {
        document.getElementById("loader").style.display = "none";
        document.getElementById("game").style.display = "block";
    } else {
        train();
        online();
    }
})

function player(cell) {
    document.getElementById("p4").style.opacity = ".5";
    document.getElementById("p4").style.filter = "saturate(0)";
    document.getElementById("p5").style.opacity = ".5";
    document.getElementById("p5").style.filter = "saturate(0)";
    document.getElementById("p6").style.opacity = ".5";
    document.getElementById("p6").style.filter = "saturate(0)";
    document.getElementById("p" + cell).style.opacity = "1";
    document.getElementById("p" + cell).style.filter = "";

    if (playable) {
        document.getElementById("wait").style.display = "block";
    }

    if (location.hash.startsWith("#online/")) {
        ws.send(JSON.stringify({action:"place",position:cell}));
        document.getElementById("last-games").innerText = "Playing with " + window.peerName + "\nWaiting for the other player to place.";
    }

    youPlaced = true;
    if (otherPlaced) {
        checkWinner();
    }
}

function disableGame() {
    document.getElementById("game").style.opacity = "0.5";
    document.getElementById("wait").style.display = "none";
    document.getElementById("game").style.pointerEvents = "none";
}

function grid() {
    try {
        return [
            document.getElementById("p1").style.opacity === "1",
            document.getElementById("p2").style.opacity === "1",
            document.getElementById("p3").style.opacity === "1",
            document.getElementById("p4").style.opacity === "1",
            document.getElementById("p5").style.opacity === "1",
            document.getElementById("p6").style.opacity === "1"
        ]
    } catch (e) {
        return [false,false,false,false,false,false];
    }
}

window.addEventListener("load", () => {
    if (location.hash === "#/train") {
        location.href = "/";
    }
    if (!location.hash.startsWith("#online/")) {
        document.getElementById("resetbtn").style.display = "none";
    }

    if (location.hash === "#/train") {
        location.href = "/";
    } else if (location.hash.startsWith("#online/")) {
        document.title = "Online Game | Rock Paper Scissors | Minteck Arcade";
    } else if (location.hash === "#/auto") {
        location.href = "/";
    } else {
        location.href = "/";
    }
});

rps = {
    action: {
        ROCK: 1,
        PAPER: 2,
        SCISSORS: 3
    },
    state: {
        WIN: 1,
        LOSE: 2,
        NONE: 3
    }
}

function checkWinner() {
    document.getElementById("counter-left-total").innerText = (document.getElementById("counter-left-total").innerText - 1 + 2).toString();
    document.getElementsByClassName("picked")[0].children[0].style.opacity = "1";
    document.getElementsByClassName("picked")[0].children[0].style.filter = "";
    disableGame();

    me = (Array.from(document.querySelectorAll("table#game td")).filter(i => i.id.substring(1) - 1 + 1 > 3 && i.style.opacity === "1")[0].id.substring(1) - 1 + 1) - 3;
    other = document.getElementsByClassName("picked")[0].id.substring(1) - 1 + 1;

    let state = rps.state.NONE;

    if (me === rps.action.ROCK && other === rps.action.ROCK) state = rps.state.NONE;
    if (me === rps.action.PAPER && other === rps.action.PAPER) state = rps.state.NONE;
    if (me === rps.action.SCISSORS && other === rps.action.SCISSORS) state = rps.state.NONE;
    if (me === rps.action.ROCK && other === rps.action.PAPER) state = rps.state.LOSE;
    if (me === rps.action.ROCK && other === rps.action.SCISSORS) state = rps.state.WIN;
    if (me === rps.action.PAPER && other === rps.action.SCISSORS) state = rps.state.LOSE;
    if (me === rps.action.PAPER && other === rps.action.ROCK) state = rps.state.WIN;
    if (me === rps.action.SCISSORS && other === rps.action.ROCK) state = rps.state.LOSE;
    if (me === rps.action.SCISSORS && other === rps.action.PAPER) state = rps.state.WIN;

    if (state === rps.state.WIN) {
        document.getElementById("counter-left-win").innerText = (document.getElementById("counter-left-win").innerText - 1 + 2).toString();
        document.getElementById("last-games").innerText = "Played with " + window.peerName + "\nGame ended, you won.";
    } else if (state === rps.state.LOSE) {
        document.getElementById("counter-left-lose").innerText = (document.getElementById("counter-left-lose").innerText - 1 + 2).toString();
        document.getElementById("last-games").innerText = "Played with " + window.peerName + "\nGame ended, you lost.";
    } else {
        document.getElementById("last-games").innerText = "Played with " + window.peerName + "\nGame ended, that's a draw.";
    }
}