Skip to content

Commit 57f3193

Browse files
authored
Merge pull request #355 from smalruby/speak-mentaiko-kousien
Speak mentaiko kousien
2 parents bfb9562 + 18246c4 commit 57f3193

File tree

13 files changed

+612
-3
lines changed

13 files changed

+612
-3
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.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"koshien.connect_game": {
3+
"snippet": "koshien.connect_game(name: ${1:\"player1\"})",
4+
"description": "プレイヤー名を (player1) にして、ゲームサーバへ接続する"
5+
},
6+
"koshien.move_to": {
7+
"snippet": "koshien.move_to(${1:0}, ${2:0})",
8+
"description": "x座標 (0)、y座標 (0) に移動する"
9+
},
10+
"koshien.get_map_area": {
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) のマップ情報"
17+
},
18+
"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) 通らない座標 リスト (通らない座標) をリスト (最短経路) に保存する"
21+
},
22+
"koshien.set_dynamite": {
23+
"snippet": "koshien.set_dynamite([${1:0},${2:0}])",
24+
"description": "ダイナマイトをx座標 (0) y座標 (0) に置く"
25+
},
26+
"koshien.set_bomb": {
27+
"snippet": "koshien.set_bomb([${1:0},${2:0}])",
28+
"description": "爆弾をx座標 (0) y座標 (0) に置く"
29+
},
30+
"koshien.save_map_all": {
31+
"snippet": "koshien.save_map_all(${1:\"map1\"})",
32+
"description": "すべてのマップ情報を (map1) に保存する"
33+
},
34+
"koshien.load_map": {
35+
"snippet": "koshien.load_map(${1:\"map1\"}, ${2:0}, ${3:0})",
36+
"description": "x座標が (0) 、y座標が (0) のマップ情報を (map1) から読み込む"
37+
},
38+
"koshien.other_player_x": {
39+
"snippet": "koshien.other_player_x",
40+
"description": "対戦キャラクタの x座標"
41+
},
42+
"koshien.other_player_y": {
43+
"snippet": "koshien.other_player_y",
44+
"description": "対戦キャラクタの y座標"
45+
},
46+
"koshien.player_x": {
47+
"snippet": "koshien.player_x",
48+
"description": "プレイヤーの x座標"
49+
},
50+
"koshien.player_y": {
51+
"snippet": "koshien.player_y",
52+
"description": "プレイヤーの y座標"
53+
},
54+
"koshien.enemy_x": {
55+
"snippet": "koshien.enemy_x",
56+
"description": "妨害キャラクタの x座標"
57+
},
58+
"koshien.enemy_y": {
59+
"snippet": "koshien.enemy_y",
60+
"description": "妨害キャラクタの y座標"
61+
},
62+
"koshien.goal_x": {
63+
"snippet": "koshien.goal_x",
64+
"description": "ゴールの x座標"
65+
},
66+
"koshien.goal_y": {
67+
"snippet": "koshien.goal_y",
68+
"description": "ゴールの y座標"
69+
},
70+
"koshien.turn_over": {
71+
"snippet": "koshien.turn_over",
72+
"description": "ターンを終了する"
73+
},
74+
"koshien.coordinate_of_x": {
75+
"snippet": "koshien.coordinate_of_x(${1:'0:0'})",
76+
"description": "(0:0) のx座標"
77+
},
78+
"koshien.coordinate_of_y": {
79+
"snippet": "koshien.coordinate_of_y(${1:'0:0'})",
80+
"description": "(0:0) のy座標"
81+
}
82+
}

src/containers/ruby-tab/snippets-completer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import MicrobitSnippets from './microbit-snippets.json';
1919
import MeshSnippets from './mesh-snippets.json';
2020
import SmalrubotS1Snippets from './smalrubot-s1-snippets.json';
2121
import MicrobitMoreSnippets from './microbit-more-snippets.json';
22+
import KoshienSnippets from './koshien-snippets.json';
2223

