This commit is contained in:
parent
320c60a0e4
commit
ec93573cba
7 changed files with 320 additions and 0 deletions
57
internal/a2s/players.go
Normal file
57
internal/a2s/players.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package a2s
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"reforgerds-updater/internal/bread"
|
||||
)
|
||||
|
||||
// https://developer.valvesoftware.com/wiki/Server_queries#Response_Format_2
|
||||
type Player struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Duration time.Duration `json:"duration,omitempty"`
|
||||
Score uint32 `json:"score,omitempty"`
|
||||
Index byte `json:"index,omitempty"`
|
||||
}
|
||||
|
||||
// Get A2S_PLAYER
|
||||
func (c *Client) GetPlayers() (*[]Player, error) {
|
||||
data, _, _, err := c.Get(PlayerRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(data)
|
||||
count, err := bread.Byte(buf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w count: %w", ErrPlayerRead, err)
|
||||
}
|
||||
|
||||
players := []Player{}
|
||||
|
||||
for i := 0; i < int(count); i++ {
|
||||
player := Player{}
|
||||
|
||||
if player.Index, err = bread.Byte(buf); err != nil {
|
||||
return nil, fmt.Errorf("%w index: %w", ErrPlayerRead, err)
|
||||
}
|
||||
|
||||
if player.Name, err = bread.String(buf); err != nil {
|
||||
return nil, fmt.Errorf("%w name: %w", ErrPlayerRead, err)
|
||||
}
|
||||
|
||||
if player.Score, err = bread.Uint32(buf); err != nil {
|
||||
return nil, fmt.Errorf("%w score: %w", ErrPlayerRead, err)
|
||||
}
|
||||
|
||||
if player.Duration, err = bread.Duration32(buf); err != nil {
|
||||
return nil, fmt.Errorf("%w duration: %w", ErrPlayerRead, err)
|
||||
}
|
||||
|
||||
players = append(players, player)
|
||||
}
|
||||
|
||||
return &players, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue