-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBattleJsLibrary.js
More file actions
140 lines (114 loc) · 3.6 KB
/
CodeBattleJsLibrary.js
File metadata and controls
140 lines (114 loc) · 3.6 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
var BombermanBlocks =
{
Unknown: 0,
Bomberman: '☺',
BombBomberman: '☻',
DeadBomberman: 'Ѡ',
OtherBomberman: '♥',
OtherBombBomberman: '♠',
OtherDeadBomberman: '♣',
BombTimer5: '5',
BombTimer4: '4',
BombTimer3: '3',
BombTimer2: '2',
BombTimer1: '1',
Boom: '҉',
Wall: '☼',
WallDestroyable: '#',
DestroyedWall: 'H',
MeatChopper: '&',
DeadMeatChopper: 'x',
Space: ' '
};
var BombAction =
{
None: 0,
BeforeTurn: 1,
AfterTurn: 2
};
class GameClient {
constructor(server, user, code) {
this.path = "ws://" + server + "/codenjoy-contest/ws?user=" + user + "&code=" + code
}
run(callback) {
this.socket = new WebSocket(this.path);
this.socket.onopen = this.onOpen;
this.socket.onerror = this.onError;
this.socket.onclose = this.onClose;
this.socket.onmessage = function (event) {
var data = event.data.substring(6);
this.size = Math.sqrt(data.length);
var currentChar = 0;
this.map = [];
for (var j = 0; j < this.size; j++) {
this.map[j] = [];
for (var i = 0; i < this.size; i++) {
for (var key in BombermanBlocks) {
if (data[currentChar] == BombermanBlocks[key]) {
this.map[j][i] = BombermanBlocks[key];
if (this.map[j][i] == BombermanBlocks.Bomberman ||
this.map[j][i] == BombermanBlocks.BombBomberman ||
this.map[j][i] == BombermanBlocks.DeadBomberman) {
this.playerX = i;
this.playerY = j;
}
}
}
currentChar++;
}
}
callback();
}
}
get size() {
return this.socket.size;
}
get map() {
return this.socket.map;
}
get playerX() {
return this.socket.playerX;
}
get playerY() {
return this.socket.playerY;
}
set textArea(text) {
this.text = text
}
onOpen() {
text.value += "Connection established\n";
}
onClose(event) {
if (event.wasClean) {
text.value += "### disconnected ###\n"
} else {
text.value += "### accidentally disconnected ###\n";
text.value += " - Err code: " + event.code + ", Reason: " + event.reason + "\n";
}
}
onError(error) {
text.value += "### error ###\n" + error.message + "\n";
}
send(msg) {
text.value += "Sending: " + msg + '\n'
this.socket.send(msg)
}
up(action = BombAction.None) {
this.send((action == BombAction.BeforeTurn ? "ACT," : "") + "UP" + (action == BombAction.AfterTurn ? ",ACT" : ""));
}
down(action = BombAction.None) {
this.send((action == BombAction.BeforeTurn ? "ACT," : "") + "DOWN" + (action == BombAction.AfterTurn ? ",ACT" : ""));
}
right(action = BombAction.None) {
this.send((action == BombAction.BeforeTurn ? "ACT," : "") + "RIGHT" + (action == BombAction.AfterTurn ? ",ACT" : ""));
}
left(action = BombAction.None) {
this.send((action == BombAction.BeforeTurn ? "ACT," : "") + "LEFT" + (action == BombAction.AfterTurn ? ",ACT" : ""));
}
act() {
this.send("ACT");
}
blank() {
this.send("");
}
}