-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
263 lines (208 loc) · 6.99 KB
/
index.js
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
import Tilebag from "./game/tilebag.js";
import Board from "./game/board.js";
import Renderer from "./game/renderer.js";
import Rules from "./game/rules.js";
import Hands from "./game/hands.js";
import { initUndo } from "./game/undo.js";
import { initSubmit } from "./game/submit.js";
import Players from "./game/players.js";
import shapesIcons from "./game/shapeIcons.js";
import { IsThemeLight } from "./tools/matchMedia.js";
import getRefs from "./tools/getRefs.js";
{
const version = 'v4';
const ref = document.getElementById('version');
if (version != ref.innerText) {
ref.innerText = version + '*';
}
}
const numTilesets = 3;
const colorNames = ["red", "orange", "yellow", "green", "blue", "purple"];
const shapeNames = ['square', 'circle', 'diamond', 'plus', 'star', 'cross'];
const numPlayers = 2;
const numTilesInHand = 6;
const viewingPlayerId = 0;
// newGame();
// localStorage.clear();
const wsw = {
ws: undefined, onopen: () => { }, onclose: () => { },
onerror: () => { }, onmessage: () => { }, init: () => { },
ref: document.getElementById('ws-status')
}; // web socket wrapper
const url = 'wss://qwirkle-ws.herokuapp.com';
// const url = 'ws://localhost:3000/ws';
wsw.onopen = () => {
console.log('ws open');
wsw.ref.classList.add('open');
// setTimeout(() => {
// wsw.ws.close();
// }, 1000);
};
wsw.onclose = () => {
console.log('ws closed');
wsw.ref.classList.remove('open');
wsw.init();
};
wsw.onerror = () => {
console.log('ws error');
wsw.ref.classList.remove('open');
wsw.init();
};
wsw.init = () => {
wsw.ws = new WebSocket(url);
wsw.ws.onopen = wsw.onopen;
wsw.ws.onclose = wsw.onclose;
wsw.ws.onerror = wsw.onerror;
wsw.ws.onmessage = wsw.onmessage;
}
newScreen();
function newScreen() {
const refs = getRefs({ home: 'new-screen', tiles: 'new-tiles' });
function setStyles(ref, attrs) {
for (const key in attrs) {
ref.style[key] = attrs[key];
}
}
function fhsla(h, s, l, a) {
return `hsla(${h},${s}%,${l}%, ${a})`;
}
function getColor(colorId) {
const hues = [0, 30, 50, 120, 200, 270];
// const hues = Array(6).fill(0).map((_, i) => 360 / 6 * i);
const color = [
hues[colorId],
100,
50
];
return color;
}
for (let colorId = 0; colorId < 6; colorId++) {
const color = getColor(colorId);
for (let shapeId = 0; shapeId < 6; shapeId++) {
const ref = document.createElement('div');
ref.classList.add('tile');
setStyles(ref, {
color: fhsla(...color, 1.0),
borderColor: fhsla(...color, 0.1),
backgroundColor: fhsla(color[0], 100, IsThemeLight() ? 95 : 10, 1.0),
});
ref.appendChild(shapesIcons[shapeId](fhsla(...color, 1.0)));
refs.tiles.appendChild(ref);
}
}
}
homeScreen();
function homeScreen() {
const refs = getRefs({ home: 'home-screen', game: 'game-screen', tiles: 'home-tiles', continue: 'btn-continue', new: 'btn-new' });
refs.continue.onclick = () => {
refs.home.classList.add('display-none');
refs.game.classList.remove('display-none');
newGame();
}
refs.new.onclick = () => {
refs.home.classList.add('display-none');
refs.game.classList.remove('display-none');
localStorage.clear();
newGame();
}
function setStyles(ref, attrs) {
for (const key in attrs) {
ref.style[key] = attrs[key];
}
}
function fhsla(h, s, l, a) {
return `hsla(${h},${s}%,${l}%, ${a})`;
}
function getColor(colorId) {
const hues = [0, 30, 50, 120, 200, 270];
// const hues = Array(6).fill(0).map((_, i) => 360 / 6 * i);
const color = [
hues[colorId],
100,
50
];
return color;
}
const rand6 = () => Math.floor(Math.random() * 6);
for (let i = 0; i < 4; i++) {
const colorId = rand6(), shapeId = rand6();
const color = getColor(colorId);
{
const ref = document.createElement('div');
ref.classList.add('tile');
setStyles(ref, {
color: fhsla(...color, 1.0),
borderColor: fhsla(...color, 0.1),
backgroundColor: fhsla(color[0], 100, IsThemeLight() ? 95 : 10, 1.0),
});
ref.appendChild(shapesIcons[shapeId](fhsla(...color, 1.0)));
refs.tiles.appendChild(ref);
}
}
}
function newGame() {
const rules = Rules(numTilesets, colorNames, shapeNames, numPlayers, numTilesInHand, viewingPlayerId);
const tilebag = Tilebag(rules);
const hands = Hands(rules, tilebag);
const board = Board(rules);
const n = 0;
for (let j = -n; j < n; j++) {
for (let i = -n; i < n; i++) {
for (let k = 0; k < 10; k++) {
if (board.addTile(i, j, Math.floor(Math.random() * 108))) break;
}
}
}
// board.addTile(0, 0, 0);
// board.addTile(1, 0, 1);
// board.addTile(2, 0, 2);
// board.addTile(3, 0, 3);
// board.addTile(4, 0, 4);
const players = Players(rules);
const renderer = Renderer(rules, board, hands, players);
initUndo(board, hands, renderer);
const submitScore = initSubmit(board, hands, renderer, players, rules, tilebag, wsw);
renderer.render();
renderer.updatePlayer();
renderer.updateScore();
document.getElementById('next-player').onclick = () => {
rules.nextViewingPlayer();
renderer.updatePlayer();
renderer.updateScore();
renderer.render();
}
window.matchMedia('(orientation: landscape)').addEventListener('change', event => {
renderer.render();
});
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
renderer.render();
});
wsw.onmessage = (msg) => {
const data = JSON.parse(msg.data);
console.log('ws msg', data);
switch (data.action) {
case 'oneMove':
if (data.data.owner !== rules.getViewingPlayer()) {
// console.log('UPDATE');
for (const tile of data.data.tiles) {
board.tiles.live.push({ x: tile.x, y: tile.y, id: tile.tileId });
// hands.recordPlacedTile(tile.x, tile.y);
}
// console.log('a', board.tiles.live);
renderer.resize();
renderer.renderTiles();
// console.log('b');
hands.setPlacedTiles(data.data.tiles);
(async () => {
await submitScore();
renderer.render();
})();
// tiles.live.push({ x, y, id });
}
break;
default:
break;
}
};
wsw.init();
}