-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
87 lines (78 loc) · 2.69 KB
/
app.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
import onChange from './node_modules/on-change/index.js';
import render from './view.js';
const appHeight = () => {
const doc = document.documentElement
doc.style.setProperty('--app-height', `${window.innerHeight}px`)
}
window.addEventListener('resize', appHeight)
appHeight();
const app = (object) => {
const state = {
init: false,
active_suit: '',
attacker: '',
deck: '',
defender: '',
passes: '',
players: '',
players_count: '',
ranks: '',
suits: '',
round: true,
};
const watchedState = onChange(state, render(state));
const playersNumberButtons = document.querySelectorAll('.players_number_button');
const dialogue = document.querySelector('.players_number_dialogue');
playersNumberButtons.forEach((button) => {
button.addEventListener('click', () => {
const playersNumber = button.dataset.playersNumber;
fetch(`${playersNumber}.json`)
.then(response =>
response.json()
.then(data => {
const { active_suit, attacker, deck, defender, passes, players, players_count } = object;
watchedState.active_suit = active_suit;
watchedState.attacker = attacker;
watchedState.deck = deck;
watchedState.defender = defender;
watchedState.passes = passes;
watchedState.players = players;
watchedState.players_count = players_count;
}))
.then(() => {
dialogue.style.display="none";
const dealCardsButton = document.querySelector('.deal_cards');
dealCardsButton.style.display = 'block';
dealCardsButton.addEventListener('click', () => {
console.log(watchedState.round)
watchedState.round = false });
watchedState.init = true;
})
})
});
};
function connect() {
var ws = new WebSocket('ws://localhost:8767');
ws.onopen = function() {
// subscribe to some channels
ws.send(JSON.stringify({
"type": "hi"
}));
};
ws.onmessage = function(e) {
console.log('Message: ', e.data);
const response = JSON.parse(e.data);
app(response);
};
ws.onclose = function(e) {
console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
setTimeout(function() {
connect();
}, 1000);
};
ws.onerror = function(err) {
console.error('Socket encountered error: ', err.message, 'Closing socket');
ws.close();
};
}
connect();