Skip to content

Commit 07c1a86

Browse files
committed
chore: fix assisted death-parsing logic
1 parent bd74ce2 commit 07c1a86

File tree

1 file changed

+118
-99
lines changed

1 file changed

+118
-99
lines changed

src/TibiaCharactersCharacter.go

Lines changed: 118 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string, url string) (CharacterR
383383
return false
384384
}
385385

386-
// Removing line breaks
386+
// Removing line breaks and sanitizing string
387387
CharacterListHTML = TibiaDataHTMLRemoveLinebreaks(CharacterListHTML)
388388
CharacterListHTML = strings.ReplaceAll(strings.ReplaceAll(CharacterListHTML, "<br/>", " "), " ", " ")
389389
CharacterListHTML = TibiaDataSanitizeStrings(CharacterListHTML)
@@ -404,6 +404,7 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string, url string) (CharacterR
404404
initCestIndexer = `CEST`
405405
levelIndexer = `at Level `
406406
killersIndexer = `by `
407+
assistedIndexer = `Assisted by `
407408
)
408409

409410
var initIndexer string
@@ -445,7 +446,7 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string, url string) (CharacterR
445446
level := TibiaDataStringToInteger(dataNoTags[levelIdx:endLevelIdx])
446447

447448
// if kill is with assist only (and level is set to 25), then we reset level
448-
if reasonStart == "Assisted by " && level == 25 {
449+
if reasonStart == assistedIndexer && level == 25 {
449450
level = 0
450451
}
451452

@@ -458,10 +459,17 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string, url string) (CharacterR
458459

459460
rawListofKillers := CharacterListHTML[killersIdx:endKillersIdx]
460461

461-
// if kill is with assist..
462-
if strings.Contains(dataNoTags, ". Assisted by ") {
463-
TmpListOfDeath := strings.Split(CharacterListHTML, ". Assisted by ")
464-
rawListofKillers = TmpListOfDeath[0][killersIdx:]
462+
// A death with assist (or with assist only)
463+
if strings.Contains(dataNoTags, assistedIndexer) {
464+
TmpListOfDeath := strings.Split(CharacterListHTML, assistedIndexer)
465+
466+
// get a list of killers
467+
if reasonStart != assistedIndexer {
468+
rawListofKillers = TmpListOfDeath[0][killersIdx:]
469+
rawListofKillers = strings.TrimSpace(strings.TrimSuffix(rawListofKillers, "."))
470+
} else {
471+
rawListofKillers = ""
472+
}
465473
TmpAssist := TmpListOfDeath[1]
466474

467475
// get a list of killers
@@ -478,120 +486,128 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string, url string) (CharacterR
478486

479487
for i := range ListOfAssists {
480488
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfAssists[i])
481-
DeathAssists = append(DeathAssists, Killers{
482-
Name: strings.TrimSuffix(strings.TrimSuffix(name, ".</td>"), "."),
483-
Player: isPlayer,
484-
Traded: isTraded,
485-
Summon: theSummon,
486-
})
489+
if name != "" { // Ensure we don't append empty names
490+
DeathAssists = append(DeathAssists, Killers{
491+
Name: name,
492+
Player: isPlayer,
493+
Traded: isTraded,
494+
Summon: theSummon,
495+
})
496+
}
487497
}
488498
}
489499

490-
// get a list of killers
491-
ListOfKillers := strings.Split(rawListofKillers, ", ")
500+
// A death with killers
501+
if rawListofKillers != "" {
502+
503+
// get a list of killers
504+
ListOfKillers := strings.Split(rawListofKillers, ", ")
492505

493-
const andStr = " and "
494-
lastItem := ListOfKillers[len(ListOfKillers)-1]
495-
lastAndIdx := strings.LastIndex(lastItem, andStr)
506+
const andStr = " and "
507+
lastItem := ListOfKillers[len(ListOfKillers)-1]
508+
lastAndIdx := strings.LastIndex(lastItem, andStr)
496509

497-
if lastAndIdx > -1 {
498-
if !strings.Contains(lastItem, "<a href=") {
499-
ListOfKillers[len(ListOfKillers)-1] = lastItem[:lastAndIdx]
500-
ListOfKillers = append(ListOfKillers, lastItem[lastAndIdx+len(andStr):])
501-
} else {
502-
ListOfKillers = ListOfKillers[:len(ListOfKillers)-1]
503-
504-
const (
505-
nonTag = iota // outside of a tag
506-
openAnchorTag // inside a <a>
507-
closeAchorTag // inside a </a>
508-
)
509-
510-
var (
511-
buffer bytes.Buffer
512-
state = nonTag
513-
)
514-
buffer.Grow(200) // arbitrary number to avoid allocations
515-
516-
for i := range lastItem {
517-
cur := lastItem[i]
518-
switch state {
519-
case nonTag:
520-
if cur == '<' {
521-
switch lastItem[i+1] {
522-
case '/':
523-
state = closeAchorTag
524-
default:
525-
state = openAnchorTag
526-
if buffer.Len() > 0 {
527-
str := buffer.String()
528-
529-
str = strings.TrimPrefix(str, " and ")
530-
str = strings.TrimSuffix(str, " and ")
531-
532-
if str == "" {
533-
buffer.Reset()
534-
buffer.WriteByte(cur)
535-
continue
536-
}
510+
if lastAndIdx > -1 {
511+
if !strings.Contains(lastItem, "<a href=") {
512+
ListOfKillers[len(ListOfKillers)-1] = lastItem[:lastAndIdx]
513+
ListOfKillers = append(ListOfKillers, lastItem[lastAndIdx+len(andStr):])
514+
} else {
515+
ListOfKillers = ListOfKillers[:len(ListOfKillers)-1]
516+
517+
const (
518+
nonTag = iota // outside of a tag
519+
openAnchorTag // inside a <a>
520+
closeAchorTag // inside a </a>
521+
)
522+
523+
var (
524+
buffer bytes.Buffer
525+
state = nonTag
526+
)
527+
buffer.Grow(200) // arbitrary number to avoid allocations
528+
529+
for i := range lastItem {
530+
cur := lastItem[i]
531+
switch state {
532+
case nonTag:
533+
if cur == '<' {
534+
switch lastItem[i+1] {
535+
case '/':
536+
state = closeAchorTag
537+
default:
538+
state = openAnchorTag
539+
if buffer.Len() > 0 {
540+
str := buffer.String()
541+
542+
str = strings.TrimPrefix(str, " and ")
543+
str = strings.TrimSuffix(str, " and ")
544+
545+
if str == "" {
546+
buffer.Reset()
547+
buffer.WriteByte(cur)
548+
continue
549+
}
550+
551+
if strings.Contains(str, "of") && !containsCreaturesWithOf(str) {
552+
// this is a summon
553+
buffer.WriteByte(cur)
554+
continue
555+
}
537556

538-
if strings.Contains(str, "of") && !containsCreaturesWithOf(str) {
539-
// this is a summon
540-
buffer.WriteByte(cur)
541-
continue
557+
buffer.Reset()
558+
ListOfKillers = append(ListOfKillers, str)
542559
}
543-
544-
buffer.Reset()
545-
ListOfKillers = append(ListOfKillers, str)
546560
}
547561
}
562+
buffer.WriteByte(cur)
563+
case openAnchorTag:
564+
if cur == '>' {
565+
state = nonTag
566+
}
567+
buffer.WriteByte(cur)
568+
case closeAchorTag:
569+
buffer.WriteByte(cur)
570+
if cur == '>' {
571+
str := buffer.String()
572+
573+
str = strings.TrimPrefix(str, " and ")
574+
str = strings.TrimSuffix(str, " and ")
575+
576+
ListOfKillers = append(ListOfKillers, str)
577+
buffer.Reset()
578+
state = nonTag
579+
}
548580
}
549-
buffer.WriteByte(cur)
550-
case openAnchorTag:
551-
if cur == '>' {
552-
state = nonTag
553-
}
554-
buffer.WriteByte(cur)
555-
case closeAchorTag:
556-
buffer.WriteByte(cur)
557-
if cur == '>' {
558-
str := buffer.String()
581+
}
559582

560-
str = strings.TrimPrefix(str, " and ")
561-
str = strings.TrimSuffix(str, " and ")
583+
if buffer.Len() > 0 {
584+
str := buffer.String()
585+
buffer.Reset()
562586

587+
str = strings.TrimPrefix(str, " and ")
588+
str = strings.TrimSuffix(str, " and ")
589+
590+
if str != "" {
563591
ListOfKillers = append(ListOfKillers, str)
564-
buffer.Reset()
565-
state = nonTag
566592
}
567593
}
568594
}
595+
}
569596

570-
if buffer.Len() > 0 {
571-
str := buffer.String()
572-
buffer.Reset()
573-
574-
str = strings.TrimPrefix(str, " and ")
575-
str = strings.TrimSuffix(str, " and ")
576-
577-
if str != "" {
578-
ListOfKillers = append(ListOfKillers, str)
579-
}
597+
// loop through all killers and append to result
598+
for i := range ListOfKillers {
599+
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfKillers[i])
600+
if name != "" { // Ensure we don't append empty names
601+
DeathKillers = append(DeathKillers, Killers{
602+
Name: name,
603+
Player: isPlayer,
604+
Traded: isTraded,
605+
Summon: theSummon,
606+
})
580607
}
581608
}
582609
}
583610

584-
// loop through all killers and append to result
585-
for i := range ListOfKillers {
586-
name, isPlayer, isTraded, theSummon := TibiaDataParseKiller(ListOfKillers[i])
587-
DeathKillers = append(DeathKillers, Killers{
588-
Name: strings.TrimSuffix(strings.TrimSuffix(name, ".</td>"), "."),
589-
Player: isPlayer,
590-
Traded: isTraded,
591-
Summon: theSummon,
592-
})
593-
}
594-
595611
// append deadentry to death list
596612
DeathsData = append(DeathsData, Deaths{
597613
Time: time,
@@ -761,6 +777,9 @@ func TibiaDataParseKiller(data string) (string, bool, bool, string) {
761777
data = RemoveHtmlTag(data)
762778
}
763779

780+
// remove htlm, spaces and dots from data-string
781+
data = strings.TrimSpace(strings.TrimSuffix(strings.TrimSuffix(data, "</td>"), "."))
782+
764783
// get summon information
765784
if strings.HasPrefix(data, "a ") || strings.HasPrefix(data, "an ") {
766785
if containsCreaturesWithOf(data) {

0 commit comments

Comments
 (0)