summaryrefslogtreecommitdiff
path: root/page.html
blob: 52cfb77b17a91d818c2131221f0571d8f5151b98 (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
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="UTF-8">
  <title>websweeper</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  </head>
  <style>
@font-face {
    font-family: vt323;
    src: url("font.ttf");
}
    #board-container {
      font-family: vt323, monospace;
      font-size: 28pt;
      line-height: 0.6em;
    }
    body {
      background-color: black;
      color: white;
    }
    .unsel {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    }
    .cursor {
      font-size: 16pt;
      pointer-events: none;
    }
    .cursor * {
      margin: 0 0;
    }
  </style>
  <body>
    <div class="">
      <div id="board-container">
      <div id="board"></div>
      </div>
      <p id="miscinfo">Loading...</p>
    </div>
  </body>
  <script>
    window.id = NaN;
    let s = new WebSocket(`ws://${window.location.hostname}:31236`);
    let info_elem = document.getElementById("miscinfo");
    let board_elem = document.getElementById("board");
    let name = "deadbeef";
    let last_packet = {};
    let cursors = new Map();
    let board = {};
    let board_bounds = {}; // init when we actually have the board loaded
    function register() {
          let name_in = document.getElementById("name-in");
          name = name_in.value;
          s.send(`register ${name}`);
        }
    s.onopen = function(e) {
          info_elem.innerHTML =
            `<input id="name-in" type="text">
   <button onclick=register()>Join</button>`;
        }
    s.onmessage = function(e) {
          last_packet = e;
          let d = e.data;
          if (typeof d == "object") {
            d.arrayBuffer().then(acceptBoard);
            info_elem.onclick = undefined;
            info_elem.innerHTML = "Running";
          } else if (typeof e.data == "string") {
            let fields = d.split(" ");
            if (d.startsWith("pos")) {
              let oid = Number(fields[1]);
              let name = fields[2];
              let x = fields[3];
              let y = fields[4];
              if (!cursors.has(oid)) {
                createCursor(oid, name);
              }
              let celem = cursors.get(oid).elem;
              celem.style.left = x + 'px';
              celem.style.top = y + 'px';
            }
            else if (d.startsWith("id")) {
              window.id = Number(fields[1]);
              createCursor(id, name);
            }
            else if (d.startsWith("win")) {
              info_elem.innerHTML = "<p>You win! Reset?</p>";
              info_elem.onclick = e => { s.send("reset") };
            }
            else if (d.startsWith("lose")) {
              let badone = fields[1];
                  info_elem.innerHTML = `<p>You lost, ${badone} was blown up. Reset?</p>`;
              info_elem.onclick = e => { s.send("reset") };
            }
            else if (d.startsWith("logoff")) {
              let oid = fields[1];
              cursors.get(oid).elem.remove();
              cursors.delete(oid);
            }
          }
      }
    s.onerror = function(e) { info_elem.innerHTML += `<br>error ${e}`; }
    s.onclose = function(e) { info_elem.innerHTML = "Closed"; }

    function acceptBoard(data) {
          let vals = new Uint8Array(data);
          board = Array.from(vals).reduce((s,c) => s + String.fromCodePoint(c), "");
          let last = board[0];
          let last_idx = 0;
          let split_board = [];
          for (let i = 1; i < board.length+1; i++) {
            let cur = board[i];
            let gamechars = /^[CFO# 1-8]+$/;
            if ((cur != last && gamechars.test(cur)) || cur == undefined) {
              let txt = board.substr(last_idx, i-last_idx);
              if (txt[0] == 'O') {
                txt = `<span style="color:red;">${txt}</span>`;
              } else if (txt[0] == 'C') {
                    txt = `<span style="color:green;">${txt}</span>`;
              } else if (txt[0] == 'F') {
                    txt = `<span style="color:yellow;">${txt}</span>`;
              } else {
                    txt = `<span style="color:white;">${txt}</span>`;
              }
              split_board.push(txt);
              last_idx = i;
            }
            last = board[i];
          }
          board_elem.innerHTML = "<span>" + split_board.join("") + "</span>";
    }

    function createCursor(id, name) {
          // shit doesn't line up
          let cursor = document.createElement("div");
          cursor.innerHTML = "<p>x</p>";
          cursor.style.position = "absolute";
          let nametag = document.createElement("p");
          nametag.innerHTML = name;
          cursor.appendChild(nametag);
          cursor.classList.add('cursor');
          let cb = cursor.getBoundingClientRect();
          let tip = cursor.firstChild.getBoundingClientRect();
          let dx = tip.width/2 - cb.width/2;
          let dy = -(tip.height/2 - cb.height/2);
          document.getElementById('board-container').append(cursor);
          if (id == window.id) {
          document.addEventListener('mousemove', e => {
                cursor.style.left = (e.pageX + dx) + 'px';
                cursor.style.top = (e.pageY + dy) + 'px';
                s.send(`pos ${e.pageX} ${e.pageY}`);
              },
                false);
          }
          cursors.set(id, {name: name, elem: cursor});
          return cursor;
        }
    function getBoardBounds() {
          let boardb = board_elem.getBoundingClientRect();
          let textb = board_elem.firstChild.getBoundingClientRect();
          return {
                ox: boardb.x,
                oy: boardb.y,
                w: textb.width,
                h: boardb.height
              };
        }

    board_elem.onclick = function(e) {
          let tpos = tilepos(e.clientX, e.clientY);
          let cmd = `reveal ${tpos.x} ${tpos.y}`;
          console.log(cmd);
          s.send(cmd);
        }
    board_elem.oncontextmenu = function(e) {
          let tpos = tilepos(e.clientX, e.clientY);
          let cmd = `flag ${tpos.x} ${tpos.y}`;
          console.log(cmd);
          s.send(cmd);
          return false;
        }
    function tilepos(x,y) {
           board_bounds = getBoardBounds();
          let b = board_bounds;
          let relx = (x - b.ox);
          let rely = (y - b.oy);
          let tilex = Math.floor(75 * relx/b.w);
          let tiley = Math.floor(35 * rely/b.h);
          return { x: tilex, y: tiley };
        }
    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
  </script>
</html>