forked from TF2Stadium/Helen
/
lobby_decorators.go
103 lines (80 loc) · 2.51 KB
/
lobby_decorators.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
package decorators
import (
"strconv"
chelpers "github.com/TF2Stadium/Helen/controllers/controllerhelpers"
db "github.com/TF2Stadium/Helen/database"
"github.com/TF2Stadium/Helen/models"
"github.com/bitly/go-simplejson"
)
func getSlotDetails(lobby *models.Lobby, slot int) (string, string, bool) {
steamid := ""
name := ""
ready := false
playerId, err := lobby.GetPlayerIdBySlot(slot)
if err == nil {
var player models.Player
db.DB.First(&player, playerId)
steamid = player.SteamId
name = player.Name
ready, _ = lobby.IsPlayerReady(&player)
}
return steamid, name, ready
}
func GetLobbyDataJSON(lobby models.Lobby) *simplejson.Json {
lobbyJs := simplejson.New()
lobbyJs.Set("id", lobby.ID)
lobbyJs.Set("type", models.FormatMap[lobby.Type])
lobbyJs.Set("createdAt", lobby.CreatedAt.Unix())
lobbyJs.Set("players", lobby.GetPlayerNumber())
lobbyJs.Set("map", lobby.MapName)
classes := simplejson.New()
var classMap = chelpers.FormatClassMap(lobby.Type)
lobbyJs.Set("maxPlayers", len(classMap)*2)
for className, slot := range classMap {
class := simplejson.New()
red := simplejson.New()
blu := simplejson.New()
steamid, name, ready := getSlotDetails(&lobby, slot)
red.Set("steamid", steamid)
red.Set("name", name)
red.Set("ready", ready)
steamid, name, ready = getSlotDetails(&lobby, slot+models.TypePlayerCount[lobby.Type])
blu.Set("steamid", steamid)
blu.Set("name", name)
blu.Set("ready", ready)
class.Set("red", red)
class.Set("blu", blu)
classes.Set(className, class)
}
lobbyJs.Set("classes", classes)
return lobbyJs
}
func GetLobbyListData(lobbies []models.Lobby) (string, error) {
if len(lobbies) == 0 {
return "{}", nil
}
var lobbyList []*simplejson.Json
for _, lobby := range lobbies {
lobbyJs := GetLobbyDataJSON(lobby)
lobbyList = append(lobbyList, lobbyJs)
}
listObj := simplejson.New()
listObj.Set("lobbies", lobbyList)
bytes, _ := listObj.MarshalJSON()
return string(bytes), nil
}
func GetLobbyConnectJSON(lobby *models.Lobby) *simplejson.Json {
json := simplejson.New()
json.Set("id", lobby.ID)
json.Set("time", lobby.CreatedAt.Unix())
json.Set("password", lobby.Server.ServerPassword)
game := simplejson.New()
game.Set("host", lobby.Server.Info.Host)
json.Set("game", game)
mumble := simplejson.New()
mumble.Set("ip", "we still need to decide on mumble connections")
mumble.Set("port", "we still need to decide on mumble connections")
mumble.Set("channel", "match"+strconv.FormatUint(uint64(lobby.ID), 10))
json.Set("mumble", mumble)
return json
}