2324
class SnippetsCompleter extends BaseCompleter {
2425
#completions = [];
@@ -45,7 +46,8 @@ class SnippetsCompleter extends BaseCompleter {
4546
MicrobitSnippets,
4647
MeshSnippets,
4748
SmalrubotS1Snippets,
48-
MicrobitMoreSnippets
49+
MicrobitMoreSnippets,
50+
KoshienSnippets
4951
];
5052
snippetsList.forEach(snippets => {
5153
for (const [caption, item] of Object.entries(snippets)) {

src/lib/libraries/extensions/index.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,7 @@ const extensions = [
403403
import microbitMore from './microbitMore/index.jsx';
404404
extensions.push(microbitMore);
405405

406+
import koshien from './koshien/index.jsx';
407+
extensions.push(koshien);
408+
406409
export default extensions;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let formatMessage = messageData => messageData.defaultMessage;
2+
3+
/**
4+
* Koshien extension
5+
*/
6+
7+
import koshienIconURL from './koshien.png';
8+
import koshienInsetIconURL from './koshien-small.png';
9+
import translations from './translations.json';
10+
11+
const entry = {
12+
get name () {
13+
return formatMessage({
14+
defaultMessage: 'Smalruby Koshien',
15+
description: "Name for the 'koshien' extension",
16+
id: 'koshien.entry.name'
17+
});
18+
},
19+
extensionId: 'koshien',
20+
iconURL: koshienIconURL,
21+
insetIconURL: koshienInsetIconURL,
22+
get description () {
23+
return formatMessage({
24+
defaultMessage: 'Smalruby Koshien AI.',
25+
description: "Description for the 'koshien' extension",
26+
id: 'koshien.entry.description'
27+
});
28+
},
29+
featured: true,
30+
disabled: false,
31+
bluetoothRequired: false,
32+
internetConnectionRequired: false,
33+
launchPeripheralConnectionFlow: false,
34+
useAutoScan: false,
35+
helpLink: 'https://smalruby-koshien.netlab.jp/',
36+
setFormatMessage: formatter => {
37+
formatMessage = formatter;
38+
},
39+
translationMap: translations
40+
};
41+
42+
export {entry}; // loadable-extension needs this line.
43+
export default entry;
4.99 KB
Loading
238 KB
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"en": {
3+
"koshien.entry.name": "Smalruby Koshien",
4+
"koshien.entry.description": "Smalruby Koshien AI."
5+
},
6+
"ja": {
7+
"koshien.entry.name": "スモウルビー甲子園",
8+
"koshien.entry.description": "スモウルビー甲子園のAIを作成する。"
9+
},
10+
"ja-Hira": {
11+
"koshien.entry.name": "スモウルビー こうしえん",
12+
"koshien.entry.description": "スモウルビー こうしえんのエー・アイをさくせいする。"
13+
}
14+
}

src/lib/ruby-generator/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import WeDo2Blocks from './wedo2.js';
3030
import GdxForBlocks from './gdx_for.js';
3131
import MeshBlocks from './mesh.js';
3232
import SmalrubotS1Blocks from './smalrubot_s1.js';
33+
import KoshienBlocks from './koshien.js';
3334

3435
const SCALAR_TYPE = '';
3536
const LIST_TYPE = 'list';
@@ -427,6 +428,29 @@ RubyGenerator.listName = function (id) {
427428
return this.variableName(id, LIST_TYPE);
428429
};
429430

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+
430454
RubyGenerator.getScripts = function () {
431455
return Generator.prototype.getScripts.call(this).sort((a, b) => {
432456
const aValue = (this.getBlock(a).opcode === 'procedures_definition' ? 1 : -1);
@@ -462,5 +486,6 @@ WeDo2Blocks(RubyGenerator);
462486
GdxForBlocks(RubyGenerator);
463487
MeshBlocks(RubyGenerator);
464488
SmalrubotS1Blocks(RubyGenerator);
489+
KoshienBlocks(RubyGenerator);
465490

466491
export default RubyGenerator;

src/lib/ruby-generator/koshien.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Define Ruby code generator for Microbit More Blocks
3+
* @param {RubyGenerator} Generator The RubyGenerator
4+
* @return {RubyGenerator} same as param.
5+
*/
6+
export default function (Generator) {
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) {
25+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
26+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
27+
return `koshien.move_to(${x}, ${y})\n`;
28+
};
29+
30+
Generator.koshien_calcRoute = function (block) {
31+
const srcX = Generator.valueToCode(block, 'SRC_X', Generator.ORDER_NONE) || 0;
32+
const srcY = Generator.valueToCode(block, 'SRC_Y', Generator.ORDER_NONE) || 0;
33+
const dstX = Generator.valueToCode(block, 'DST_X', Generator.ORDER_NONE) || 0;
34+
const dstY = Generator.valueToCode(block, 'DST_Y', 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+
);
41+
42+
// eslint-disable-next-line max-len
43+
return `koshien.calc_route(src: [${srcX}, ${srcY}], dst: [${dstX}, ${dstY}], except_cells: ${exceptCells}, result: ${result})\n`;
44+
};
45+
46+
Generator.koshien_setItem = function (block) {
47+
const item = Generator.getFieldValue(block, 'ITEM') || null;
48+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
49+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
50+
return `koshien.set_${item}(${x}, ${y})\n`;
51+
};
52+
53+
Generator.koshien_loadMap = function (block) {
54+
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
55+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
56+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
57+
return [`koshien.load_map(${location}, ${x}, ${y})`];
58+
};
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`;
63+
};
64+
65+
Generator.koshien_locateObjects = function (block) {
66+
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
67+
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
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+
);
73+
74+
// eslint-disable-next-line max-len
75+
return `koshien.locate_objects(sq_size: ${sqSize}, cent: [${x}, ${y}], objects: ${objects}, result: ${result})\n`;
76+
};
77+
Generator.koshien_targetCoordinate = function (block) {
78+
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
79+
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
80+
return [`koshien.${target}_${coordinate}`];
81+
};
82+
Generator.koshien_turnOver = function () {
83+
return `koshien.turn_over\n`;
84+
};
85+
Generator.koshien_coordinateOf = function (block) {
86+
const where = Generator.valueToCode(block, 'WHERE', Generator.ORDER_NONE) || Generator.quote_('0:0');
87+
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
88+
return [`koshien.coordinate_of_${coordinate}(${where})`];
89+
};
90+
91+
return Generator;
92+
}

0 commit comments

Comments
 (0)