Skip to content

Commit 323d821

Browse files
authored
Merge pull request #30 from smalruby/fix-koshien
Fix koshien
2 parents 30de145 + 5808c98 commit 323d821

File tree

2 files changed

+321
-11
lines changed

2 files changed

+321
-11
lines changed

src/extensions/koshien/index.js

Lines changed: 281 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const KoshienItemName = {
3939
* @enum {string}
4040
*/
4141
const KoshienCoordinateName = {
42+
POSITION: 'position',
4243
X: 'x',
4344
Y: 'y'
4445
};
@@ -55,6 +56,30 @@ const KoshienTargetName = {
5556
PLAYER: 'player'
5657
};
5758

59+
/**
60+
* Enum for object
61+
* @readonly
62+
* @enum {string}
63+
*/
64+
const KoshienObjectName = {
65+
UNKNOWN: 'unknown',
66+
SPACE: 'space',
67+
WALL: 'wall',
68+
STOREHOUSE: 'storehouse',
69+
GOAL: 'goal',
70+
WATER: 'water',
71+
BREAKABLE_WALL: 'breakable wall',
72+
TEA: 'tea',
73+
SWEETS: 'sweets',
74+
COIN: 'COIN',
75+
DOLPHIN: 'dolphin',
76+
SWORD: 'sword',
77+
POISON: 'poison',
78+
SNAKE: 'snake',
79+
TRAP: 'trap',
80+
BOMB: 'bomb'
81+
};
82+
5883
/**
5984
* A client of Smalruby Koshien game server.
6085
*/
@@ -97,8 +122,9 @@ class KoshienClient {
97122
return new Promise(resolve => resolve());
98123
}
99124

100-
// eslint-disable-next-line no-unused-vars
101-
calcRoute (src, dst, exceptCells, path) {
125+
calcRoute (props) {
126+
// eslint-disable-next-line no-unused-vars
127+
const {src, dst, exceptCells, result} = props;
102128
return new Promise(resolve => resolve());
103129
}
104130
}
@@ -214,6 +240,152 @@ class KoshienBlocks {
214240
];
215241
}
216242

