aboutsummaryrefslogtreecommitdiff
path: root/templates/index.html
blob: 2cfb3a0da2c2b6e91341f4dbd0c627a9413a8407 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <!-- we do not have a favicon -->
    <link rel="icon" href="data:," />

    <title>Cyberschuessel</title>

    <!-- css needed to keep the footer at the bottom of the viewport -->
    <style>
      html {
        position: relative;
        min-height: 100%;
      }

      body {
        margin: 0 0 100px; 
      }

      .content { 
        padding: 15px; 
        box-sizing: border-box;
        text-align: center;
      }

      footer {
        position: absolute;
        left: 0;
        bottom: 0;
        height: 100px;
        width: 100%;
        text-align: center;
      }
    </style>

    <script language="javascript" type="text/javascript">
      /*    
      @licstart  The following is the entire license notice for the 
      JavaScript code in this page.

      Copyright (C) 2020 Florian Fischer

      The JavaScript code in this page is free software: you can
      redistribute it and/or modify it under the terms of the GNU
      General Public License (GNU GPL) as published by the Free Software
      Foundation, either version 3 of the License, or any later version. 
      The code is distributed WITHOUT ANY WARRANTY;
      without even the implied warranty of MERCHANTABILITY or FITNESS
      FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

      As additional permission under GNU GPL version 3 section 7, you
      may distribute non-source (e.g., minimized or compacted) forms of
      that code without the copy of the GNU GPL normally required by
      section 4, provided you include this license notice and a URL
      through which recipients can access the Corresponding Source.   

      @licend  The above is the entire license notice
      for the JavaScript code in this page.
      */

      // Extend Storage prototype to support objects
      Storage.prototype.setObject = function(key, value) {
        this.setItem(key, JSON.stringify(value));
      }

      Storage.prototype.getObject = function(key) {
        var value = this.getItem(key);
        return value && JSON.parse(value);
      }

      let strings = {};
      strings.phase_prefix = {'en': 'Round:', 'de': 'Runde:'};
      strings.words_prefix = {'en': 'Words to guess:', 'de': 'Wörter noch zu erraten:'};
      strings.guessed_prefix = {'en': 'Words already guessed:', 'de': 'Wörter bereits erraten:'};
      strings.drawn_word_prefix = {'en': 'Current word:', 'de': 'Aktuelles Wort:'};
      strings.draw_button = {'en': 'Draw word', 'de': 'Wort ziehen'};
      strings.reset_button = {'en': 'Reset', 'de': 'Neu'};
      strings.add_button = {'en': 'Add word', 'de': 'Wort hinzufügen:'};
      strings.add_word = {'en': 'Anything', 'de': 'Irgendwas'};
      strings.phase_changed = {'en': 'Next Phase', 'de': 'Nächste Runde'};
      strings.reload = {'en': 'Connection not available. Trying to reload ...',
                        'de': 'Verbindung unterbrochen. Seite wird neu geladen ...'};

      let userLang = navigator.language || navigator.userLanguage;
      userLang = userLang.substring(0, 2);

      let phase = 0;
      let words_to_guess = 0;
      let words_guessed = 0;

      // logical clock counting all draw events to check if our stored last_word is outdated
      let draw_counter = 0;

      set_words_to_guess = function(val) {
          words_to_guess = val;
          let word_count_text = document.getElementById('word_count');
          word_count_text.innerText = strings.words_prefix[userLang] + " " + words_to_guess;
      }

      change_words_to_guess = function(diff) {
        set_words_to_guess(words_to_guess+ diff);
      }

      set_words_guessed = function(val) {
          words_guessed = val;
          let words_guessed_text = document.getElementById('guessed_count');
          words_guessed_text.innerText = strings.guessed_prefix[userLang] + " "  + words_guessed;
      }

      update_drawn_word = function(draw_counter) {
        let drawn_word = document.getElementById('drawn_word')
        let last_word = localStorage.getObject('last_word');
        // we drew last -> show our word
        if (last_word && last_word.draw_counter == draw_counter) {
          drawn_word.innerText = strings.drawn_word_prefix[userLang] + " " + last_word.word;
        } else {
          drawn_word.innerText = "";
        }
      }

      set_phase = function(val) {
        phase = val;
        let phase_text = document.getElementById('schuessel_phase');
        phase_text.innerText = strings.phase_prefix[userLang] + " " + phase;

        let add_div = document.getElementById('add_div');
        if (phase != 0) {
          add_div.style.display = 'none';
        } else {
          add_div.style.display = 'block';
        }
      }

      conn_open = function(uri) {
        let ws = new WebSocket(uri);

        ws.onerror = conn_onerror;
        ws.onmessage = conn_onmessage;
        ws.onclose = conn_onclose;

        return ws;
      }

      conn_onerror = function(e) {
        console.log("Websocket error " + e);
        conn = conn_open(wsUri);
      };

      conn_onclose = function(e) {
        console.log("Websocket closed " + e);
        conn = conn_open(wsUri);
      };

      conn_onmessage = function(e) {
        let data = JSON.parse(e.data);
        switch (data.action) {
        case 'init':
          set_words_to_guess(data.words);
          set_words_guessed(data.guessed);
          set_phase(data.phase);

          if (draw_counter > data.draw_counter) {
            localStorage.removeItem('last_word');
          }

          update_drawn_word(data.draw_counter)
          draw_counter = data.draw_counter;
          break;

        case 'drawn':
          set_words_to_guess(data.words);
          set_words_guessed(data.guessed);
          set_phase(data.phase);

          if (data.draw_counter > draw_counter) {
            draw_counter = data.draw_counter;
          }

          // update drawn word element
          update_drawn_word(draw_counter);
          break;

        case 'draw':
          if (data.draw_counter >= draw_counter) {
            draw_counter = data.draw_counter;
            last_word = {'word': data.drawn_word, 'draw_counter': draw_counter};
            localStorage.setObject('last_word', last_word);
          }

          update_drawn_word(draw_counter);
          // schuessel is empty when we draw
          // alert the user that a phase change occurred
          if (data.phase_change) {
            alert(strings.phase_changed[userLang]);
          }
          break;

        case 'add':
          change_words_to_guess(1);
          break;
        }
      }

      let wsUri = (window.location.protocol=='https:'&&'wss://'||'ws://') + window.location.host;
      // our websocket connection
      let conn;

      document.addEventListener('DOMContentLoaded', function() {
        // init visual strings
        let reset_button = document.getElementById("reset_button");
        reset_button.value = strings.reset_button[userLang];

        let add_word = document.getElementById('add_word');
        add_word.placeholder = strings.add_word[userLang];

        let add_button = document.getElementById('add_button');
        add_button.value = strings.add_button[userLang];

        let draw_button = document.getElementById("draw_button");
        draw_button.value = strings.draw_button[userLang];

        // open websocket connection if DOM was loaded to make all received events
        // can access the DOM elements
        conn = conn_open(wsUri);

        document.getElementById("add_form").onsubmit = function() {
          let word_add_form = document.getElementById('add_form');

          let word = word_add_form[0].value;
          if (word) {
            let data = {};
            data.action = 'add';
            data.word = word;
            try {
              conn.send(JSON.stringify(data));
            } catch(err) {
              alert(strings.reload[userLang]);
              location.reload();
            }
          }
          document.getElementById('add_form').reset();
          return false;
        }

        document.getElementById("draw_button").onclick = function() {
          let data = {};
          data.action = 'draw';
          data.last_word = localStorage.getObject('last_word');
          try {
            conn.send(JSON.stringify(data));
          } catch(err) {
            alert(strings.reload[userLang]);
            location.reload();
          }
          return false;
        }

        document.getElementById("reset_button").onclick = function() {
          let data = {};
          data.action = 'reset';
          try {
            conn.send(JSON.stringify(data));
          } catch(err) {
            alert(strings.reload[userLang]);
            location.reload();
          }
          return false;
        }
      });
    </script>
  </head>

  <body>
    <!-- <h3 id="schuessel_id"></h3> -->
    <div class="content">
        <input id="reset_button" type="button"/>
        <h3 id="schuessel_phase" style="display:inline-block" ></h3>
        <h3 id="word_count"></h3>
        <h3 id="guessed_count"></h3>
        <div id="add_div">
          <form id="add_form">
            <input id="add_word" type="text"/>
            <!-- non visual submit input to submit on "Enter" -->
            <input type="submit" style="display: none" />
            <input id="add_button" type="submit"/>
          </form>
        </div>
        <h3 id="drawn_word"></h3>
        <input id="draw_button" type="button"/>
    </div>

    <footer>
      This website as well as the server code for cyberschuessel by Florian Fischer is free software licenced under the <a href="">GNU General Public License 3</a>.
      </br>
      The source code can be found at <a href="https://muhq.space/software/cyberschuessel.html">https://muhq.space/</a>.
    </footer>
  </body>
</html>