2023-12-21 06:28:53 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2023-12-24 10:44:20 +00:00
|
|
|
"fmt"
|
2023-12-28 10:56:59 +00:00
|
|
|
"math"
|
2023-12-21 06:28:53 +00:00
|
|
|
"net/url"
|
2023-12-25 09:13:42 +00:00
|
|
|
"slices"
|
2023-12-24 10:44:20 +00:00
|
|
|
"time"
|
2023-12-21 06:28:53 +00:00
|
|
|
)
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
type Server struct {
|
|
|
|
key string
|
|
|
|
versionCode string
|
|
|
|
url *url.URL
|
|
|
|
open bool
|
|
|
|
special bool
|
|
|
|
numPlayers int
|
|
|
|
numTribes int
|
|
|
|
numVillages int
|
|
|
|
numPlayerVillages int
|
|
|
|
numBarbarianVillages int
|
|
|
|
numBonusVillages int
|
|
|
|
config ServerConfig
|
|
|
|
buildingInfo BuildingInfo
|
|
|
|
unitInfo UnitInfo
|
|
|
|
createdAt time.Time
|
|
|
|
playerDataSyncedAt time.Time
|
|
|
|
playerSnapshotsCreatedAt time.Time
|
|
|
|
tribeDataSyncedAt time.Time
|
|
|
|
tribeSnapshotsCreatedAt time.Time
|
|
|
|
villageDataSyncedAt time.Time
|
|
|
|
ennoblementDataSyncedAt time.Time
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 10:56:59 +00:00
|
|
|
const serverModelName = "Server"
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
// UnmarshalServerFromDatabase unmarshals Server from the database.
|
|
|
|
//
|
|
|
|
// It should be used only for unmarshalling from the database!
|
|
|
|
// You can't use UnmarshalServerFromDatabase as constructor - It may put domain into the invalid state!
|
|
|
|
func UnmarshalServerFromDatabase(
|
|
|
|
key string,
|
|
|
|
versionCode string,
|
|
|
|
rawURL string,
|
|
|
|
open bool,
|
|
|
|
special bool,
|
|
|
|
numPlayers int,
|
|
|
|
numTribes int,
|
|
|
|
numVillages int,
|
|
|
|
numPlayerVillages int,
|
|
|
|
numBarbarianVillages int,
|
|
|
|
numBonusVillages int,
|
|
|
|
config ServerConfig,
|
|
|
|
buildingInfo BuildingInfo,
|
|
|
|
unitInfo UnitInfo,
|
|
|
|
createdAt time.Time,
|
|
|
|
playerDataSyncedAt time.Time,
|
|
|
|
playerSnapshotsCreatedAt time.Time,
|
|
|
|
tribeDataSyncedAt time.Time,
|
|
|
|
tribeSnapshotsCreatedAt time.Time,
|
|
|
|
villageDataSyncedAt time.Time,
|
|
|
|
ennoblementDataSyncedAt time.Time,
|
|
|
|
) (Server, error) {
|
|
|
|
if key == "" {
|
2023-12-28 10:56:59 +00:00
|
|
|
return Server{}, ValidationError{
|
|
|
|
Model: serverModelName,
|
|
|
|
Field: "key",
|
|
|
|
Err: ErrRequired,
|
|
|
|
}
|
2023-12-24 10:44:20 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 06:28:53 +00:00
|
|
|
if versionCode == "" {
|
2023-12-28 10:56:59 +00:00
|
|
|
return Server{}, ValidationError{
|
|
|
|
Model: serverModelName,
|
|
|
|
Field: "versionCode",
|
|
|
|
Err: ErrRequired,
|
|
|
|
}
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
u, err := parseURL(rawURL)
|
|
|
|
if err != nil {
|
2023-12-28 10:56:59 +00:00
|
|
|
return Server{}, ValidationError{
|
|
|
|
Model: serverModelName,
|
|
|
|
Field: "url",
|
|
|
|
Err: err,
|
|
|
|
}
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
return Server{
|
|
|
|
key: key,
|
|
|
|
url: u,
|
|
|
|
open: open,
|
|
|
|
special: special,
|
|
|
|
numPlayers: numPlayers,
|
|
|
|
numTribes: numTribes,
|
|
|
|
numVillages: numVillages,
|
|
|
|
numPlayerVillages: numPlayerVillages,
|
|
|
|
numBarbarianVillages: numBarbarianVillages,
|
|
|
|
numBonusVillages: numBonusVillages,
|
|
|
|
config: config,
|
|
|
|
buildingInfo: buildingInfo,
|
|
|
|
unitInfo: unitInfo,
|
|
|
|
createdAt: createdAt,
|
|
|
|
playerDataSyncedAt: playerDataSyncedAt,
|
|
|
|
playerSnapshotsCreatedAt: playerSnapshotsCreatedAt,
|
|
|
|
tribeDataSyncedAt: tribeDataSyncedAt,
|
|
|
|
tribeSnapshotsCreatedAt: tribeSnapshotsCreatedAt,
|
|
|
|
villageDataSyncedAt: villageDataSyncedAt,
|
|
|
|
ennoblementDataSyncedAt: ennoblementDataSyncedAt,
|
|
|
|
versionCode: versionCode,
|
|
|
|
}, nil
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func (s Server) Key() string {
|
|
|
|
return s.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) VersionCode() string {
|
|
|
|
return s.versionCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) URL() *url.URL {
|
|
|
|
return s.url
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) Open() bool {
|
|
|
|
return s.open
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) Special() bool {
|
|
|
|
return s.special
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) NumPlayers() int {
|
|
|
|
return s.numPlayers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) NumTribes() int {
|
|
|
|
return s.numTribes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) NumVillages() int {
|
|
|
|
return s.numVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) NumPlayerVillages() int {
|
|
|
|
return s.numPlayerVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) NumBarbarianVillages() int {
|
|
|
|
return s.numBarbarianVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) NumBonusVillages() int {
|
|
|
|
return s.numBonusVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) Config() ServerConfig {
|
|
|
|
return s.config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) BuildingInfo() BuildingInfo {
|
|
|
|
return s.buildingInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) UnitInfo() UnitInfo {
|
|
|
|
return s.unitInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) CreatedAt() time.Time {
|
|
|
|
return s.createdAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) PlayerDataSyncedAt() time.Time {
|
|
|
|
return s.playerDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) PlayerSnapshotsCreatedAt() time.Time {
|
|
|
|
return s.playerSnapshotsCreatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) TribeDataSyncedAt() time.Time {
|
|
|
|
return s.tribeDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) TribeSnapshotsCreatedAt() time.Time {
|
|
|
|
return s.tribeSnapshotsCreatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) VillageDataSyncedAt() time.Time {
|
|
|
|
return s.villageDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Server) EnnoblementDataSyncedAt() time.Time {
|
|
|
|
return s.ennoblementDataSyncedAt
|
|
|
|
}
|
|
|
|
|
2024-03-06 06:35:26 +00:00
|
|
|
func (s Server) ToCursor() (ServerCursor, error) {
|
|
|
|
return NewServerCursor(s.key, s.open)
|
2024-02-07 07:17:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 10:56:59 +00:00
|
|
|
func (s Server) Base() BaseServer {
|
|
|
|
return BaseServer{
|
|
|
|
key: s.key,
|
|
|
|
url: s.url,
|
|
|
|
open: s.open,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 06:35:26 +00:00
|
|
|
func (s Server) IsZero() bool {
|
|
|
|
return s == Server{}
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
type Servers []Server
|
|
|
|
|
2023-12-25 09:13:42 +00:00
|
|
|
// Close finds all servers with Server.Open returning true that are not in the given slice with open servers
|
|
|
|
// and then converts them to BaseServers with the corrected open value.
|
|
|
|
func (ss Servers) Close(open BaseServers) (BaseServers, error) {
|
|
|
|
res := make(BaseServers, 0, len(ss))
|
|
|
|
|
|
|
|
for _, s := range ss {
|
|
|
|
if !s.Open() || slices.ContainsFunc(open, func(openServer BaseServer) bool {
|
|
|
|
return openServer.Key() == s.Key() && openServer.Open() == s.Open()
|
|
|
|
}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-27 08:07:40 +00:00
|
|
|
base, err := NewBaseServer(s.Key(), s.URL(), false)
|
2023-12-25 09:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't construct BaseServer for server with key '%s': %w", s.Key(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
type CreateServerParams struct {
|
|
|
|
base BaseServer
|
|
|
|
versionCode string
|
|
|
|
}
|
|
|
|
|
2023-12-27 08:07:40 +00:00
|
|
|
const createServerParamsModelName = "CreateServerParams"
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func NewCreateServerParams(servers BaseServers, versionCode string) ([]CreateServerParams, error) {
|
|
|
|
if err := validateVersionCode(versionCode); err != nil {
|
|
|
|
return nil, ValidationError{
|
2023-12-27 08:07:40 +00:00
|
|
|
Model: createServerParamsModelName,
|
2023-12-24 10:44:20 +00:00
|
|
|
Field: "versionCode",
|
2023-12-27 08:07:40 +00:00
|
|
|
Err: err,
|
2023-12-24 10:44:20 +00:00
|
|
|
}
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
res := make([]CreateServerParams, 0, len(servers))
|
|
|
|
|
|
|
|
for i, s := range servers {
|
|
|
|
if s.IsZero() {
|
|
|
|
return nil, fmt.Errorf("servers[%d] is an empty struct", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, CreateServerParams{
|
|
|
|
base: s,
|
|
|
|
versionCode: versionCode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params CreateServerParams) Base() BaseServer {
|
|
|
|
return params.base
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params CreateServerParams) VersionCode() string {
|
|
|
|
return params.versionCode
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 11:04:12 +00:00
|
|
|
type UpdateServerParams struct {
|
2024-01-16 06:28:03 +00:00
|
|
|
config NullServerConfig
|
|
|
|
buildingInfo NullBuildingInfo
|
|
|
|
unitInfo NullUnitInfo
|
|
|
|
numTribes NullInt
|
|
|
|
tribeDataSyncedAt NullTime
|
|
|
|
numPlayers NullInt
|
|
|
|
playerDataSyncedAt NullTime
|
|
|
|
numVillages NullInt
|
|
|
|
numPlayerVillages NullInt
|
|
|
|
numBarbarianVillages NullInt
|
|
|
|
numBonusVillages NullInt
|
|
|
|
villageDataSyncedAt NullTime
|
|
|
|
ennoblementDataSyncedAt NullTime
|
|
|
|
tribeSnapshotsCreatedAt NullTime
|
|
|
|
playerSnapshotsCreatedAt NullTime
|
2023-12-25 11:04:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 09:10:03 +00:00
|
|
|
const updateServerParamsModelName = "UpdateServerParams"
|
|
|
|
|
2023-12-25 11:04:12 +00:00
|
|
|
func (params *UpdateServerParams) Config() NullServerConfig {
|
|
|
|
return params.config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetConfig(config NullServerConfig) error {
|
|
|
|
params.config = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) BuildingInfo() NullBuildingInfo {
|
|
|
|
return params.buildingInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetBuildingInfo(buildingInfo NullBuildingInfo) error {
|
|
|
|
params.buildingInfo = buildingInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) UnitInfo() NullUnitInfo {
|
|
|
|
return params.unitInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetUnitInfo(unitInfo NullUnitInfo) error {
|
|
|
|
params.unitInfo = unitInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-30 09:10:03 +00:00
|
|
|
func (params *UpdateServerParams) NumTribes() NullInt {
|
|
|
|
return params.numTribes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetNumTribes(numTribes NullInt) error {
|
|
|
|
if numTribes.Valid {
|
2024-02-28 06:59:10 +00:00
|
|
|
if err := validateIntInRange(numTribes.V, 0, math.MaxInt); err != nil {
|
2023-12-30 09:10:03 +00:00
|
|
|
return ValidationError{
|
|
|
|
Model: updateServerParamsModelName,
|
|
|
|
Field: "numTribes",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params.numTribes = numTribes
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) TribeDataSyncedAt() NullTime {
|
|
|
|
return params.tribeDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetTribeDataSyncedAt(tribeDataSyncedAt NullTime) error {
|
|
|
|
params.tribeDataSyncedAt = tribeDataSyncedAt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:53:59 +00:00
|
|
|
func (params *UpdateServerParams) NumPlayers() NullInt {
|
|
|
|
return params.numPlayers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetNumPlayers(numPlayers NullInt) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
if numPlayers.Valid {
|
|
|
|
if err := validateIntInRange(numPlayers.V, 0, math.MaxInt); err != nil {
|
|
|
|
return ValidationError{
|
|
|
|
Model: updateServerParamsModelName,
|
|
|
|
Field: "numPlayers",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:53:59 +00:00
|
|
|
params.numPlayers = numPlayers
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2024-01-01 10:53:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) PlayerDataSyncedAt() NullTime {
|
|
|
|
return params.playerDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetPlayerDataSyncedAt(playerDataSyncedAt NullTime) error {
|
|
|
|
params.playerDataSyncedAt = playerDataSyncedAt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) NumVillages() NullInt {
|
|
|
|
return params.numVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetNumVillages(numVillages NullInt) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
if numVillages.Valid {
|
|
|
|
if err := validateIntInRange(numVillages.V, 0, math.MaxInt); err != nil {
|
|
|
|
return ValidationError{
|
|
|
|
Model: updateServerParamsModelName,
|
|
|
|
Field: "numVillages",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:53:59 +00:00
|
|
|
params.numVillages = numVillages
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2024-01-01 10:53:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
func (params *UpdateServerParams) NumPlayerVillages() NullInt {
|
|
|
|
return params.numPlayerVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetNumPlayerVillages(numPlayerVillages NullInt) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
if numPlayerVillages.Valid {
|
|
|
|
if err := validateIntInRange(numPlayerVillages.V, 0, math.MaxInt); err != nil {
|
|
|
|
return ValidationError{
|
|
|
|
Model: updateServerParamsModelName,
|
|
|
|
Field: "numPlayerVillages",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
params.numPlayerVillages = numPlayerVillages
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) NumBarbarianVillages() NullInt {
|
|
|
|
return params.numBarbarianVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetNumBarbarianVillages(numBarbarianVillages NullInt) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
if numBarbarianVillages.Valid {
|
|
|
|
if err := validateIntInRange(numBarbarianVillages.V, 0, math.MaxInt); err != nil {
|
|
|
|
return ValidationError{
|
|
|
|
Model: updateServerParamsModelName,
|
|
|
|
Field: "numBarbarianVillages",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
params.numBarbarianVillages = numBarbarianVillages
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) NumBonusVillages() NullInt {
|
|
|
|
return params.numBonusVillages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetNumBonusVillages(numBonusVillages NullInt) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
if numBonusVillages.Valid {
|
|
|
|
if err := validateIntInRange(numBonusVillages.V, 0, math.MaxInt); err != nil {
|
|
|
|
return ValidationError{
|
|
|
|
Model: updateServerParamsModelName,
|
|
|
|
Field: "numBonusVillages",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
params.numBonusVillages = numBonusVillages
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2024-01-02 10:47:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 10:53:59 +00:00
|
|
|
func (params *UpdateServerParams) VillageDataSyncedAt() NullTime {
|
|
|
|
return params.villageDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetVillageDataSyncedAt(villageDataSyncedAt NullTime) error {
|
|
|
|
params.villageDataSyncedAt = villageDataSyncedAt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) EnnoblementDataSyncedAt() NullTime {
|
|
|
|
return params.ennoblementDataSyncedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetEnnoblementDataSyncedAt(ennoblementDataSyncedAt NullTime) error {
|
|
|
|
params.ennoblementDataSyncedAt = ennoblementDataSyncedAt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 07:00:08 +00:00
|
|
|
func (params *UpdateServerParams) TribeSnapshotsCreatedAt() NullTime {
|
|
|
|
return params.tribeSnapshotsCreatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetTribeSnapshotsCreatedAt(tribeSnapshotsCreatedAt NullTime) error {
|
|
|
|
params.tribeSnapshotsCreatedAt = tribeSnapshotsCreatedAt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-16 06:28:03 +00:00
|
|
|
func (params *UpdateServerParams) PlayerSnapshotsCreatedAt() NullTime {
|
|
|
|
return params.playerSnapshotsCreatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *UpdateServerParams) SetPlayerSnapshotsCreatedAt(playerSnapshotsCreatedAt NullTime) error {
|
|
|
|
params.playerSnapshotsCreatedAt = playerSnapshotsCreatedAt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 07:00:08 +00:00
|
|
|
//nolint:gocyclo
|
2023-12-25 11:04:12 +00:00
|
|
|
func (params *UpdateServerParams) IsZero() bool {
|
|
|
|
return !params.config.Valid &&
|
|
|
|
!params.buildingInfo.Valid &&
|
2023-12-30 09:10:03 +00:00
|
|
|
!params.unitInfo.Valid &&
|
|
|
|
!params.numTribes.Valid &&
|
2024-01-01 10:53:59 +00:00
|
|
|
!params.tribeDataSyncedAt.Valid &&
|
|
|
|
!params.numPlayers.Valid &&
|
|
|
|
!params.playerDataSyncedAt.Valid &&
|
|
|
|
!params.numVillages.Valid &&
|
2024-03-01 06:33:48 +00:00
|
|
|
!params.numPlayerVillages.Valid &&
|
|
|
|
!params.numBarbarianVillages.Valid &&
|
|
|
|
!params.numBonusVillages.Valid &&
|
2024-01-01 10:53:59 +00:00
|
|
|
!params.villageDataSyncedAt.Valid &&
|
2024-01-15 07:00:08 +00:00
|
|
|
!params.ennoblementDataSyncedAt.Valid &&
|
2024-01-16 06:28:03 +00:00
|
|
|
!params.tribeSnapshotsCreatedAt.Valid &&
|
|
|
|
!params.playerSnapshotsCreatedAt.Valid
|
2023-12-25 11:04:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
type ServerSort uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
ServerSortKeyASC ServerSort = iota + 1
|
|
|
|
ServerSortKeyDESC
|
|
|
|
ServerSortOpenASC
|
|
|
|
ServerSortOpenDESC
|
|
|
|
)
|
|
|
|
|
2024-03-02 08:49:42 +00:00
|
|
|
// IsInConflict returns true if two sorts can't be used together (e.g. ServerSortKeyASC and ServerSortKeyDESC).
|
|
|
|
func (s ServerSort) IsInConflict(s2 ServerSort) bool {
|
2024-03-15 06:57:21 +00:00
|
|
|
return isSortInConflict(s, s2)
|
2024-03-02 08:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s ServerSort) String() string {
|
|
|
|
switch s {
|
|
|
|
case ServerSortKeyASC:
|
|
|
|
return "key:ASC"
|
|
|
|
case ServerSortKeyDESC:
|
|
|
|
return "key:DESC"
|
|
|
|
case ServerSortOpenASC:
|
|
|
|
return "open:ASC"
|
|
|
|
case ServerSortOpenDESC:
|
|
|
|
return "open:DESC"
|
|
|
|
default:
|
|
|
|
return "unknown server sort"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 07:17:47 +00:00
|
|
|
type ServerCursor struct {
|
|
|
|
key string
|
|
|
|
open bool
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverCursorModelName = "ServerCursor"
|
|
|
|
|
|
|
|
func NewServerCursor(key string, open bool) (ServerCursor, error) {
|
|
|
|
if err := validateServerKey(key); err != nil {
|
|
|
|
return ServerCursor{}, ValidationError{
|
|
|
|
Model: serverCursorModelName,
|
|
|
|
Field: "key",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ServerCursor{key: key, open: open}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeServerCursor(encoded string) (ServerCursor, error) {
|
|
|
|
m, err := decodeCursor(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return ServerCursor{}, err
|
|
|
|
}
|
|
|
|
|
2024-02-19 07:17:38 +00:00
|
|
|
key, err := m.string("key")
|
2024-02-07 07:17:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return ServerCursor{}, ErrInvalidCursor
|
|
|
|
}
|
|
|
|
|
2024-02-19 07:17:38 +00:00
|
|
|
open, err := m.bool("open")
|
|
|
|
if err != nil {
|
|
|
|
return ServerCursor{}, ErrInvalidCursor
|
|
|
|
}
|
|
|
|
|
|
|
|
vc, err := NewServerCursor(key, open)
|
2024-02-07 07:17:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return ServerCursor{}, ErrInvalidCursor
|
|
|
|
}
|
|
|
|
|
|
|
|
return vc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc ServerCursor) Key() string {
|
|
|
|
return sc.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc ServerCursor) Open() bool {
|
|
|
|
return sc.open
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc ServerCursor) IsZero() bool {
|
|
|
|
return sc == ServerCursor{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc ServerCursor) Encode() string {
|
|
|
|
if sc.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-02-19 07:17:38 +00:00
|
|
|
return encodeCursor([]keyValuePair{
|
2024-02-07 07:17:47 +00:00
|
|
|
{"key", sc.key},
|
2024-02-19 07:17:38 +00:00
|
|
|
{"open", sc.open},
|
2024-02-07 07:17:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
type ListServersParams struct {
|
2024-01-14 11:57:32 +00:00
|
|
|
keys []string
|
|
|
|
versionCodes []string
|
|
|
|
open NullBool
|
|
|
|
special NullBool
|
|
|
|
tribeSnapshotsCreatedAtLT NullTime
|
|
|
|
playerSnapshotsCreatedAtLT NullTime
|
|
|
|
sort []ServerSort
|
2024-02-07 07:17:47 +00:00
|
|
|
cursor ServerCursor
|
2024-01-14 11:57:32 +00:00
|
|
|
limit int
|
2023-12-24 10:44:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 08:37:12 +00:00
|
|
|
const (
|
|
|
|
ServerListMaxLimit = 500
|
|
|
|
listServersParamsModelName = "ListServersParams"
|
|
|
|
)
|
2023-12-27 08:07:40 +00:00
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func NewListServersParams() ListServersParams {
|
|
|
|
return ListServersParams{
|
|
|
|
sort: []ServerSort{ServerSortKeyASC},
|
|
|
|
limit: ServerListMaxLimit,
|
|
|
|
special: NullBool{
|
2024-02-28 06:59:10 +00:00
|
|
|
V: false,
|
2023-12-24 10:44:20 +00:00
|
|
|
Valid: true,
|
|
|
|
},
|
|
|
|
}
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func (params *ListServersParams) Keys() []string {
|
|
|
|
return params.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetKeys(keys []string) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
for i, k := range keys {
|
|
|
|
if err := validateServerKey(k); err != nil {
|
|
|
|
return SliceElementValidationError{
|
|
|
|
Model: listServersParamsModelName,
|
|
|
|
Field: "keys",
|
|
|
|
Index: i,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
params.keys = keys
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-25 09:13:42 +00:00
|
|
|
func (params *ListServersParams) VersionCodes() []string {
|
|
|
|
return params.versionCodes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetVersionCodes(versionCodes []string) error {
|
2024-03-01 06:33:48 +00:00
|
|
|
for i, vc := range versionCodes {
|
|
|
|
if err := validateVersionCode(vc); err != nil {
|
|
|
|
return SliceElementValidationError{
|
|
|
|
Model: listServersParamsModelName,
|
|
|
|
Field: "versionCodes",
|
|
|
|
Index: i,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 09:13:42 +00:00
|
|
|
params.versionCodes = versionCodes
|
2024-03-01 06:33:48 +00:00
|
|
|
|
2023-12-25 09:13:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func (params *ListServersParams) Open() NullBool {
|
|
|
|
return params.open
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetOpen(open NullBool) error {
|
|
|
|
params.open = open
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) Special() NullBool {
|
|
|
|
return params.special
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetSpecial(special NullBool) error {
|
|
|
|
params.special = special
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-14 11:57:32 +00:00
|
|
|
func (params *ListServersParams) TribeSnapshotsCreatedAtLT() NullTime {
|
|
|
|
return params.tribeSnapshotsCreatedAtLT
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetTribeSnapshotsCreatedAtLT(tribeSnapshotsCreatedAtLT NullTime) error {
|
|
|
|
params.tribeSnapshotsCreatedAtLT = tribeSnapshotsCreatedAtLT
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) PlayerSnapshotsCreatedAtLT() NullTime {
|
|
|
|
return params.playerSnapshotsCreatedAtLT
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetPlayerSnapshotsCreatedAtLT(playerSnapshotsCreatedAtLT NullTime) error {
|
|
|
|
params.playerSnapshotsCreatedAtLT = playerSnapshotsCreatedAtLT
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func (params *ListServersParams) Sort() []ServerSort {
|
|
|
|
return params.sort
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
serverSortMinLength = 1
|
|
|
|
serverSortMaxLength = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetSort(sort []ServerSort) error {
|
2024-03-02 08:49:42 +00:00
|
|
|
if err := validateSort(sort, serverSortMinLength, serverSortMaxLength); err != nil {
|
2023-12-24 10:44:20 +00:00
|
|
|
return ValidationError{
|
2023-12-27 08:07:40 +00:00
|
|
|
Model: listServersParamsModelName,
|
2023-12-24 10:44:20 +00:00
|
|
|
Field: "sort",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params.sort = sort
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 07:17:47 +00:00
|
|
|
func (params *ListServersParams) Cursor() ServerCursor {
|
|
|
|
return params.cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetCursor(cursor ServerCursor) error {
|
|
|
|
params.cursor = cursor
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetEncodedCursor(encoded string) error {
|
|
|
|
decoded, err := decodeServerCursor(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return ValidationError{
|
|
|
|
Model: listServersParamsModelName,
|
|
|
|
Field: "cursor",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params.cursor = decoded
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 10:44:20 +00:00
|
|
|
func (params *ListServersParams) Limit() int {
|
|
|
|
return params.limit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *ListServersParams) SetLimit(limit int) error {
|
2023-12-28 10:56:59 +00:00
|
|
|
if err := validateIntInRange(limit, 1, ServerListMaxLimit); err != nil {
|
2023-12-24 10:44:20 +00:00
|
|
|
return ValidationError{
|
2023-12-27 08:07:40 +00:00
|
|
|
Model: listServersParamsModelName,
|
2023-12-24 10:44:20 +00:00
|
|
|
Field: "limit",
|
2023-12-28 10:56:59 +00:00
|
|
|
Err: err,
|
2023-12-24 10:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params.limit = limit
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 07:17:47 +00:00
|
|
|
type ListServersResult struct {
|
|
|
|
servers Servers
|
|
|
|
self ServerCursor
|
|
|
|
next ServerCursor
|
2023-12-24 10:44:20 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 07:17:47 +00:00
|
|
|
const listServersResultModelName = "ListServersResult"
|
|
|
|
|
|
|
|
func NewListServersResult(servers Servers, next Server) (ListServersResult, error) {
|
|
|
|
var err error
|
|
|
|
res := ListServersResult{
|
|
|
|
servers: servers,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(servers) > 0 {
|
2024-03-06 06:35:26 +00:00
|
|
|
res.self, err = servers[0].ToCursor()
|
2024-02-07 07:17:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return ListServersResult{}, ValidationError{
|
|
|
|
Model: listServersResultModelName,
|
|
|
|
Field: "self",
|
|
|
|
Err: err,
|
|
|
|
}
|
2023-12-24 10:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 07:17:47 +00:00
|
|
|
if !next.IsZero() {
|
2024-03-06 06:35:26 +00:00
|
|
|
res.next, err = next.ToCursor()
|
2024-02-07 07:17:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return ListServersResult{}, ValidationError{
|
|
|
|
Model: listServersResultModelName,
|
|
|
|
Field: "next",
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-24 10:44:20 +00:00
|
|
|
|
2024-02-07 07:17:47 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (res ListServersResult) Servers() Servers {
|
|
|
|
return res.servers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (res ListServersResult) Self() ServerCursor {
|
|
|
|
return res.self
|
|
|
|
}
|
|
|
|
|
|
|
|
func (res ListServersResult) Next() ServerCursor {
|
|
|
|
return res.next
|
2023-12-21 06:28:53 +00:00
|
|
|
}
|
2023-12-25 11:04:12 +00:00
|
|
|
|
|
|
|
type ServerNotFoundError struct {
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ErrorWithParams = ServerNotFoundError{}
|
|
|
|
|
|
|
|
func (e ServerNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("server with key %s not found", e.Key)
|
|
|
|
}
|
|
|
|
|
2024-02-01 06:48:03 +00:00
|
|
|
func (e ServerNotFoundError) Type() ErrorType {
|
|
|
|
return ErrorTypeNotFound
|
2023-12-25 11:04:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 07:01:47 +00:00
|
|
|
const errorCodeServerNotFound = "server-not-found"
|
|
|
|
|
2024-02-01 06:48:03 +00:00
|
|
|
func (e ServerNotFoundError) Code() string {
|
2024-03-18 07:01:47 +00:00
|
|
|
return errorCodeServerNotFound
|
2023-12-25 11:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ServerNotFoundError) Params() map[string]any {
|
|
|
|
return map[string]any{
|
|
|
|
"Key": e.Key,
|
|
|
|
}
|
|
|
|
}
|