-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_ws.js
358 lines (340 loc) · 8.5 KB
/
game_ws.js
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* This Codebase is a mess :)
* Most of the mess is from Alexander 'Pfannkuchensack' Eichhorn
* Thanks for the great npm packages: tetris-engine, gamepad, serialport, cli-color
* In use Arduino Uno + 384 WS2812B. Communication wokrs over Serial. RPi/Laptop -> usb -> Uno
*/
let Engine = require('tetris-engine').Engine;
let gamepad = require("gamepad");
const WebSocket = require('ws');
const ws = new WebSocket('ws://10.0.40.210:81');
const debug = true; // Want Timings for Debug?
const co = false; // Want Console Tetris output ?
// Game Field x y
const areaHeight = 16;
const areaWidth = 24;
gamepad.init()
// Color for Console
if(co) { let clc = require("cli-color"); }
// https://www.npmjs.com/package/gamepad
// Basic code for running. Need Update for better handling
// Create a game loop and poll for events
setInterval(gamepad.processEvents, 16);
// Scan for new gamepads as a slower rate
setInterval(gamepad.detectDevices, 500);
// All commands work then the Buttons is pressed down ones.
// so if you want go fast down, press many times. @todo
// Keys Values for USB Classic Nintendo Controller, need change for another Controller maybe
var movedownint = false;
var moveleftint = false;
var moverightint = false;
var gamelevel = false;
var isPaused = false;
// Level 1: 1 Sec
var firstLevelInterval = 1000;
var heapcounter = 0;
// Listen for move events on all gamepads
gamepad.on("move", function (id, axis, value) {
/*console.log("move", {
id: id,
axis: axis,
value: value,
});*/
if(value != 1 && value != -1)
{
clearInterval(movedownint);
clearInterval(moverightint);
clearInterval(moveleftint);
}
else if(axis == 1 && value == 1)
{
game.moveDown();
movedownint = setInterval(function() { game.moveDown(); }, 100);
}
else if(axis == 0 && value == -1)
{
game.moveLeft();
moveleftint = setInterval(function() { game.moveLeft(); }, 100);
}
else if(axis == 0 && value == 1)
{
game.moveRight();
moverightint = setInterval(function() { game.moveRight(); }, 100);
}
/*else if(axis == 1 && value == -1)
{
game.moveUp()
}*/
/*if(axis == 1 && value == 1)
game.moveDown();
else if(axis == 0 && value == -1)
game.moveLeft();
else if(axis == 0 && value == 1)
game.moveRight();
*/
});
// Listen for button up events on all gamepads
gamepad.on("up", function (id, num) {
/*console.log("up", {
id: id,
num: num,
});
*/
});
// Listen for button down events on all gamepads
gamepad.on("down", function (id, num) {
if(num == 1)
game.rotate();
else if(num == 0)
game.rotateBack();
else if(num == 9)
{
if(isPaused)
{
game.start();
isPaused = false;
}
else
{
game.pause();
isPaused = true;
}
}
else if(num == 8)
{
setTimeout(function () {
process.on("exit", function () {
require("child_process").spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached : true,
stdio: "inherit"
});
});
process.exit();
}, 5000);
}
});
function done() {}
// Change is ready?
let change = false;
let wsready = false;
// https://github.com/petelinmn/tetris-engine
let renderFunc = (gameState) => {
if(debug) {console.time("gameState");}
// Jump over this State cause Device is not ready!
if(change)
return;
change = true;
if(co) { var colpaint = ''; }
let buf = [];
let tmp = [];
let k = 0;
let hc = 0;
gameState.body.forEach(function(row,x) {
row.forEach(function(col,y) {
// Terminal
if(co)
{
if(col.val == 1)
colpaint = colpaint + '' + color_for_terminal(get_color_to_shape(col.cssClasses[2]), 'X');
else if(col.val == 2)
colpaint = colpaint + '' + color_for_terminal(get_color_to_shape(col.cssClasses[3]), 'X');
else
colpaint = colpaint + '' + ' ';
}
// Game
if(col.val == 1)
{
let lnr = matrix_mapping(x,y);
tmp[lnr] = get_color_to_shape(col.cssClasses[2]);
}
else if(col.val == 2)
{
let lnr = matrix_mapping(x,y);
tmp[lnr] = get_color_to_shape(col.cssClasses[3]);
hc++;
}
else
{
let lnr = matrix_mapping(x,y);
tmp[lnr] = [0,0,0];
}
});
if(co) { console.log(colpaint); }
if(co) { colpaint = ''; }
});
tmp.forEach(function(werte, nr) {
buf[k] = werte[0];
buf[k+1] = werte[1];
buf[k+2] = werte[2];
k = k + 3;
});
// Block to Heap
if(hc > heapcounter)
{
clearInterval(movedownint);
clearInterval(moverightint);
clearInterval(moveleftint);
}
console.log(buf.length);
//writeAndDrain(Buffer.from(buf), changer);
// WS Testen
if(wsready)
ws.send(Buffer.from(buf))
changer();
heapcounter = hc;
/*if(gameState.statistic.countLinesReduced != 0 && gameState.statistic.countLinesReduced % 2 == 0)
{
console.log(gameState.statistic);
let mi = gameState.statistic.countLinesReduced / 2;
clearInterval(gamelevel);
gamelevel = setInterval(function() { game.moveDown(); }, 1000 - (mi * 100));
}*/
if(debug) {console.timeEnd("gameState");}
};
function changer()
{
change = false;
}
// Color Mapping for the LED // Orange dont work really well :(
function get_color_to_shape(shape)
{
//return [155,155,155]; // Alles eine Farbe
switch(shape) {
case 'IShape':
return [0,0,255];
case 'ZShape':
return [255,0,0];
case 'LShape':
return [0,255,0];
case 'TShape':
return [255,255,0];
case 'OShape':
return [255,0,255];
case 'SShape':
return [0,255,255];
case 'JShape':
return [255,255,255];
default:
return [155,155,155];
}
}
// @todo: Terminal Color dont work cause of change from one Value of a Color to three
// Terminal Color Thema
function color_for_terminal(color, val)
{
return val; //clc.green(val);
switch(color) {
case 0:
return clc.blue(val);
case 1:
return clc.red(val);
case 2:
return clc.green(val);
case 3:
return clc.yellow(val);
case 4:
return clc.magenta(val);
case 5:
return clc.cyan(val);
case 6:
return clc.redBright(val);
}
}
// Mapping your LED Matrix to x and y of the GameField
function matrix_mapping(x, y)
{
let matrix_array = [
[0,47,48,95,96,143,144,191,192,239,240,287,288,335,336,383],
[1,46,49,94,97,142,145,190,193,238,241,286,289,334,337,382],
[2,45,50,93,98,141,146,189,194,237,242,285,290,333,338,381],
[3,44,51,92,99,140,147,188,195,236,243,284,291,332,339,380],
[4,43,52,91,100,139,148,187,196,235,244,283,292,331,340,379],
[5,42,53,90,101,138,149,186,197,234,245,282,293,330,341,378],
[6,41,54,89,102,137,150,185,198,233,246,281,294,329,342,377],
[7,40,55,88,103,136,151,184,199,232,247,280,295,328,343,376],
[8,39,56,87,104,135,152,183,200,231,248,279,296,327,344,375],
[9,38,57,86,105,134,153,182,201,230,249,278,297,326,345,374],
[10,37,58,85,106,133,154,181,202,229,250,277,298,325,346,373],
[11,36,59,84,107,132,155,180,203,228,251,276,299,324,347,372],
[12,35,60,83,108,131,156,179,204,227,252,275,300,323,348,371],
[13,34,61,82,109,130,157,178,205,226,253,274,301,322,349,370],
[14,33,62,81,110,129,158,177,206,225,254,273,302,321,350,369],
[15,32,63,80,111,128,159,176,207,224,255,272,303,320,351,368],
[16,31,64,79,112,127,160,175,208,223,256,271,304,319,352,367],
[17,30,65,78,113,126,161,174,209,222,257,270,305,318,353,366],
[18,29,66,77,114,125,162,173,210,221,258,269,306,317,354,365],
[19,28,67,76,115,124,163,172,211,220,259,268,307,316,355,364],
[20,27,68,75,116,123,164,171,212,219,260,267,308,315,356,363],
[21,26,69,74,117,122,165,170,213,218,261,266,309,314,357,362],
[22,25,70,73,118,121,166,169,214,217,262,265,310,313,358,361],
[23,24,71,72,119,120,167,168,215,216,263,264,311,312,359,360],
];
return matrix_array[x][y];
}
let additionalShapes = {
IShape: [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
SShape: [
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 1],
[0, 0, 0, 1, 1],
],
OShape: [
[0, 0, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
],
TShape: [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
],
JShape: [
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
LShape: [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
],
ZShape: [
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
]
};
// Start the Engine
let game = new Engine(
areaHeight,
areaWidth,
renderFunc,
null,
//additionalShapes
);
ws.on('open', function open() {
wsready = true;
game.start();
setTimeout(() => {
gamelevel = setInterval(function() { game.moveDown(); }, firstLevelInterval);
},50);
});