Spaces:
Build error
Build error
File size: 5,871 Bytes
72c8f1c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');
const Cast = require('../../util/cast');
const log = require('../../util/log');
const formatMessage = require('format-message');
const Message = {
open_ml2scratch: {
'ja': 'ML2Scratchのページを開く',
'ja-Hira': 'ML2Scratchのページをひらく',
'en': 'Open ML2Scratch page',
'zh-cn': '打开ML2Scratch页面'
},
set_wss_url: {
'ja': '接続先を[WSS_URL]にする',
'ja-Hira': 'せつぞくさきを[WSS_URL]にする',
'en': 'Connect to [WSS_URL]',
'zh-cn': 'Connect to [WSS_URL]'
},
connect_block: {
'ja': 'ID:[CONN_ID]で接続する',
'ja-Hira': 'ID:[CONN_ID]でせつぞくする',
'en': 'Connect with ID:[CONN_ID]',
'zh-cn': '用ID:[CONN_ID]连接'
},
when_received_block: {
'ja': '分類[CLASS_INDEX]を受け取ったとき',
'ja-Hira': 'ぶんるい[CLASS_INDEX]をうけとったとき',
'en': 'when received class index:[CLASS_INDEX]',
'zh-cn': '接收到类别[CLASS_INDEX]时'
},
class_index_block: {
'ja': '分類',
'ja-Hira': 'ぶんるい',
'en': 'class index',
'zh-cn': '类索引'
},
label_block: {
'ja': 'ラベル',
'ja-Hira': 'ラベル',
'en': 'label',
'zh-cn': '标签'
},
any_class_index: {
'ja': 'どれか',
'ja-Hira': 'どれか',
'en': 'any',
'zh-cn': '任何'
},
blank_id_error: {
'ja': 'IDを入力してください。',
'ja-Hira': 'IDをにゅうりょくしてください。',
'en': 'Blank ID is invalid.',
'zh-cn': '空白ID无效。'
}
}
const AvailableLocales = ['en', 'ja', 'ja-Hira', 'zh-cn'];
class Scratch3ML2ScratchBlocks {
constructor (runtime) {
this.runtime = runtime;
this._wss_url = "wss://ml2scratch-helper.glitch.me";
this._ws = null;
this._when_received = false;
this._when_received_arr = Array(10).fill(false);
this._class_index = null;
this._label = null;
this._locale = this.setLocale();
this._conn_id = Math.floor(Math.random(100000000) * 100000000);
}
getInfo() {
this._locale = this.setLocale();
return {
id: 'ml2scratch',
name: 'ML2Scratch',
blocks: [
{
opcode: 'openMl2scratch',
blockType: BlockType.COMMAND,
text: Message.open_ml2scratch[this._locale]
},
{
opcode: 'setWssUrl',
blockType: BlockType.COMMAND,
text: Message.set_wss_url[this._locale],
arguments: {
WSS_URL: {
type: ArgumentType.STRING,
defaultValue: this._wss_url
}
}
},
{
opcode: 'connect',
blockType: BlockType.COMMAND,
text: Message.connect_block[this._locale],
arguments: {
CONN_ID: {
type: ArgumentType.STRING,
defaultValue: this._conn_id
}
}
},
{
opcode: 'whenReceived',
text: Message.when_received_block[this._locale],
blockType: BlockType.HAT,
arguments: {
CLASS_INDEX: {
type: ArgumentType.STRING,
menu: 'class_indexes',
defaultValue: '0'
}
}
},
{
opcode: 'getClassIndex',
text: Message.class_index_block[this._locale],
blockType: BlockType.REPORTER
},
{
opcode: 'getLabel',
text: Message.label_block[this._locale],
blockType: BlockType.REPORTER
}
],
menus: {
class_indexes: this.getIndexMenu()
}
};
}
openMl2scratch() {
window.open('https://champierre.github.io/ml2scratch/?conn_id=' + this._conn_id, '_blank');
this.connectToWSS(this._conn_id);
}
setWssUrl(args) {
this._wss_url = args.WSS_URL;
}
connect(args) {
this._conn_id = args.CONN_ID;
if (this._conn_id.length == 0) {
alert(Message.blank_id_error[this._locale]);
return;
}
this.connectToWSS(this._conn_id);
}
connectToWSS(conn_id) {
this._ws = new WebSocket(this._wss_url + '/scratchx');
this._ws.onmessage = (evt) => {
let data = JSON.parse(evt.data);
if (data.action == 'predict') {
log.log(data);
this._class_index = data.value;
this._label = data.label;
this._when_received = true;
this._when_received_arr[data.value] = true
}
}
this._ws.onopen = (evt) => {
this._ws.send(JSON.stringify({action: 'connect', conn_id: conn_id}));
}
}
whenReceived(args) {
if (args.CLASS_INDEX === "any") {
if (this._when_received) {
this._when_received = false;
return true;
}
return false;
} else {
if (this._when_received_arr[args.CLASS_INDEX]) {
this._when_received_arr[args.CLASS_INDEX] = false;
return true;
}
return false;
}
}
getClassIndex() {
return this._class_index;
}
getLabel() {
return this._label;
}
getIndexMenu() {
const arr = []
arr.push({text: Message.any_class_index[this._locale], value: 'any'});
for(let i = 0; i <= 9; i++) {
const obj = {};
obj.text = i.toString(10);
obj.value = i.toString(10);
arr.push(obj);
};
return arr;
}
setLocale() {
let locale = formatMessage.setup().locale;
if (AvailableLocales.includes(locale)) {
return locale;
} else {
return 'en';
}
}
}
module.exports = Scratch3ML2ScratchBlocks;
|