-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlobbyClient.go
96 lines (80 loc) · 2.45 KB
/
lobbyClient.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
package main
import (
"bytes"
"io"
"log"
"net/http"
"github.com/goccy/go-json"
)
const (
//LOBBY_ENDPOINT_UPSERT = "http://127.0.0.1:8080/server"
//LOBBY_ENDPOINT_UPSERT = "http://lobby.rogersm.net:8080/server"
LOBBY_ENDPOINT_UPSERT = "http://lobby.fujinet.online/server"
)
// Defaults for this game server
// Appkey/game are hard coded, but the others could be read from a config file
var DefaultGameServerDetails = GameServer{
Appkey: 1,
Game: "5 Card Stud",
Region: "us",
Serverurl: "https://5card.carr-designs.com/",
Clients: []GameClient{
{Platform: "atari", Url: "tnfs://ec.tnfs.io/atari/5card.xex"},
{Platform: "apple2", Url: "tnfs://ec.tnfs.io/apple2/5card.po"},
},
}
var UpdateLobby bool
type GameServer struct {
// Properties being sent from Game Server
Game string `json:"game"`
Appkey int `json:"appkey"`
Server string `json:"server"`
Region string `json:"region"`
Serverurl string `json:"serverurl"`
Status string `json:"status"`
Maxplayers int `json:"maxplayers"`
Curplayers int `json:"curplayers"`
Clients []GameClient `json:"clients"`
}
type GameClient struct {
Platform string `json:"platform"`
Url string `json:"url"`
}
func sendStateToLobby(maxPlayers int, curPlayers int, isOnline bool, server string, instanceUrlSuffix string) {
if !UpdateLobby {
return
}
// Start with copy of default game server details
serverDetails := DefaultGameServerDetails
serverDetails.Maxplayers = maxPlayers
serverDetails.Curplayers = curPlayers
if isOnline {
serverDetails.Status = "online"
} else {
serverDetails.Status = "offline"
}
serverDetails.Server = server
serverDetails.Serverurl += instanceUrlSuffix
jsonPayload, err := json.Marshal(serverDetails)
if err != nil {
panic(err)
}
log.Printf("Updating Lobby: %s", jsonPayload)
request, err := http.NewRequest("POST", LOBBY_ENDPOINT_UPSERT, bytes.NewBuffer(jsonPayload))
if err != nil {
panic(err)
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Println(err)
return
}
defer response.Body.Close()
log.Printf("Lobby Response: %s", response.Status)
if response.StatusCode > 300 {
body, _ := io.ReadAll(response.Body)
log.Println("response Body:", string(body))
}
}