-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainpage.html
215 lines (211 loc) · 9.02 KB
/
mainpage.html
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
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--
source: https://gist.github.com/tanmaykm/5111225
-->
<html>
<head>
<title>Websocket Demo</title>
<style>
.cell {
border: dotted 1px #444444;
font: 40px arial, sans-serif;
}
</style>
<script>
function log_msg(msg) {
console.log(msg)
}
var owner
var owner_array = []
function timer() {
var counter = 30
var interval = setInterval(() => {
document.getElementById('timer').innerHTML = counter
counter--
if (counter < 0) {
clearInterval(interval)
count_total()
}
}, 1000)
}
var s //socket object for connection
function count_total() {
var me = 0
var other = 0
var me2 = 0
var other2 = 0
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
// log_msg(i.toString() + j.toString())
var num = parseInt(document.getElementById(i.toString() + j.toString()).innerHTML)
if (owner_array[i][j] == owner) {
me += num
me2 += 1
} else if (owner_array[i][j] != null) {
other += num
other2 += 1
}
}
}
log_msg(me, other)
alert(
'The game is over \nYou score: ' +
me +
'\n Your boxes: ' +
me2 +
'\nYour opponent score: ' +
other +
'\nYour opponent boxes: ' +
other2
)
document.location.reload()
}
function doInit() {
try {
for (var i = 0; i < 5; i++) {
owner_array.push([])
for (var j = 0; j < 5; j++) {
owner_array[i].push(null)
}
}
owner = document.getElementById('user').value
var host = 'ws://localhost:4545/' //設定socker server的ip:port
/*if(window.location.hostname) {
host = "ws://" + window.location.hostname + ":4545/";
}*/
s = new WebSocket(host) //建立socket元件
//設定幾個主要事件
s.onopen = function (e) {
log_msg('connected...')
}
s.onclose = function (e) {
log_msg('connection closed.')
}
s.onerror = function (e) {
log_msg('connection error.')
}
//當server送訊息來時
s.onmessage = function (e) {
log_msg('message: ' + e.data)
log_msg(owner_array)
//e.data是Socket server送來的訊息
//用, 來切割訊息
strs = e.data.split(',')
cellOwner = strs[1]
cellID = strs[0]
if (strs[2] == 'change') {
if (strs[1] == 'null') owner_array[cellID[0]][cellID[1]] = null
else owner_array[cellID[0]][cellID[1]] = strs[1]
if (owner_array[cellID[0]][cellID[1]] == null) changeBack(cellID, 'White')
else if (cellOwner == owner) changeBack(cellID, 'green')
else changeBack(cellID, 'red')
} else if (strs[0] == 'timer') {
timer()
} //解讀各段訊息的內容
else {
setCell(cellID, cellOwner) //改格子的符號
}
}
} catch (ex) {
log_msg('connection exception:' + ex)
}
//取得class是 "cell" 的元件
const cells = document.querySelectorAll('.cell')
//逐一設定onclick事件
cells.forEach(function (el) {
el.onclick = function () {
// log_msg(owner_array)
id = this.id
now = parseInt(document.getElementById(id).innerText)
//log_msg(id[0])
log_msg('click origin owner', owner_array[id[0]][id[1]])
if (owner_array[id[0]][id[1]] == null) {
owner_array[id[0]][id[1]] = owner
log_msg('new owner', owner_array[id[0]][id[1]])
s.send(id + ',' + owner + ',' + 'change')
s.send(id + ',' + owner + ',' + 'change')
s.send(id + ',' + owner + ',' + 'attack')
document.getElementById(id).innerHTML = now + 1
} else if (owner_array[id[0]][id[1]] == owner) {
document.getElementById(id).innerHTML = now + 1
s.send(id + ',' + owner + ',' + 'defense')
} else if (owner_array[id[0]][id[1]] != owner) {
document.getElementById(id).innerHTML = now - 1
s.send(id + ',' + owner + ',' + 'attack')
if (now - 1 == 0) {
owner_array[id[0]][id[1]] = null
s.send(id + ',' + 'null' + ',' + 'change')
}
} else log_msg('click bug')
}
})
}
function changeBack(id, color) {
document.getElementById(id).style.backgroundColor = color
}
//設定格子的符號
function setCell(id, clicker) {
now = parseInt(document.getElementById(id).innerText)
if (clicker != owner) {
if (owner_array[id[0]][id[1]] == clicker) {
document.getElementById(id).innerHTML = now + 1
} else if (owner_array[id[0]][id[1]] != clicker) {
document.getElementById(id).innerHTML = now - 1
if (now - 1 == 0) {
owner_array[id[0]][id[1]] = null
s.send(id + ',' + 'null' + ',' + 'change')
}
} else {
log_msg('!!!!!bug!!!!!!!!')
}
}
}
</script>
</head>
<body onload="doInit()">
<h3>搶地遊戲</h3>
<h1 id="timer">0</h1>
<br />
<button onclick='(function(){ s.send("timer") })();'>start</button> <br />
輸入使用者名稱<input type="text" value="O" id="user" />
<button onclick="count_total()">total</button>
<table border="1" width="500px" height="500px">
<tr>
<td class="cell" id="00">0</td>
<td class="cell" id="10">0</td>
<td class="cell" id="20">0</td>
<td class="cell" id="30">0</td>
<td class="cell" id="40">0</td>
</tr>
<tr>
<td class="cell" id="01">0</td>
<td class="cell" id="11">0</td>
<td class="cell" id="21">0</td>
<td class="cell" id="31">0</td>
<td class="cell" id="41">0</td>
</tr>
<tr>
<td class="cell" id="02">0</td>
<td class="cell" id="12">0</td>
<td class="cell" id="22">0</td>
<td class="cell" id="32">0</td>
<td class="cell" id="42">0</td>
</tr>
<tr>
<td class="cell" id="03">0</td>
<td class="cell" id="13">0</td>
<td class="cell" id="23">0</td>
<td class="cell" id="33">0</td>
<td class="cell" id="43">0</td>
</tr>
<tr>
<td class="cell" id="04">0</td>
<td class="cell" id="14">0</td>
<td class="cell" id="24">0</td>
<td class="cell" id="34">0</td>
<td class="cell" id="44">0</td>
</tr>
</table>
</body>
</html>