Skip to content

Commit 7a0c248

Browse files
authored
TibiaCharactersCharacter: remove death regex (#225)
1 parent 698edcc commit 7a0c248

File tree

3 files changed

+2690
-65
lines changed

3 files changed

+2690
-65
lines changed

src/TibiaCharactersCharacter.go

Lines changed: 88 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ type CharacterResponse struct {
123123
const Br = 0x202
124124

125125
var (
126-
deathRegex = regexp.MustCompile(`<td.*>(.*)<\/td><td>(.*) at Level ([0-9]+) by (.*).<\/td>`)
127126
summonRegex = regexp.MustCompile(`(an? .+) of ([^<]+)`)
128127
titleRegex = regexp.MustCompile(`(.*) \(([0-9]+).*`)
129128
characterInfoRegex = regexp.MustCompile(`<td.*<nobr>[0-9]+\..(.*)<\/nobr><\/td><td.*><nobr>(.*)<\/nobr><\/td><td style="width: 70%">(.*)<\/td><td.*`)
@@ -376,81 +375,119 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string) (*CharacterResponse, er
376375
// Removing line breaks
377376
CharacterListHTML = TibiaDataHTMLRemoveLinebreaks(CharacterListHTML)
378377
CharacterListHTML = strings.ReplaceAll(CharacterListHTML, ".<br/>Assisted by", ". Assisted by")
378+
CharacterListHTML = TibiaDataSanitizeStrings(CharacterListHTML)
379379

380-
// Regex to get data for deaths
381-
subma1 := deathRegex.FindAllStringSubmatch(CharacterListHTML, -1)
380+
dataNoTags := RemoveHtmlTag(CharacterListHTML)
382381

383-
if len(subma1) > 0 {
384-
// defining responses
385-
DeathKillers := []Killers{}
386-
DeathAssists := []Killers{}
382+
// defining responses
383+
DeathKillers := []Killers{}
384+
DeathAssists := []Killers{}
387385

388-
// store for reply later on.. and sanitizing string
389-
ReasonString := TibiaDataSanitizeStrings(RemoveHtmlTag(subma1[0][2] + " at Level " + subma1[0][3] + " by " + subma1[0][4] + "."))
386+
const (
387+
initIndexer = `CET`
388+
levelIndexer = `at Level `
389+
killersIndexer = `by `
390+
)
390391

391-
// if kill is with assist..
392-
if strings.Contains(subma1[0][4], ". Assisted by ") {
393-
TmpListOfDeath := strings.Split(subma1[0][4], ". Assisted by ")
394-
subma1[0][4] = TmpListOfDeath[0]
395-
TmpAssist := TmpListOfDeath[1]
392+
initIdx := strings.Index(
393+
dataNoTags, initIndexer,
394+
) + len(initIndexer)
395+
endInitIdx := strings.Index(
396+
dataNoTags[initIdx:], `by `,
397+
) + initIdx + len(`by `)
396398

397-
// get a list of killers
398-
ListOfAssists := strings.Split(TmpAssist, ", ")
399+
reasonStart := dataNoTags[initIdx:endInitIdx]
400+
reasonRest := dataNoTags[endInitIdx:]
399401

400-
// extract if "and" is in last ss1
401-
ListOfAssistsTmp := strings.Split(ListOfAssists[len(ListOfAssists)-1], " and ")
402+
// store for reply later on.. and sanitizing string
403+
reasonString := reasonStart + reasonRest
402404

403-
// if there is an "and", then we split it..
404-
if len(ListOfAssistsTmp) > 1 {
405-
ListOfAssists[len(ListOfAssists)-1] = ListOfAssistsTmp[0]
406-
ListOfAssists = append(ListOfAssists, ListOfAssistsTmp[1])
407-
}
405+
timeIdx := 0
406+
endTimeIdx := strings.Index(
407+
dataNoTags[timeIdx:], `CET`,
408+
) + timeIdx + len(`CET`)
408409

409-
// loop through all killers and append to result
410-
for i := range ListOfAssists {
411-
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfAssists[i])
412-
DeathAssists = append(DeathAssists, Killers{
413-
Name: name,
414-
Player: isPlayer,
415-
Traded: isTraded,
416-
Summon: theSummon,
417-
})
418-
}
419-
}
410+
time := TibiaDataDatetime(dataNoTags[timeIdx:endTimeIdx])
411+
412+
levelIdx := strings.Index(
413+
dataNoTags, levelIndexer,
414+
) + len(levelIndexer)
415+
endLevelIdx := strings.Index(
416+
dataNoTags[levelIdx:], " ",
417+
) + levelIdx
418+
419+
level := TibiaDataStringToInteger(dataNoTags[levelIdx:endLevelIdx])
420+
421+
killersIdx := strings.Index(
422+
CharacterListHTML, killersIndexer,
423+
) + len(killersIndexer)
424+
endKillersIdx := strings.Index(
425+
CharacterListHTML[killersIdx:], `.</td>`,
426+
) + killersIdx
427+
428+
rawListofKillers := CharacterListHTML[killersIdx:endKillersIdx]
429+
430+
// if kill is with assist..
431+
if strings.Contains(dataNoTags, ". Assisted by ") {
432+
TmpListOfDeath := strings.Split(CharacterListHTML, ". Assisted by ")
433+
rawListofKillers = TmpListOfDeath[0][killersIdx:]
434+
TmpAssist := TmpListOfDeath[1]
420435

421436
// get a list of killers
422-
ListOfKillers := strings.Split(subma1[0][4], ", ")
437+
ListOfAssists := strings.Split(TmpAssist, ", ")
423438

424439
// extract if "and" is in last ss1
425-
ListOfKillersTmp := strings.Split(ListOfKillers[len(ListOfKillers)-1], " and ")
440+
ListOfAssistsTmp := strings.Split(ListOfAssists[len(ListOfAssists)-1], " and ")
426441

427442
// if there is an "and", then we split it..
428-
if len(ListOfKillersTmp) > 1 {
429-
ListOfKillers[len(ListOfKillers)-1] = ListOfKillersTmp[0]
430-
ListOfKillers = append(ListOfKillers, ListOfKillersTmp[1])
443+
if len(ListOfAssistsTmp) > 1 {
444+
ListOfAssists[len(ListOfAssists)-1] = ListOfAssistsTmp[0]
445+
ListOfAssists = append(ListOfAssists, ListOfAssistsTmp[1])
431446
}
432447

433-
// loop through all killers and append to result
434-
for i := range ListOfKillers {
435-
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfKillers[i])
436-
DeathKillers = append(DeathKillers, Killers{
437-
Name: name,
448+
for i := range ListOfAssists {
449+
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfAssists[i])
450+
DeathAssists = append(DeathAssists, Killers{
451+
Name: strings.TrimSuffix(strings.TrimSuffix(name, ".</td>"), "."),
438452
Player: isPlayer,
439453
Traded: isTraded,
440454
Summon: theSummon,
441455
})
442456
}
457+
}
458+
459+
// get a list of killers
460+
ListOfKillers := strings.Split(rawListofKillers, ", ")
461+
462+
// extract if "and" is in last ss1
463+
ListOfKillersTmp := strings.Split(ListOfKillers[len(ListOfKillers)-1], " and ")
443464

444-
// append deadentry to death list
445-
DeathsData = append(DeathsData, Deaths{
446-
Time: TibiaDataDatetime(subma1[0][1]),
447-
Level: TibiaDataStringToInteger(subma1[0][3]),
448-
Killers: DeathKillers,
449-
Assists: DeathAssists,
450-
Reason: ReasonString,
465+
// if there is an "and", then we split it..
466+
if len(ListOfKillersTmp) > 1 {
467+
ListOfKillers[len(ListOfKillers)-1] = ListOfKillersTmp[0]
468+
ListOfKillers = append(ListOfKillers, ListOfKillersTmp[1])
469+
}
470+
471+
// loop through all killers and append to result
472+
for i := range ListOfKillers {
473+
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfKillers[i])
474+
DeathKillers = append(DeathKillers, Killers{
475+
Name: strings.TrimSuffix(strings.TrimSuffix(name, ".</td>"), "."),
476+
Player: isPlayer,
477+
Traded: isTraded,
478+
Summon: theSummon,
451479
})
452480
}
453481

482+
// append deadentry to death list
483+
DeathsData = append(DeathsData, Deaths{
484+
Time: time,
485+
Level: level,
486+
Killers: DeathKillers,
487+
Assists: DeathAssists,
488+
Reason: reasonString,
489+
})
490+
454491
return true
455492
})
456493
case "Characters":

0 commit comments

Comments
 (0)