Skip to content

Commit 15e1aff

Browse files
committed
定数ブロック追加など
1 parent a9a3be7 commit 15e1aff

4 files changed

Lines changed: 139 additions & 18 deletions

File tree

packages/web/src/blocks.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,42 @@ Blockly.JavaScript[GET_PROPERTY_OF_FIGHTER] = (block: Blockly.Block) => [
136136
Blockly.JavaScript.ORDER_MEMBER,
137137
];
138138

139+
// 定数
140+
141+
export const CONSTANT = "constant";
142+
const constantNames = {
143+
armLength: "0",
144+
stageWidth: "1",
145+
stageHeight: "2",
146+
maxHp: "3",
147+
maxStamina: "4",
148+
weaponRange: "5",
149+
};
150+
const constants = [40, 800, 600, 100, 100, 400];
151+
const CONSTANT_NAME = "constantName";
152+
Blockly.Blocks[CONSTANT] = {
153+
init(this: Blockly.Block) {
154+
this.appendDummyInput().appendField(
155+
new Blockly.FieldDropdown([
156+
["腕の長さ(40)", constantNames.armLength],
157+
["ステージの横の長さ(800)", constantNames.stageWidth],
158+
["ステージの縦の長さ(600)", constantNames.stageHeight],
159+
["最大HP(100)", constantNames.maxHp],
160+
["最大元気(100)", constantNames.maxStamina],
161+
["武器の射程(400)", constantNames.weaponRange],
162+
]),
163+
CONSTANT_NAME
164+
);
165+
this.setOutput(true, Number);
166+
this.setColour(20);
167+
this.setTooltip("ルールで決められている定数です。");
168+
},
169+
};
170+
Blockly.JavaScript[CONSTANT] = (block: Blockly.Block) => [
171+
`${constants[block.getFieldValue(CONSTANT_NAME)]}`,
172+
Blockly.JavaScript.ORDER_ATOMIC,
173+
];
174+
139175
// 意思決定
140176

141177
const TARGET = "target";
@@ -329,11 +365,58 @@ Blockly.JavaScript[CLOSEST_WEAPON] = () => [
329365
];
330366

331367
// 日本語訳がおかしい物の修正など
332-
// while
368+
// if
333369

334370
const STATEMENT = "statement";
335371
const CONDITION = "condition";
336372

