Skip to content

Commit 85843ba

Browse files
authored
Merge pull request #86 from ut-code/add-constants
定数ブロック追加など
2 parents 787ae33 + 6d9dbdb commit 85843ba

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
@@ -140,6 +140,42 @@ Blockly.JavaScript[GET_PROPERTY_OF_FIGHTER] = (block: Blockly.Block) => [
140140
Blockly.JavaScript.ORDER_CONDITIONAL,
141141
];
142142

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

145181
const TARGET = "target";
@@ -333,11 +369,58 @@ Blockly.JavaScript[CLOSEST_WEAPON] = () => [
333369
];
334370

335371
// 日本語訳がおかしい物の修正など
336-
// while
372+
// if
337373

338374
const STATEMENT = "statement";
339375
const CONDITION = "condition";
340376

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

packages/web/src/component/Arena.tsx

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

139139
const handleUpload = () => {
140+
const newProgram = Blockly.JavaScript.workspaceToCode(workspaceRef.current);
140141
uploadProgram(
141142
{
142143
userId: currentUser.id,
143-
program: Blockly.JavaScript.workspaceToCode(workspaceRef.current),
144+
program: newProgram,
144145
},
145146
password
146147
);
148+
setCurrentUser({
149+
name: currentUser.name,
150+
id: currentUser.id,
151+
program: newProgram,
152+
rank: currentUser.rank,
153+
});
147154
setUploaded(true);
148155
};
149156

@@ -153,6 +160,14 @@ export default function Arena(props: ArenaProps) {
153160
setOpen(true);
154161
};
155162

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

158173
return (
@@ -199,7 +214,7 @@ export default function Arena(props: ArenaProps) {
199214
width: 1 / 5,
200215
}}
201216
>
202-
{currentUser.rank ? currentUser.rank : "-"}
217+
{currentUser.rank ? calculateRank(currentUser.rank) : "-"}
203218
</Typography>
204219
<Typography
205220
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)