Skip to content

Commit

Permalink
Merge pull request #355 from smalruby/speak-mentaiko-kousien
Browse files Browse the repository at this point in the history
Speak mentaiko kousien
  • Loading branch information
takaokouji authored Aug 19, 2024
2 parents bfb9562 + 18246c4 commit 57f3193
Show file tree
Hide file tree
Showing 13 changed files with 612 additions and 3 deletions.
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.

82 changes: 82 additions & 0 deletions src/containers/ruby-tab/koshien-snippets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"koshien.connect_game": {
"snippet": "koshien.connect_game(name: ${1:\"player1\"})",
"description": "プレイヤー名を (player1) にして、ゲームサーバへ接続する"
},
"koshien.move_to": {
"snippet": "koshien.move_to(${1:0}, ${2:0})",
"description": "x座標 (0)、y座標 (0) に移動する"
},
"koshien.get_map_area": {
"snippet": "koshien.get_map_area(${1:0}, ${2:0})",
"description": "x座標が (0) 、y座標が (0) 付近のマップ情報を取得する"
},
"koshien.map": {
"snippet": "koshien.map(${1:0}, ${2:0})",
"description": "x座標が (0) 、y座標が (0) のマップ情報"
},
"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) 通らない座標 リスト (通らない座標) をリスト (最短経路) に保存する"
},
"koshien.set_dynamite": {
"snippet": "koshien.set_dynamite([${1:0},${2:0}])",
"description": "ダイナマイトをx座標 (0) y座標 (0) に置く"
},
"koshien.set_bomb": {
"snippet": "koshien.set_bomb([${1:0},${2:0}])",
"description": "爆弾をx座標 (0) y座標 (0) に置く"
},
"koshien.save_map_all": {
"snippet": "koshien.save_map_all(${1:\"map1\"})",
"description": "すべてのマップ情報を (map1) に保存する"
},
"koshien.load_map": {
"snippet": "koshien.load_map(${1:\"map1\"}, ${2:0}, ${3:0})",
"description": "x座標が (0) 、y座標が (0) のマップ情報を (map1) から読み込む"
},
"koshien.other_player_x": {
"snippet": "koshien.other_player_x",
"description": "対戦キャラクタの x座標"
},
"koshien.other_player_y": {
"snippet": "koshien.other_player_y",
"description": "対戦キャラクタの y座標"
},
"koshien.player_x": {
"snippet": "koshien.player_x",
"description": "プレイヤーの x座標"
},
"koshien.player_y": {
"snippet": "koshien.player_y",
"description": "プレイヤーの y座標"
},
"koshien.enemy_x": {
"snippet": "koshien.enemy_x",
"description": "妨害キャラクタの x座標"
},
"koshien.enemy_y": {
"snippet": "koshien.enemy_y",
"description": "妨害キャラクタの y座標"
},
"koshien.goal_x": {
"snippet": "koshien.goal_x",
"description": "ゴールの x座標"
},
"koshien.goal_y": {
"snippet": "koshien.goal_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.coordinate_of_y": {
"snippet": "koshien.coordinate_of_y(${1:'0:0'})",
"description": "(0:0) のy座標"
}
}
4 changes: 3 additions & 1 deletion src/containers/ruby-tab/snippets-completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import MicrobitSnippets from './microbit-snippets.json';
import MeshSnippets from './mesh-snippets.json';
import SmalrubotS1Snippets from './smalrubot-s1-snippets.json';
import MicrobitMoreSnippets from './microbit-more-snippets.json';
import KoshienSnippets from './koshien-snippets.json';

