-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.jade
More file actions
103 lines (92 loc) · 3.17 KB
/
Copy pathindex.jade
File metadata and controls
103 lines (92 loc) · 3.17 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
doctype 5
html
head
title Coding Contest - Pong
style(type='text/css')
#myCanvas {
border: 1px solid black;
background: red
}
script(type='text/javascript')
(function() {
var canvas;
var context;
var x, y;
var paddleLeft, paddleRight;
var radius = 10;
var config;
function getHash() {
var hash = window.location.hash;
return hash.substring(1); // remove #
}
function getBaseUrl() {
return getHash().split('&')[0] || 'http://localhost:#{port}/';
}
function getGameName() {
return getHash().split('&')[1] || '#{game}';
}
function circle(x, y, radius, color) {
context.beginPath();
context.arc(x, y, radius, 0, 2 * Math.PI, false);
context.fillStyle = color;
context.fill();
}
function rectangle(x, y, width, height, color) {
context.beginPath();
context.rect(x, y, width, height);
context.fillStyle = color;
context.fill();
}
function updateField(err, status)
{
if (status) {
radius = config.BALL_RADIUS;
circle(x, y, radius+1, 'white');
rectangle(0, paddleLeft-config.PADDLE_HEIGHT/2,
config.PADDLE_WIDTH, config.PADDLE_HEIGHT, 'white');
rectangle(config.FIELD_WIDTH, paddleRight-config.PADDLE_HEIGHT/2,
config.PADDLE_WIDTH, config.PADDLE_HEIGHT, 'white');
x = status.ball[0];
y = status.ball[1];
paddleLeft = status.paddleLeft;
paddleRight = status.paddleRight;
circle(x, y, radius, 'green');
rectangle(0, paddleLeft-config.PADDLE_HEIGHT/2,
config.PADDLE_WIDTH, config.PADDLE_HEIGHT, 'red');
rectangle(config.FIELD_WIDTH-config.PADDLE_WIDTH,
paddleRight-config.PADDLE_HEIGHT/2,
config.PADDLE_WIDTH, config.PADDLE_HEIGHT, 'blue');
}
setTimeout(function() {
requestStatus(updateField);
}, 100);
}
function requestConfig(configDone) {
request('game/' + getGameName() + '/config', configDone);
}
function requestStatus(statusDone) {
request('game/' + getGameName() + '/status', statusDone);
}
function request(url, requestDone) {
var request = new XMLHttpRequest();
request.open('GET', getBaseUrl() + url, true); ;
request.setRequestHeader ("Accept", "application/json");
request.onreadystatechange = function () { ;
if (request.readyState != 4 || request.status != 200) {
return;
}
requestDone(null, JSON.parse(request.responseText));
};
request.send();
}
window.onload = function(){
canvas = document.getElementById("field");
context = canvas.getContext("2d");
requestConfig(function(err, newConfig) {
config = newConfig;
updateField();
});
};
}());
body
canvas#field(width=800, height=600)