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
|
; (() => {
let connected = false
function dial() {
if (connected) {
return
}
const conn = new WebSocket(`ws://${location.host}/subscribe`)
conn.addEventListener('close', ev => {
console.error(`WebSocket Disconnected code: ${ev.code}, reason: ${ev.reason}`)
connected = false
if (ev.code !== 1001 && ev.code !== 404) {
console.error('Reconnecting in 1s')
setTimeout(dial, 1000)
}
})
conn.addEventListener('open', ev => {
connected = true
console.info('websocket connected')
})
conn.addEventListener('message', ev => {
if (typeof ev.data !== 'string') {
console.error('unexpected message type', typeof ev.data)
return
}
if (ev.data === "START") {
appendGameLog("The game has started!")
}
})
}
async function loggedIn() {
try {
const resp = await fetch('/loggedin', {
method: 'GET'
})
if (resp.status === 404) {
console.warn("User not logged in.");
return;
}
if (resp.status !== 200) {
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
}
dial()
} catch (err) {
console.error(`Login check failed: ${err.message}`)
}
}
loggedIn()
const gameLog = document.getElementById('log')
const loginForm = document.getElementById('login-form')
const loginInput = document.getElementById('login-input')
const startButton = document.getElementById('start')
const rollButton = document.getElementById('roll')
const buyButton = document.getElementById('buy')
function appendGameLog(text) {
const p = document.createElement('p')
p.innerText = text
gameLog.append(p)
return p
}
appendGameLog('Welcome to Monopoly Web')
loginForm.onsubmit = async ev => {
ev.preventDefault()
const msg = loginInput.value
if (msg === '') {
return
}
loginInput.value = ''
try {
const resp = await fetch('/login', {
method: 'POST',
body: msg,
})
if (resp.status !== 200) {
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
}
dial()
} catch (err) {
console.error(`Login failed: ${err.message}`)
}
}
startButton.addEventListener('click', async () => {
try {
const resp = await fetch('/start', {
method: 'POST',
})
if (resp.status !== 202) {
const errMsg = await resp.text()
appendGameLog(errMsg)
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
}
} catch (err) {
console.error(`Start failed: ${err.message}`)
}
})
rollButton.addEventListener('click', async () => {
try {
const resp = await fetch('/roll', {
method: 'POST',
})
if (resp.status !== 200) {
const errMsg = await resp.text()
appendGameLog(errMsg)
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
}
} catch (err) {
console.error(`Start failed: ${err.message}`)
}
})
buyButton.addEventListener('click', async () => {
try {
const resp = await fetch('/buy', {
method: 'POST',
})
if (resp.status !== 200) {
const errMsg = await resp.text()
appendGameLog(errMsg)
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
}
} catch (err) {
console.error(`Start failed: ${err.message}`)
}
})
})()
|