Skip to content

Commit 8b83ca3

Browse files
committed
refactor simple game server
1 parent 67f23df commit 8b83ca3

File tree

3 files changed

+702
-566
lines changed

3 files changed

+702
-566
lines changed

examples/simple-game-server/helper.go

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"strconv"
8+
"strings"
9+
"time"
10+
11+
coresdk "agones.dev/agones/pkg/sdk"
12+
sdk "agones.dev/agones/sdks/go"
13+
)
14+
15+
// ready attempts to mark this gameserver as ready
16+
func ready(s *sdk.SDK) {
17+
err := s.Ready()
18+
if err != nil {
19+
log.Fatalf("Could not send ready message")
20+
}
21+
}
22+
23+
// allocate attempts to allocate this gameserver
24+
func allocate(s *sdk.SDK) {
25+
err := s.Allocate()
26+
if err != nil {
27+
log.Fatalf("could not allocate gameserver: %v", err)
28+
}
29+
}
30+
31+
// reserve for 10 seconds
32+
func reserve(s *sdk.SDK, duration time.Duration) {
33+
if err := s.Reserve(duration); err != nil {
34+
log.Fatalf("could not reserve gameserver: %v", err)
35+
}
36+
}
37+
38+
// exit shutdowns the server
39+
func exit(s *sdk.SDK) {
40+
log.Printf("Received EXIT command. Exiting.")
41+
// This tells Agones to shutdown this Game Server
42+
shutdownErr := s.Shutdown()
43+
if shutdownErr != nil {
44+
log.Printf("Could not shutdown")
45+
}
46+
// The process will exit when Agones removes the pod and the
47+
// container receives the SIGTERM signal
48+
}
49+
50+
// gameServerName returns the GameServer name
51+
func gameServerName(s *sdk.SDK) string {
52+
var gs *coresdk.GameServer
53+
gs, err := s.GameServer()
54+
if err != nil {
55+
log.Fatalf("Could not retrieve GameServer: %v", err)
56+
}
57+
var j []byte
58+
j, err = json.Marshal(gs)
59+
if err != nil {
60+
log.Fatalf("error mashalling GameServer to JSON: %v", err)
61+
}
62+
log.Printf("GameServer: %s \n", string(j))
63+
return "NAME: " + gs.ObjectMeta.Name + "\n"
64+
}
65+
66+
// watchGameServerEvents creates a callback to log when
67+
// gameserver events occur
68+
func watchGameServerEvents(s *sdk.SDK) {
69+
err := s.WatchGameServer(func(gs *coresdk.GameServer) {
70+
j, err := json.Marshal(gs)
71+
if err != nil {
72+
log.Fatalf("error mashalling GameServer to JSON: %v", err)
73+
}
74+
log.Printf("GameServer Event: %s \n", string(j))
75+
})
76+
if err != nil {
77+
log.Fatalf("Could not watch Game Server events, %v", err)
78+
}
79+
}
80+
81+
// setAnnotation sets a given annotation
82+
func setAnnotation(s *sdk.SDK, key, value string) {
83+
log.Printf("Setting annotation %v=%v", key, value)
84+
err := s.SetAnnotation(key, value)
85+
if err != nil {
86+
log.Fatalf("could not set annotation: %v", err)
87+
}
88+
}
89+
90+
// setLabel sets a given label
91+
func setLabel(s *sdk.SDK, key, value string) {
92+
log.Printf("Setting label %v=%v", key, value)
93+
// label values can only be alpha, - and .
94+
err := s.SetLabel(key, value)
95+
if err != nil {
96+
log.Fatalf("could not set label: %v", err)
97+
}
98+
}
99+
100+
// setPlayerCapacity sets the player capacity to the given value
101+
func setPlayerCapacity(s *sdk.SDK, capacity int64) {
102+
log.Printf("Setting Player Capacity to %d", capacity)
103+
if err := s.Alpha().SetPlayerCapacity(capacity); err != nil {
104+
log.Fatalf("could not set capacity: %v", err)
105+
}
106+
}
107+
108+
// getPlayerCapacity returns the current player capacity as a string
109+
func getPlayerCapacity(s *sdk.SDK) string {
110+
log.Print("Getting Player Capacity")
111+
capacity, err := s.Alpha().GetPlayerCapacity()
112+
if err != nil {
113+
log.Fatalf("could not get capacity: %v", err)
114+
}
115+
return strconv.FormatInt(capacity, 10) + "\n"
116+
}
117+
118+
// playerConnect connects a given player
119+
func playerConnect(s *sdk.SDK, id string) {
120+
log.Printf("Connecting Player: %s", id)
121+
if _, err := s.Alpha().PlayerConnect(id); err != nil {
122+
log.Fatalf("could not connect player: %v", err)
123+
}
124+
}
125+
126+
// playerDisconnect disconnects a given player
127+
func playerDisconnect(s *sdk.SDK, id string) {
128+
log.Printf("Disconnecting Player: %s", id)
129+
if _, err := s.Alpha().PlayerDisconnect(id); err != nil {
130+
log.Fatalf("could not disconnect player: %v", err)
131+
}
132+
}
133+
134+
// playerIsConnected returns a bool as a string if a player is connected
135+
func playerIsConnected(s *sdk.SDK, id string) string {
136+
log.Printf("Checking if player %s is connected", id)
137+
138+
connected, err := s.Alpha().IsPlayerConnected(id)
139+
if err != nil {
140+
log.Fatalf("could not retrieve if player is connected: %v", err)
141+
}
142+
143+
return strconv.FormatBool(connected) + "\n"
144+
}
145+
146+
// getConnectedPlayers returns a comma delimeted list of connected players
147+
func getConnectedPlayers(s *sdk.SDK) string {
148+
log.Print("Retrieving connected player list")
149+
list, err := s.Alpha().GetConnectedPlayers()
150+
if err != nil {
151+
log.Fatalf("could not retrieve connected players: %s", err)
152+
}
153+
154+
return strings.Join(list, ",") + "\n"
155+
}
156+
157+
// getPlayerCount returns the count of connected players as a string
158+
func getPlayerCount(s *sdk.SDK) string {
159+
log.Print("Retrieving connected player count")
160+
count, err := s.Alpha().GetPlayerCount()
161+
if err != nil {
162+
log.Fatalf("could not retrieve player count: %s", err)
163+
}
164+
return strconv.FormatInt(count, 10) + "\n"
165+
}
166+
167+
// getCounterCount returns the Count of the given Counter as a string
168+
func getCounterCount(s *sdk.SDK, counterName string) (string, error) {
169+
log.Printf("Retrieving Counter %s Count", counterName)
170+
count, err := s.Beta().GetCounterCount(counterName)
171+
if err != nil {
172+
log.Printf("Error getting Counter %s Count: %s", counterName, err)
173+
return strconv.FormatInt(count, 10), err
174+
}
175+
return "COUNTER: " + strconv.FormatInt(count, 10) + "\n", nil
176+
}
177+
178+
// incrementCounter returns the if the Counter Count was incremented successfully or not
179+
func incrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) {
180+
amountInt, err := strconv.ParseInt(amount, 10, 64)
181+
if err != nil {
182+
return "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", counterName, amount, err)
183+
}
184+
log.Printf("Incrementing Counter %s Count by amount %d", counterName, amountInt)
185+
err = s.Beta().IncrementCounter(counterName, amountInt)
186+
if err != nil {
187+
log.Printf("Error incrementing Counter %s Count by amount %d: %s", counterName, amountInt, err)
188+
return "", err
189+
}
190+
return "SUCCESS\n", nil
191+
}
192+
193+
// decrementCounter returns if the Counter Count was decremented successfully or not
194+
func decrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) {
195+
amountInt, err := strconv.ParseInt(amount, 10, 64)
196+
if err != nil {
197+
return "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", counterName, amount, err)
198+
}
199+
log.Printf("Decrementing Counter %s Count by amount %d", counterName, amountInt)
200+
err = s.Beta().DecrementCounter(counterName, amountInt)
201+
if err != nil {
202+
log.Printf("Error decrementing Counter %s Count by amount %d: %s", counterName, amountInt, err)
203+
return "", err
204+
}
205+
return "SUCCESS\n", nil
206+
}
207+
208+
// setCounterCount returns the if the Counter was set to a new Count successfully or not
209+
func setCounterCount(s *sdk.SDK, counterName string, amount string) (string, error) {
210+
amountInt, err := strconv.ParseInt(amount, 10, 64)
211+
if err != nil {
212+
return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err)
213+
}
214+
log.Printf("Setting Counter %s Count to amount %d", counterName, amountInt)
215+
err = s.Beta().SetCounterCount(counterName, amountInt)
216+
if err != nil {
217+
log.Printf("Error setting Counter %s Count by amount %d: %s", counterName, amountInt, err)
218+
return "", err
219+
}
220+
return "SUCCESS\n", nil
221+
}
222+
223+
// getCounterCapacity returns the Capacity of the given Counter as a string
224+
func getCounterCapacity(s *sdk.SDK, counterName string) (string, error) {
225+
log.Printf("Retrieving Counter %s Capacity", counterName)
226+
count, err := s.Beta().GetCounterCapacity(counterName)
227+
if err != nil {
228+
log.Printf("Error getting Counter %s Capacity: %s", counterName, err)
229+
return strconv.FormatInt(count, 10), err
230+
}
231+
return "CAPACITY: " + strconv.FormatInt(count, 10) + "\n", nil
232+
}
233+
234+
// setCounterCapacity returns the if the Counter was set to a new Capacity successfully or not
235+
func setCounterCapacity(s *sdk.SDK, counterName string, amount string) (string, error) {
236+
amountInt, err := strconv.ParseInt(amount, 10, 64)
237+
if err != nil {
238+
return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err)
239+
}
240+
log.Printf("Setting Counter %s Capacity to amount %d", counterName, amountInt)
241+
err = s.Beta().SetCounterCapacity(counterName, amountInt)
242+
if err != nil {
243+
log.Printf("Error setting Counter %s Capacity to amount %d: %s", counterName, amountInt, err)
244+
return "", err
245+
}
246+
return "SUCCESS\n", nil
247+
}
248+
249+
// getListCapacity returns the Capacity of the given List as a string
250+
func getListCapacity(s *sdk.SDK, listName string) (string, error) {
251+
log.Printf("Retrieving List %s Capacity", listName)
252+
capacity, err := s.Beta().GetListCapacity(listName)
253+
if err != nil {
254+
log.Printf("Error getting List %s Capacity: %s", listName, err)
255+
return strconv.FormatInt(capacity, 10), err
256+
}
257+
return "CAPACITY: " + strconv.FormatInt(capacity, 10) + "\n", nil
258+
}
259+
260+
// setListCapacity returns if the List was set to a new Capacity successfully or not
261+
func setListCapacity(s *sdk.SDK, listName string, amount string) (string, error) {
262+
amountInt, err := strconv.ParseInt(amount, 10, 64)
263+
if err != nil {
264+
return "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", listName, amount, err)
265+
}
266+
log.Printf("Setting List %s Capacity to amount %d", listName, amountInt)
267+
err = s.Beta().SetListCapacity(listName, amountInt)
268+
if err != nil {
269+
log.Printf("Error setting List %s Capacity to amount %d: %s", listName, amountInt, err)
270+
return "", err
271+
}
272+
return "SUCCESS\n", nil
273+
}
274+
275+
// listContains returns true if the given value is in the given List, false otherwise
276+
func listContains(s *sdk.SDK, listName string, value string) (string, error) {
277+
log.Printf("Getting List %s contains value %s", listName, value)
278+
ok, err := s.Beta().ListContains(listName, value)
279+
if err != nil {
280+
log.Printf("Error getting List %s contains value %s: %s", listName, value, err)
281+
return strconv.FormatBool(ok), err
282+
}
283+
return "FOUND: " + strconv.FormatBool(ok) + "\n", nil
284+
}
285+
286+
// getListLength returns the length (number of values) of the given List as a string
287+
func getListLength(s *sdk.SDK, listName string) (string, error) {
288+
log.Printf("Getting List %s length", listName)
289+
length, err := s.Beta().GetListLength(listName)
290+
if err != nil {
291+
log.Printf("Error getting List %s length: %s", listName, err)
292+
return strconv.Itoa(length), err
293+
}
294+
return "LENGTH: " + strconv.Itoa(length) + "\n", nil
295+
}
296+
297+
// getListValues return the values in the given List as a comma delineated string
298+
func getListValues(s *sdk.SDK, listName string) (string, error) {
299+
log.Printf("Getting List %s values", listName)
300+
values, err := s.Beta().GetListValues(listName)
301+
if err != nil {
302+
log.Printf("Error getting List %s values: %s", listName, err)
303+
return "INVALID LIST NAME", err
304+
}
305+
if len(values) > 0 {
306+
return "VALUES: " + strings.Join(values, ",") + "\n", nil
307+
}
308+
return "VALUES: <none>\n", nil
309+
}
310+
311+
// appendListValue returns if the given value was successfuly added to the List or not
312+
func appendListValue(s *sdk.SDK, listName string, value string) (string, error) {
313+
log.Printf("Appending Value %s to List %s", value, listName)
314+
err := s.Beta().AppendListValue(listName, value)
315+
if err != nil {
316+
log.Printf("Error appending Value %s to List %s: %s", value, listName, err)
317+
return "", err
318+
}
319+
return "SUCCESS\n", nil
320+
}
321+
322+
// deleteListValue returns if the given value was successfuly deleted from the List or not
323+
func deleteListValue(s *sdk.SDK, listName string, value string) (string, error) {
324+
log.Printf("Deleting Value %s from List %s", value, listName)
325+
err := s.Beta().DeleteListValue(listName, value)
326+
if err != nil {
327+
log.Printf("Error deleting Value %s to List %s: %s", value, listName, err)
328+
return "", err
329+
}
330+
return "SUCCESS\n", nil
331+
}

0 commit comments

Comments
 (0)