aboutsummaryrefslogtreecommitdiff
path: root/html/tools.html
blob: 9265774881c05779395fae473de7f16ffa8123c4 (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
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>Muhq’s Game Web Tools</title>

  <style>
:root {
  /* define applied colors */
  --bg: #fdfdfd;
  --fg: #1a1a1a;
}

@media (prefers-color-scheme: dark) {
  /* change apllied colors to the dark gruvbox pallet
   * https://github.com/morhetz/gruvbox
   */
  :root {
    --bg: #282828; /* dark bg */
    --fg: #ebdbb2; /* dark fg */
  }
}

[class*="cell-"] {
  min-width: 20px;
  min-height: 20px;
  display: inline-block;
}

.cell-hidden {
  margin: 1px;
}

.cell-visible {
  border: 1px solid gray;
}

input[type=number] {
  max-width: 5em;
}

html {
  line-height: 1.5;
  font-family: Georgia, serif;
  font-size: 20px;
  color: var(--fg);
  background-color: var(--bg);
}

body {
  margin: 0 auto;
  max-width: 36em;
  padding-left: 50px;
  padding-right: 50px;
  padding-top: 50px;
  padding-bottom: 50px;
  hyphens: auto;
  word-wrap: break-word;
  text-rendering: optimizeLegibility;
  font-kerning: normal;
}

@media (max-width: 600px) {
  body {
    font-size: 0.9em;
    padding: 1em;
  }
}
  </style>

  <script src="js/wasm_exec.js"></script>

  <script>
    const go = new Go();
    WebAssembly.instantiateStreaming(fetch("webtools.wasm"), go.importObject).then((result) => {
      go.run(result.instance);
    });
  </script>

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

    Copyright (C) 2021 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.
    */

    // return a random number between 1 and max
    function randomInt(max) {
      return Math.floor(Math.random() * Number(max)) + 1;
    }

    // return True if the point (x,y) is in Range r
    // Thanks Wolfgang for this function :)
    function xyInRange(x, y, r) {
      let ax = Math.abs(x);
      let ay = Math.abs(y);
      let abs = 0;

      if (ax > ay) {
        abs = ax + Math.floor(ay / 2);
      } else {
        abs = ay + Math.floor(ax / 2); 
      }

      return abs <= r;
    }

    // Create a Range X
    function range(r) {
      let range_div = document.createElement("div");

      for (y = -r; y <= r; y++) {
        let row = document.createElement("div");
        range_div.appendChild(row).className = "gridRow";
        for (x = -r; x <= r; x++) {
          let cell = document.createElement("div");

          if (xyInRange(x, y, r)) {
            cell.className = "cell-visible";
          } else {
            cell.className = "cell-hidden";
          }

          if (x == 0 && y == 0) {
            cell.id = "center";
            cell.style['backgroundColor'] = "yellow";
          }


          row.appendChild(cell);
        }
      }

      return range_div;
    }
    
    function highlightRandom(range_div, include_center) {
      let cells = range_div.querySelectorAll(".cell-visible");
      while (true) {
        let r = randomInt(cells.length) - 1;
        let cell = cells[r];

        if (cell.id == "center" && !include_center) {
          continue;
        }

        cell.style['backgroundColor'] = "red";
        return;
      }
    }

    document.addEventListener('DOMContentLoaded', function() {
      let result = document.getElementById("result");

      document.getElementById("random_number_form").onsubmit = function() {
        let max_input = document.getElementById('random_number_max');
        result.innerHTML = "Random number: " + randomInt(max_input.value); 
        return false;
      }

      document.getElementById("random_tile_from_xy_form").onsubmit = function() {
        let x_input = document.getElementById('random_tile_from_x');
        let y_input = document.getElementById('random_tile_from_y');
        result.innerHTML = "Random tile: (x:" + randomInt(x_input.value)
                           + ", y:" + randomInt(y_input.value) + ")";
        return false;
      }

      document.getElementById("random_tile_from_range_form").onsubmit = function() {
        let x_input = document.getElementById('random_tile_from_range_x');
        let include_center = document.getElementById('random_tile_from_range_include_center');
        let range_div = range(x_input.value);
        // highlight random tile
        highlightRandom(range_div, include_center.checked);

        result.innerHTML = "";
        result.appendChild(range_div);
        return false;
      }

      document.getElementById("random_map_tile_form").onsubmit = function() {
        const mapName = document.getElementById("random_map_tile_map_select").value;
        const mapUrl = "maps/" + mapName + ".yml"
        const symbols =  document.getElementById("random_map_tile_symbols").value.split(" ");
        const request = new XMLHttpRequest();
        request.onreadystatechange = function() {
          if (request.readyState == 4 && request.status == 200) {
             const pos = randomTileFromSymbol(request.responseText, symbols);
             if (!pos) {
               result.innerHTML = "The Go websassembly returned no value";
             } else if (typeof pos === "string" || pos instanceof String) {
               result.innerHTML = "An error occured: " + pos;
             } else {
               result.innerHTML = "Random tile: (x:" + pos.x + ", y:" + pos.y + ")";
             }
          }
        }
        request.open("GET", mapUrl, true);
        request.send(null);

        return false;
      }
    });
  </script>

</head>

<body>
<h2>General tools</h2>
<div>
<form id="random_number_form">
Random number
  <code>from 1-
  <input id="random_number_max" type="number"/>
  <!-- non visual submit input to submit on "Enter" -->               
  <input type="submit" style="display: none" />
  <input id="random_number_button" value="generate" type="submit">
  </code>
</form>
</div>

<div>
<form id="random_tile_from_xy_form">
Random tile within
  <input id="random_tile_from_x" type="number"/>
  <input id="random_tile_from_y" type="number"/>
  <input id="random_tile_from_xy_button" value="generate" type="submit">
</form>
</div>

<div>
<form id="random_tile_from_range_form">
Random tile within Range
  <input id="random_tile_from_range_x" type="number"/>
  <!-- non visual submit input to submit on "Enter" -->               
  <input type="submit" style="display: none" />
  <input id="random_tile_from_range_button" value="generate" type="submit">
  <input id="random_tile_from_range_include_center" type="checkbox">
  <label for="random_tile_from_range_include_center">include center</label>
</form>
</div>

<h2>The tyrant tools</h2>

<div>
<form id="random_map_tile_form">
Random tile from
  <select name="map" id="random_map_tile_map_select">
    <option value="the-tyrant">the-tyrant</option>
    <option value="2P-ring-street">2P-ring-street</option>
    <option value="2P-river-king">2P-river-king</option>
    <option value="3P-ring-street">3P-ring-street</option>
    <option value="4P-islands">4P-islands</option>
    <option value="the-kraken">the-kraken</option>
  </select>
  <input id="random_map_tile_symbols" value="symbols">
  <input id="random_map_tile_button" value="generate" type="submit">
</form>
</div>

<div id="result">
</div>

</body>
</html>