-
Notifications
You must be signed in to change notification settings - Fork 2
/
templating.go
392 lines (333 loc) · 9.68 KB
/
templating.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package dodumap
import (
"fmt"
"log"
"regexp"
"slices"
"strconv"
"strings"
)
func Min(a, b int) int {
if a < b {
return a
}
return b
}
func Max(a, b int) int {
if a > b {
return a
}
return b
}
func DeleteReplacer(input string) string {
replacer := []string{
"#",
"%",
}
for i := 1; i < 6; i++ {
for _, replace := range replacer {
numRegex := regexp.MustCompile(fmt.Sprintf(" ?%s%d", replace, i))
input = numRegex.ReplaceAllString(input, "")
}
}
return input
}
func DeleteDamageFormatter(input string) string {
input, regex := PrepareAndCreateRangeRegex(input, false)
if strings.Contains(input, "+#1{~1~2 to } level #2") {
return "level"
}
input = strings.ReplaceAll(input, "#1{~1~2 -}#2", "#1{~1~2 - }#2") // bug from ankama
input = regex.ReplaceAllString(input, "")
input = strings.ReplaceAll(input, "{~1~2 to }", "")
input = DeleteReplacer(input)
input = strings.ReplaceAll(input, " ", " ")
input = strings.TrimSpace(input)
return input
}
func SingularPluralFormatter(input string, amount int, lang string) string {
str := strings.ReplaceAll(input, "{~s}", "") // avoid only s without what to append
str = strings.ReplaceAll(str, "{~p}", "") // same
// delete unknown z
unknownZRegex := regexp.MustCompile("{~z[^}]*}")
str = unknownZRegex.ReplaceAllString(str, "")
var indicator rune
if amount > 1 {
indicator = 'p'
} else {
indicator = 's'
}
indicators := []rune{'s', 'p'}
var regexps []*regexp.Regexp
for _, indicatorIt := range indicators {
regex := fmt.Sprintf("{~%c([^}]*)}", indicatorIt) // capturing with everything inside ()
regexExtract := regexp.MustCompile(regex)
regexps = append(regexps, regexExtract)
// if lang == "es" || lang == "pt" {
if indicatorIt != indicator {
continue
}
extractedEntries := regexExtract.FindAllStringSubmatch(str, -1)
for _, extracted := range extractedEntries {
str = strings.ReplaceAll(str, extracted[0], extracted[1])
}
}
for _, regexIt := range regexps {
str = regexIt.ReplaceAllString(str, "")
}
return str
}
func ElementFromCode(codeUndef string) int {
code := strings.ToLower(codeUndef)
switch code {
case "cs":
return 501945 // "Strength"
case "ci":
return 501944 // "Intelligence"
case "cv":
return 501947 // "Vitality"
case "ca":
return 501941 // "Agility"
case "cc":
return 501942 // "Chance"
case "cw":
return 501946 // "Wisdom"
case "pk":
if codeUndef == "PK" {
return 66438 // "Kamas"
} else {
return 422874 // "Set-Bonus"
}
case "pl":
return 837224 // "Mindestens Stufe %1"
case "cm":
return 67248 // "Bewegungsp. (BP)"
case "cp":
return 67755 // "Aktionsp. (AP)"
case "po":
return 335357 // Anderes Gebiet als: %1
case "pf":
return 644231 // Nicht ausgerüstetes %1-Reittier
//case "": // Ps=1
// return 644230 // Ausgerüstetes %1-Reittier
case "pa":
return 66566 // Gesinunngsstufe
//case "":
// return 637203 // Kein ausgerüstetes %1-Reittier haben
case "of":
return 637212 // Ein ausgerüstetes %1-Reittier haben
case "pz":
return 66351 // Abonniert sein
}
return -1
}
func ConditionWithOperator(input string, operator string, langs *map[string]LangDict, out *MappedMultilangCondition, data *JSONGameData) bool {
partSplit := strings.Split(input, operator)
rawElement := ElementFromCode(partSplit[0])
if rawElement == -1 {
return false
}
out.Element = partSplit[0]
out.Value, _ = strconv.Atoi(partSplit[1])
for _, lang := range Languages {
langStr := (*langs)[lang].Texts[rawElement]
if lang == "en" {
if langStr == "()" {
return false
}
keySanitized := DeleteReplacer(langStr)
if PersistedElements.Entries == nil {
log.Fatal("Elements Entries is nil")
}
key, foundKey := PersistedElements.Entries.GetKey(keySanitized)
if foundKey {
out.ElementId = key.(int)
} else {
PersistedElements.Entries.Put(PersistedElements.NextId, keySanitized)
PersistedElements.NextId++
}
}
switch rawElement {
case 837224: // %1 replace
intVal, _ := strconv.Atoi(partSplit[1])
langStr = strings.ReplaceAll(langStr, "%1", fmt.Sprint(intVal+1))
case 335357: // anderes gebiet als %1
langStr = strings.ReplaceAll(langStr, "%1", (*langs)[lang].Texts[data.areas[out.Value].NameId])
case 637212: // reittier %1
case 644231:
langStr = strings.ReplaceAll(langStr, "%1", (*langs)[lang].Texts[data.Mounts[out.Value].NameId])
}
out.Templated[lang] = langStr
}
out.Operator = operator
buggyConditions := []int{181}
return !slices.Contains(buggyConditions, out.ElementId)
}
// NumSpellFormatter returns info about min max with in. -1 "only_min", -2 "no_min_max"
func NumSpellFormatter(input string, lang string, gameData *JSONGameData, langs *map[string]LangDict, diceNum *int, diceSide *int, value *int, effectNameId int, numIsSpell bool, useDice bool, frNumSigned *int, frSideSigned *int) (string, int) {
diceNumIsSpellId := *diceNum > 12000 || numIsSpell
diceSideIsSpellId := *diceSide > 12000
valueIsSpellId := *value > 12000
onlyNoMinMax := 0
// when + xp
if !useDice && *diceNum == 0 && *value == 0 && *diceSide != 0 {
*value = *diceSide
*diceSide = 0
}
delValue := false
input, concatRegex := PrepareAndCreateRangeRegex(input, true)
var numSigned bool
var sideSigned bool
var ptSideSigned bool
_, ptSideSigned = ParseSigness(input)
if *frNumSigned != 2 || *frSideSigned != 2 { // 2 is unset, 0 is false, 1 is true
numSigned = *frNumSigned == 1
sideSigned = *frSideSigned == 1
} else {
if lang == "fr" {
numSigned, sideSigned = ParseSigness(input)
if numSigned {
*frNumSigned = 1
} else {
*frNumSigned = 0
}
if sideSigned {
*frSideSigned = 1
} else {
*frSideSigned = 0
}
} else {
log.Fatalf("frNumSigned and frSideSigned must be set for %s", lang)
}
}
concatEntries := concatRegex.FindAllStringSubmatch(input, -1)
if *diceSide == 0 { // only replace #1 with dice_num
for _, extracted := range concatEntries {
input = strings.ReplaceAll(input, extracted[0], "")
}
} else {
for _, extracted := range concatEntries {
input = strings.ReplaceAll(input, extracted[0], fmt.Sprintf(" %s", extracted[1]))
}
}
num1Regex := regexp.MustCompile("([-,+]?)#1")
num1Entries := num1Regex.FindAllStringSubmatch(input, -1)
for _, extracted := range num1Entries {
var diceNumStr string
if diceNumIsSpellId {
diceNumStr = (*langs)[lang].Texts[gameData.spells[*diceNum].NameId]
} else {
diceNumStr = fmt.Sprint(*diceNum)
}
input = strings.ReplaceAll(input, extracted[0], fmt.Sprintf("%s%s", extracted[1], diceNumStr))
}
if *diceSide == 0 {
input = strings.ReplaceAll(input, "#2", "")
} else {
var diceSideStr string
if diceSideIsSpellId {
diceSideStr = (*langs)[lang].Texts[gameData.spells[*diceSide].NameId]
//del_dice_side = true
} else {
if sideSigned && lang == "pt" && !ptSideSigned {
diceSideStr = fmt.Sprintf("-%d", *diceSide)
} else {
diceSideStr = fmt.Sprint(*diceSide)
}
}
input = strings.ReplaceAll(input, "#2", diceSideStr)
}
var valueStr string
if valueIsSpellId {
valueStr = (*langs)[lang].Texts[gameData.spells[*value].NameId]
delValue = true
} else {
valueStr = fmt.Sprint(*value)
}
if effectNameId == 427090 { // go to <npc> for more info
return "", -2
}
input = strings.ReplaceAll(input, "#3", valueStr)
if delValue {
*diceNum = Min(*diceNum, *diceSide)
}
if !useDice {
// avoid min = 0, max > x
if *diceNum == 0 && *diceSide != 0 {
*diceNum = *diceSide
*diceSide = 0
}
}
if *diceNum == 0 && *diceSide == 0 {
onlyNoMinMax = -2
}
if *diceNum != 0 && *diceSide == 0 {
onlyNoMinMax = -1
}
input = strings.TrimSpace(input)
if numSigned {
*diceNum *= -1
}
if sideSigned {
*diceSide *= -1
}
if *diceNum < 0 && *diceSide < 0 {
*diceNum, *diceSide = *diceSide, *diceNum
diceSideFmt := fmt.Sprint((*diceSide) * -1)
diceNumFmt := fmt.Sprint((*diceNum) * -1)
input = strings.ReplaceAll(input, diceSideFmt, "-diceSideFmt-")
input = strings.ReplaceAll(input, diceNumFmt, diceSideFmt)
input = strings.ReplaceAll(input, "-diceSideFmt-", diceNumFmt)
if !strings.Contains(input, "-"+diceSideFmt) {
input = strings.ReplaceAll(input, diceSideFmt, "-"+diceSideFmt)
}
if !strings.Contains(input, "-"+diceNumFmt) {
input = strings.ReplaceAll(input, diceNumFmt, "-"+diceNumFmt)
}
}
return input, onlyNoMinMax
}
func PrepareTextForRegex(input string) string {
input = strings.ReplaceAll(input, "{~1~2 -}", "{~1~2 - }")
input = strings.ReplaceAll(input, "{~1~2 to}level", "{~1~2 to } level") // {~1~2 to}level
input = strings.ReplaceAll(input, "{~1~2 to}", "{~1~2 to }")
input = strings.ReplaceAll(input, "\"\"", "")
input = strings.TrimPrefix(input, ":")
input = strings.TrimSuffix(input, ":")
input = strings.TrimPrefix(input, "+")
return input
}
func PrepareAndCreateRangeRegex(input string, extract bool) (string, *regexp.Regexp) {
var regexStr string
combiningWords := "(und|et|and|bis|to|a|à|-|auf)"
if extract {
regexStr = fmt.Sprintf("{~1~2 (%s [-,+]?)}", combiningWords)
} else {
regexStr = fmt.Sprintf("[-,+]?#1{~1~2 %s [-,+]?}#2", combiningWords)
}
concatRegex := regexp.MustCompile(regexStr)
return PrepareTextForRegex(input), concatRegex
}
func ParseSigness(input string) (bool, bool) {
numSigness := false
sideSigness := false
regexNum := regexp.MustCompile("(([+,-])?#1)")
entriesNum := regexNum.FindAllStringSubmatch(input, -1)
for _, extracted := range entriesNum {
for _, entry := range extracted {
if entry == "-" {
numSigness = true
}
}
}
regexSide := regexp.MustCompile("([+,-])?}?#2")
entriesSide := regexSide.FindAllStringSubmatch(input, -1)
for _, extracted := range entriesSide {
for _, entry := range extracted {
if entry == "-" {
sideSigness = true
}
}
}
return numSigness, sideSigness
}