-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
142 lines (122 loc) · 3.46 KB
/
script.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
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
// 初始化之後
const gameData1 = {
playerHealth: 50,
playerHand: [2],
opponentHealth: 50
}
// 抽牌
const gameData2 = {
playerHand: [2,5],
}
// 出牌
const gameData3 = {
playerHand: [2],
opponentHealth: 45
}
// 將遊戲資訊的 JSON 資料更新到頁面上
function updateGameInfo(gameData) {
// 更新玩家血量
if (gameData.playerHealth) document.getElementById("player-health").textContent = gameData.playerHealth
// 更新玩家手牌數
if (gameData.playerHand) document.getElementById("player-hand").textContent = gameData.playerHand.join(', ')
// 更新對手血量
if (gameData.opponentHealth) document.getElementById("opponent-health").textContent = gameData.opponentHealth
}
// 在前端頁面新增內容,後期無用
const sendChatMessage = () => {
const messageInput = document.getElementById("message-input");
const message = messageInput.value;
if (message.trim() !== "") {
sendMessage('chat', { message })
// 清空輸入欄位
messageInput.value = "";
}
}
// 處理送出按鈕的點擊事件
document.getElementById("send-button").addEventListener("click", sendChatMessage)
document.getElementById("message-input").addEventListener('keydown', (e) => {
// console.log(e.code)
if (e.code === 'Enter' || e.code === 'NumpadEnter') sendChatMessage()
}, false);
const ws = new WebSocket("wss://shrouded-castle-56322-ddcaf8d0cc91.herokuapp.com/")
var messages = document.getElementById("messages");
function showMessage(message, type='info') {
const p = document.createElement("p");
let fixedMessage
switch(type) {
case 'error':
fixedMessage = `[錯誤] ${message}`
break
case 'sys':
fixedMessage = `[通知] ${message}`
break
case 'info':
default:
fixedMessage = message
}
p.textContent = fixedMessage;
messages.appendChild(p);
messages.scrollTop = messages.scrollHeight; // 捲動置底
}
// 跟 websocket 溝通的最底層
function sendMessage(action, payload = null) {
if (ws && ws.readyState === WebSocket.OPEN) {
var message = JSON.stringify({ action: action, payload: payload });
ws.send(message);
} else {
showMessage("WebSocket not connected.", "error");
}
}
function createRoom() {
sendMessage('create_room')
}
function joinRoom() {
var room_id = parseInt(prompt("Enter room ID:"));
if (isNaN(room_id)) {
showMessage("room_id is not number.", "error");
return
}
sendMessage('join_room', { room_id: room_id });
}
function startGame() {
var room_id = parseInt(prompt("Enter room ID to start the game:"));
if (isNaN(room_id)) {
showMessage("room_id is not number.", "error");
return
}
sendMessage('start_game', { room_id: room_id });
}
function playerDrawCard() {
sendMessage('player_draw_card')
}
function playerPlayCard() {
sendMessage('player_play_card')
}
function handlGameEvent(payload) {
const { message, gameEvent, gameData } = payload
switch(gameEvent) {
case 'setState':
showMessage(message, 'sys')
updateGameInfo(gameData)
break
case 'turnStart':
showMessage(message, 'sys')
break
case 'waitOther':
showMessage(message, 'sys')
break
case 'gameOver':
showMessage(message, 'sys')
break
}
}
ws.onmessage = function(event) {
const data = JSON.parse(event.data)
if (data.gameEvent) {
handlGameEvent(data)
} else if (data.error) {
showMessage(data.message, 'error')
} else {
showMessage(data.message, 'info')
}
}