Skip to content

Commit 6b94b09

Browse files
committed
feat: add koshien blocks
1 parent 42ae1bf commit 6b94b09

File tree

4 files changed

+227
-142
lines changed

4 files changed

+227
-142
lines changed

src/containers/ruby-tab/koshien-snippets.json

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
{
2+
"koshien.connect_game": {
3+
"snippet": "koshien.connect_game(name: ${1:\"player1\"})",
4+
"description": "プレイヤー名を (player1) にして、ゲームサーバへ接続する"
5+
},
26
"koshien.move_to": {
3-
"snippet": "koshien.move_to([${1:0},${2:0}])",
4-
"description": "x座標 (0) y座標 (0) に移動する"
7+
"snippet": "koshien.move_to(${1:0}, ${2:0})",
8+
"description": "x座標 (0)y座標 (0) に移動する"
59
},
610
"koshien.get_map_area": {
7-
"snippet": "koshien.get_map_area([${1:0},${2:0}])",
8-
"description": "x座標 (0) y座標 (0) 付近のマップ情報を取得"
11+
"snippet": "koshien.get_map_area(${1:0}, ${2:0})",
12+
"description": "x座標が (0) 、y座標が (0) 付近のマップ情報を取得する"
13+
},
14+
"koshien.map": {
15+
"snippet": "koshien.map(${1:0}, ${2:0})",
16+
"description": "x座標が (0) 、y座標が (0) のマップ情報"
917
},
1018
"koshien.calc_route": {
11-
"snippet": "koshien.calc_route(src:'${1:0:0}',dst:'${2:0:0}' except_cells:${3:1}])",
12-
"description": "2点間の最短距離 始点 x座標 (0) y座標 (0) 終点 x座標 (0) y座標 (0) 通らない道 リスト (0)"
19+
"snippet": "koshien.calc_route(src: [${1:0}, ${2:0}], dst: [${3:0}, ${4:0}], except_cells: ${5:\"通らない座標\"}, result: ${6:\"最短経路\"})",
20+
"description": "2点間の最短経路 始点 x座標 (0) y座標 (0) 終点 x座標 (0) y座標 (0) 通らない座標 リスト (通らない座標) をリスト (最短経路) に保存する"
1321
},
1422
"koshien.set_dynamite": {
1523
"snippet": "koshien.set_dynamite([${1:0},${2:0}])",
@@ -19,13 +27,13 @@
1927
"snippet": "koshien.set_bomb([${1:0},${2:0}])",
2028
"description": "爆弾をx座標 (0) y座標 (0) に置く"
2129
},
22-
"koshien.map": {
23-
"snippet": "koshien.map(${1:0},${2:0} on:${3:'map_1'})",
24-
"description": "x座標 (0) y座標 (0) のマップ情報を('map_1')に保存"
30+
"koshien.save_map_all": {
31+
"snippet": "koshien.save_map_all(${1:\"map1\"})",
32+
"description": "すべてのマップ情報を (map1) に保存する"
2533
},
26-
"koshien.map_all": {
27-
"snippet": "koshien.map_all(save_as:${'map_1'})",
28-
"description": "マップ情報を('map_1')に保存"
34+
"koshien.load_map": {
35+
"snippet": "koshien.load_map(${1:\"map1\"}, ${2:0}, ${3:0})",
36+
"description": "x座標が (0) 、y座標が (0) のマップ情報を (map1) から読み込む"
2937
},
3038
"koshien.other_player_x": {
3139
"snippet": "koshien.other_player_x",
@@ -63,16 +71,12 @@
6371
"snippet": "koshien.turn_over",
6472
"description": "ターンを終了する"
6573
},
66-
"koshien.connect_game": {
67-
"snippet": "koshien.connect_game(player_name:${1:'test'})",
68-
"description": "プレイヤー名('test')ゲームに接続する"
69-
},
70-
"koshien.position_x": {
71-
"snippet": "koshien.position_x(${1:'0:0'})",
72-
"description": "('0:0')のx座標"
74+
"koshien.coordinate_of_x": {
75+
"snippet": "koshien.coordinate_of_x(${1:'0:0'})",
76+
"description": "(0:0) のx座標"
7377
},
74-
"koshien.position_y": {
75-
"snippet": "koshien.position_y(${1:'0:0'})",
76-
"description": "('0:0')のy座標"
78+
"koshien.coordinate_of_y": {
79+
"snippet": "koshien.coordinate_of_y(${1:'0:0'})",
80+
"description": "(0:0) のy座標"
7781
}
78-
}
82+
}

src/lib/ruby-generator/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,29 @@ RubyGenerator.listName = function (id) {
428428
return this.variableName(id, LIST_TYPE);
429429
};
430430

431+
RubyGenerator.variableNameByName = function (name, type = SCALAR_TYPE) {
432+
let currVar;
433+
let isStage;
434+
const target = this.currentTarget;
435+
if (target.runtime) {
436+
const stage = target.runtime.getTargetForStage();
437+
currVar = stage.lookupVariableByNameAndType(name, type);
438+
isStage = true;
439+
}
440+
if (!currVar) {
441+
currVar = target.lookupVariableByNameAndType(name, type);
442+
isStage = target.isStage;
443+
}
444+
if (currVar && currVar.type === type) {
445+
return this.makeVariableName(isStage, currVar.name);
446+
}
447+
return null;
448+
};
449+
450+
RubyGenerator.listNameByName = function (name) {
451+
return this.variableNameByName(name, LIST_TYPE);
452+
};
453+
431454
RubyGenerator.getScripts = function () {
432455
return Generator.prototype.getScripts.call(this).sort((a, b) => {
433456
const aValue = (this.getBlock(a).opcode === 'procedures_definition' ? 1 : -1);

src/lib/ruby-generator/koshien.js

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,88 @@
44
* @return {RubyGenerator} same as param.
55
*/
66
export default function (Generator) {
7-
Generator.koshien_move_to = function (block) {
7+
Generator.koshien_connectGame = function (block) {
8+
const name = Generator.valueToCode(block, 'NAME', Generator.ORDER_NONE) || Generator.quote_('player1');
9+
return `koshien.connect_game(name: ${name})\n`;
10+
};
11+
12+
Generator.koshien_getMapArea = function (block) {
13+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
14+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
15+
return `koshien.get_map_area(${x}, ${y})\n`;
16+
};
17+
18+
Generator.koshien_map = function (block) {
19+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
20+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
21+
return [`koshien.map(${x}, ${y})`];
22+
};
23+
24+
Generator.koshien_moveTo = function (block) {
825
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
926
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
10-
return `koshien.move_to([${x},${y}])\n`;
27+
return `koshien.move_to(${x}, ${y})\n`;
1128
};
12-
Generator.koshien_calc_route = function (block) {
29+
30+
Generator.koshien_calcRoute = function (block) {
1331
const srcX = Generator.valueToCode(block, 'SRC_X', Generator.ORDER_NONE) || 0;
1432
const srcY = Generator.valueToCode(block, 'SRC_Y', Generator.ORDER_NONE) || 0;
1533
const dstX = Generator.valueToCode(block, 'DST_X', Generator.ORDER_NONE) || 0;
1634
const dstY = Generator.valueToCode(block, 'DST_Y', Generator.ORDER_NONE) || 0;
17-
const list = Generator.valueToCode(block, 'LIST', Generator.ORDER_NONE) || 0;
35+
const exceptCells = Generator.quote_(
36+
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE) || ' '
37+
);
38+
const result = Generator.quote_(
39+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
40+
);
1841

19-
return [`koshien.calc_route(src:"${srcX}:${srcY}",dst:"${dstX}:${dstY}",except_cells:${list})`];
20-
};
21-
Generator.koshien_get_map_area = function (block) {
22-
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
23-
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
24-
return `koshien.get_map_area([${x},${y}])\n`;
42+
// eslint-disable-next-line max-len
43+
return `koshien.calc_route(src: [${srcX}, ${srcY}], dst: [${dstX}, ${dstY}], except_cells: ${exceptCells}, result: ${result})\n`;
2544
};
26-
Generator.koshien_set_item = function (block) {
45+
46+
Generator.koshien_setItem = function (block) {
2747
const item = Generator.getFieldValue(block, 'ITEM') || null;
2848
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
2949
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
30-
return `koshien.set_${item}([${x},${y}])\n`;
50+
return `koshien.set_${item}(${x}, ${y})\n`;
3151
};
32-
Generator.koshien_map = function (block) {
52+
53+
Generator.koshien_loadMap = function (block) {
54+
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
3355
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
3456
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
35-
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map_1');
36-
return [`koshien.map(${x},${y}, on: ${location})`];
57+
return [`koshien.load_map(${location}, ${x}, ${y})`];
3758
};
38-
Generator.koshien_map_all = function (block) {
39-
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map_1');
40-
return [`koshien.map_all(save_as: ${location})`];
59+
60+
Generator.koshien_saveMapAll = function (block) {
61+
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
62+
return `koshien.save_map_all(${location})\n`;
4163
};
42-
Generator.koshien_locate_objects = function (block) {
64+
65+
Generator.koshien_locateObjects = function (block) {
4366
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
4467
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
45-
const size = Generator.valueToCode(block, 'SIZE', Generator.ORDER_NONE) || 5;
46-
const item =
47-
Generator.valueToCode(block, 'ITEM', Generator.ORDER_NONE) || Generator.quote_('["A","B","C","D"]');
68+
const sqSize = Generator.valueToCode(block, 'SQ_SIZE', Generator.ORDER_NONE) || 5;
69+
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('A B C D');
70+
const result = Generator.quote_(
71+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
72+
);
4873

49-
return [`koshien.locate_objects(sq_size:${size},cent:"${x}:${y}",objects:${item})`];
74+
// eslint-disable-next-line max-len
75+
return `koshien.locate_objects(sq_size: ${sqSize}, cent: [${x}, ${y}], objects: ${objects}, result: ${result})\n`;
5076
};
51-
Generator.koshien_target_coordinate = function (block) {
52-
const target = Generator.getFieldValue(block, 'TARGET') || null;
53-
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
77+
Generator.koshien_targetCoordinate = function (block) {
78+
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
79+
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
5480
return [`koshien.${target}_${coordinate}`];
5581
};
56-
Generator.koshien_turn_over = function () {
82+
Generator.koshien_turnOver = function () {
5783
return `koshien.turn_over\n`;
5884
};
59-
Generator.koshien_connect_game = function (block) {
60-
const name = Generator.valueToCode(block, 'NAME', Generator.ORDER_NONE) || Generator.quote_('test');
61-
return `koshien.connect_game(player_name:${name})\n`;
62-
};
63-
Generator.koshien_position_coordinate = function (block) {
85+
Generator.koshien_coordinateOf = function (block) {
6486
const where = Generator.valueToCode(block, 'WHERE', Generator.ORDER_NONE) || Generator.quote_('0:0');
6587
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
66-
return [`koshien.position_${coordinate}(${where})`];
88+
return [`koshien.coordinate_of_${coordinate}(${where})`];
6789
};
6890

6991
return Generator;

0 commit comments

Comments
 (0)