Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework of Scores: Scoreoperation Compiler #35

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Data Storage improvements
Stevertus committed Nov 27, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit e57e0c1311aae6de17742eb26ff03337e9ff0d3b
9 changes: 8 additions & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -23,4 +23,11 @@ jobs:
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart run test
run: pub run test --coverage="coverage"
- name: Convert coverage to ICOV
run: pub run coverage:format_coverage --lcov --in=coverage --out=coverage.lcov --packages=.packages --report-on=lib
- name: Upload coverage to Codecov
uses: codecov/[email protected]
with:
token: ${{secrets.CODECOV_TOKEN}}
file: coverage.lcov
3 changes: 2 additions & 1 deletion example/files/load.dart
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ class LoadFile extends Widget {
Data.merge(Entity.All(), nbt: {
"uuid": UUID(1, 2, 3, 4),
'name': TextComponent('name'),
})
}),
Bossbar("test").get(BossbarOption.value) + 1
]);
}
}
12 changes: 5 additions & 7 deletions example/score_test.dart
Original file line number Diff line number Diff line change
@@ -3,15 +3,13 @@ import 'package:objd/core.dart';
void main(List<String> args) {
var s1 = Score(Entity.All(), "s1");
var s2 = Score.Self('s2');
var t = (s1 + s2) +
s1 / (s2 + 2) +
(Bossbar("test") << s1) +
Bossbar("test").get(BossbarOption.value);
var t = Bossbar("test").get(BossbarOption.value) + 1;

//print(t);
print(s1 + (s2 + s1));
print(t);
// print(s1 + (s2 + s1));

final (_, ops) = t.copy();
print(ops);
//print(getCommands(For.of(ops)).join('\n'));
print(getCommands(s1 + s2 + s1).join('\n')); //TODO: Caveat s1 + s2 != s2 + s1
print(getCommands(t).join('\n')); //TODO: Caveat s1 + s2 != s2 + s1
}
1 change: 1 addition & 0 deletions folder name/data/minecraft/tags/functions/load.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"values":["mypack:load"]}
1 change: 1 addition & 0 deletions folder name/data/minecraft/tags/functions/tick.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"values":["mypack:main"]}
3 changes: 3 additions & 0 deletions folder name/data/mypack/functions/load.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scoreboard objectives add objd_temp dummy
data merge entity @a {uuid:[I;1,2,3,4],name:{"text":"name"}}
scoreboard players add #at6AbnE8 objd_temp 1
Empty file.
1 change: 1 addition & 0 deletions folder name/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pack":{"pack_format":8,"description":"This is a datapack generated with objd by Stevertus"}}
5 changes: 2 additions & 3 deletions lib/src/basic/pack.dart
Original file line number Diff line number Diff line change
@@ -57,9 +57,8 @@ class Pack extends Widget {
'name': name,
'main': main?.toMap(),
'load': load?.toMap(),
'modules':
modules == null ? null : modules!.map((x) => x.toMap()).toList(),
'files': files == null ? null : files!.map((x) => x.toMap()).toList(),
'modules': modules?.map((x) => x.toMap()).toList(),
'files': files?.map((x) => x.toMap()).toList(),
}
};
}
2 changes: 1 addition & 1 deletion lib/src/basic/parsable.dart
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@ import 'package:gson/parsable.dart';

class Parsable extends GsonParsable {
/// ObjD's own type of a Parsable
Parsable(String parsable) : super(parsable);
Parsable(super.parsable);
}
165 changes: 82 additions & 83 deletions lib/src/basic/score/score.dart
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ import 'package:objd/src/basic/types/entity.dart';
import 'package:objd/src/basic/widget.dart';
import 'package:objd/src/build/context.dart';

