Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some method #362

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 60 additions & 32 deletions src/containers/ruby-tab/koshien-snippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,107 @@
"description": "プレイヤー名を (player1) にして、ゲームサーバへ接続する"
},
"koshien.move_to": {
"snippet": "koshien.move_to(${1:0}, ${2:0})",
"description": "x座標 (0)、y座標 (0) に移動する"
"snippet": "koshien.move_to(${1:\"0:0\"})",
"description": "座標 (0:0) に移動する"
},
"koshien.get_map_area": {
"snippet": "koshien.get_map_area(${1:0}, ${2:0})",
"description": "x座標が (0) 、y座標が (0) 付近のマップ情報を取得する"
"snippet": "koshien.get_map_area(${1:\"0:0\"})",
"description": "座標 (0:0) 付近のマップ情報を取得する"
},
"koshien.map": {
"snippet": "koshien.map(${1:0}, ${2:0})",
"description": "x座標が (0) 、y座標が (0) のマップ情報"
"snippet": "koshien.map(${1:\"0:0\"})",
"description": "座標 (0:0) のマップ情報"
},
"koshien.calc_goal_route": {
"snippet": "koshien.calc_route(result: list(${4:\"$最短経路\"}))",
"description": "ゴールまでの最短経路をリスト [最短経路] に保存する"
},
"koshien.calc_route": {
"snippet": "koshien.calc_route(src: [${1:0}, ${2:0}], dst: [${3:0}, ${4:0}], except_cells: ${5:\"通らない座標\"}, result: ${6:\"最短経路\"})",
"description": "2点間の最短経路 始点 x座標 (0) y座標 (0) 終点 x座標 (0) y座標 (0) 通らない座標 リスト (通らない座標) をリスト (最短経路) に保存する"
"snippet": "koshien.calc_route(result: list(${1:\"$最短経路\"}), src: ${2:\"0:0\"}, dst: ${3:\"0:0\"}, except_cells: list(${4:\"$通らない座標\"}))",
"description": "最短経路 (始点 座標 (0:0) 終点 座標 (0:0) 通らない座標 リスト [通らない座標]) をリスト [最短経路] に保存する"
},
"koshien.set_dynamite": {
"snippet": "koshien.set_dynamite([${1:0},${2:0}])",
"description": "ダイナマイトをx座標 (0) y座標 (0) に置く"
"snippet": "koshien.set_dynamite(${1:\"0:0\"})",
"description": "ダイナマイトを座標 (0:0) に置く"
},
"koshien.set_bomb": {
"snippet": "koshien.set_bomb([${1:0},${2:0}])",
"description": "爆弾をx座標 (0) y座標 (0) に置く"
"snippet": "koshien.set_bomb(${1:\"0:0\"})",
"description": "爆弾を座標 (0:0) に置く"
},
"koshien.map_all": {
"snippet": "$すべてのマップ情報 = koshien.map_all",
"description": "すべてのマップ情報"
},
"koshien.save_map_all": {
"snippet": "koshien.save_map_all(${1:\"map1\"})",
"description": "すべてのマップ情報を (map1) に保存する"
"koshien.map_from": {
"snippet": "$マップ情報 = koshien.map_from(${1:\"0:0\"}, ${2:$すべてのマップ情報})",
"description": "座標 (0:0) のマップ情報を [すべてのマップ情報] から読み込む"
},
"koshien.load_map": {
"snippet": "koshien.load_map(${1:\"map1\"}, ${2:0}, ${3:0})",
"description": "x座標が (0) 、y座標が (0) のマップ情報を (map1) から読み込む"
"koshien.locate_objects": {
"snippet": "koshien.locate_objects(result: list(${1:\"$地形・アイテムの座標\"}), cent: ${2:\"0:0\"}, sq_size: ${3:5}, objects: ${4:\"ABCD\"})",
"description": "範囲内の地形・アイテム (中心 座標 (0:0) 、範囲 (5) 、地形・アイテム (ABCD) )をリスト [地形・アイテムの座標] に保存する"
},
"koshien.other_player": {
"snippet": "koshien.other_player",
"description": "[対戦キャラクタ] の [座標]"
},
"koshien.other_player_x": {
"snippet": "koshien.other_player_x",
"description": "対戦キャラクタの x座標"
"description": "[対戦キャラクタ] の [x座標]"
},
"koshien.other_player_y": {
"snippet": "koshien.other_player_y",
"description": "対戦キャラクタの y座標"
"description": "[対戦キャラクタ] の [y座標]"
},
"koshien.player": {
"snippet": "koshien.player",
"description": "[プレイヤー] の [座標]"
},
"koshien.player_x": {
"snippet": "koshien.player_x",
"description": "プレイヤーの x座標"
"description": "[プレイヤー] の [x座標]"
},
"koshien.player_y": {
"snippet": "koshien.player_y",
"description": "プレイヤーの y座標"
"description": "[プレイヤー] の [y座標]"
},
"koshien.enemy": {
"snippet": "koshien.enemy",
"description": "[妨害キャラクター] の [座標]"
},
"koshien.enemy_x": {
"snippet": "koshien.enemy_x",
"description": "妨害キャラクタの x座標"
"description": "[妨害キャラクター] の [x座標]"
},
"koshien.enemy_y": {
"snippet": "koshien.enemy_y",
"description": "妨害キャラクタの y座標"
"description": "[妨害キャラクター] の [y座標]"
},
"koshien.goal": {
"snippet": "koshien.goal",
"description": "[ゴール] の [x座標]"
},
"koshien.goal_x": {
"snippet": "koshien.goal_x",
"description": "ゴールの x座標"
"description": "[ゴール] の [x座標]"
},
"koshien.goal_y": {
"snippet": "koshien.goal_y",
"description": "ゴールの y座標"
"description": "[ゴール] の [y座標]"
},
"koshien.turn_over": {
"snippet": "koshien.turn_over",
"description": "ターンを終了する"
},
"koshien.coordinate_of_x": {
"snippet": "koshien.coordinate_of_x(${1:'0:0'})",
"description": "(0:0) のx座標"
"koshien.position_of_x": {
"snippet": "koshien.position_of_x(${1:\"0:0\"})",
"description": "(0:0) の [x座標]"
},
"koshien.position_of_y": {
"snippet": "koshien.position_of_y(${1:\"0:0\"})",
"description": "(0:0) の [y座標]"
},
"koshien.coordinate_of_y": {
"snippet": "koshien.coordinate_of_y(${1:'0:0'})",
"description": "(0:0) のy座標"
"koshien.object": {
"snippet": "koshien.object(${1:\"unknown\"})",
"description": "[未探索のマス]"
}
}
23 changes: 20 additions & 3 deletions src/lib/ruby-generator/koshien.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export default function (Generator) {
return `koshien.move_to(${position})\n`;
};

Generator.koshien_calcGoalRoute = function (block) {
const resultListName = Generator.listNameByName(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
);
const result = resultListName ? `list(${Generator.quote_(resultListName)})` : 'nil';

return `koshien.calc_route(result: ${result})\n`;
};

Generator.koshien_calcRoute = function (block) {
const src = Generator.valueToCode(block, 'SRC', Generator.ORDER_NONE) || Generator.quote_('0:0');
const dst = Generator.valueToCode(block, 'DST', Generator.ORDER_NONE) || Generator.quote_('0:0');
Expand Down Expand Up @@ -61,7 +70,7 @@ export default function (Generator) {
Generator.koshien_locateObjects = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
const sqSize = Generator.valueToCode(block, 'SQ_SIZE', Generator.ORDER_NONE) || 5;
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('A B C D');
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('ABCD');
const resultListName = Generator.listNameByName(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
);
Expand All @@ -73,7 +82,10 @@ export default function (Generator) {

Generator.koshien_targetCoordinate = function (block) {
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'position';
if (coordinate === 'position') {
return [`koshien.${target}`];
}
return [`koshien.${target}_${coordinate}`];
};

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

Generator.koshien_positionOf = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
return [`koshien.position_of_${coordinate}(${position})`];
};

Generator.koshien_object = function (block) {
const object = Generator.quote_(Generator.getFieldValue(block, 'OBJECT') || 'unknown');
return [`koshien.object(${object})`];
};

return Generator;
}
53 changes: 52 additions & 1 deletion src/lib/ruby-to-blocks-converter/koshien.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ const KoshienConverter = {
const exceptCells = args[0].get('sym:except_cells');
const result = args[0].get('sym:result');

if (!src && !dst && !exceptCells) {
if (!converter.isListBlock(result) && !converter.isNil(result)) return null;

const block = converter.changeRubyExpressionBlock(receiver, 'koshien_calcGoalRoute', 'statement');
converter.addField(block, 'RESULT', converter.lookupListFromListBlock(result)?.name || ' ');
converter.removeBlock(result);
return block;
}

if (!checkPosition(src)) return null;
if (!checkPosition(dst)) return null;
if (!converter.isListBlock(exceptCells) && !converter.isNil(exceptCells)) return null;
Expand Down Expand Up @@ -138,12 +147,20 @@ const KoshienConverter = {
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_locateObjects', 'statement');
converter.addNumberInput(block, 'SQ_SIZE', 'math_number', sqSize, 0);
converter.addTextInput(block, 'POSITION', cent, '0:0');
converter.addTextInput(block, 'OBJECTS', objects, 'A B C D');
converter.addTextInput(block, 'OBJECTS', objects, 'ABCD');
converter.addField(block, 'RESULT', converter.lookupListFromListBlock(result)?.name || ' ');
converter.removeBlock(result);
return block;
});

converter.registerCallMethod(Koshien, 'other_player', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
converter.addField(block, 'TARGET', 'other_player');
converter.addField(block, 'COORDINATE', 'position');
return block;
});

converter.registerCallMethod(Koshien, 'other_player_x', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
Expand All @@ -160,6 +177,14 @@ const KoshienConverter = {
return block;
});

converter.registerCallMethod(Koshien, 'enemy', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
converter.addField(block, 'TARGET', 'enemy');
converter.addField(block, 'COORDINATE', 'position');
return block;
});

converter.registerCallMethod(Koshien, 'enemy_x', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
Expand All @@ -176,6 +201,14 @@ const KoshienConverter = {
return block;
});

converter.registerCallMethod(Koshien, 'goal', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
converter.addField(block, 'TARGET', 'goal');
converter.addField(block, 'COORDINATE', 'position');
return block;
});

converter.registerCallMethod(Koshien, 'goal_x', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
Expand All @@ -192,6 +225,14 @@ const KoshienConverter = {
return block;
});

converter.registerCallMethod(Koshien, 'player', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
converter.addField(block, 'TARGET', 'player');
converter.addField(block, 'COORDINATE', 'position');
return block;
});

