Paginate match history

This commit is contained in:
Trevor Slocum 2024-01-13 17:45:02 -08:00
parent d3ac00eb34
commit 7e06bf4e32
3 changed files with 29 additions and 3 deletions

View File

@ -54,7 +54,7 @@ formatted responses are more easily parsed by computers.
- `replay <id>`
- Retrieve replay of the specified game.
- `history <username>`
- `history <username> [page]`
- Retrieve match history of the specified player.
- `json <on/off>`

View File

@ -146,6 +146,8 @@ type HistoryMatch struct {
type EventHistory struct {
Event
Page int
Pages int
Matches []*HistoryMatch
CasualBackgammonSingle int
CasualBackgammonMulti int

View File

@ -1161,15 +1161,39 @@ COMMANDS:
cmd.client.sendNotice("Please specify the player as follows: history <username>")
continue
}
const historyPageSize = 50
page := 1
if len(params) > 1 {
p, err := strconv.Atoi(string(params[1]))
if err == nil && p >= 1 {
page = p
}
}
matches, err := matchHistory(string(params[0]))
if err != nil {
cmd.client.sendNotice("Invalid replay ID provided.")
continue
}
ev := &bgammon.EventHistory{
Matches: matches,
pages := (len(matches) / historyPageSize)
if pages == 0 {
pages = 1
}
ev := &bgammon.EventHistory{
Page: page,
Pages: pages,
}
if len(matches) > 0 && page <= pages {
max := page * historyPageSize
if max > len(matches) {
max = len(matches)
}
ev.Matches = matches[(page-1)*historyPageSize : max]
}
ev.Player = string(params[0])
a, err := accountByUsername(string(params[0]))
if err == nil && a != nil {