abstract mixin class ScoreStoreable {
abstract mixin class ScoreStoreable implements Widget {
// block, bossbar, entity, score, storage
String get_assignable_right();
Widget get_assignable_right(Context context) => generate(context);

ScoreOperation toScore({Score? out}) => StoreScoreOperation(
out ?? Score.tmp(),
@@ -47,6 +47,7 @@ abstract mixin class ScoreAssignable {
sealed class ScoreOperation extends Widget implements ScoreStoreable {
/// add
@override
@override
BinaryScoreOperation operator +(dynamic other) => switch (other) {
int val => add(val),
ScoreOperation s => addScore(s),
@@ -55,6 +56,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// subtract
@override
BinaryScoreOperation operator -(dynamic other) => switch (other) {
int val => subtract(val),
ScoreOperation s => subtractScore(s),
@@ -63,6 +65,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// modulo by
@override
BinaryScoreOperation operator %(dynamic other) => switch (other) {
int val => modulo(Score.con(val)),
ScoreOperation s => modulo(s),
@@ -71,6 +74,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// divide by
@override
BinaryScoreOperation operator /(dynamic other) => switch (other) {
int val => divideByScore(Score.con(val)),
ScoreOperation s => divideByScore(s),
@@ -79,6 +83,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// multiply by
@override
BinaryScoreOperation operator *(dynamic other) => switch (other) {
int val => multiplyByScore(Score.con(val)),
ScoreOperation s => multiplyByScore(s),
@@ -87,6 +92,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// greater than
@override
ScoreCondition operator >(dynamic other) => switch (other) {
int val => matchesRange(Range.from(val + 1)),
ScoreOperation s => isBigger(s),
@@ -95,6 +101,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// less than
@override
ScoreCondition operator <(dynamic other) => switch (other) {
int val => matchesRange(Range.to(val - 1)),
ScoreOperation s => isSmaller(s),
@@ -103,6 +110,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// bigger or equal
@override
ScoreCondition operator >=(dynamic other) => switch (other) {
int val => matchesRange(Range.from(val)),
ScoreOperation s => isBiggerOrEqual(s),
@@ -113,6 +121,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// less or equal
@override
ScoreCondition operator <=(dynamic other) => switch (other) {
int val => matchesRange(Range.to(val)),
ScoreOperation s => isSmallerOrEqual(s),
@@ -123,6 +132,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
};

/// matches
@override
ScoreCondition operator &(dynamic other) => switch (other) {
int val => matches(val),
Range r => matchesRange(r),
@@ -150,13 +160,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {

/// gets the value of the score to work with it further
// TODO
// Score get() {
// return addCommandRet(
// Command(
// 'scoreboard players get ${_getESStr()}',
// ),
// );
// }
Command get() => Command('scoreboard players get $this');

// binary operations

@@ -180,51 +184,6 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
BinaryScoreOperation modulo(ScoreOperation score) =>
BinaryScoreOperation(this, ScoreOperator.Modulo, score);

/// sets the score to an nbt value
// Score setToData(Data data) {
// if (!data.isGetting) {
// throw ('Please set a score to Data.get and not Data.${data.subcommand}');
// }
// return addCommandRet(
// Builder(
// (c) => Command(
// 'execute store result score ${_getESStr()} run data get ${data.getTarget(c)} ${data.path} ${data.scale ?? 1}',
// ),
// ),
// );
// }

// /// set to functions return value(or number of commands)
// Group setToFunction(File file) => setToWidget(file.run(create: true));

// /// sets the score to the success of the given Command
// Score setToResult(Command commmand, {bool useSuccess = false}) {
// return addCommandRet(
// Command(
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} run $commmand',
// ),
// );
// }

// /// sets the score to the result of the given Widget
// /// JUST one Command should be the input
// Group setToWidget(Widget widget, {bool useSuccess = false}) {
// return Group(
// prefix:
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} run',
// children: [widget],
// );
// }

// /// sets the score to the success of the given condition result
// Score setToCondition(Condition cond, {bool useSuccess = false}) {
// return addCommandRet(
// Command(
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} ${Condition.getPrefixes(cond.getList())[0]}',
// ),
// );
// }

/// tests

BinaryScoreCondition isEqual(ScoreOperation score) =>
@@ -259,14 +218,13 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
return (out, [out, ...builder.compile(this, out: out)]);
}

@override
String get_assignable_right() {
throw UnimplementedError();
}

@override
ScoreOperation toScore({Score? out}) =>
out == null ? this : BinaryScoreOperation.assign(out, this);

//TODO: Multistep
@override
Widget get_assignable_right(Context context) => generate(context);
}

sealed class ElementaryScoreOperation extends ScoreOperation {}
@@ -319,32 +277,36 @@ final class ResetScoreOperation extends ElementaryScoreOperation {
final class StoreScoreOperation extends ElementaryScoreOperation {
final ScoreAssignable left;
final ScoreStoreable right;
final bool storeResult;
final bool useSuccess;

StoreScoreOperation(this.left, this.right, {this.storeResult = true});
StoreScoreOperation(this.left, this.right, {this.useSuccess = false});

@override
generate(Context context) => Command(
'execute store ${storeResult ? "result" : "success"} ${left.get_assignable_left()} run ${right.get_assignable_right()}' // TODO: right operation?
Group generate(Context context) => Execute.internal_store_command(
left.get_assignable_left(),
right.get_assignable_right(context),
useSuccess,
);

@override
String toString() => [
' | store ${left.get_assignable_left()}',
'<<',
' | ${right.get_assignable_right()}',
' | $right',
].join('\n');

@override
(Score, List<ElementaryScoreOperation>) copy({
Score? out,
ScoreBuilder? builder,
bool compact = false,
}) =>
switch ((left, compact)) {
(Score s, true) => s.copy(out: out, builder: builder, compact: compact),
_ => super.copy(out: out, builder: builder, compact: compact)
};
}) {
final (copyScore, ops) = switch ((left, compact)) {
(Score s, true) => s.copy(out: out, builder: builder, compact: compact),
_ => super.copy(out: out, builder: builder, compact: compact)
};
return (copyScore, [StoreScoreOperation(copyScore, right), ...ops]);
}
}

final class ElementaryBinaryScoreOperation extends ElementaryScoreOperation {
@@ -579,12 +541,14 @@ class Score extends ElementaryScoreOperation
score,
);

@override
String toString({Entity? entity, String? score}) {
entity ??= this.entity;
score ??= this.score;
return '$entity $score';
}

@override
(Score, List<ElementaryScoreOperation>) copy({
Score? out,
ScoreBuilder? builder,
@@ -597,21 +561,56 @@ class Score extends ElementaryScoreOperation
return super.copy(out: out, builder: builder, compact: compact);
}

/// sets the score to an nbt value
StoreScoreOperation setToData(DataGet data) =>
StoreScoreOperation(this, data);

// /// set to functions return value(or number of commands)
// Group setToFunction(File file) => setToWidget(file.run(create: true));

// /// sets the score to the success of the given Command
// Score setToResult(Command commmand, {bool useSuccess = false}) {
// return addCommandRet(
// Command(
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} run $commmand',
// ),
// );
// }

// /// sets the score to the result of the given Widget
// /// JUST one Command should be the input
// Group setToWidget(Widget widget, {bool useSuccess = false}) {
// return Group(
// prefix:
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} run',
// children: [widget],
// );
// }

// /// sets the score to the success of the given condition result
// Score setToCondition(Condition cond, {bool useSuccess = false}) {
// return addCommandRet(
// Command(
// 'execute store ${useSuccess ? 'success' : 'result'} score ${_getESStr()} ${Condition.getPrefixes(cond.getList())[0]}',
// ),
// );
// }

/// assign value(int, Score, Data or Condition)
ScoreOperation setTo(dynamic other) {
if (other is int) return set(other);
if (other is ScoreOperation) return setEqual(other);

///if (other is Data) return setToData(other);
// if (other is Condition) return setToCondition(other);
// if (other is Command) return setToResult(other);
// if (other is File) return setToFunction(other);
// if (other is Widget) return setToWidget(other);
throw ('Please use either a Score, Data, Condition, Command or an Int in the operator >>');
}
@override
ScoreOperation setTo(dynamic other) => switch (other) {
int val => set(val),
ScoreOperation s => setEqual(s),
DataGet s => setToData(s),
ScoreStoreable s => StoreScoreOperation(this, s),
_ =>
throw ('Please use either a Score, Data, Condition, Command or an Int in the operator >>'),
};

Widget operator >>(dynamic other) => setTo(other);
Widget operator <<(dynamic other) => setTo(other);
@override
ScoreOperation operator >>(dynamic other) => setTo(other);
@override
ScoreOperation operator <<(dynamic other) => setTo(other);

/// sets the score to a given value of int
BinaryScoreOperation set(int val) {
@@ -685,7 +684,7 @@ class Score extends ElementaryScoreOperation
String get_assignable_left() => 'score $this';

@override
String get_assignable_right() => 'scoreboard players get $this';
Widget get_assignable_right(Context _) => get();

@override
ScoreStoreable toStorable() => this;
Loading