forked from scratchfoundation/scratch-vm
/
index.js
479 lines (439 loc) · 18.7 KB
/
index.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');
const Cast = require('../../util/cast');
const log = require('../../util/log');
const net = require('net');
/**
* Icon svg to be displayed at the left edge of each extension block, encoded as a data URI.
* @type {string}
*/
// eslint-disable-next-line max-len
const blockIconURI = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHdpZHRoPSI0MCIgaGVpZ2h0PSI0MCIgdmlld0JveD0iMCAwIDQwIDQwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48ZyBpZD0iSUQwLjA4NjgyNDQzOTAwMDMzODMyIiB0cmFuc2Zvcm09Im1hdHJpeCgwLjQ5MTU0NjY2MDY2MTY5NzQsIDAsIDAsIDAuNDkxNTQ2NjYwNjYxNjk3NCwgLTY0LjUsIC03Ny4yNSkiPjxwYXRoIGlkPSJJRDAuNTcyMTQ2MjMwMzc3MjU2OSIgZmlsbD0iI0ZGOTQwMCIgc3Ryb2tlPSJub25lIiBkPSJNIDE4OCAxNDEgTCAyNTAgMTQxIEwgMjUwIDIwMyBMIDE4OCAyMDMgTCAxODggMTQxIFogIiB0cmFuc2Zvcm09Im1hdHJpeCgxLjI4NzkwMzMwODg2ODQwODIsIDAsIDAsIDEuMjg3OTAzMzA4ODY4NDA4MiwgLTExMC45LCAtMjQuNCkiLz48cGF0aCBpZD0iSUQwLjYzODMzNjEzNTA3NDQ5NjMiIGZpbGw9Im5vbmUiIHN0cm9rZT0iI0ZGRkZGRiIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIGQ9Ik0gMTk2IDIwNCBDIDE5NiAyMDQgMTkyLjcwNiAxOTAuMDU4IDE5MyAxODMgQyAxOTMuMDc0IDE4MS4yMzYgMTk1Ljg4NiAxNzguNDU4IDE5NyAxODAgQyAyMDEuNDU1IDE4Ni4xNjggMjAzLjQ0MyAyMDMuN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41MDAgQyAxNzUgMjAwLjUwMCAxNjkuODA1IDIyMS45MTMgMTcxIDIyMi43NTAgQyAxNzIuMTk1IDIyMy41ODcgMTc4Ljc5NSAyMDUuMjk1IDE4Mi41MDAgMjA1Ljc1MCBDIDE4NS45MjAgMjA2LjE3MCAxODEuODU5IDIyNC41MDAgMTg1LjI1MCAyMjQuNTAwIEMgMTg5LjIxMyAyMjQuNTAwIDE5Ny4yNTAgMjA1Ljc1MCAxOTcuMjUwIDIwNS43NTAgIi8+PC9nPjwvc3ZnPg==';
/**
* Icon svg to be displayed in the category menu, encoded as a data URI.
* @type {string}
*/
// eslint-disable-next-line max-len
const menuIconURI = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHdpZHRoPSI0MCIgaGVpZ2h0PSI0MCIgdmlld0JveD0iMCAwIDQwIDQwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48ZyBpZD0iSUQwLjA4NjgyNDQzOTAwMDMzODMyIiB0cmFuc2Zvcm09Im1hdHJpeCgwLjQ5MTU0NjY2MDY2MTY5NzQsIDAsIDAsIDAuNDkxNTQ2NjYwNjYxNjk3NCwgLTY0LjUsIC03Ny4yNSkiPjxwYXRoIGlkPSJJRDAuNTcyMTQ2MjMwMzc3MjU2OSIgZmlsbD0iI0ZGOTQwMCIgc3Ryb2tlPSJub25lIiBkPSJNIDE4OCAxNDEgTCAyNTAgMTQxIEwgMjUwIDIwMyBMIDE4OCAyMDMgTCAxODggMTQxIFogIiB0cmFuc2Zvcm09Im1hdHJpeCgxLjI4NzkwMzMwODg2ODQwODIsIDAsIDAsIDEuMjg3OTAzMzA4ODY4NDA4MiwgLTExMC45LCAtMjQuNCkiLz48cGF0aCBpZD0iSUQwLjYzODMzNjEzNTA3NDQ5NjMiIGZpbGw9Im5vbmUiIHN0cm9rZT0iI0ZGRkZGRiI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4NyAiIHRyYW5zZm9ybT0ibWF0cml4KDEsIDAsIDAsIDEsIC01NywgMTUuOCkiLz48cGF0aCBpZD0iSUQwLjc5MzkzOTQ4MTk1NTAyMTYiIGZpbGw9Im5vbmUiIHN0cm9rZT0iI0ZGRkZGRiIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIGQ9Ik0gMTc1IDIwMC41MDAgQyAxNzUgMjAwLjUwMCAxNjkuODA1IDIyMS45MTMgMTcxIDIyMi43NTAgQyAxNzIuMTk1IDIyMy41ODcgMTc4Ljc5NSAyMDUuMjk1IDE4Mi41MDAgMjA1Ljc1MCBDIDE4NS45MjAgMjA2LjE3MCAxODEuODU5IDIyNC41MDAgMTg1LjI1MCAyMjQuNTAwIEMgMTg5LjIxMyAyMjQuNTAwIDE5Ny4yNTAgMjA1Ljc1MCAxOTcuMjUwIDIwNS43NTAgIi8+PC9nPjwvc3ZnPg==';
/**
* Direction
* @type {number}
*/
const CHaserDirection = {
UP: 1,
DOWN: 7,
LEFT: 3,
RIGHT: 5
}
/**
* Cell Type
* @type {number}
*/
const CHaserCellType = {
NONE: 0,
ENEMY: 1,
BLOCK: 2,
ITEM: 3
}
/**
* Class for CHaser Client
*/
class CHaserClient {
constructor () {
this.response = "";
this.values = [];
this.websocketPromises = [];
this._isGameSet = false;
// this をCHaserClientのインスタンスにbind
this.newSocketCallback = this.newSocketCallback.bind(this);
this.initCallback = this.initCallback.bind(this);
this.sendCallback = this.sendCallback.bind(this);
this.messageCallback = this.messageCallback.bind(this);
}
// https://qiita.com/Zumwalt/items/060ae7654c9dfe538ee7
connect(host, port, name) {
//. 接続
const p = new Promise(this.newSocketCallback)
p.then((event) => {
this.client = event.target;
console.log( '接続: ' + host + ':' + port + ':' + name);
console.log(event);
this.client.send(['connect', host, port, name].join(" "));
//. 接続が切断されたら、その旨をメッセージで表示する
this.client.onclose = function(){
console.log('切断');
}
}).catch((error) => {
console.log('onerror: ' + error);
});
return p;
}
getReady() {
const p = new Promise((resolve) => {
this.websocketPromises.push(resolve);
this.client.send("gr");
this.client.onmessage = this.messageCallback;
})
.then(this.sendCallback);
const values = this.response.split("")
this.values = values.slice(1);
return p;
}
getValues() {
return this.values;
}
_send(command) {
log.log(command);
const p = new Promise(this.initCallback)
.then(this.sendCallback);
return p;
}
newSocketCallback(resolve, reject) {
this.client = new WebSocket("ws://localhost:8080/ws");
this.client.addEventListener('open', resolve);
this.client.addEventListener('error', reject);
}
initCallback(resolve) {
this.websocketPromises.push(resolve);
this.client.send(command);
this.client.onmessage = this.messageCallback;
}
messageCallback(event) {
console.log('messageCallback: ' + event);
if (event.data[0] == '0') {
this.client.close();
this._isGameSet = true;
}
this.response = event.data;
const values = this.response.split("")
this.values = values.slice(1);
for(i = 0; i < this.websocketPromises.length; i++) {
resolve = this.websocketPromises[i];
resolve();
}
this.websocketPromises = [];
}
sendCallback() {
console.log('sendCallback: ' + this.response);
}
walk(direction) {
command = direction == 1 ? 'wu'
: direction == 3 ? 'wl'
: direction == 5 ? 'wr'
: direction == 7 ? 'wd'
: 'error';
return this._send(command);
}
put(direction) {
command = direction == 1 ? 'pu'
: direction == 3 ? 'pl'
: direction == 5 ? 'pr'
: direction == 7 ? 'pd'
: 'error';
return this._send(command);
}
isGameSet() {
return this._isGameSet;
}
}
/**
* Class for the new blocks in Scratch 3.0
* @param {Runtime} runtime - the runtime instantiating this block package.
* @constructor
*/
class Scratch3CHaser {
constructor (runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
//this._onTargetCreated = this._onTargetCreated.bind(this);
//this.runtime.on('targetWasCreated', this._onTargetCreated);
}
/**
* @return {array} - text and values for each cells types
*/
get CELL_TYPE_MENU () {
return [
{ text: 'なし', value: 0 },
{ text: '相手', value: 1 },
{ text: 'ブロック', value: 2 },
{ text: 'アイテム', value: 3 }
]
}
/**
* @return {array} - text and values for each cells element
*/
get DIRECTION_MENU8 () {
return [
{ value: 0, text: '左上' },
{ value: 1, text: '上' },
{ value: 2, text: '右上' },
{ value: 3, text: '左' },
{ value: 5, text: '右', },
{ value: 6, text: '左下' },
{ value: 7, text: '下' },
{ value: 8, text: '右下' }
]
}
/**
* @return {array} - text and values for each walk action menu element
*/
get DIRECTION_MENU4 () {
return [
{ value: 1, text: '上' },
{ value: 3, text: '左' },
{ value: 5, text: '右' },
{ value: 7, text: '下' }
]
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo () {
return {
id: 'chaser',
name: 'CHaser',
// menuIconURI: menuIconURI,
// blockIconURI: blockIconURI,
blocks: [
{
opcode: 'connect',
blockType: BlockType.COMMAND,
text: 'サーバー [HOST] に [PORT] に [NAME] という名前で接続する',
arguments: {
HOST: {
type: ArgumentType.STRING,
defaultValue: '127.0.0.1'
},
PORT: {
type: ArgumentType.NUMBER,
menu: 'port',
defaultValue: 2009
},
NAME: {
type: ArgumentType.STRING,
defaultValue: 'ユーザー1'
}
}
},
{
opcode: 'isGameSet',
blockType: BlockType.BOOLEAN,
text: 'ゲーム終了?'
},
{
opcode: 'getReady',
blockType: BlockType.COMMAND,
text: '準備する'
},
{
opcode: 'isValueEqual',
blockType: BlockType.BOOLEAN,
text: '[DIRECTION] のマスが [CELL_TYPE]?',
arguments: {
DIRECTION: {
type: ArgumentType.NUMBER,
menu: 'direction8',
defaultValue: CHaserDirection.UP
},
CELL_TYPE: {
type: ArgumentType.NUMBER,
menu: 'cellType',
defaultValue: CHaserCellType.BLOCK
}
}
},
{
opcode: 'walk',
blockType: BlockType.COMMAND,
text: '[DIRECTION] に歩く',
arguments: {
DIRECTION: {
type: ArgumentType.NUMBER,
menu: 'direction4',
defaultValue: CHaserDirection.UP
}
}
},
{
opcode: 'put',
blockType: BlockType.COMMAND,
text: '[DIRECTION] にブロックを置く',
arguments: {
DIRECTION: {
type: ArgumentType.NUMBER,
menu: 'direction4',
defaultValue: CHaserDirection.UP
}
}
},
// {
// opcode: 'search',
// blockType: BlockType.COMMAND,
// text: '[DIRECTION] の直線方向を調べる',
// arguments: {
// DIRECTION: {
// type: ArgumentType.NUMBER,
// menu: 'direction4',
// defaultValue: CHaserDirection.UP
// }
// }
// },
// {
// opcode: 'look',
// blockType: BlockType.COMMAND,
// text: '[DIRECTION] の正方形範囲を調べる',
// arguments: {
// DIRECTION: {
// type: ArgumentType.NUMBER,
// menu: 'direction4',
// defaultValue: CHaserDirection.UP
// }
// }
// },
{
opcode: 'getGameSet',
blockType: BlockType.REPORTER,
text: 'ゲーム終了'
},
{
opcode: 'getValues',
blockType: BlockType.REPORTER,
text: 'ボット周辺のセル'
}
],
menus: {
port: {
acceptReporters: false,
items: [
{text:'Cool', value: 2009},
{text:'Hot', value: 2010}
]
},
cellType: {
acceptReporters: false,
items: this.CELL_TYPE_MENU
},
direction8: {
acceptReporters: false,
items: this.DIRECTION_MENU8
},
direction4: {
acceptReporters: false,
items: this.DIRECTION_MENU4
}
}
};
}
/**
* Connect to CHaser server.
* CHaserサーバーへのconnectが終わるまでブロックの進行を待ちたいためPromiseオブジェクトを返す
* @param {object} args - the arguments.
* @property {string} HOST - hostname or ip address for CHaser server.
* @property {number} PORT - the port number.
* @property {string} NAME - username.
* @return {Promise} A promise that will resolve when getReady is complete.
*/
connect (args) {
const host = Cast.toString(args.HOST);
const port = Cast.toNumber(args.PORT);
const name = Cast.toString(args.NAME);
log.log(host, port, name);
this.client = new CHaserClient()
return this.client.connect(host, port, name);
}
/**
* Do getReady.
* getReady()が終わるまでブロックの進行を待ちたいためPromiseオブジェクトを返す
* @return {Promise} A promise that will resolve when getReady is complete.
*/
getReady () {
const promise = this.client.getReady();
promise.then(() => {
this.values = this.client.getValues();
});
return promise;
}
/**
* Return True if the game is set.
* @return {boolean}
*/
isGameSet () {
return this.client.isGameSet();
}
/**
* Return True if a value around the bot equal to specified cell type.
* @param {object} args - the arguments.
* @property {number} DIRECTION - the direction.
* @property {number} CELL_TYPE - the cell type.
* @return {boolean}
*/
isValueEqual (args) {
const direction = Cast.toNumber(args.DIRECTION);
const cell_type = Cast.toNumber(args.CELL_TYPE);
if (typeof(this.values) == "undefined") {
return false;
}
const theCell = parseInt(this.values[direction]);
log.log(this.values, direction, theCell, cell_type);
log.log(theCell === cell_type);
return theCell === cell_type;
}
/**
* Walk
* 行動が終わるまでブロックの進行を待ちたいためPromiseオブジェクトを返す
* @param {object} args - the arguments.
* @property {number} DIRECTION - the direction.
* @return {Promise} A promise that will resolve when Walk is complete.
*/
walk (args) {
const direction = Cast.toNumber(args.DIRECTION);
const promise = this.client.walk(direction);
promise.then(() => {
this.values = this.client.getValues();
});
return promise;
}
/**
* Put
* 行動が終わるまでブロックの進行を待ちたいためPromiseオブジェクトを返す
* @param {object} args - the arguments.
* @property {number} DIRECTION - the direction.
* @return {Promise} A promise that will resolve when Put is complete.
*/
put (args) {
const direction = Cast.toNumber(args.DIRECTION);
const promise = this.client.put(direction);
promise.then(() => {
this.values = this.client.getValues();
});
return promise;
}
/**
* Get a GameSet variable.
* @return {boolean} - true if the game is set
*/
getGameSet () {
return this.client.isGameSet();
}
/**
* Get values arround the bot.
* @return {array} - cell values arroud the bot.
*/
getValues () {
if (typeof(this.values) == "undefined") {
return ""
}
return this.values.join(",");
}
/**
* Get the browser.
* @return {number} - the user agent.
*/
getBrowser () {
return navigator.userAgent;
}
}
module.exports = Scratch3CHaser;