243+
get COORDINATES_AND_POSITION_MENU () {
244+
return [
245+
{
246+
text: formatMessage({
247+
id: 'koshien.coordinatesMenu.position',
248+
default: 'position',
249+
description: 'label for position in coordinate picker for koshien extension'
250+
}),
251+
value: KoshienCoordinateName.POSITION
252+
}
253+
].concat(this.COORDINATES_MENU);
254+
}
255+
256+
get OBJECTS_MENU () {
257+
return [
258+
{
259+
text: formatMessage({
260+
id: 'koshien.objectsMenu.unknown',
261+
default: 'unknown',
262+
description: 'label for unknown in object picker for koshien extension'
263+
}),
264+
value: KoshienObjectName.UNKNOWN
265+
},
266+
{
267+
text: formatMessage({
268+
id: 'koshien.objectsMenu.space',
269+
default: 'space',
270+
description: 'label for space in object picker for koshien extension'
271+
}),
272+
value: KoshienObjectName.SPACE
273+
},
274+
{
275+
text: formatMessage({
276+
id: 'koshien.objectsMenu.wall',
277+
default: 'wall',
278+
description: 'label for wall in object picker for koshien extension'
279+
}),
280+
value: KoshienObjectName.WALL
281+
},
282+
{
283+
text: formatMessage({
284+
id: 'koshien.objectsMenu.storehouse',
285+
default: 'storehouse',
286+
description: 'label for storehouse in object picker for koshien extension'
287+
}),
288+
value: KoshienObjectName.STOREHOUSE
289+
},
290+
{
291+
text: formatMessage({
292+
id: 'koshien.objectsMenu.goal',
293+
default: 'goal',
294+
description: 'label for goal in object picker for koshien extension'
295+
}),
296+
value: KoshienObjectName.GOAL
297+
},
298+
{
299+
text: formatMessage({
300+
id: 'koshien.objectsMenu.water',
301+
default: 'water',
302+
description: 'label for water in object picker for koshien extension'
303+
}),
304+
value: KoshienObjectName.WATER
305+
},
306+
{
307+
text: formatMessage({
308+
id: 'koshien.objectsMenu.breakableWall',
309+
default: 'breakable wall',
310+
description: 'label for breakable_wall in object picker for koshien extension'
311+
}),
312+
value: KoshienObjectName.BREAKABLE_WALL
313+
},
314+
{
315+
text: formatMessage({
316+
id: 'koshien.objectsMenu.tea',
317+
default: 'tea',
318+
description: 'label for tea in object picker for koshien extension'
319+
}),
320+
value: KoshienObjectName.TEA
321+
},
322+
{
323+
text: formatMessage({
324+
id: 'koshien.objectsMenu.sweets',
325+
default: 'sweets',
326+
description: 'label for sweets in object picker for koshien extension'
327+
}),
328+
value: KoshienObjectName.SWEETS
329+
},
330+
{
331+
text: formatMessage({
332+
id: 'koshien.objectsMenu.coin',
333+
default: 'coin',
334+
description: 'label for coin in object picker for koshien extension'
335+
}),
336+
value: KoshienObjectName.COIN
337+
},
338+
{
339+
text: formatMessage({
340+
id: 'koshien.objectsMenu.dolphin',
341+
default: 'dolphin',
342+
description: 'label for dolphin in object picker for koshien extension'
343+
}),
344+
value: KoshienObjectName.DOLPHIN
345+
},
346+
{
347+
text: formatMessage({
348+
id: 'koshien.objectsMenu.sword',
349+
default: 'sword',
350+
description: 'label for sword in object picker for koshien extension'
351+
}),
352+
value: KoshienObjectName.SWORD
353+
},
354+
{
355+
text: formatMessage({
356+
id: 'koshien.objectsMenu.poison',
357+
default: 'poison',
358+
description: 'label for poison in object picker for koshien extension'
359+
}),
360+
value: KoshienObjectName.POISON
361+
},
362+
{
363+
text: formatMessage({
364+
id: 'koshien.objectsMenu.snake',
365+
default: 'snake',
366+
description: 'label for snake in object picker for koshien extension'
367+
}),
368+
value: KoshienObjectName.SNAKE
369+
},
370+
{
371+
text: formatMessage({
372+
id: 'koshien.objectsMenu.trap',
373+
default: 'trap',
374+
description: 'label for trap in object picker for koshien extension'
375+
}),
376+
value: KoshienObjectName.TRAP
377+
},
378+
{
379+
text: formatMessage({
380+
id: 'koshien.objectsMenu.bomb',
381+
default: 'bomb',
382+
description: 'label for bomb in object picker for koshien extension'
383+
}),
384+
value: KoshienObjectName.BOMB
385+
}
386+
];
387+
}
388+
217389
/**
218390
* Construct a set of Koshien blocks.
219391
* @param {Runtime} runtime - the Scratch 3.0 runtime.
@@ -308,6 +480,23 @@ class KoshienBlocks {
308480
},
309481
filter: [TargetType.SPRITE]
310482
},
483+
{
484+
opcode: 'calcGoalRoute',
485+
blockType: BlockType.COMMAND,
486+
text: formatMessage({
487+
id: 'koshien.calcGoalRoute',
488+
default: 'store shortest goal path to list: [RESULT]',
489+
description: 'store shortest path between player and goal to list'
490+
}),
491+
arguments: {
492+
RESULT: {
493+
type: ArgumentType.STRING,
494+
menu: 'listNames',
495+
defaultValue: ' '
496+
}
497+
},
498+
filter: [TargetType.SPRITE]
499+
},
311500
{
312501
opcode: 'calcRoute',
313502
blockType: BlockType.COMMAND,
@@ -411,7 +600,7 @@ class KoshienBlocks {
411600
},
412601
OBJECTS: {
413602
type: ArgumentType.STRING,
414-
defaultValue: 'A B C D'
603+
defaultValue: 'ABCD'
415604
},
416605
RESULT: {
417606
type: ArgumentType.STRING,
@@ -437,8 +626,8 @@ class KoshienBlocks {
437626
},
438627
COORDINATE: {
439628
type: ArgumentType.STRING,
440-
menu: 'coordinateMenu',
441-
defaultValue: KoshienCoordinateName.X
629+
menu: 'coordinateAndPositionMenu',
630+
defaultValue: KoshienCoordinateName.POSITION
442631
}
443632
},
444633
filter: [TargetType.SPRITE]
@@ -493,6 +682,24 @@ class KoshienBlocks {
493682
defaultValue: KoshienCoordinateName.X
494683
}
495684

685+
},
686+
filter: [TargetType.SPRITE]
687+
},
688+
{
689+
opcode: 'object',
690+
blockType: BlockType.REPORTER,
691+
text: formatMessage({
692+
id: 'koshien.object',
693+
default: '[OBJECT]',
694+
description: 'object of code'
695+
}),
696+
arguments: {
697+
OBJECT: {
698+
type: ArgumentType.STRING,
699+
menu: 'objectMenu',
700+
defaultValue: KoshienObjectName.UNKNOWN
701+
}
702+
496703
},
497704
filter: [TargetType.SPRITE]
498705
}
@@ -514,11 +721,18 @@ class KoshienBlocks {
514721
acceptReporters: false,
515722
items: this.COORDINATES_MENU
516723
},
724+
coordinateAndPositionMenu: {
725+
acceptReporters: false,
726+
items: this.COORDINATES_AND_POSITION_MENU
727+
},
517728
targetMenu: {
518729
acceptReporters: false,
519730
items: this.TARGETS_MENU
731+
},
732+
objectMenu: {
733+
acceptReporters: false,
734+
items: this.OBJECTS_MENU
520735
}
521-
522736
},
523737
translationMap: translations
524738
};
@@ -585,6 +799,17 @@ class KoshienBlocks {
585799
return this._client.moveTo(args.POSITION);
586800
}
587801

802+
/**
803+
* shortest path between player and goal
804+
* @param {object} args - the block's arguments.
805+
* @param {string} args.RESULT - result.
806+
* @return {Promise} - promise
807+
*/
808+
// eslint-disable-next-line no-unused-vars
809+
calcGoalRoute (args) {
810+
return this._client.calcRoute({result: args.RESULT});
811+
}
812+
588813
/**
589814
* shortest path between two points
590815
* @param {object} args - the block's arguments.
@@ -596,8 +821,11 @@ class KoshienBlocks {
596821
*/
597822
// eslint-disable-next-line no-unused-vars
598823
calcRoute (args) {
599-
return this._client.calcRoute(args.SRC, args.DST, args.EXCEPT_CELLS, args.RESULT);
824+
return this._client.calcRoute(
825+
{src: args.SRC, dst: args.DST, exceptCells: args.EXCEPT_CELLS, result: args.RESULT}
826+
);
600827
}
828+
601829
/**
602830
* place an item at position
603831
* @param {object} args - the block's arguments.
@@ -674,17 +902,61 @@ class KoshienBlocks {
674902
}
675903

676904
/**
677-
* where of coordinate
905+
* position of coordinate
678906
* @param {object} args - the block's arguments.
679907
* @param {string} args.POSITION - position.
680908
* @param {string} args.COORDINATE - coordinate.
681909
* @return {number} - position of x or y
682910
*/
683-
// eslint-disable-next-line no-unused-vars
684911
positionOf (args) {
685912
const position = args.POSITION.split(':');
686913
return Number(args.COORDINATE === 'x' ? position[0] : position[1]);
687914
}
915+
916+
/**
917+
* object of code
918+
* @param {object} args - the block's arguments.
919+
* @param {string} args.OBJECT - object.
920+
* @return {number} - object of code
921+
*/
922+
object (args) {
923+
switch (args.OBJECT) {
924+
case KoshienObjectName.UNKNOWN:
925+
return -1;
926+
case KoshienObjectName.SPACE:
927+
return 0;
928+
case KoshienObjectName.WALL:
929+
return 1;
930+
case KoshienObjectName.STOREHOUSE:
931+
return 2;
932+
case KoshienObjectName.GOAL:
933+
return 3;
934+
case KoshienObjectName.WATER:
935+
return 4;
936+
case KoshienObjectName.BREAKABLE_WALL:
937+
return 5;
938+
case KoshienObjectName.TEA:
939+
return 'a';
940+
case KoshienObjectName.SWEETS:
941+
return 'b';
942+
case KoshienObjectName.COIN:
943+
return 'c';
944+
case KoshienObjectName.DOLPHIN:
945+
return 'd';
946+
case KoshienObjectName.SWORD:
947+
return 'e';
948+
case KoshienObjectName.POISON:
949+
return 'A';
950+
case KoshienObjectName.SNAKE:
951+
return 'B';
952+
case KoshienObjectName.TRAP:
953+
return 'C';
954+
case KoshienObjectName.BOMB:
955+
return 'D';
956+
default:
957+
return -1;
958+
}
959+
}
688960
}
689961

690962
export {

0 commit comments

Comments
 (0)