Skip to content

Commit 5bcab1b

Browse files
authored
Merge pull request #362 from smalruby/fix-koshien
feat: add some method
2 parents d1dafe3 + c64b028 commit 5bcab1b

File tree

5 files changed

+176
-39
lines changed

5 files changed

+176
-39
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,107 @@
44
"description": "プレイヤー名を (player1) にして、ゲームサーバへ接続する"
55
},
66
"koshien.move_to": {
7-
"snippet": "koshien.move_to(${1:0}, ${2:0})",
8-
"description": "x座標 (0)、y座標 (0) に移動する"
7+
"snippet": "koshien.move_to(${1:\"0:0\"})",
8+
"description": "座標 (0:0) に移動する"
99
},
1010
"koshien.get_map_area": {
11-
"snippet": "koshien.get_map_area(${1:0}, ${2:0})",
12-
"description": "x座標が (0) 、y座標が (0) 付近のマップ情報を取得する"
11+
"snippet": "koshien.get_map_area(${1:\"0:0\"})",
12+
"description": "座標 (0:0) 付近のマップ情報を取得する"
1313
},
1414
"koshien.map": {
15-
"snippet": "koshien.map(${1:0}, ${2:0})",
16-
"description": "x座標が (0) 、y座標が (0) のマップ情報"
15+
"snippet": "koshien.map(${1:\"0:0\"})",
16+
"description": "座標 (0:0) のマップ情報"
17+
},
18+
"koshien.calc_goal_route": {
19+
"snippet": "koshien.calc_route(result: list(${4:\"$最短経路\"}))",
20+
"description": "ゴールまでの最短経路をリスト [最短経路] に保存する"
1721
},
1822
"koshien.calc_route": {
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) 通らない座標 リスト (通らない座標) をリスト (最短経路) に保存する"
23+
"snippet": "koshien.calc_route(result: list(${1:\"$最短経路\"}), src: ${2:\"0:0\"}, dst: ${3:\"0:0\"}, except_cells: list(${4:\"$通らない座標\"}))",
24+
"description": "最短経路 (始点 座標 (0:0) 終点 座標 (0:0) 通らない座標 リスト [通らない座標]) をリスト [最短経路] に保存する"
2125
},
2226
"koshien.set_dynamite": {
23-
"snippet": "koshien.set_dynamite([${1:0},${2:0}])",
24-
"description": "ダイナマイトをx座標 (0) y座標 (0) に置く"
27+
"snippet": "koshien.set_dynamite(${1:\"0:0\"})",
28+
"description": "ダイナマイトを座標 (0:0) に置く"
2529
},
2630
"koshien.set_bomb": {
27-
"snippet": "koshien.set_bomb([${1:0},${2:0}])",
28-
"description": "爆弾をx座標 (0) y座標 (0) に置く"
31+
"snippet": "koshien.set_bomb(${1:\"0:0\"})",
32+
"description": "爆弾を座標 (0:0) に置く"
33+
},
34+
"koshien.map_all": {
35+
"snippet": "$すべてのマップ情報 = koshien.map_all",
36+
"description": "すべてのマップ情報"
2937
},
30-
"koshien.save_map_all": {
31-
"snippet": "koshien.save_map_all(${1:\"map1\"})",
32-
"description": "すべてのマップ情報を (map1) に保存する"
38+
"koshien.map_from": {
39+
"snippet": "$マップ情報 = koshien.map_from(${1:\"0:0\"}, ${2:$すべてのマップ情報})",
40+
"description": "座標 (0:0) のマップ情報を [すべてのマップ情報] から読み込む"
3341
},
34-
"koshien.load_map": {
35-
"snippet": "koshien.load_map(${1:\"map1\"}, ${2:0}, ${3:0})",
36-
"description": "x座標が (0) 、y座標が (0) のマップ情報を (map1) から読み込む"
42+
"koshien.locate_objects": {
43+
"snippet": "koshien.locate_objects(result: list(${1:\"$地形・アイテムの座標\"}), cent: ${2:\"0:0\"}, sq_size: ${3:5}, objects: ${4:\"ABCD\"})",
44+
"description": "範囲内の地形・アイテム (中心 座標 (0:0) 、範囲 (5) 、地形・アイテム (ABCD) )をリスト [地形・アイテムの座標] に保存する"
45+
},
46+
"koshien.other_player": {
47+
"snippet": "koshien.other_player",
48+
"description": "[対戦キャラクタ] の [座標]"
3749
},
3850
"koshien.other_player_x": {
3951
"snippet": "koshien.other_player_x",
40-
"description": "対戦キャラクタの x座標"
52+
"description": "[対戦キャラクタ] の [x座標]"
4153
},
4254
"koshien.other_player_y": {
4355
"snippet": "koshien.other_player_y",
44-
"description": "対戦キャラクタの y座標"
56+
"description": "[対戦キャラクタ] の [y座標]"
57+
},
58+
"koshien.player": {
59+
"snippet": "koshien.player",
60+
"description": "[プレイヤー] の [座標]"
4561
},
4662
"koshien.player_x": {
4763
"snippet": "koshien.player_x",
48-
"description": "プレイヤーの x座標"
64+
"description": "[プレイヤー] の [x座標]"
4965
},
5066
"koshien.player_y": {
5167
"snippet": "koshien.player_y",
52-
"description": "プレイヤーの y座標"
68+
"description": "[プレイヤー] の [y座標]"
69+
},
70+
"koshien.enemy": {
71+
"snippet": "koshien.enemy",
72+
"description": "[妨害キャラクター] の [座標]"
5373
},
5474
"koshien.enemy_x": {
5575
"snippet": "koshien.enemy_x",
56-
"description": "妨害キャラクタの x座標"
76+
"description": "[妨害キャラクター] の [x座標]"
5777
},
5878
"koshien.enemy_y": {
5979
"snippet": "koshien.enemy_y",
60-
"description": "妨害キャラクタの y座標"
80+
"description": "[妨害キャラクター] の [y座標]"
81+
},
82+
"koshien.goal": {
83+
"snippet": "koshien.goal",
84+
"description": "[ゴール] の [x座標]"
6185
},
6286
"koshien.goal_x": {
6387
"snippet": "koshien.goal_x",
64-
"description": "ゴールの x座標"
88+
"description": "[ゴール] の [x座標]"
6589
},
6690
"koshien.goal_y": {
6791
"snippet": "koshien.goal_y",
68-
"description": "ゴールの y座標"
92+
"description": "[ゴール] の [y座標]"
6993
},
7094
"koshien.turn_over": {
7195
"snippet": "koshien.turn_over",
7296
"description": "ターンを終了する"
7397
},
74-
"koshien.coordinate_of_x": {
75-
"snippet": "koshien.coordinate_of_x(${1:'0:0'})",
76-
"description": "(0:0) のx座標"
98+
"koshien.position_of_x": {
99+
"snippet": "koshien.position_of_x(${1:\"0:0\"})",
100+
"description": "(0:0) の [x座標]"
101+
},
102+
"koshien.position_of_y": {
103+
"snippet": "koshien.position_of_y(${1:\"0:0\"})",
104+
"description": "(0:0) の [y座標]"
77105
},
78-
"koshien.coordinate_of_y": {
79-
"snippet": "koshien.coordinate_of_y(${1:'0:0'})",
80-
"description": "(0:0) のy座標"
106+
"koshien.object": {
107+
"snippet": "koshien.object(${1:\"unknown\"})",
108+
"description": "[未探索のマス]"
81109
}
82110
}

src/lib/ruby-generator/koshien.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ export default function (Generator) {
2424
return `koshien.move_to(${position})\n`;
2525
};
2626

27+
Generator.koshien_calcGoalRoute = function (block) {
28+
const resultListName = Generator.listNameByName(
29+
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
30+
);
31+
const result = resultListName ? `list(${Generator.quote_(resultListName)})` : 'nil';
32+
33+
return `koshien.calc_route(result: ${result})\n`;
34+
};
35+
2736
Generator.koshien_calcRoute = function (block) {
2837
const src = Generator.valueToCode(block, 'SRC', Generator.ORDER_NONE) || Generator.quote_('0:0');
2938
const dst = Generator.valueToCode(block, 'DST', Generator.ORDER_NONE) || Generator.quote_('0:0');
@@ -61,7 +70,7 @@ export default function (Generator) {
6170
Generator.koshien_locateObjects = function (block) {
6271
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
6372
const sqSize = Generator.valueToCode(block, 'SQ_SIZE', Generator.ORDER_NONE) || 5;
64-
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('A B C D');
73+
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('ABCD');
6574
const resultListName = Generator.listNameByName(
6675
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
6776
);
@@ -73,7 +82,10 @@ export default function (Generator) {
7382

7483
Generator.koshien_targetCoordinate = function (block) {
7584
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
76-
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
85+
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'position';
86+
if (coordinate === 'position') {
87+
return [`koshien.${target}`];
88+
}
7789
return [`koshien.${target}_${coordinate}`];
7890
};
7991

@@ -89,9 +101,14 @@ export default function (Generator) {
89101

90102
Generator.koshien_positionOf = function (block) {
91103
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
92-
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
104+
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
93105
return [`koshien.position_of_${coordinate}(${position})`];
94106
};
95107

108+
Generator.koshien_object = function (block) {
109+
const object = Generator.quote_(Generator.getFieldValue(block, 'OBJECT') || 'unknown');
110+
return [`koshien.object(${object})`];
111+
};
112+
96113
return Generator;
97114
}

src/lib/ruby-to-blocks-converter/koshien.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ const KoshienConverter = {
6666
const exceptCells = args[0].get('sym:except_cells');
6767
const result = args[0].get('sym:result');
6868

69+
if (!src && !dst && !exceptCells) {
70+
if (!converter.isListBlock(result) && !converter.isNil(result)) return null;
71+
72+
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_calcGoalRoute', 'statement');
73+
converter.addField(block, 'RESULT', converter.lookupListFromListBlock(result)?.name || ' ');
74+
converter.removeBlock(result);
75+
return block;
76+
}
77+
6978
if (!checkPosition(src)) return null;
7079
if (!checkPosition(dst)) return null;
7180
if (!converter.isListBlock(exceptCells) && !converter.isNil(exceptCells)) return null;
@@ -138,12 +147,20 @@ const KoshienConverter = {
138147
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_locateObjects', 'statement');
139148
converter.addNumberInput(block, 'SQ_SIZE', 'math_number', sqSize, 0);
140149
converter.addTextInput(block, 'POSITION', cent, '0:0');
141-
converter.addTextInput(block, 'OBJECTS', objects, 'A B C D');
150+
converter.addTextInput(block, 'OBJECTS', objects, 'ABCD');
142151
converter.addField(block, 'RESULT', converter.lookupListFromListBlock(result)?.name || ' ');
143152
converter.removeBlock(result);
144153
return block;
145154
});
146155

156+
converter.registerCallMethod(Koshien, 'other_player', 0, params => {
157+
const {receiver} = params;
158+
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
159+
converter.addField(block, 'TARGET', 'other_player');
160+
converter.addField(block, 'COORDINATE', 'position');
161+
return block;
162+
});
163+
147164
converter.registerCallMethod(Koshien, 'other_player_x', 0, params => {
148165
const {receiver} = params;
149166
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
@@ -160,6 +177,14 @@ const KoshienConverter = {
160177
return block;
161178
});
162179

180+
converter.registerCallMethod(Koshien, 'enemy', 0, params => {
181+
const {receiver} = params;
182+
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
183+
converter.addField(block, 'TARGET', 'enemy');
184+
converter.addField(block, 'COORDINATE', 'position');
185+
return block;
186+
});
187+
163188
converter.registerCallMethod(Koshien, 'enemy_x', 0, params => {
164189
const {receiver} = params;
165190
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
@@ -176,6 +201,14 @@ const KoshienConverter = {
176201
return block;
177202
});
178203

204+
converter.registerCallMethod(Koshien, 'goal', 0, params => {
205+
const {receiver} = params;
206+
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
207+
converter.addField(block, 'TARGET', 'goal');
208+
converter.addField(block, 'COORDINATE', 'position');
209+
return block;
210+
});
211+
179212
converter.registerCallMethod(Koshien, 'goal_x', 0, params => {
180213
const {receiver} = params;
181214
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
@@ -192,6 +225,14 @@ const KoshienConverter = {
192225
return block;
193226
});
194227

228+
converter.registerCallMethod(Koshien, 'player', 0, params => {
229+
const {receiver} = params;
230+
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
231+
converter.addField(block, 'TARGET', 'player');
232+
converter.addField(block, 'COORDINATE', 'position');
233+
return block;
234+
});
235+
195236
converter.registerCallMethod(Koshien, 'player_x', 0, params => {
196237
const {receiver} = params;
197238
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
@@ -252,6 +293,16 @@ const KoshienConverter = {
252293
converter.addField(block, 'COORDINATE', 'y');
253294
return block;
254295
});
296+
297+
converter.registerCallMethod(Koshien, 'object', 1, params => {
298+
const {receiver, args} = params;
299+
300+
if (!converter.isString(args[0])) return null;
301+
302+
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_object', 'value');
303+
converter.addField(block, 'OBJECT', args[0]);
304+
return block;
305+
});
255306
}
256307
};
257308

test/integration/ruby-tab/extension_koshien.test.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('Ruby Tab: Koshien extension blocks', () => {
3232
koshien.connect_game(name: "player1")
3333
koshien.get_map_area("0:1")
3434
koshien.move_to("2:3")
35+
koshien.calc_route(result: list("$最短経路"))
3536
koshien.calc_route(result: list("$最短経路"), src: "4:5", dst: "6:7", except_cells: list("$通らない座標"))
3637
koshien.calc_route(result: nil, src: "4:5", dst: "6:7", except_cells: nil)
3738
koshien.set_dynamite("8:9")
@@ -40,8 +41,8 @@ describe('Ruby Tab: Koshien extension blocks', () => {
4041
$すべてのマップ情報 = koshien.map_all
4142
$マップ情報 = koshien.map_from("14:0", $すべてのマップ情報)
4243
$マップ情報 = koshien.map_from("14:0", nil)
43-
koshien.locate_objects(result: list("$地形・アイテム"), sq_size: 3, cent: "1:2", objects: "A B C D")
44-
koshien.locate_objects(result: nil, sq_size: 3, cent: "1:2", objects: "A B C D")
44+
koshien.locate_objects(result: list("$地形・アイテム"), sq_size: 3, cent: "1:2", objects: "ABCD")
45+
koshien.locate_objects(result: nil, sq_size: 3, cent: "1:2", objects: "ABCD")
4546
koshien.turn_over
4647
4748
koshien.position_of_x("0:1")
@@ -50,22 +51,62 @@ describe('Ruby Tab: Koshien extension blocks', () => {
5051
5152
koshien.position(4, 5)
5253
54+
koshien.player
55+
5356
koshien.player_x
5457
5558
koshien.player_y
5659
60+
koshien.other_player
61+
5762
koshien.other_player_x
5863
5964
koshien.other_player_y
6065
66+
koshien.goal
67+
6168
koshien.goal_x
6269
6370
koshien.goal_y
6471
72+
koshien.enemy
73+
6574
koshien.enemy_x
6675
6776
koshien.enemy_y
6877
78+
koshien.object("unknown")
79+
80+
koshien.object("space")
81+
82+
koshien.object("wall")
83+
84+
koshien.object("storehouse")
85+
86+
koshien.object("goal")
87+
88+
koshien.object("water")
89+
90+
koshien.object("breakable wall")
91+
92+
koshien.object("tea")
93+
94+
koshien.object("sweets")
95+
96+
koshien.object("coin")
97+
98+
koshien.object("dolphin")
99+
100+
koshien.object("sword")
101+
102+
koshien.object("poison")
103+
104+
koshien.object("snake")
105+
106+
koshien.object("trap")
107+
108+
koshien.object("bomb")
109+
69110
`;
70111
await expectInterconvertBetweenCodeAndRuby(code);
71112
});

0 commit comments

Comments
 (0)