Paginate match history
This commit is contained in:
parent
d3ac00eb34
commit
7e06bf4e32
3 changed files with 29 additions and 3 deletions
|
@ -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>`
|
||||
|
|
2
event.go
2
event.go
|
@ -146,6 +146,8 @@ type HistoryMatch struct {
|
|||
|
||||
type EventHistory struct {
|
||||
Event
|
||||
Page int
|
||||
Pages int
|
||||
Matches []*HistoryMatch
|
||||
CasualBackgammonSingle int
|
||||
CasualBackgammonMulti int
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue