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

fix character deaths where timestamp contains CEST #265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/TibiaCharactersCharacter.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,21 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string) (*CharacterResponse, er
DeathAssists := []Killers{}

const (
initIndexer = `CET`
levelIndexer = `at Level `
killersIndexer = `by `
initCetIndexer = `CET`
initCestIndexer = `CEST`
levelIndexer = `at Level `
killersIndexer = `by `
)

var initIndexer string
timeZoneIdentifiers := []string{initCetIndexer, initCestIndexer}
for _, tz := range timeZoneIdentifiers {
if strings.Contains(dataNoTags, tz) {
initIndexer = tz
break
}
}

initIdx := strings.Index(
dataNoTags, initIndexer,
) + len(initIndexer)
Expand All @@ -412,8 +422,8 @@ func TibiaCharactersCharacterImpl(BoxContentHTML string) (*CharacterResponse, er

timeIdx := 0
endTimeIdx := strings.Index(
dataNoTags[timeIdx:], `CET`,
) + timeIdx + len(`CET`)
dataNoTags[timeIdx:], initIndexer,
) + timeIdx + len(initIndexer)

time := TibiaDataDatetime(dataNoTags[timeIdx:endTimeIdx])

Expand Down
81 changes: 81 additions & 0 deletions src/TibiaCharactersCharacter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3034,6 +3034,87 @@ func TestNumber10(t *testing.T) {
assert.Equal(6, character.UnlockedTitles)
}

func TestNumber11(t *testing.T) {
file, err := static.TestFiles.Open("testdata/characters/Orca Kaoksh.html")
if err != nil {
t.Fatalf("file opening error: %s", err)
}
defer file.Close()

data, err := io.ReadAll(file)
if err != nil {
t.Fatalf("File reading error: %s", err)
}

characterJson, err := TibiaCharactersCharacterImpl(string(data))
if err != nil {
t.Fatal(err)
}

assert := assert.New(t)
character := characterJson.Character.CharacterInfo

assert.Equal("Orca Kaoksh", character.Name)

//validate death data
assert.Equal(2, len(characterJson.Character.Deaths))
deaths := characterJson.Character.Deaths

for idx, tc := range []struct {
Assists []Killers
Killers []Killers
Level int
Reason string
Time string
}{
{
Assists: []Killers{},
Killers: []Killers{
{Name: "fire", Player: false, Traded: false, Summon: ""},
},
Level: 10,
Reason: "Died at Level 10 by fire.",
Time: "2023-10-08T16:19:35Z",
},
{
Assists: []Killers{},
Killers: []Killers{
{Name: "wasp", Player: false, Traded: false, Summon: ""},
},
Level: 8,
Reason: "Died at Level 8 by wasp.",
Time: "2023-10-07T00:27:38Z",
},
} {
assert.True(
reflect.DeepEqual(deaths[idx].Assists, tc.Assists),
"Wrong assists\nidx: %d\nwant: %#v\n\ngot: %#v",
idx, tc.Assists, deaths[idx].Assists,
)
assert.True(
reflect.DeepEqual(deaths[idx].Killers, tc.Killers),
"Wrong killers\nidx: %d\nwant: %#v\n\ngot: %#v",
idx, tc.Killers, deaths[idx].Killers,
)
assert.Equal(
deaths[idx].Level, tc.Level,
"Wrong Level\nidx: %d\nwant: %d\n\ngot: %d",
idx, tc.Level, deaths[idx].Level,
)
assert.Equal(
deaths[idx].Reason, tc.Reason,
"Wrong Reason\nidx: %d\nwant: %s\n\ngot: %s",
idx, tc.Reason, deaths[idx].Reason,
)
assert.Equal(
deaths[idx].Time, tc.Time,
"Wrong Time\nidx: %d\nwant: %s\n\ngot: %s",
idx, tc.Time, deaths[idx].Time,
)
}

}

func BenchmarkNumber1(b *testing.B) {
file, err := static.TestFiles.Open("testdata/characters/Darkside Rafa.html")
if err != nil {
Expand Down
Loading