converter.registerCallMethod(Koshien, 'player_x', 0, params => {
const {receiver} = params;
const block = converter.changeRubyExpressionBlock(receiver, 'koshien_targetCoordinate', 'value');
Expand Down Expand Up @@ -252,6 +293,16 @@ const KoshienConverter = {
converter.addField(block, 'COORDINATE', 'y');
return block;
});

converter.registerCallMethod(Koshien, 'object', 1, params => {
const {receiver, args} = params;

if (!converter.isString(args[0])) return null;

const block = converter.changeRubyExpressionBlock(receiver, 'koshien_object', 'value');
converter.addField(block, 'OBJECT', args[0]);
return block;
});
}
};

Expand Down
45 changes: 43 additions & 2 deletions test/integration/ruby-tab/extension_koshien.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Ruby Tab: Koshien extension blocks', () => {
koshien.connect_game(name: "player1")
koshien.get_map_area("0:1")
koshien.move_to("2:3")
koshien.calc_route(result: list("$最短経路"))
koshien.calc_route(result: list("$最短経路"), src: "4:5", dst: "6:7", except_cells: list("$通らない座標"))
koshien.calc_route(result: nil, src: "4:5", dst: "6:7", except_cells: nil)
koshien.set_dynamite("8:9")
Expand All @@ -40,8 +41,8 @@ describe('Ruby Tab: Koshien extension blocks', () => {
$すべてのマップ情報 = koshien.map_all
$マップ情報 = koshien.map_from("14:0", $すべてのマップ情報)
$マップ情報 = koshien.map_from("14:0", nil)
koshien.locate_objects(result: list("$地形・アイテム"), sq_size: 3, cent: "1:2", objects: "A B C D")
koshien.locate_objects(result: nil, sq_size: 3, cent: "1:2", objects: "A B C D")
koshien.locate_objects(result: list("$地形・アイテム"), sq_size: 3, cent: "1:2", objects: "ABCD")
koshien.locate_objects(result: nil, sq_size: 3, cent: "1:2", objects: "ABCD")
koshien.turn_over

koshien.position_of_x("0:1")
Expand All @@ -50,22 +51,62 @@ describe('Ruby Tab: Koshien extension blocks', () => {

koshien.position(4, 5)

koshien.player

koshien.player_x

koshien.player_y

koshien.other_player

koshien.other_player_x

koshien.other_player_y

koshien.goal

koshien.goal_x

koshien.goal_y

koshien.enemy

koshien.enemy_x

koshien.enemy_y

koshien.object("unknown")

koshien.object("space")

koshien.object("wall")

koshien.object("storehouse")

koshien.object("goal")

koshien.object("water")

koshien.object("breakable wall")

koshien.object("tea")

koshien.object("sweets")

koshien.object("coin")

koshien.object("dolphin")

koshien.object("sword")

koshien.object("poison")

koshien.object("snake")

koshien.object("trap")

koshien.object("bomb")

`;
await expectInterconvertBetweenCodeAndRuby(code);
});
Expand Down
Loading