373+
export const CUSTOM_IF = "custom_if";
374+
Blockly.Blocks[CUSTOM_IF] = {
375+
init(this: Blockly.Block) {
376+
this.appendValueInput(CONDITION).setCheck(Boolean).appendField("もし");
377+
this.appendDummyInput().appendField("なら");
378+
this.appendStatementInput(STATEMENT);
379+
this.setPreviousStatement(true);
380+
this.setNextStatement(true);
381+
this.setColour(210);
382+
this.setTooltip("条件が満たされる場合、内部の文を実行します。");
383+
},
384+
};
385+
Blockly.JavaScript[CUSTOM_IF] = (block: Blockly.Block) =>
386+
`if(${Blockly.JavaScript.valueToCode(
387+
block,
388+
CONDITION,
389+
Blockly.JavaScript.ORDER_NONE
390+
)}){${Blockly.JavaScript.statementToCode(block, STATEMENT)}};`;
391+
392+
const STATEMENT2 = "statement2";
393+
export const CUSTOM_IFELSE = "custom_ifelse";
394+
Blockly.Blocks[CUSTOM_IFELSE] = {
395+
init(this: Blockly.Block) {
396+
this.appendValueInput(CONDITION).setCheck(Boolean).appendField("もし");
397+
this.appendDummyInput().appendField("なら");
398+
this.appendStatementInput(STATEMENT);
399+
this.appendDummyInput().appendField("そうでなければ");
400+
this.appendStatementInput(STATEMENT2);
401+
this.setPreviousStatement(true);
402+
this.setNextStatement(true);
403+
this.setColour(210);
404+
this.setTooltip(
405+
"条件が満たされる場合は1番目の文を、満たされない場合は2番目の文を実行します。"
406+
);
407+
},
408+
};
409+
Blockly.JavaScript[CUSTOM_IFELSE] = (block: Blockly.Block) =>
410+
`if(${Blockly.JavaScript.valueToCode(
411+
block,
412+
CONDITION,
413+
Blockly.JavaScript.ORDER_NONE
414+
)}){${Blockly.JavaScript.statementToCode(block, STATEMENT)}}else{
415+
${Blockly.JavaScript.statementToCode(block, STATEMENT2)}
416+
};`;
417+
418+
// while
419+
337420
export const CUSTOM_WHILE = "custom_while";
338421
Blockly.Blocks[CUSTOM_WHILE] = {
339422
init(this: Blockly.Block) {

packages/web/src/component/Arena.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,26 @@ export default function Arena(props: ArenaProps) {
130130
const handleChange = async (_: unknown, expanded: boolean) => {
131131
if (expanded) {
132132
setCurrentUser(await getUser(currentUser.id));
133-
setNumberOfUsers(users.length);
133+
setNumberOfUsers(users.filter((user) => user.program).length);
134134
setLoaded(true);
135135
} else setLoaded(false);
136136
};
137137

138138
const handleUpload = () => {
139+
const newProgram = Blockly.JavaScript.workspaceToCode(workspaceRef.current);
139140
uploadProgram(
140141
{
141142
userId: currentUser.id,
142-
program: Blockly.JavaScript.workspaceToCode(workspaceRef.current),
143+
program: newProgram,
143144
},
144145
password
145146
);
147+
setCurrentUser({
148+
name: currentUser.name,
149+
id: currentUser.id,
150+
program: newProgram,
151+
rank: currentUser.rank,
152+
});
146153
};
147154

148155
const handleClickOpen = () => {
@@ -151,6 +158,14 @@ export default function Arena(props: ArenaProps) {
151158
setOpen(true);
152159
};
153160

161+
const calculateRank = (rank: number) => {
162+
let ret = 0;
163+
for (const user of users) {
164+
if (user.program && rank >= user.rank) ret += 1;
165+
}
166+
return ret;
167+
};
168+
154169
const accordionRef = useRef<HTMLDivElement>(null);
155170

156171
return (
@@ -197,7 +212,7 @@ export default function Arena(props: ArenaProps) {
197212
width: 1 / 5,
198213
}}
199214
>
200-
{currentUser.rank ? currentUser.rank : "-"}
215+
{currentUser.rank ? calculateRank(currentUser.rank) : "-"}
201216
</Typography>
202217
<Typography
203218
color="text.secondary"

packages/web/src/component/TestPlay.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,14 @@ export default function TestPlay(props: TestPlayProps) {
377377
onClick={() => {
378378
setExecutionId((previous) => previous + 1);
379379
setIsActive(false);
380+
setCurrentUser({
381+
id: currentUser.id,
382+
name: currentUser.name,
383+
program: Blockly.JavaScript.workspaceToCode(
384+
workspaceRef.current
385+
),
386+
rank: currentUser.rank,
387+
});
380388
}}
381389
startIcon={<RestartAlt />}
382390
>

packages/web/src/options.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import {
2121
CUSTOM_LISTS_INSERT_INDEX,
2222
CUSTOM_LISTS_DELETE_INDEX,
2323
CONSOLE_LOG,
24+
CONSTANT,
25+
CUSTOM_IF,
26+
CUSTOM_IFELSE,
2427
} from "./blocks";
2528

2629
const numberInput = (initialInput: number) => {
@@ -44,6 +47,14 @@ const options: any = {
4447
kind: "category",
4548
name: "論理",
4649
contents: [
50+
{
51+
kind: "block",
52+
type: CUSTOM_IF,
53+
},
54+
{
55+
kind: "block",
56+
type: CUSTOM_IFELSE,
57+
},
4758
{
4859
kind: "block",
4960
type: "controls_if",
@@ -66,6 +77,10 @@ const options: any = {
6677
kind: "category",
6778
name: "ループ",
6879
contents: [
80+
{
81+
kind: "block",
82+
type: "controls_forEach",
83+
},
6984
{
7085
kind: "block",
7186
type: "controls_repeat_ext",
@@ -86,10 +101,6 @@ const options: any = {
86101
BY: numberInput(1),
87102
},
88103
},
89-
{
90-
kind: "block",
91-
type: "controls_forEach",
92-
},
93104
{
94105
kind: "block",
95106
type: "controls_flow_statements",
@@ -104,6 +115,10 @@ const options: any = {
104115
kind: "block",
105116
type: "math_number",
106117
},
118+
{
119+
kind: "block",
120+
type: DISTANCE,
121+
},
107122
{
108123
kind: "block",
109124
type: "math_arithmetic",
@@ -136,10 +151,6 @@ const options: any = {
136151
Y: numberInput(0),
137152
},
138153
},
139-
{
140-
kind: "block",
141-
type: DISTANCE,
142-
},
143154
{
144155
kind: "block",
145156
type: "math_single",
@@ -178,31 +189,35 @@ const options: any = {
178189
},
179190
{
180191
kind: "block",
181-
type: ENEMIES,
192+
type: CLOSEST_ENEMY,
182193
},
183194
{
184195
kind: "block",
185-
type: PORTIONS,
196+
type: CLOSEST_PORTION,
186197
},
187198
{
188199
kind: "block",
189-
type: WEAPONS,
200+
type: CLOSEST_WEAPON,
190201
},
191202
{
192203
kind: "block",
193204
type: GET_PROPERTY_OF_FIGHTER,
194205
},
195206
{
196207
kind: "block",
197-
type: CLOSEST_ENEMY,
208+
type: CONSTANT,
198209
},
199210
{
200211
kind: "block",
201-
type: CLOSEST_PORTION,
212+
type: ENEMIES,
202213
},
203214
{
204215
kind: "block",
205-
type: CLOSEST_WEAPON,
216+
type: PORTIONS,
217+
},
218+
{
219+
kind: "block",
220+
type: WEAPONS,
206221
},
207222
],
208223
},

0 commit comments

Comments
 (0)