Skip to content

Commit 605ca80

Browse files
committed
temporary solution for score conditions
1 parent 86cb4ec commit 605ca80

5 files changed

Lines changed: 188 additions & 135 deletions

File tree

lib/src/basic/score/score.dart

Lines changed: 153 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ abstract mixin class ScoreStoreable implements Widget {
2626
BinaryScoreOperation operator %(dynamic other) => toScore() % other;
2727
BinaryScoreOperation operator /(dynamic other) => toScore() / other;
2828
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;
29+
// TODO: Not finished, refactor of If needed
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;
34+
// ScoreCondition operator &(dynamic other) => toScore() & other;
3435
}
3536

3637
abstract mixin class ScoreAssignable {
@@ -91,59 +92,6 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
9192
_ => throw ('Please use either a Score or an Int in the operator *')
9293
};
9394

94-
/// greater than
95-
@override
96-
ScoreCondition operator >(dynamic other) => switch (other) {
97-
int val => matchesRange(Range.from(val + 1)),
98-
ScoreOperation s => isBigger(s),
99-
ScoreStoreable s => isBigger(s.toScore()),
100-
_ => throw ('Please use either a Score or an Int in the operator >')
101-
};
102-
103-
/// less than
104-
@override
105-
ScoreCondition operator <(dynamic other) => switch (other) {
106-
int val => matchesRange(Range.to(val - 1)),
107-
ScoreOperation s => isSmaller(s),
108-
ScoreStoreable s => isSmaller(s.toScore()),
109-
_ => throw ('Please use either a Score or an Int in the operator <')
110-
};
111-
112-
/// bigger or equal
113-
@override
114-
ScoreCondition operator >=(dynamic other) => switch (other) {
115-
int val => matchesRange(Range.from(val)),
116-
ScoreOperation s => isBiggerOrEqual(s),
117-
ScoreStoreable s => isBiggerOrEqual(
118-
s.toScore(),
119-
),
120-
_ => throw ('Please use either a Score or an Int in the operator >=')
121-
};
122-
123-
/// less or equal
124-
@override
125-
ScoreCondition operator <=(dynamic other) => switch (other) {
126-
int val => matchesRange(Range.to(val)),
127-
ScoreOperation s => isSmallerOrEqual(s),
128-
ScoreStoreable s => isSmallerOrEqual(
129-
s.toScore(),
130-
),
131-
_ => throw ('Please use either a Score or an Int in the operator <=')
132-
};
133-
134-
/// matches
135-
@override
136-
ScoreCondition operator &(dynamic other) => switch (other) {
137-
int val => matches(val),
138-
Range r => matchesRange(r),
139-
ScoreOperation s => isEqual(s),
140-
ScoreStoreable s => isEqual(
141-
s.toScore(),
142-
),
143-
_ =>
144-
throw ('Please use either a Score, Range or an Int in the operator &')
145-
};
146-
14795
/// adds a value to the score
14896
BinaryScoreOperation add([int val = 1]) => BinaryScoreOperation(
14997
this,
@@ -160,7 +108,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
160108

161109
/// gets the value of the score to work with it further
162110
// TODO
163-
Command get() => Command('scoreboard players get $this');
111+
Widget get() => For.of([this, Command('scoreboard players get $this')]);
164112

165113
// binary operations
166114

@@ -184,23 +132,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
184132
BinaryScoreOperation modulo(ScoreOperation score) =>
185133
BinaryScoreOperation(this, ScoreOperator.Modulo, score);
186134

187-
/// tests
188-
189-
BinaryScoreCondition isEqual(ScoreOperation score) =>
190-
BinaryScoreCondition(this, ConditionalOperator.Equal, score);
191-
BinaryScoreCondition isSmaller(ScoreOperation score) =>
192-
BinaryScoreCondition(this, ConditionalOperator.Less, score);
193-
BinaryScoreCondition isSmallerOrEqual(ScoreOperation score) =>
194-
BinaryScoreCondition(this, ConditionalOperator.LessEqual, score);
195-
BinaryScoreCondition isBiggerOrEqual(ScoreOperation score) =>
196-
BinaryScoreCondition(this, ConditionalOperator.BiggerEqual, score);
197-
BinaryScoreCondition isBigger(ScoreOperation score) =>
198-
BinaryScoreCondition(this, ConditionalOperator.Bigger, score);
199-
200-
MatchesScoreCondition matches(int value) =>
201-
MatchesScoreCondition(this, Range.exact(value));
202-
MatchesScoreCondition matchesRange(Range range) =>
203-
MatchesScoreCondition(this, range);
135+
//TODO: Move Condition Operators here
204136

205137
void build() {
206138
print('test');
@@ -276,17 +208,22 @@ final class ResetScoreOperation extends ElementaryScoreOperation {
276208

277209
final class StoreScoreOperation extends ElementaryScoreOperation {
278210
final ScoreAssignable left;
279-
final ScoreStoreable right;
211+
final Widget right;
280212
final bool useSuccess;
281213

282214
StoreScoreOperation(this.left, this.right, {this.useSuccess = false});
283215

284216
@override
285-
Group generate(Context context) => Execute.internal_store_command(
286-
left.get_assignable_left(),
287-
right.get_assignable_right(context),
288-
useSuccess,
289-
);
217+
Widget generate(Context context) => For.of([
218+
if (left is Score) left as Score,
219+
Execute.internal_store_command(
220+
left.get_assignable_left(),
221+
right is ScoreStoreable
222+
? (right as ScoreStoreable).get_assignable_right(context)
223+
: right.generate(context),
224+
useSuccess,
225+
)
226+
]);
290227

291228
@override
292229
String toString() => [
@@ -309,6 +246,46 @@ final class StoreScoreOperation extends ElementaryScoreOperation {
309246
}
310247
}
311248

249+
final class StoreConditionScoreOperation extends ElementaryScoreOperation {
250+
final ScoreAssignable left;
251+
final Condition right;
252+
final bool useSuccess;
253+
254+
StoreConditionScoreOperation(this.left, this.right,
255+
{this.useSuccess = false});
256+
257+
@override
258+
Widget generate(Context context) => For.of([
259+
if (left is Score) left as Score,
260+
Command(
261+
'execute store ${useSuccess ? 'success' : 'result'} ${left.get_assignable_left()} ${Condition.getPrefixes(right.getList())[0]}',
262+
)
263+
]);
264+
265+
@override
266+
String toString() => [
267+
' | store ${left.get_assignable_left()}',
268+
'<<',
269+
' | $right',
270+
].join('\n');
271+
272+
@override
273+
(Score, List<ElementaryScoreOperation>) copy({
274+
Score? out,
275+
ScoreBuilder? builder,
276+
bool compact = false,
277+
}) {
278+
final (copyScore, ops) = switch ((left, compact)) {
279+
(Score s, true) => s.copy(out: out, builder: builder, compact: compact),
280+
_ => super.copy(out: out, builder: builder, compact: compact)
281+
};
282+
return (
283+
copyScore,
284+
[StoreConditionScoreOperation(copyScore, right), ...ops]
285+
);
286+
}
287+
}
288+
312289
final class ElementaryBinaryScoreOperation extends ElementaryScoreOperation {
313290
final Score left;
314291
final Score right;
@@ -562,49 +539,41 @@ class Score extends ElementaryScoreOperation
562539
}
563540

564541
/// sets the score to an nbt value
565-
StoreScoreOperation setToData(DataGet data) =>
566-
StoreScoreOperation(this, data);
567-
568-
// /// set to functions return value(or number of commands)
569-
// Group setToFunction(File file) => setToWidget(file.run(create: true));
570-
571-
// /// sets the score to the success of the given Command
572-
// Score setToResult(Command commmand, {bool useSuccess = false}) {
573-
// return addCommandRet(
574-
// Command(
575-
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} run $commmand',
576-
// ),
577-
// );
578-
// }
579-
580-
// /// sets the score to the result of the given Widget
581-
// /// JUST one Command should be the input
582-
// Group setToWidget(Widget widget, {bool useSuccess = false}) {
583-
// return Group(
584-
// prefix:
585-
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} run',
586-
// children: [widget],
587-
// );
588-
// }
589-
590-
// /// sets the score to the success of the given condition result
591-
// Score setToCondition(Condition cond, {bool useSuccess = false}) {
592-
// return addCommandRet(
593-
// Command(
594-
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} ${Condition.getPrefixes(cond.getList())[0]}',
595-
// ),
596-
// );
597-
// }
542+
StoreScoreOperation setToData(DataGet data, {bool useSuccess = false}) =>
543+
StoreScoreOperation(this, data, useSuccess: useSuccess);
544+
545+
/// set to functions return value(or number of commands)
546+
StoreScoreOperation setToFunction(File file, {bool useSuccess = false}) =>
547+
setToWidget(file.run(create: true));
548+
549+
/// sets the score to the success of the given Command
550+
StoreScoreOperation setToResult(Command commmand,
551+
{bool useSuccess = false}) =>
552+
setToWidget(commmand, useSuccess: useSuccess);
553+
554+
/// sets the score to the result of the given Widget
555+
/// JUST one Command should be the input
556+
StoreScoreOperation setToWidget(Widget widget, {bool useSuccess = false}) =>
557+
StoreScoreOperation(this, widget, useSuccess: useSuccess);
558+
559+
/// sets the score to the success of the given condition result
560+
StoreConditionScoreOperation setToCondition(Condition cond,
561+
{bool useSuccess = false}) =>
562+
StoreConditionScoreOperation(this, cond, useSuccess: useSuccess);
598563

599564
/// assign value(int, Score, Data or Condition)
600565
@override
601-
ScoreOperation setTo(dynamic other) => switch (other) {
566+
ScoreOperation setTo(dynamic other, {bool useSuccess = false}) =>
567+
switch (other) {
602568
int val => set(val),
603569
ScoreOperation s => setEqual(s),
604-
DataGet s => setToData(s),
605-
ScoreStoreable s => StoreScoreOperation(this, s),
570+
DataGet s => setToData(s, useSuccess: useSuccess),
571+
ScoreStoreable s =>
572+
StoreScoreOperation(this, s, useSuccess: useSuccess),
573+
Condition c => setToCondition(c, useSuccess: useSuccess),
574+
Widget w => setToWidget(w, useSuccess: useSuccess),
606575
_ =>
607-
throw ('Please use either a Score, Data, Condition, Command or an Int in the operator >>'),
576+
throw ('Please use either a Score, Data, Condition, Command or an Int in the operator >>, got $other'),
608577
};
609578

610579
@override
@@ -639,6 +608,71 @@ class Score extends ElementaryScoreOperation
639608
BinaryScoreOperation setToBiggest(ScoreOperation score) =>
640609
BinaryScoreOperation(this, ScoreOperator.Max, score);
641610

611+
/// tests
612+
613+
/// greater than
614+
@override
615+
ScoreCondition operator >(dynamic other) => switch (other) {
616+
int val => matchesRange(Range.from(val + 1)),
617+
Score s => isBigger(s),
618+
// ScoreStoreable s => isBigger(s.toScore()),
619+
_ => throw ('Please use either a Score or an Int in the operator >')
620+
};
621+
622+
/// less than
623+
@override
624+
ScoreCondition operator <(dynamic other) => switch (other) {
625+
int val => matchesRange(Range.to(val - 1)),
626+
Score s => isSmaller(s),
627+
//ScoreStoreable s => isSmaller(s.toScore()),
628+
_ => throw ('Please use either a Score or an Int in the operator <')
629+
};
630+
631+
/// bigger or equal
632+
@override
633+
ScoreCondition operator >=(dynamic other) => switch (other) {
634+
int val => matchesRange(Range.from(val)),
635+
Score s => isBiggerOrEqual(s),
636+
//ScoreStoreable s => isBiggerOrEqual(s.toScore()),
637+
_ => throw ('Please use either a Score or an Int in the operator >=')
638+
};
639+
640+
/// less or equal
641+
@override
642+
ScoreCondition operator <=(dynamic other) => switch (other) {
643+
int val => matchesRange(Range.to(val)),
644+
Score s => isSmallerOrEqual(s),
645+
//ScoreStoreable s => isSmallerOrEqual(s.toScore()),
646+
_ => throw ('Please use either a Score or an Int in the operator <=')
647+
};
648+
649+
/// matches
650+
@override
651+
ScoreCondition operator &(dynamic other) => switch (other) {
652+
int val => matches(val),
653+
Range r => matchesRange(r),
654+
Score s => isEqual(s),
655+
//ScoreStoreable s => isEqual(s.toScore()),
656+
_ =>
657+
throw ('Please use either a Score, Range or an Int in the operator &')
658+
};
659+
660+
BinaryScoreCondition isEqual(Score score) =>
661+
BinaryScoreCondition(this, ConditionalOperator.Equal, score);
662+
BinaryScoreCondition isSmaller(Score score) =>
663+
BinaryScoreCondition(this, ConditionalOperator.Less, score);
664+
BinaryScoreCondition isSmallerOrEqual(Score score) =>
665+
BinaryScoreCondition(this, ConditionalOperator.LessEqual, score);
666+
BinaryScoreCondition isBiggerOrEqual(Score score) =>
667+
BinaryScoreCondition(this, ConditionalOperator.BiggerEqual, score);
668+
BinaryScoreCondition isBigger(Score score) =>
669+
BinaryScoreCondition(this, ConditionalOperator.Bigger, score);
670+
671+
MatchesScoreCondition matches(int value) =>
672+
MatchesScoreCondition(this, Range.exact(value));
673+
MatchesScoreCondition matchesRange(Range range) =>
674+
MatchesScoreCondition(this, range);
675+
642676
/// finds the smallest value in a list of scores
643677
Widget findSmallest(List<Score> scores, {int? min}) {
644678
return For(

lib/src/basic/score/score_builder.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class ScoreBuilder {
2121
input,
2222
if (out != null) ElementaryBinaryScoreOperation.assign(out, score),
2323
],
24+
StoreConditionScoreOperation(left: final left) => [
25+
input,
26+
if (out != null) StoreScoreOperation(out, left.toStorable())
27+
],
2428
StoreScoreOperation(left: final left) => [
2529
input,
2630
if (out != null) StoreScoreOperation(out, left.toStorable())
@@ -45,10 +49,11 @@ class ScoreBuilder {
4549

4650
if (left is Score) {
4751
if (right is ConstScore) {
48-
return [SetScoreOperation(left, right.value)];
52+
return [left, SetScoreOperation(left, right.value)];
4953
}
5054

5155
return [
56+
left,
5257
...prevactions,
5358
ElementaryBinaryScoreOperation.assign(left, tmpRight)
5459
];

0 commit comments

Comments
 (0)