-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
275 lines (260 loc) · 8.41 KB
/
scripts.js
File metadata and controls
275 lines (260 loc) · 8.41 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
$(document).ready(function() {
var game = {
init: function() {
canvas = document.getElementById("gamecanvas");
context = canvas.getContext("2d");
}
}
var sketch=false;
var timing=0;
var wait;
var socket = io('ws://127.0.0.1:3000');
$(function () {
$('form').submit(function(){
socket.emit('chat message', $('#input').val());
document.getElementById("input").value="";
return false;
});
socket.on('chat message', function(msg){
var json = JSON.parse(msg);
addMessage(json.author, json.text, json.color, new Date(json.time));
});
socket.on('joined', function(msg){
var json = JSON.parse(msg);
addMessage(json.author, json.text, json.color, new Date(json.time));
});
socket.on('left', function(msg){
var json = JSON.parse(msg);
addMessage(json.author, json.text, json.color, new Date(json.time));
});
socket.on('accepted', function(wha){
document.getElementById("block").style.display = "none";
});
socket.on('nick', function(nick){
document.getElementById("nick").innerHTML = "<b>"+nick+"</b>";
});
socket.on('drawing', function(json){
var drawin = JSON.parse(json);
addClickd( drawin.x, drawin.y, drawin.color, drawin.drag, drawin.size);
draw();
});
socket.on('clear', function(json){
clearine();
});
socket.on('master', function(pick){
sketch=true;
document.getElementById("przybornik").style.visibility = "visible";
document.getElementById("info").innerHTML="<b>Pokemon którego masz narysować to "+pick+".</b>";
});
socket.on('new turn', function(master){
clearine();
document.getElementById("przybornik").style.visibility = "hidden";
sketch=false;
document.getElementById("info").innerHTML="<b>Nastepna runda.</b>";
clearInterval(wait);
timing=10;
document.getElementById("timer").innerHTML=timing;
wait = setInterval(() => {
timing--;
document.getElementById("timer").innerHTML=timing;
}, 1000);
});
socket.on('start turn', function(master){
document.getElementById("info").innerHTML="<b>Teraz rysuje "+master+".</b>";
addMessage(true, master, "artist", true);
clearInterval(wait);
timing=60;
document.getElementById("timer").innerHTML=timing;
wait = setInterval(() => {
timing--;
document.getElementById("timer").innerHTML=timing;
}, 1000);
});
socket.on('win', function(winner){
addMessage(true, winner, "winner", true);
});
socket.on('stop', function(winner){
clearInterval(wait);
document.getElementById("timer").innerHTML="-";
document.getElementById("info").innerHTML="Gra przerwana, zbyt mało graczy.";
sketch=false;
document.getElementById("przybornik").style.visibility = "hidden";
});
socket.on('hold', function(winner){
document.getElementById("timer").innerHTML="-";
document.getElementById("info").innerHTML="Zbyt malo graczy aby rozpoczac gre.";
sketch=false;
document.getElementById("przybornik").style.visibility = "hidden";
});
socket.on('history', function(history){
var json = JSON.parse(history);
for (var i=0; i < json.length; i++) {
addMessage(json[i].author, json[i].text,
json[i].color, new Date(json[i].time));
}
});
socket.on('onlist', function(list){
var listt = JSON.parse(list);
console.log("list: "+list);
var nick = listt.name;
var points = listt.points;
document.getElementById("online").innerHTML="";
for(i=1;i<=nick.length;i++){
document.getElementById("online").innerHTML+="<b>"+i+". "+nick[i-1]+" "+points[i-1]
+"</b><br>";
}
});
socket.on('hisdrawing', function(history){
var json = JSON.parse(history);
for (var i=0; i < json.length; i++) {
addClickd(json[i].x, json[i].y,
json[i].color, json[i].drag, json[i].size);
}
//x,y,color,drag,size
redraw();
});
});
function addMessage(author, message, color, dt) {
if(color=="whoolean"){
document.getElementById("content").innerHTML+='<p><span style="color:red"><b>'+message+' dolaczyl do gry!</b></span></p>';
}else if(color=="left"){
document.getElementById("content").innerHTML+='<p><span style="color:red"><b>'+message+' opuscil gre.</b></span></p>';
}else if(color=="winner"){
document.getElementById("content").innerHTML+='<p><span style="color:black"><b>Gracz '+message+' odgadl!</b></span></p>';
}else if(color=="artist"){
document.getElementById("content").innerHTML+='<p><span style="color:green"><b>Teraz rysuje '+message+'.</b></span></p>';
}else{
document.getElementById("content").innerHTML+='<p><span style="color:' + color + '"><b>' + author + '</b></span> @ ' + (dt.getHours() < 10 ? '0'
+ dt.getHours() : dt.getHours()) + ':' + (dt.getMinutes() < 10
? '0' + dt.getMinutes() : dt.getMinutes())+ ': ' + message + '</p>';
}
content.scrollTop+=35;
}
var coloring ;
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var colored = new Array();
var size = new Array();
var paint;
function draw(){
canvas = document.getElementById("gamecanvas");
context = canvas.getContext("2d");
// clear the canvas
context.lineJoin = "round";
//for(var i=0; i < clickX.length; i++) {
context.beginPath();
//..draw all the points
if(clickDrag[clickDrag.length-1]){
context.moveTo(clickX[clickX.length-2], clickY[clickY.length-2]);
}else{
context.moveTo(clickX[clickX.length-1]-1, clickY[clickY.length-1]);
}
context.lineTo(clickX[clickX.length-1], clickY[clickY.length-1]); // where
context.strokeStyle = colored[colored.length-1]; // color
context.lineWidth = size[size.length-1]; // radius
context.closePath(); // close
context.stroke(); // draw
//}
context.strokeStyle = "black"; // isnt working without it dunnno why
}
function redraw(){
canvas = document.getElementById("gamecanvas");
context = canvas.getContext("2d");
// clear the canvas
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.lineJoin = "round";
for(var i=0; i < clickX.length; i++) {
context.beginPath();
//draw all the points
if(clickDrag[i]){
context.moveTo(clickX[i-1], clickY[i-1]);
}else{
context.moveTo(clickX[i]-1, clickY[i]);
}
context.lineTo(clickX[i], clickY[i]); // where
context.strokeStyle = colored[i]; // color
context.lineWidth = size[i]; // radius
context.closePath(); // close
context.stroke(); // draw
}
context.strokeStyle = "black"; // isnt working without it dunnno why
}
function cleard(){
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
}
$("#gamecanvas").mousedown(function(){
// mouse position
if(sketch==false) return;
mousex = event.clientX-this.offsetLeft;
mousey = event.clientY-this.offsetTop;
// send info to the addClick
coloring = document.getElementById("color").value;
size.push(document.getElementById("radius").value)
addClick(mousex, mousey, coloring, false);
draw();
$(this).mousemove(function(){
// mouse position
if(sketch==false) return;
mousex = event.clientX-this.offsetLeft;
mousey = event.clientY-this.offsetTop;
coloring = document.getElementById("color").value;
// send info to the addClick
addClick(mousex, mousey, coloring, true);
draw();
});
}).mouseup(function () {
// stop drawing after click up
$(this).unbind('mousemove');
});
$("#gamecanvas").mouseleave(function(){
// stop drawing after leaving
$("#gamecanvas").unbind('mousemove');
});
$("#clear").click(function(){
socket.emit('clear', true);
clearine();
});
function clearine(){
//clear field
canvas = document.getElementById("gamecanvas");
context = canvas.getContext("2d");
context.clearRect(0, 0, 640, 480);
clickX=[];
clickY=[];
clickDrag=[];
colored=[];
size=[];
}
function addClick(x, y, color, dragging)
{
// add all the points to the arrays
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
colored.push(color);
size.push(document.getElementById("radius").value)
var send = {
x: clickX[clickX.length-1],
y: clickY[clickY.length-1],
drag: clickDrag[clickDrag.length-1],
color: colored[colored.length-1],
size: size[size.length-1]
};
var json = JSON.stringify(send);
socket.emit('draw', json);
}
function addClickd(x, y, color, dragging, sizer)
{
// add all the points to the arrays
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
colored.push(color);
size.push(sizer)
}
$("#color").change(function(){
//color picking
coloring = document.getElementById("color").value;
});
});