class SnippetsCompleter extends BaseCompleter {
#completions = [];
Expand All @@ -45,7 +46,8 @@ class SnippetsCompleter extends BaseCompleter {
MicrobitSnippets,
MeshSnippets,
SmalrubotS1Snippets,
MicrobitMoreSnippets
MicrobitMoreSnippets,
KoshienSnippets
];
snippetsList.forEach(snippets => {
for (const [caption, item] of Object.entries(snippets)) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/libraries/extensions/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,7 @@ const extensions = [
import microbitMore from './microbitMore/index.jsx';
extensions.push(microbitMore);

import koshien from './koshien/index.jsx';
extensions.push(koshien);

export default extensions;
43 changes: 43 additions & 0 deletions src/lib/libraries/extensions/koshien/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
let formatMessage = messageData => messageData.defaultMessage;

/**
* Koshien extension
*/

import koshienIconURL from './koshien.png';
import koshienInsetIconURL from './koshien-small.png';
import translations from './translations.json';

const entry = {
get name () {
return formatMessage({
defaultMessage: 'Smalruby Koshien',
description: "Name for the 'koshien' extension",
id: 'koshien.entry.name'
});
},
extensionId: 'koshien',
iconURL: koshienIconURL,
insetIconURL: koshienInsetIconURL,
get description () {
return formatMessage({
defaultMessage: 'Smalruby Koshien AI.',
description: "Description for the 'koshien' extension",
id: 'koshien.entry.description'
});
},
featured: true,
disabled: false,
bluetoothRequired: false,
internetConnectionRequired: false,
launchPeripheralConnectionFlow: false,
useAutoScan: false,
helpLink: 'https://smalruby-koshien.netlab.jp/',
setFormatMessage: formatter => {
formatMessage = formatter;
},
translationMap: translations
};

export {entry}; // loadable-extension needs this line.
export default entry;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/libraries/extensions/koshien/koshien.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/lib/libraries/extensions/koshien/translations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"en": {
"koshien.entry.name": "Smalruby Koshien",
"koshien.entry.description": "Smalruby Koshien AI."
},
"ja": {
"koshien.entry.name": "スモウルビー甲子園",
"koshien.entry.description": "スモウルビー甲子園のAIを作成する。"
},
"ja-Hira": {
"koshien.entry.name": "スモウルビー こうしえん",
"koshien.entry.description": "スモウルビー こうしえんのエー・アイをさくせいする。"
}
}
25 changes: 25 additions & 0 deletions src/lib/ruby-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import WeDo2Blocks from './wedo2.js';
import GdxForBlocks from './gdx_for.js';
import MeshBlocks from './mesh.js';
import SmalrubotS1Blocks from './smalrubot_s1.js';
import KoshienBlocks from './koshien.js';

const SCALAR_TYPE = '';
const LIST_TYPE = 'list';
Expand Down Expand Up @@ -427,6 +428,29 @@ RubyGenerator.listName = function (id) {
return this.variableName(id, LIST_TYPE);
};

RubyGenerator.variableNameByName = function (name, type = SCALAR_TYPE) {
let currVar;
let isStage;
const target = this.currentTarget;
if (target.runtime) {
const stage = target.runtime.getTargetForStage();
currVar = stage.lookupVariableByNameAndType(name, type);
isStage = true;
}
if (!currVar) {
currVar = target.lookupVariableByNameAndType(name, type);
isStage = target.isStage;
}
if (currVar && currVar.type === type) {
return this.makeVariableName(isStage, currVar.name);
}
return null;
};

RubyGenerator.listNameByName = function (name) {
return this.variableNameByName(name, LIST_TYPE);
};

RubyGenerator.getScripts = function () {
return Generator.prototype.getScripts.call(this).sort((a, b) => {
const aValue = (this.getBlock(a).opcode === 'procedures_definition' ? 1 : -1);
Expand Down Expand Up @@ -462,5 +486,6 @@ WeDo2Blocks(RubyGenerator);
GdxForBlocks(RubyGenerator);
MeshBlocks(RubyGenerator);
SmalrubotS1Blocks(RubyGenerator);
KoshienBlocks(RubyGenerator);

export default RubyGenerator;
92 changes: 92 additions & 0 deletions src/lib/ruby-generator/koshien.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Define Ruby code generator for Microbit More Blocks
* @param {RubyGenerator} Generator The RubyGenerator
* @return {RubyGenerator} same as param.
*/
export default function (Generator) {
Generator.koshien_connectGame = function (block) {
const name = Generator.valueToCode(block, 'NAME', Generator.ORDER_NONE) || Generator.quote_('player1');
return `koshien.connect_game(name: ${name})\n`;
};

Generator.koshien_getMapArea = function (block) {
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
return `koshien.get_map_area(${x}, ${y})\n`;
};

Generator.koshien_map = function (block) {
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
return [`koshien.map(${x}, ${y})`];
};

Generator.koshien_moveTo = function (block) {
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
return `koshien.move_to(${x}, ${y})\n`;
};

Generator.koshien_calcRoute = function (block) {
const srcX = Generator.valueToCode(block, 'SRC_X', Generator.ORDER_NONE) || 0;
const srcY = Generator.valueToCode(block, 'SRC_Y', Generator.ORDER_NONE) || 0;
const dstX = Generator.valueToCode(block, 'DST_X', Generator.ORDER_NONE) || 0;
const dstY = Generator.valueToCode(block, 'DST_Y', Generator.ORDER_NONE) || 0;
const exceptCells = Generator.quote_(
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE) || ' '
);
const result = Generator.quote_(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
);

// eslint-disable-next-line max-len
return `koshien.calc_route(src: [${srcX}, ${srcY}], dst: [${dstX}, ${dstY}], except_cells: ${exceptCells}, result: ${result})\n`;
};

Generator.koshien_setItem = function (block) {
const item = Generator.getFieldValue(block, 'ITEM') || null;
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
return `koshien.set_${item}(${x}, ${y})\n`;
};

Generator.koshien_loadMap = function (block) {
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
return [`koshien.load_map(${location}, ${x}, ${y})`];
};

Generator.koshien_saveMapAll = function (block) {
const location = Generator.valueToCode(block, 'LOCATION', Generator.ORDER_NONE) || Generator.quote_('map1');
return `koshien.save_map_all(${location})\n`;
};

Generator.koshien_locateObjects = function (block) {
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 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 result = Generator.quote_(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE) || ' '
);

// eslint-disable-next-line max-len
return `koshien.locate_objects(sq_size: ${sqSize}, cent: [${x}, ${y}], objects: ${objects}, result: ${result})\n`;
};
Generator.koshien_targetCoordinate = function (block) {
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
return [`koshien.${target}_${coordinate}`];
};
Generator.koshien_turnOver = function () {
return `koshien.turn_over\n`;
};
Generator.koshien_coordinateOf = function (block) {
const where = Generator.valueToCode(block, 'WHERE', Generator.ORDER_NONE) || Generator.quote_('0:0');
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || null;
return [`koshien.coordinate_of_${coordinate}(${where})`];
};

return Generator;
}
4 changes: 3 additions & 1 deletion src/lib/ruby-to-blocks-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import TranslateConverter from './translate';
import MakeyMakeyConverter from './makeymakey';
import VideoConverter from './video';
import Text2SpeechConverter from './text2speech';
import KoshienConverter from './koshien';

const messages = defineMessages({
couldNotConvertPrimitive: {
Expand Down Expand Up @@ -113,7 +114,8 @@ class RubyToBlocksConverter {
Text2SpeechConverter,
Wedo2Converter,
MicrobitMoreConverter,
MeshConverter
MeshConverter,
KoshienConverter
].forEach(x => x.register(this));
}

Expand Down
Loading

0 comments on commit 57f3193

Please sign in to comment.