-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.js
More file actions
158 lines (113 loc) · 4 KB
/
connection.js
File metadata and controls
158 lines (113 loc) · 4 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
var peer;
var num="";
var BtoM;
var MtoB;
var gameId="";
var who="";
//This function runs if the user indicates they are the Maker. The function
//turns on the Maker board element in the HTML, and waits for commands from
//Breaker to set it up. Since Breaker provides the only input to the game,
//Maker doesn't do anything without a signal from Breaker.
document.getElementById("makerId").onclick = function (){
gameId=document.getElementById("gameid").value;
who="maker";
peer = new Peer("IAmMaker"+gameId);
playerNames = getNames();
document.getElementById("audio").play()
turnOff(document.getElementsByName("initial"));
document.getElementById("warning").innerText="waiting for Breaker..."
turnOn(document.getElementsByName("isMaker"));
peer.on('connection', function(conn) {
conn.on('data',function(data){
//There are 5 signals Breaker can send to Maker:
//"hello": asking for player names
//"initialize": set up the board
//"turn": switch turns
//"wins": run end game
//anything else: process a click
if(data[0]=="hello"){
conn.send(playerNames);
}else if(data[0]=="initialize"){
turnOff(document.getElementsByName("warning"));
makeMaker(data[1],data[2]);
}else if(data[0]=="turn"){
makeTurn();
}else if(data[0]=="goodbye"){
document.write("Goodbye!");
}else if(data[0]=="wins"){
wins(data[1]);
turnOff(document.getElementsByName("playing"));
turnOn(document.getElementsByName("ending"));
turnOn(document.getElementsByName("PlayAgain"));
document.getElementById("PlayNo").onclick=function(){
document.write("Goodbye!");
}
document.getElementById("PlayYes").onclick=function(){
conn.send("new");
turnOff(document.getElementsByName("isMaker"));
var makerBoard=document.getElementById("MakerBoard");
while(makerBoard.firstChild){
makerBoard.removeChild(makerBoard.firstChild);
}
turnOff(document.getElementsByName("PlayAgain"));
turnOn(document.getElementsByName("SameMaker"));
document.getElementById("endGameText").innerText = "Are the codemakers the same?";
document.getElementById("SameYes").onclick=function(){
turnOff(document.getElementsByName("SameMaker"));
turnOff(document.getElementsByName("ending"));
turnOn(document.getElementsByName("isMaker"));
document.getElementById("endGameText").innerText="Would you like to play again?";
reset();
conn.send(playerNames);
}
document.getElementById("SameNo").onclick=function(){
playerNames=getNames();
turnOff(document.getElementsByName("SameMaker"));
turnOff(document.getElementsByName("ending"));
turnOn(document.getElementsByName("isMaker"));
document.getElementById("endGameText").innerText="Would you like to play again?";
reset();
conn.send(playerNames);
}
}
}else{
BreakerClick(data);
}
});
});
}
//This function runs if the user indicates that they are the Breaker. It
//turns on the Breaker board in the HTML, and opens a channel for Breaker to
//send data to Maker.
document.getElementById("breakerId").onclick=function (){
gameId=document.getElementById("gameid").value;
who="breaker";
turnOff(document.getElementsByName("initial"));
turnOff(document.getElementsByName("warning"));
turnOn(document.getElementsByName("pickCategories"));
peer = new Peer("IAmBreaker"+gameId);
BtoM=peer.connect("IAmMaker"+gameId);
BtoM.on('open',function(){
BtoM.on('data',function(data){
if(data=="new"){
NewGame();
}else{
playerNames=data;
makeBreaker();
setColors();
}
});
});
addCategories();
document.getElementById("submitCategories").onclick=function(){
wordData=basic;
options = document.getElementsByName("category");
for(var i=0;i<options.length;i++){
if(options[i].checked){
wordData = wordData.concat(categories[Object.keys(categories)[i]]);
}
}
turnOff(document.getElementsByName("pickCategories"));
BtoM.send(["hello"]);
}
}