-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (100 loc) · 3.5 KB
/
main.js
File metadata and controls
115 lines (100 loc) · 3.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
import {createBoard, playMove} from "./connect4.js"
window.addEventListener("DOMContentLoaded", () => {
const board = document.querySelector(".board");
const form = document.getElementById("chat-form");
createBoard(board);
const websocket = new WebSocket(getWebSocketServer());
// sendMessage(form, websocket);
initGame(websocket);
recieveMoves(board, websocket);
sendMoves(board, websocket);
});
function showMessage(message){
window.setTimeout(() => window.alert(message), 50);
}
function sendMoves(board, websocket){
// prevent moves from being sent to spectators
const params = new URLSearchParams(window.location.search);
if (params.has("watch")){
return;
}
// send play event for moves in a column when clicking the column
board.addEventListener("click", ({target}) => {
const column = target.dataset.column;
// ignore clicks outside the board
if(column === undefined){
return;
}
const event = {
type: "play",
column: parseInt(column, 10),
};
websocket.send(JSON.stringify(event));
})
}
function recieveMoves(board, websocket){
websocket.addEventListener("message", ({data}) => {
const event = JSON.parse(data);
switch (event.type){
case "init":
document.querySelector(".join").href = "?join=" + event.join;
document.querySelector(".watch").href = "?watch=" + event.watch;
case "play":
playMove(board, event.player, event.column, event.row);
break;
case "win":
showMessage(`Player ${event.player} wins!`);
websocket.close(1000);
break;
case "chat":
displayMessages(event.message);
break;
case "error":
showMessage(event.message);
break;
default:
throw new Error (`Unsupported event type ${event.type}.`);
}
});
}
function initGame(websocket){
websocket.addEventListener("open" ,() => {
const params = new URLSearchParams(window.location.search)
let event = {type : "init"};
if (params.has("join")){
event.join = params.get("join")
}else if (params.has("watch")) {
event.watch = params.get("watch")
}else {
}
websocket.send(JSON.stringify(event));
});
}
function getWebSocketServer(){
if(window.location.host == "asiakn.github.io"){
return "wss://connect4-game.herokuapp.com/";
} else if(window.location.host == "localhost:8000"){
return "ws://localhost:8001";
}else{
throw new Error(`Unsupported host: ${window.location.host}`);
}
}
function sendMessage (form, websocket){
form.addEventListener("submit", (form) => {
const chatInput = document.getElementById('chat-input');
const event = {
type: "chat",
message: chatInput.value
};
websocket.send(JSON.stringify(event));
chatInput.value = '';
form.preventDefault();
})
}
function displayMessages(message) {
const messages = document.getElementById("chatbox")
const linebreak = document.createElement('br');
const content = document.createTextNode(message)
messages.appendChild(linebreak);
messages.appendChild(content)
}