-
Notifications
You must be signed in to change notification settings - Fork 5
/
ds.go
184 lines (159 loc) · 5.58 KB
/
ds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2019-2020 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package model
import (
"errors"
"fmt"
"time"
)
// Server status constants
const (
ServerStatusCreating = "CREATING"
ServerStatusReady = "READY"
ServerStatusBusy = "BUSY"
)
// Error is response sent when an error occurs
type Error struct {
ErrorCode int `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
}
// CreateSessionRequest contains
// request to spawn a new dedicated server
type CreateSessionRequest struct {
SessionID string `json:"session_id"`
Namespace string `json:"namespace"`
GameMode string `json:"game_mode"`
PodName string `json:"pod_name"`
ClientVersion string `json:"client_version"`
Region string `json:"region"`
Configuration string `json:"configuration"`
MatchingAllies []RequestMatchingAlly `json:"matching_allies"`
Deployment string `json:"deployment"`
}
func (req *CreateSessionRequest) String() string {
return fmt.Sprintf("CreateSessionRequest{SessionID:%s, Namespace:%s, GameMode:%s, PodName:%s, ClientVersion:%s, Region:%s, MatchingAllies:%+v, Deployment:%s}", req.SessionID, req.Namespace, req.GameMode, req.PodName, req.ClientVersion, req.Region, req.MatchingAllies, req.Deployment)
}
// Validate returns error if any field is missing
func (req *CreateSessionRequest) Validate() error {
if req.Namespace == "" {
return errors.New("namespace cannot be empty")
}
if req.SessionID == "" {
return errors.New("session ID cannot be empty")
}
if req.GameMode == "" {
return errors.New("game mode cannot be empty")
}
if len(req.MatchingAllies) == 0 {
return errors.New("need 1 or more allies")
}
for _, ally := range req.MatchingAllies {
if err := ally.Validate(); err != nil {
return err
}
}
return nil
}
// RequestMatchingAlly contains party on the same side
type RequestMatchingAlly struct {
MatchingParties []RequestMatchParty `json:"matching_parties"`
}
// Validate returns error if any field is missing
func (req *RequestMatchingAlly) Validate() error {
if len(req.MatchingParties) == 0 {
return errors.New("need 1 or more parties")
}
for _, party := range req.MatchingParties {
if err := party.Validate(); err != nil {
return err
}
}
return nil
}
// RequestMatchParty is the matching party of a match
type RequestMatchParty struct {
PartyID string `json:"party_id"`
PartyAttributes map[string]interface{} `json:"party_attributes"`
PartyMembers []RequestMatchMember `json:"party_members"`
}
// Validate returns error if any field is missing
func (req *RequestMatchParty) Validate() error {
if req.PartyID == "" {
return errors.New("party ID cannot be empty")
}
if len(req.PartyMembers) == 0 {
return errors.New("need 1 or more party members")
}
for _, member := range req.PartyMembers {
if err := member.Validate(); err != nil {
return err
}
}
return nil
}
// Members return party member user ID in slice format
func (req *RequestMatchParty) Members() []string {
ids := make([]string, 0, len(req.PartyMembers))
for _, member := range req.PartyMembers {
ids = append(ids, member.UserID)
}
return ids
}
// RequestMatchMember is the member of match party
type RequestMatchMember struct {
UserID string `json:"user_id"`
}
// Validate returns error if any field is missing
func (req *RequestMatchMember) Validate() error {
if req.UserID == "" {
return errors.New("party member user ID cannot be empty")
}
return nil
}
// Server represents a DS server
// currently managed by the service
type Server struct {
PodName string `json:"pod_name"`
ImageVersion string `json:"image_version"`
Namespace string `json:"namespace"`
IP string `json:"ip"`
AlternateIPs []string `json:"alternate_ips"`
Port int `json:"port"`
Protocol string `json:"protocol"`
Ports map[string]int `json:"ports"`
Provider string `json:"provider"`
GameVersion string `json:"game_version"`
StatusText string `json:"status"`
LastUpdate time.Time `json:"last_update"`
SessionID string `json:"session_id"`
Deployment string `json:"deployment"`
Region string `json:"region"`
IsOverrideGameVersion bool `json:"is_override_game_version"`
CustomAttribute string `json:"custom_attribute"`
}
func (s *Server) String() string {
return fmt.Sprintf("Server{PodName:%s, ImageVersion: %s, Namespace:%s, IP:%s, AlternateIPs:%v, Port:%d, StatusText:%s, Deployment:%s}",
s.PodName, s.ImageVersion, s.Namespace, s.IP, s.AlternateIPs, s.Port, s.StatusText, s.Deployment)
}
// Session represents a game session
// currently managed by the service
type Session struct {
ID string `json:"id"`
Region string `json:"region"`
*Server `json:"Server"`
}
func (s *Session) String() string {
return fmt.Sprintf("Session{ID:%s, Region:%s, Server:%v}", s.ID, s.Region, s.Server)
}
// ClaimSessionRequest contains
// request to claim a dedicated server
type ClaimSessionRequest struct {
SessionID string `json:"session_id"`
Namespace string `json:"-"`
}
// SessionResponse is the response
// for get server endpoint
type SessionResponse struct {
Session *Session `json:"session"`
}