~jshsj/go_quiz

go_quiz/home.html -rw-r--r-- 4.6 KiB
7f7816a2 — JSH initial commit 1 year, 8 months ago
                                                                                
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
<!DOCTYPE html>
<html lang="en">

    <head>
        <title>Chat Example</title>
        <script type="text/javascript">
            window.onload = function () {
                const START = 0;
                const QUESTIONS = 1;
                const RESULT = 2;

                var conn;
                let images = document.querySelectorAll(".initial-image");
                let defaultImage = document.querySelector(".default-image");
                let state = START;
                // let timer = null;
                // let countdown = 30;
                // let conn = null;
                // let joined = false;

                // document.getElementById("form").onsubmit = function () {
                //     if (!conn) {
                //         return false;
                //     }
                //     if (!msg.value) {
                //         return false;
                //     }
                //     conn.send(msg.value);
                //     msg.value = "";
                //     return false;
                // };

                document.addEventListener("keydown", (e) => {
                    switch (e.key) {
                        case "1": send_event("gut"); break;
                        case "2": send_event("unsicher"); break;
                        case "3": send_event("schlecht"); break;
                        default: break;
                    }
                });

                function send_event(answer) {
                    console.log("Sending" + answer);
                    conn.send(answer);
                }

                if (window["WebSocket"]) {
                    conn = new WebSocket("ws://" + document.location.host + "/ws");
                    conn.onclose = function (evt) {
                        //CLOSE
                    };
                    conn.onmessage = function (evt) {
                        handleMessage(evt.data)
                    };
                } else {
                    var item = document.createElement("div");
                    item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
                    appendLog(item);
                }

                function handleMessage(message) {
                    console.log(message);
                    if (message.startsWith("Start")) {
                        showStart();
                    } else if (message.startsWith("Question")) {
                        let num = message.split(" ")[1];
                        showQuestion(num);
                    } else if (message.startsWith("Result")) {
                        let num = message.split(" ")[1];
                        showResult(num);
                    }
                }

                function showStart() {
                    clearActive();
                    defaultImage.classList.add("active");
                }

                function showQuestion(num) {
                    clearActive();
                    images[num].classList.add("active");
                }

                function showResult(num) {
                    clearActive();
                    //results[num].classList.add("active");

                }

                function clearActive() {
                    let active = document.querySelector(".active");
                    if (active != null) {
                        active.classList.remove("active");
                    }
                }
            };
        </script>
        <style type="text/css">
            html {
                overflow: hidden;
            }

            body {
                overflow: hidden;
                padding: 0;
                margin: 0;
                width: 100%;
                height: 100%;
                background: gray;
            }


            .initial-image {}

            .image {
                opacity: 0;
                position: absolute;
                top: 0;
                left: 0;
            }


            .active {
                opacity: 1;
            }
        </style>
    </head>

    <body>
        <img class="default-image image" src="/static/images/mountains.png">
        <img class="initial-image image" id="initial-1" src="/static/images/image_0.jpg">
        <img class="initial-image image" id="initial-2" src="/static/images/image_1.jpg">
        <img class="initial-image image" id="initial-3" src="/static/images/image_2.jpg">
        <img class="initial-image image" id="initial-4" src="/static/images/image_3.jpg">
        <img class="initial-image image" id="initial-5" src="/static/images/image_4.jpg">
        <img class="initial-image image" id="initial-6" src="/static/images/image_5.jpg">
    </body>

</html>