Skip to content

Commit a51671c

Browse files
committed
fix Score operators + BinaryScore generate
1 parent 020ff81 commit a51671c

4 files changed

Lines changed: 79 additions & 45 deletions

File tree

example/score_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ void main(List<String> args) {
88
(Bossbar("test") << s1) +
99
Bossbar("test").get(BossbarOption.value);
1010

11-
print(t);
11+
//print(t);
12+
print(s1 + (s2 + s1));
1213

1314
final (_, ops) = t.copy();
14-
print(getCommands(For.of(ops)).join('\n'));
15+
//print(getCommands(For.of(ops)).join('\n'));
16+
print(getCommands(s1 + s2 + s1).join('\n')); //TODO: Caveat s1 + s2 != s2 + s1
1517
}

lib/src/basic/score/score.dart

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ abstract mixin class ScoreStoreable {
1616
// block, bossbar, entity, score, storage
1717
String get_assignable_right();
1818

19-
ScoreOperation asScore() => StoreScoreOperation(Score.tmp(), this);
20-
21-
BinaryScoreOperation operator +(dynamic other) => asScore() + other;
22-
BinaryScoreOperation operator -(dynamic other) => asScore() - other;
23-
BinaryScoreOperation operator %(dynamic other) => asScore() % other;
24-
BinaryScoreOperation operator /(dynamic other) => asScore() / other;
25-
BinaryScoreOperation operator *(dynamic other) => asScore() * other;
26-
ScoreCondition operator >(dynamic other) => asScore() > other;
27-
ScoreCondition operator >=(dynamic other) => asScore() >= other;
28-
ScoreCondition operator <=(dynamic other) => asScore() <= other;
29-
ScoreCondition operator <(dynamic other) => asScore() < other;
30-
ScoreCondition operator &(dynamic other) => asScore() & other;
19+
ScoreOperation toScore({Score? out}) => StoreScoreOperation(
20+
out ?? Score.tmp(),
21+
this,
22+
);
23+
24+
BinaryScoreOperation operator +(dynamic other) => toScore() + other;
25+
BinaryScoreOperation operator -(dynamic other) => toScore() - other;
26+
BinaryScoreOperation operator %(dynamic other) => toScore() % other;
27+
BinaryScoreOperation operator /(dynamic other) => toScore() / other;
28+
BinaryScoreOperation operator *(dynamic other) => toScore() * other;
29+
ScoreCondition operator >(dynamic other) => toScore() > other;
30+
ScoreCondition operator >=(dynamic other) => toScore() >= other;
31+
ScoreCondition operator <=(dynamic other) => toScore() <= other;
32+
ScoreCondition operator <(dynamic other) => toScore() < other;
33+
ScoreCondition operator &(dynamic other) => toScore() & other;
3134
}
3235

3336
abstract mixin class ScoreAssignable {
@@ -43,58 +46,59 @@ abstract mixin class ScoreAssignable {
4346

4447
sealed class ScoreOperation extends Widget implements ScoreStoreable {
4548
/// add
49+
@override
4650
BinaryScoreOperation operator +(dynamic other) => switch (other) {
4751
int val => add(val),
4852
ScoreOperation s => addScore(s),
49-
ScoreStoreable s => addScore(s.asScore()),
53+
ScoreStoreable s => addScore(s.toScore()),
5054
_ => throw ('Please use either a Score or an Int in the operator +')
5155
};
5256

5357
/// subtract
5458
BinaryScoreOperation operator -(dynamic other) => switch (other) {
5559
int val => subtract(val),
5660
ScoreOperation s => subtractScore(s),
57-
ScoreStoreable s => subtractScore(s.asScore()),
61+
ScoreStoreable s => subtractScore(s.toScore()),
5862
_ => throw ('Please use either a Score or an Int in the operator -')
5963
};
6064

6165
/// modulo by
6266
BinaryScoreOperation operator %(dynamic other) => switch (other) {
6367
int val => modulo(Score.con(val)),
6468
ScoreOperation s => modulo(s),
65-
ScoreStoreable s => modulo(s.asScore()),
69+
ScoreStoreable s => modulo(s.toScore()),
6670
_ => throw ('Please use either a Score or an Int in the operator %')
6771
};
6872

6973
/// divide by
7074
BinaryScoreOperation operator /(dynamic other) => switch (other) {
7175
int val => divideByScore(Score.con(val)),
7276
ScoreOperation s => divideByScore(s),
73-
ScoreStoreable s => divideByScore(s.asScore()),
77+
ScoreStoreable s => divideByScore(s.toScore()),
7478
_ => throw ('Please use either a Score or an Int in the operator /')
7579
};
7680

7781
/// multiply by
7882
BinaryScoreOperation operator *(dynamic other) => switch (other) {
7983
int val => multiplyByScore(Score.con(val)),
8084
ScoreOperation s => multiplyByScore(s),
81-
ScoreStoreable s => multiplyByScore(s.asScore()),
85+
ScoreStoreable s => multiplyByScore(s.toScore()),
8286
_ => throw ('Please use either a Score or an Int in the operator *')
8387
};
8488

8589
/// greater than
8690
ScoreCondition operator >(dynamic other) => switch (other) {
8791
int val => matchesRange(Range.from(val + 1)),
8892
ScoreOperation s => isBigger(s),
89-
ScoreStoreable s => isBigger(s.asScore()),
93+
ScoreStoreable s => isBigger(s.toScore()),
9094
_ => throw ('Please use either a Score or an Int in the operator >')
9195
};
9296

9397
/// less than
9498
ScoreCondition operator <(dynamic other) => switch (other) {
9599
int val => matchesRange(Range.to(val - 1)),
96100
ScoreOperation s => isSmaller(s),
97-
ScoreStoreable s => isSmaller(s.asScore()),
101+
ScoreStoreable s => isSmaller(s.toScore()),
98102
_ => throw ('Please use either a Score or an Int in the operator <')
99103
};
100104

@@ -103,7 +107,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
103107
int val => matchesRange(Range.from(val)),
104108
ScoreOperation s => isBiggerOrEqual(s),
105109
ScoreStoreable s => isBiggerOrEqual(
106-
s.asScore(),
110+
s.toScore(),
107111
),
108112
_ => throw ('Please use either a Score or an Int in the operator >=')
109113
};
@@ -113,7 +117,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
113117
int val => matchesRange(Range.to(val)),
114118
ScoreOperation s => isSmallerOrEqual(s),
115119
ScoreStoreable s => isSmallerOrEqual(
116-
s.asScore(),
120+
s.toScore(),
117121
),
118122
_ => throw ('Please use either a Score or an Int in the operator <=')
119123
};
@@ -124,7 +128,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
124128
Range r => matchesRange(r),
125129
ScoreOperation s => isEqual(s),
126130
ScoreStoreable s => isEqual(
127-
s.asScore(),
131+
s.toScore(),
128132
),
129133
_ =>
130134
throw ('Please use either a Score, Range or an Int in the operator &')
@@ -261,7 +265,8 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
261265
}
262266

