blob: 702764488a30f16a88f37b2547c8cc93bf800944 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
// ignore_for_file: avoid-non-null-assertion
import 'package:x_haven_server/core/models/monster.dart';
import 'package:x_haven_server/core/state/game_state.dart';
import 'package:x_haven_server/core/service_locator.dart';
import 'game_methods.dart';
class StatCalculator {
static int? calculateFormula(final Object stat, [GameState? gameState]) {
if (stat is IntStatValue) return stat.value;
if (stat is int) return stat;
final String str;
if (stat is FormulaStatValue) {
str = stat.formula;
} else if (stat is String) {
str = stat;
} else {
return null;
}
final gs = gameState ?? getIt<GameState>();
int C = GameMethods.getCurrentCharacterAmount(gameState: gs);
final int minNrCharacters = 2;
if (C < minNrCharacters) {
C = minNrCharacters;
}
final int L = gs.level;
String formula = str.replaceAll("C", C.toString());
formula = formula.replaceAll("L", L.toString());
return eval(formula);
}
static int? eval(final String str) {
return Parser(str).parse();
}
static bool evaluateCondition(final Object str) {
return calculateFormula(str) == 1;
}
}
class Parser {
int pos = -1;
String ch = '';
String str;
Parser(this.str);
void nextChar() {
ch = (++pos < str.length) ? str[pos] : '-1';
}
bool eat(String charToEat) {
while (ch == ' ') {
nextChar();
}
if (ch == charToEat.toString()) {
nextChar();
return true;
}
return false;
}
int? parse() {
try {
nextChar();
final int? result = parseCondition();
if (pos < str.length) {
// ignore: avoid_print
print("Unexpected: $ch");
return null;
}
return result;
} catch (_) {
return null;
}
}
// Grammar:
// condition = expression | `>` expression | `<` expression | '=' expression
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)` | number
// | functionName `(` expression `)` | functionName factor
// | factor `^` factor
int? parseCondition() {
try {
int result = parseExpression()!;
for (;;) {
if (eat('<')) {
result = result < parseExpression()! ? 1 : 0;
} else if (eat('>')) {
result = result > parseExpression()! ? 1 : 0;
} else if (eat('=')) {
result = result == parseExpression()! ? 1 : 0;
} else {
return result;
}
}
} catch (_) {
return null;
}
}
int? parseExpression() {
try {
int result = parseTerm()!;
for (;;) {
if (eat('+')) {
result += parseTerm()!;
} else if (eat('-')) {
result -= parseTerm()!;
} else {
return result;
}
}
} catch (_) {
return null;
}
}
int? parseTerm() {
try {
int result = parseFactor()!;
for (;;) {
if (eat('*')) result *= parseFactor()!; // multiplication
if (eat('x')) {
result *= parseFactor()!; // multiplication
} else if (eat('/')) {
result = (result / parseFactor()!).ceil();
} else if (eat('d')) {
result = (result / parseFactor()!).floor();
} else {
return result;
}
}
} catch (_) {
return null;
}
}
int? parseFactor() {
try {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor()!; // unary minus
int? result;
final int startPos = pos;
int asciiValue = ch.codeUnits.first;
if (eat('(')) {
// parentheses
result = parseExpression();
if (!eat(')')) throw Exception("Missing ')'");
} else if (asciiValue >= '0'.codeUnits.first &&
asciiValue <= '9'.codeUnits.first) {
// numbers
while ((asciiValue >= '0'.codeUnits.first &&
asciiValue <= '9'.codeUnits.first)) {
nextChar();
asciiValue = ch.codeUnits.first;
}
result = int.parse(str.substring(startPos, pos));
} else {
throw Exception("Unexpected: $ch");
}
return result;
} catch (_) {
return null;
}
}
}
|