forked from iteratec/traze-javascript-vanilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
215 lines (215 loc) · 8.87 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Traze vanilla JS</title>
<style>
body {
background:#010302;
font-family: monospace;
padding: 20px;
color: #eee;
display: flex;
align-items: flex-start;
justify-content: center;
}
#grid {
box-shadow: 0px 0px 30px 3px rgba(68,206,243,1);
}
#grid > div {
display: flex;
}
#grid > div > span {
background: currentColor;
display: block;
margin: 2px;
width: 6px;
height: 6px;
}
#status, #controls {
margin: 0 0 40px 30px;
display: flex;
align-items: center;
flex-direction: column;
}
table {
margin-left: 20px;
}
table thead {
font-weight: bold;
}
table th, table td {
padding: 2px 10px;
text-align: left;
}
</style>
</head>
<body>
<div id="grid"></div>
<div id="info">
<div id="status"></div>
<div id="controls">
<div><button id="steer-up" class="steer" disabled>▲ Up</button></div>
<div><button id="steer-left" class="steer" disabled>◀ Left</button><button id="steer-right" class="steer" disabled>Right ▶</button></div>
<div><button id="steer-down" class="steer" disabled>▼ Down</button></div>
<span>Or use your arrow keys to steer</span>
</div>
<table id="players">
<thead><tr><th>Player</th><th>Frags</th><th>Owned</th></tr></thead>
<tbody></tbody>
</table>
</div>
<script src="./mqtt.min.js"></script>
<script>
const url = 'wss://traze.iteratec.de:9443';
const clientName = makeId();
const instance = "1";
const subscriptions = {
['traze/' + instance + '/player/' + clientName]: onJoinSuccess,
['traze/' + instance + '/ticker']: onTicker,
['traze/' + instance + '/grid']: onGrid,
['traze/' + instance + '/players']: onPlayers
};
let playerId = 0;
let secretToken = "";
let playerColors = { 0: 'black' };
let client = null;
let gridModel = [];
function connect () {
client = mqtt.connect(url, {clientId: clientName});
client.on('connect', () => {
setStatus("Connected.");
join();
});
client.on('error', (error) => {
setStatus("Error: " + error);
});
client.on('message', function (topic, message) {
message = JSON.parse(message);
if (subscriptions[topic]) {
subscriptions[topic](message);
}
});
Object.keys(subscriptions).forEach(topic => {
client.subscribe(topic);
})
}
function join() {
const joinMsg = {
"name": "Master Control",
"mqttClientName": clientName
};
client.publish('traze/' + instance + "/join", JSON.stringify(joinMsg));
}
function onJoinSuccess(message) {
setStatus("Joined as: " + message.name, playerColors[message.id]);
playerId = message.id;
secretToken = message.secretUserToken;
}
function onTicker(message) {
if ((message.type == "frag" || message.type == "suicide") && message.casualty == playerId) {
setStatus('died');
setTimeout(() => {
join();
}, 3000);
}
}
function onGrid(message) {
const newGrid = transformGrid(message).map(row => row.map(cell => playerColors[cell] || "white"));
message.spawns.forEach(pos => {
newGrid[transformY(pos[1], message.height)][pos[0]] = "white";
})
renderGrid(newGrid);
}
function renderGrid(grid) {
const gridElement = document.getElementById("grid");
grid.forEach((row, rowIndex) => {
gridModel[rowIndex] = gridModel[rowIndex] || [];;
const rowElement = getChildOrCreate(gridElement, rowIndex, "div")
row.forEach((cell, columnIndex) => {
const cellElement = getChildOrCreate(rowElement, columnIndex, "span");
if (gridModel[rowIndex][columnIndex] != cell) {
cellElement.style.color = cell;
gridModel[rowIndex][columnIndex] = cell;
}
})
});
}
function getChildOrCreate(parent, index, type) {
let child = parent.childNodes[index];
if (!child) {
child = document.createElement(type);
parent.appendChild(child);
}
return child;
}
function transformY(y, height) {
return height -y -1;
}
function transformGrid(message) {
const grid = new Array(message.height);
for (let x = 0; x < message.width; x++) {
for (let y = 0; y < message.height; y++) {
const targetY = transformY(y, message.height);
grid[targetY] = grid[targetY] || new Array(grid.width);
grid[targetY][x] = message.tiles[x][y];
}
}
return grid;
}
function onPlayers(message) {
let tableHtml = "";
message.forEach(player => {
playerColors[player.id] = player.color;
tableHtml += "<tr><td style='color:" + player.color +"'>" + player.name + "</td>"
+ "<td>" + player.frags + "</td>"
+ "<td>" + player.owned + "</td></tr>";
});
document.querySelector("#players tbody").innerHTML = tableHtml;
}
function setStatus(status, color) {
const statusElement = document.querySelector("#status");
statusElement.innerHTML = status;
statusElement.style.color = color || "white";
}
function makeId() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 15; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function registerEvents() {
document.querySelector("#steer-up").addEventListener('click', () => steer('N'));
document.querySelector("#steer-down").addEventListener('click', () => steer('S'));
document.querySelector("#steer-left").addEventListener('click', () => steer('W'));
document.querySelector("#steer-right").addEventListener('click', () => steer('E'));
document.querySelectorAll('.steer').forEach(element => {
element.disabled = false;
});
document.addEventListener('keydown', event => {
event = event || window.event;
if (event.keyCode == '38') {
steer('N');
} else if (event.keyCode == '40') {
steer('S');
} else if (event.keyCode == '37') {
steer('W');
} else if (event.keyCode == '39') {
steer('E');
}
});
}
function steer(course) {
if (!secretToken || !playerId || !client) {
return;
}
const steerMsg = { course: course, playerToken: secretToken };
client.publish('traze/' + instance + '/' + playerId + '/steer', JSON.stringify(steerMsg));
}
connect();
window.addEventListener('load', () => registerEvents());
</script>
</body>
</html>