-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
210 lines (188 loc) · 8.29 KB
/
index.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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>websocket-chat</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript"
src="/socket.io/socket.io.js"></script>
</head>
<body>
<div class="container">
<h1>werewolf-html-client</h1>
<form id="form-beforegame" class="form-inline">
<input type="text" class="form-control" id="name_form">
<button type="button" class="btn btn-primary" id="join">join room</button>
<button type="button" class="btn btn-primary" id="exit">exit room</button>
<button type="button" class="btn btn-primary" id="game_start">gameStart</button>
<div id="id_name_role"></div>
</form>
<form id="form-phaseshift" class="form-inline">
<button type="button" class="btn btn-primary" id="morning_check">morning check</button>
<button type="button" class="btn btn-primary" id="finish_daytime">finish daytime</button>
<select id="vote_cands">
<option value="none">vote</option>
</select>
<button type="button" class="btn btn-primary" id="vote">vote</button>
<button type="button" class="btn btn-primary" id="evening_check">evening check</button>
<button type="button" class="btn btn-primary" id="evening_chat">sending chat</button>
</form>
<form id="form-action" class="form-inline">
<select id="action_cands">
<option value="none">act</option>
</select>
<select id="bite_power">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" class="btn btn-primary" id="action">action</button>
</form>
<form id="form-debug" class="form-inline">
<button type="button" class="btn btn-primary" id="clear_log">clear log</button>
<button type="button" class="btn btn-primary" id="end_night">end night</button>
</form>
<div id="logs">log:</div>
</div>
<script type="text/javascript">
function appendLog(text){ $("#logs").append("<div>" + text + "</div>"); }
function clearLog() { $("#logs").html(""); }
// init
function randomString(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
};
const userId = randomString(8)
let name = "";
let role = ""
$("#id_name_role").html("id: " + userId + ", name: " + name + ", role: " + role);
// socket on
let socket = io.connect();
socket.on("connectionEstablished", function(){ appendLog("connect!"); });
socket.on("debug", function(debug){
console.log(debug);
appendLog(debug);
})
// debug
$("#clear_log").click(function(e){ clearLog(); });
$("#end_night").click(function(e){ socket.emit("endNight"); });
// before
$("#join").click(function(e){
const message = $("#name_form").val();
if(message == ""){
appendLog("input your name");
} else {
$("#name_form").val("");
socket.emit("joinRoom", {userId : userId, name: message});
name = message
$("#id_name_role").html("id: " + userId + ", name: " + name + ", role: " + role);
e.preventDefault();
}
});
$("#exit").click(function(e){
socket.emit("exitRoom");
$("#id_name_role").html("id: " + userId + ", name: " + name + ", role: " + role);
});
$("#game_start").click(function(e){ socket.emit("startGame"); });
// phase
let id2nameMap = {};
let name2idMap = {};
socket.on("memberChanged", function(pairs){ // pairs:[{id, name}]
id2nameMap = pairs.reduce((ret,p)=>{ // setup map
ret[p.id] = p.name;
return ret;
},{});
name2idMap = pairs.reduce((ret,p)=>{
ret[p.name] = p.id;
return ret;
},{});
appendLog(pairs.reduce((ret,p)=>{ret.push(p.name); return ret;},[]));
});
socket.on("phaseChange", function(data){
appendLog(data.phase + " of day " + data.dayCount + ", sec:" + data.timeCount);
});
socket.on("roleAck", function(roleName){
role = roleName;
$("#id_name_role").html("id: " + userId + ", name: " + name + ", role: " + role);
appendLog("role: " + role);
});
$("#morning_check").click(function(e){ socket.emit("morningResultChecked"); });
$("#finish_daytime").click(function(e){ socket.emit("finishDiscussion"); });
$("#evening_check").click(function(e){ socket.emit("eveningResultChecked"); });
// vote
socket.on("voteCandidates", function(ids){
appendLog("select and vote")
$("#vote_cands").html("");
for(const id of ids){
$("#vote_cands").append("<option value="+id+">"+id2nameMap[id]+"</option>");
}
})
$("#vote").click(function(e){
const voteId = document.getElementById("vote_cands").value;
// if(id!="" || id!="none"){ socket.emit("vote", [voteId]); }
appendLog(voteId)
socket.emit("vote", [voteId]);
$("#vote_cands").html("");
});
socket.on("voteResult", function(result){
console.log(result);
appendLog("We excute " + id2nameMap[result.executedId] + ", and We hope the village become peace");
})
// chat
$("#evening_chat").click(function(e){
const message = $("#name_form").val();
if(message == ""){
appendLog("input message");
} else {
$("#name_form").val("");
socket.emit("chat", {message: message});
e.preventDefault();
}
});
socket.on("chat", function(result){
console.log(result);
appendLog(id2nameMap[result.userId] + "さんが発言しました:" + result.message)
});
// action
socket.on("actionCandidates", function(ids){
appendLog("select and action")
$("#action_cands").html("");
for(const id of ids){
$("#action_cands").append("<option value="+id+">"+id2nameMap[id]+"</option>");
}
})
$("#action").click(function(e){
const actId = document.getElementById("action_cands").value;
const power = document.getElementById("bite_power").value;
socket.emit("action", {userId:actId, bitePower:power});
$("#action_cands").html("");
});
socket.on("actionResult", function(result){
console.log(result);
appendLog(id2nameMap[result.objectId]+" : "+result.type);
})
//
socket.on("morningResult", function(result){
console.log(result);
let str = result.deadIds.reduce((ret,id)=>{
return ret + id2nameMap[id] + ", ";
}, "");
appendLog("deads:" + str);
});
//
socket.on("gameResult", function(result){
console.log(result);
appendLog(result.winTeam + "wins!");
let str = result.winIds.reduce((ret,id)=>{
return ret + id2nameMap[id] + ", ";
}, "");
appendLog("win: " + str);
});
socket.on("error", function(data){
console.log(data);
appendLog("status:"+data.statusCode+" "+data.message);
})
</script>
</body>
</html>