2024-05-09 07:49:04 +00:00
|
|
|
package bunmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerSnapshot struct {
|
|
|
|
bun.BaseModel `bun:"table:server_snapshots,alias:ss"`
|
|
|
|
|
|
|
|
ID int `bun:"id,pk,autoincrement,identity"`
|
|
|
|
ServerKey string `bun:"server_key,nullzero"`
|
2024-05-13 05:12:05 +00:00
|
|
|
Server Server `bun:"server,rel:belongs-to,join:server_key=key"`
|
2024-05-09 07:49:04 +00:00
|
|
|
NumPlayers int `bun:"num_players"`
|
|
|
|
NumActivePlayers int `bun:"num_active_players"`
|
|
|
|
NumInactivePlayers int `bun:"num_inactive_players"`
|
|
|
|
NumTribes int `bun:"num_tribes"`
|
|
|
|
NumActiveTribes int `bun:"num_active_tribes"`
|
|
|
|
NumInactiveTribes int `bun:"num_inactive_tribes"`
|
|
|
|
NumVillages int `bun:"num_villages"`
|
|
|
|
NumPlayerVillages int `bun:"num_player_villages"`
|
|
|
|
NumBarbarianVillages int `bun:"num_barbarian_villages"`
|
|
|
|
NumBonusVillages int `bun:"num_bonus_villages"`
|
|
|
|
Date time.Time `bun:"date,nullzero"`
|
|
|
|
CreatedAt time.Time `bun:"created_at,nullzero"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss ServerSnapshot) ToDomain() (domain.ServerSnapshot, error) {
|
|
|
|
converted, err := domain.UnmarshalServerSnapshotFromDatabase(
|
|
|
|
ss.ID,
|
|
|
|
ss.ServerKey,
|
|
|
|
ss.NumPlayers,
|
|
|
|
ss.NumActivePlayers,
|
|
|
|
ss.NumInactivePlayers,
|
|
|
|
ss.NumTribes,
|
|
|
|
ss.NumActiveTribes,
|
|
|
|
ss.NumInactiveTribes,
|
|
|
|
ss.NumVillages,
|
|
|
|
ss.NumPlayerVillages,
|
|
|
|
ss.NumBarbarianVillages,
|
|
|
|
ss.NumBonusVillages,
|
|
|
|
ss.Date,
|
|
|
|
ss.CreatedAt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return domain.ServerSnapshot{}, fmt.Errorf(
|
|
|
|
"couldn't construct domain.ServerSnapshot (id=%d): %w",
|
|
|
|
ss.ID,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return converted, nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 05:12:05 +00:00
|
|
|
func (ss ServerSnapshot) ToDomainWithRelations() (domain.ServerSnapshotWithRelations, error) {
|
|
|
|
converted, err := ss.ToDomain()
|
|
|
|
if err != nil {
|
|
|
|
return domain.ServerSnapshotWithRelations{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := ss.Server.ToMeta()
|
|
|
|
if err != nil {
|
|
|
|
return domain.ServerSnapshotWithRelations{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return converted.WithRelations(server), nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 07:49:04 +00:00
|
|
|
type ServerSnapshots []ServerSnapshot
|
|
|
|
|
|
|
|
func (sss ServerSnapshots) ToDomain() (domain.ServerSnapshots, error) {
|
|
|
|
return sliceToDomain(sss)
|
|
|
|
}
|
2024-05-13 05:12:05 +00:00
|
|
|
|
|
|
|
func (sss ServerSnapshots) ToDomainWithRelations() (domain.ServerSnapshotsWithRelations, error) {
|
|
|
|
return sliceToDomainWithRelations(sss)
|
|
|
|
}
|