263267
@override
264-
ScoreOperation asScore() => this;
268+
ScoreOperation toScore({Score? out}) =>
269+
out == null ? this : BinaryScoreOperation.assign(out, this);
265270
}
266271

267272
sealed class ElementaryScoreOperation extends ScoreOperation {}
@@ -302,7 +307,10 @@ final class ResetScoreOperation extends ElementaryScoreOperation {
302307
ResetScoreOperation(this.score);
303308

304309
@override
305-
generate(Context context) => Command('scoreboard players reset $score');
310+
generate(Context context) => For.of([
311+
score,
312+
Command('scoreboard players reset $score'),
313+
]);
306314

307315
@override
308316
String toString() => 'reset $score';
@@ -345,11 +353,11 @@ final class ElementaryBinaryScoreOperation extends ElementaryScoreOperation {
345353

346354
final ScoreOperator operation;
347355

348-
ElementaryBinaryScoreOperation({
349-
required this.left,
350-
required this.right,
351-
required this.operation,
352-
});
356+
ElementaryBinaryScoreOperation(
357+
this.left,
358+
this.operation,
359+
this.right,
360+
);
353361

354362
ElementaryBinaryScoreOperation.assign(this.left, this.right)
355363
: operation = ScoreOperator.Assign;
@@ -390,12 +398,41 @@ class BinaryScoreOperation extends ScoreOperation {
390398
BinaryScoreOperation(this.left, this.operation, this.right);
391399

392400
BinaryScoreOperation.assign(this.left, this.right)
393-
: this.operation = ScoreOperator.Assign;
401+
: operation = ScoreOperator.Assign;
394402

395403
@override
396-
generate(Context context) {
397-
// TODO: implement generate
398-
throw UnimplementedError();
404+
Widget generate(Context context) {
405+
final ScoreBuilder builder = ScoreBuilder();
406+
407+
final (rScore, rActions) = right.copy(compact: true, builder: builder);
408+
409+
print(left);
410+
411+
if (left is Score) {
412+
return For.of([
413+
left,
414+
...rActions,
415+
...builder.compileBinary(left, operation, rScore),
416+
]);
417+
}
418+
419+
if (left case StoreScoreOperation(right: ScoreAssignable store)) {
420+
final (lScore, lActions) = left.copy(compact: true, builder: builder);
421+
return For.of([
422+
...lActions,
423+
...rActions,
424+
...builder.compileBinary(lScore, operation, rScore),
425+
store << lScore
426+
]);
427+
}
428+
429+
final (lScore, lActions) = left.copy(builder: builder);
430+
431+
return For.of([
432+
...lActions,
433+
...rActions,
434+
...builder.compileBinary(lScore, operation, rScore),
435+
]);
399436
}
400437

401438
@override
@@ -441,7 +478,7 @@ class ConstScore extends Score {
441478
}
442479

443480
class Score extends ElementaryScoreOperation
444-
with ScoreAssignable, ScoreStoreable {
481+
implements ScoreAssignable, ScoreStoreable {
445482
final Entity entity;
446483
String score;
447484
final String type;
@@ -563,7 +600,7 @@ class Score extends ElementaryScoreOperation
563600
/// assign value(int, Score, Data or Condition)
564601
ScoreOperation setTo(dynamic other) {
565602
if (other is int) return set(other);
566-
if (other is Score) return setEqual(other);
603+
if (other is ScoreOperation) return setEqual(other);
567604

568605
///if (other is Data) return setToData(other);
569606
// if (other is Condition) return setToCondition(other);

lib/src/basic/score/score_builder.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ class ScoreBuilder {
8888
...leftActions,
8989
...rightActions,
9090
ElementaryBinaryScoreOperation(
91-
left: out,
92-
right: tmpRight,
93-
operation: operator,
91+
out,
92+
operator,
93+
tmpRight,
9494
)
9595
];
9696
}

test/score_test.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ void main() {
3737
});
3838

3939
group('Score Conditions', () {
40-
commands(
41-
"empty",
42-
Score(Entity.All(), "test").isBigger(Score(Entity.Self(), "new")),
43-
[],
44-
);
4540
command(
4641
"simple if",
4742
If(

0 commit comments

Comments
 (0)