-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
392 lines (346 loc) · 13.5 KB
/
index.html
File metadata and controls
392 lines (346 loc) · 13.5 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>中興大學美食對戰系統</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.6.1/socket.io.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1, h2 {
text-align: center;
color: #007BFF;
}
.section {
background-color: #fff;
border-radius: 8px;
margin-bottom: 20px;
padding: 20px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input, select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #0056b3;
}
#output, #history {
margin-top: 15px;
border: 1px solid #ccc;
border-radius: 4px;
background: #f9f9f9;
padding: 15px;
overflow-y: auto;
max-height: 200px;
}
.battle-options {
margin-top: 20px;
text-align: center;
}
.battle-options button {
margin: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>中興大學美食對戰系統</h1>
<!-- 註冊與登入區塊 -->
<div class="section">
<h2>註冊 / 登入</h2>
<div class="form-group">
<label for="username">用戶名</label>
<input type="text" id="username" placeholder="輸入用戶名">
</div>
<div class="form-group">
<label for="password">密碼</label>
<input type="password" id="password" placeholder="輸入密碼">
</div>
<button onclick="register()">註冊</button>
<button onclick="login()">登入</button>
<button onclick="logout()">登出</button>
</div>
<!-- 配對與對戰區塊 -->
<div class="section">
<h2>配對與對戰</h2>
<button onclick="joinQueue()">加入配對隊列</button>
<!-- 餐廳選擇(先隱藏) -->
<div class="form-group" id="restaurant" style="display: none">
<label for="restaurant-selection">選擇餐廳</label>
<select id="restaurant-selection" onchange="handleRestaurantSelection()">
<option value="">請選擇</option>
<option value="random">隨機選擇</option>
<option value="list">指定餐廳</option>
<option value="custom">自訂輸入</option>
</select>
</div>
<div id="restaurant-list" style="display:none">
<ul id="restaurant-options"></ul>
</div>
<div id="custom-restaurant-input" style="display:none">
<input type="text" id="custom-restaurant" placeholder="輸入餐廳名稱" onblur="submitCustomRestaurant()">
</div>
<div class="battle-options" id="battleOptions" style="display:none">
<h3>對戰選項</h3>
<button onclick="chooseOption('剪刀')">剪刀</button>
<button onclick="chooseOption('石頭')">石頭</button>
<button onclick="chooseOption('布')">布</button>
</div>
<button id="confirm" style="display:none">確認選擇</button>
<button id="rematchButton" style="display:none;" onclick="rematch()">重新配對</button>
</div>
<!-- 系統訊息區塊 -->
<div class="section">
<h2>系統訊息</h2>
<div id="output"></div>
</div>
<!-- 歷史戰績區塊 -->
<div class="section">
<h2>歷史戰績</h2>
<button onclick="viewHistory()">查看歷史戰績</button>
<div id="history"></div>
</div>
</div>
<script>
const socket = io("http://127.0.0.1:5000");
let currentUser = null;
let selectedRestaurant = null;
let selectedOption = null;
function log(message) {
const output = document.getElementById("output");
output.innerHTML += `<p>${message}</p>`;
output.scrollTop = output.scrollHeight;
}
function register() {
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
const instagram = window.prompt("請輸入 Instagram 帳號");
const gender = window.prompt("請輸入性別 (男 / 女 / 邪惡的大卡車)");
fetch("http://127.0.0.1:5000/register", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({username, password, instagram, gender})
})
.then(response => response.json())
.then(data => log(data.message || data.error));
}
function login() {
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
fetch("http://127.0.0.1:5000/login", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({username, password})
})
.then(response => response.json())
.then(data => {
if (data.message) {
currentUser = username;
log(data.message);
} else {
log(data.error);
}
});
}
function logout() {
currentUser = null;
log("已登出");
}
function joinQueue() {
if (!currentUser) {
log("請先登入");
return;
}
socket.emit("join_queue", {username: currentUser});
}
function handleRestaurantSelection() {
const selection = document.getElementById("restaurant-selection").value;
const listDiv = document.getElementById("restaurant-list");
const customInputDiv = document.getElementById("custom-restaurant-input");
// 重置區塊顯示
listDiv.style.display = "none";
customInputDiv.style.display = "none";
if (selection === "random") {
fetch('http://127.0.0.1:5000/restaurant/random')
.then(response => response.json())
.then(data => {
if (data.error) {
log(data.error);
} else {
selectedRestaurant = data.name; // 設定選擇餐廳
log(`隨機選擇餐廳: ${selectedRestaurant} (評分: ${data.rating})`);
}
});
} else if (selection === "list") {
fetch('http://127.0.0.1:5000/restaurant/list')
.then(response => response.json())
.then(data => {
const restaurantOptions = document.getElementById("restaurant-options");
restaurantOptions.innerHTML = ""; // 清空清單
if (data.length === 0) {
log("目前沒有餐廳資料");
} else {
data.forEach(restaurant => {
const li = document.createElement("li");
li.textContent = `${restaurant.name} - 評分: ${restaurant.rating}`;
li.style.cursor = "pointer";
li.onclick = () => {
selectedRestaurant = restaurant.name; // 點選後儲存名稱
log(`您選擇了: ${selectedRestaurant}`);
};
restaurantOptions.appendChild(li);
});
listDiv.style.display = "block";
}
});
} else if (selection === "custom") {
customInputDiv.style.display = "block";
}
}
function submitCustomRestaurant() {
const restaurantName = document.getElementById("custom-restaurant").value.trim();
if (restaurantName === "") {
log("請輸入有效的餐廳名稱");
return;
}
selectedRestaurant = restaurantName; // 設定自訂餐廳
log(`自訂餐廳名稱: ${selectedRestaurant}`);
}
function confirmSelection() {
if (!selectedRestaurant) {
log("請選擇餐廳");
return;
}
log(`最終選擇的餐廳: ${selectedRestaurant}`);
}
function chooseOption(option) {
selectedOption = option;
log(`您選擇了 ${option}`);
}
function rematch() {
document.getElementById("battleOptions").style.display = "none";
joinQueue();
log("已重新加入配對隊列");
}
function viewHistory() {
fetch("http://127.0.0.1:5000/history")
.then(response => response.json())
.then(data => {
const history = document.getElementById("history");
history.innerHTML = ""; // 清空現有內容
data.forEach((record, index) => {
const gameDate = new Date(record.date); // 假設 API 返回的資料包含日期欄位
const formattedDate = `${gameDate.getFullYear()}/${gameDate.getMonth() + 1}/${gameDate.getDate()}`;
history.innerHTML += `
<div class="game-record">
<h4>Game ${index + 1} (${formattedDate})</h4>
<p>出拳: ${record.option1} / ${record.option2}</p>
<p>結果: ${record.result1 === "win" ? "勝" : "敗"}</p>
<p>獲勝餐廳: ${record.restaurant1}</p>
<p>對手: ${record.opponent}</p>
<p>對手Instagram: ${record.opponent_instagram || "無資料"}</p>
</div>
`;
});
})
.catch(error => console.error("Error fetching history:", error));
}
socket.on("queue_status", data => log(data.message));
socket.on("match_ready", data => {
const opponent = currentUser === data.player1 ? `${data.player2} (${data.player2_gender})` : `${data.player1} (${data.player1_gender})`;
log(`配對成功!對手是 ${opponent}`);
log("請選擇餐廳和對戰選項");
document.getElementById('battleOptions').style.display = 'block';
document.getElementById('restaurant').style.display = 'block';
document.getElementById('rematchButton').style.display = 'none';
const confirmButton = document.getElementById('confirm');
confirmButton.style.display = 'block';
const restaurantSelection = document.getElementById('restaurant-selection');
const battleOptions = document.getElementById('battleOptions');
window.chooseOption = function (option) {
selectedOption = option;
log(`已選擇: ${option}`);
};
confirmButton.onclick = function () {
if (restaurantSelection.value && battleOptions) {
socket.emit("confirm_ready", {
player1: data.player1,
player2: data.player2,
user: currentUser,
});
log("等待對手確認...");
} else {
log("請選擇餐廳和對戰選項");
}
};
});
socket.on("start_battle", data => {
log(data.message);
const opponent = currentUser === data.player1 ? data.player2 : data.player1;
const confirmButton = document.getElementById('confirm');
confirmButton.onclick = confirmSelection; // 綁定確認邏輯
socket.emit("start_game", {
username: currentUser,
opponent: opponent,
rest: selectedRestaurant,
opt: selectedOption
});
});
socket.on("game_result", data => {
const {
user,
restaurant1,
option1,
result1,
opponent,
restaurant2,
option2,
result2,
winning_restaurant
} = data.details;
// 顯示詳細對戰結果
log(`${user}(${restaurant1}) ${result1} vs ${opponent}(${restaurant2}) ${result2}`);
log(`勝者餐廳選擇: ${winning_restaurant}`);
// 顯示重新配對按鈕
document.getElementById("rematchButton").style.display = "block";
document.getElementById("battleOptions").style.display = "none";
document.getElementById("restaurant").style.display = "none";
document.getElementById("confirm").style.display = "none";
document.getElementById("custom-restaurant-input").style.display = "none";
document.getElementById("restaurant-list").style.display = "none";
});
socket.on("error", error => log(`Error: ${error.message}`));
</script>
</body>
</html>