本文整理匯總了Golang中github.com/TF2Stadium/Helen/controllers/controllerhelpers.BuildSuccessJSON函數的典型用法代碼示例。如果您正苦於以下問題:Golang BuildSuccessJSON函數的具體用法?Golang BuildSuccessJSON怎麽用?Golang BuildSuccessJSON使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BuildSuccessJSON函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PlayerSettingsGet
func PlayerSettingsGet(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, playerSettingsGetFilter,
func(params map[string]interface{}) string {
player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
key := params["key"].(string)
var err error
var settings []models.PlayerSetting
var setting models.PlayerSetting
if key == "" {
settings, err = player.GetSettings()
} else {
setting, err = player.GetSetting(key)
settings = append(settings, setting)
}
if err != nil {
bytes, _ := chelpers.BuildFailureJSON(err.Error(), 0).Encode()
return string(bytes)
}
result := models.DecoratePlayerSettingsJson(settings)
resp, _ := chelpers.BuildSuccessJSON(result).Encode()
return string(resp)
})
}
示例2: PlayerSettingsGet
func (Player) PlayerSettingsGet(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
reqerr := chelpers.FilterRequest(so, 0, true)
if reqerr != nil {
return reqerr.Encode()
}
var args struct {
Key string `json:"key"`
}
err := chelpers.GetParams(data, &args)
if err != nil {
return helpers.NewTPErrorFromError(err).Encode()
}
player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
var settings []models.PlayerSetting
var setting models.PlayerSetting
if args.Key == "*" {
settings, err = player.GetSettings()
} else {
setting, err = player.GetSetting(args.Key)
settings = append(settings, setting)
}
if err != nil {
return helpers.NewTPErrorFromError(err).Encode()
}
result := models.DecoratePlayerSettingsJson(settings)
resp, _ := chelpers.BuildSuccessJSON(result).Encode()
return resp
}
示例3: PlayerProfile
func (Player) PlayerProfile(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
reqerr := chelpers.FilterRequest(so, 0, true)
if reqerr != nil {
return reqerr.Encode()
}
var args struct {
Steamid string `json:"steamid"`
}
err := chelpers.GetParams(data, &args)
if err != nil {
return helpers.NewTPErrorFromError(err).Encode()
}
steamid := args.Steamid
if steamid == "" {
steamid = chelpers.GetSteamId(so.Id())
}
player, playErr := models.GetPlayerWithStats(steamid)
if playErr != nil {
return playErr.Encode()
}
result := models.DecoratePlayerProfileJson(player)
resp, _ := chelpers.BuildSuccessJSON(result).Encode()
return resp
}
示例4: DebugLobbyFill
func DebugLobbyFill(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, debugLobbyFillFilter,
func(param map[string]interface{}) string {
id := param["id"].(uint)
lobby, _ := models.GetLobbyById(id)
var players []*models.Player
for i := 1; i < int(lobby.Type)*2; i++ {
steamid := "DEBUG" + strconv.FormatUint(uint64(time.Now().Unix()), 10) + strconv.Itoa(i)
player, _ := models.NewPlayer(steamid)
player.Debug = true
player.Save()
players = append(players, player)
lobby.AddPlayer(player, i)
}
lobby.State = models.LobbyStateReadyingUp
lobby.Save()
room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobby.ID))
broadcaster.SendMessageToRoom(room, "lobbyReadyUp", "")
lobby.ReadyUpTimeoutCheck()
bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}
示例5: LobbyClose
func LobbyClose(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, lobbyCloseFilters,
func(params map[string]interface{}) string {
player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
lobbyid := params["id"].(uint)
lob, tperr := models.GetLobbyById(uint(lobbyid))
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
if player.SteamId != lob.CreatedBySteamID && player.Role != helpers.RoleAdmin {
bytes, _ := chelpers.BuildFailureJSON("Player not authorized to close lobby.", 1).Encode()
return string(bytes)
}
if lob.State == models.LobbyStateEnded {
bytes, _ := chelpers.BuildFailureJSON("Lobby already closed.", -1).Encode()
return string(bytes)
}
helpers.LockRecord(lob.ID, lob)
lob.Close(true)
helpers.UnlockRecord(lob.ID, lob)
chelpers.StopLogger(lobbyid)
models.BroadcastLobbyList() // has to be done manually for now
bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}
示例6: LobbyCreate
func LobbyCreate(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, lobbyCreateFilters,
func(params map[string]interface{}) string {
player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
mapName := params["mapName"].(string)
lobbytypestring := params["type"].(string)
league := params["league"].(string)
server := params["server"].(string)
rconPwd := params["rconpwd"].(string)
whitelist := int(params["whitelist"].(uint))
mumble := params["mumbleRequired"].(bool)
var playermap = map[string]models.LobbyType{
"debug": models.LobbyTypeDebug,
"sixes": models.LobbyTypeSixes,
"highlander": models.LobbyTypeHighlander,
}
lobbytype, _ := playermap[lobbytypestring]
randBytes := make([]byte, 6)
rand.Read(randBytes)
serverPwd := base64.URLEncoding.EncodeToString(randBytes)
//TODO what if playermap[lobbytype] is nil?
info := models.ServerRecord{
Host: server,
RconPassword: rconPwd,
ServerPassword: serverPwd}
err := models.VerifyInfo(info)
if err != nil {
return err.Error()
}
lob := models.NewLobby(mapName, lobbytype, league, info, whitelist, mumble)
lob.CreatedBySteamID = player.SteamId
lob.Save()
err = lob.SetupServer()
if err != nil {
bytes, _ := err.(*helpers.TPError).ErrorJSON().Encode()
return string(bytes)
}
lob.State = models.LobbyStateWaiting
lob.Save()
lobby_id := simplejson.New()
lobby_id.Set("id", lob.ID)
bytes, _ := chelpers.BuildSuccessJSON(lobby_id).Encode()
return string(bytes)
})
}
示例7: DebugRequestLobbyStart
func DebugRequestLobbyStart(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, debugRequestLobbyStartFilter,
func(params map[string]interface{}) string {
lobby, _ := models.GetLobbyById(params["id"].(uint))
bytes, _ := models.DecorateLobbyConnectJSON(lobby).Encode()
room := fmt.Sprintf("%s_private", chelpers.GetLobbyRoom(lobby.ID))
broadcaster.SendMessageToRoom(room,
"lobbyStart", string(bytes))
bytes, _ = chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}
示例8: PlayerReady
func PlayerReady(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, playerReadyFilter,
func(_ map[string]interface{}) string {
steamid := chelpers.GetSteamId(so.Id())
player, tperr := models.GetPlayerBySteamId(steamid)
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
lobbyid, tperr := player.GetLobbyId()
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
lobby, tperr := models.GetLobbyById(lobbyid)
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
if lobby.State != models.LobbyStateReadyingUp {
bytes, _ := helpers.NewTPError("Lobby hasn't been filled up yet.", 4).ErrorJSON().Encode()
return string(bytes)
}
helpers.LockRecord(lobby.ID, lobby)
tperr = lobby.ReadyPlayer(player)
defer helpers.UnlockRecord(lobby.ID, lobby)
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
if lobby.IsEveryoneReady() {
lobby.State = models.LobbyStateInProgress
lobby.Save()
bytes, _ := models.DecorateLobbyConnectJSON(lobby).Encode()
room := fmt.Sprintf("%s_private",
chelpers.GetLobbyRoom(lobby.ID))
broadcaster.SendMessageToRoom(room,
"lobbyStart", string(bytes))
models.BroadcastLobbyList()
}
bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}
示例9: RequestLobbyListData
func RequestLobbyListData(so socketio.Socket) func(string) string {
return func(s string) string {
var lobbies []models.Lobby
db.DB.Where("state = ?", models.LobbyStateWaiting).Order("id desc").Find(&lobbies)
list, err := models.DecorateLobbyListData(lobbies)
if err != nil {
helpers.Logger.Warning("Failed to send lobby list: %s", err.Error())
} else {
so.Emit("lobbyListData", list)
}
resp, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(resp)
}
}
示例10: ChatSend
func ChatSend(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, chatSendFilter,
func(params map[string]interface{}) string {
message := params["message"].(string)
room := params["room"].(int)
player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
helpers.Logger.Debug("received chat message: %s %s", message, player.Name)
spec := player.IsSpectatingId(uint(room))
//Check if player has either joined, or is spectating lobby
lobbyId, tperr := player.GetLobbyId()
if room > 0 {
if tperr != nil && !spec && lobbyId != uint(room) {
bytes, _ := chelpers.BuildFailureJSON("Player is not in the lobby.", 5).Encode()
return string(bytes)
}
} else {
// else room is the lobby list room
room, _ = strconv.Atoi(config.Constants.GlobalChatRoom)
}
chatMessage := simplejson.New()
// TODO send proper timestamps
chatMessage.Set("timestamp", time.Now().Unix())
chatMessage.Set("message", message)
chatMessage.Set("room", room)
chatMessage.Set("player", models.DecoratePlayerSummaryJson(player))
bytes, _ := chatMessage.Encode()
broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public",
chelpers.GetLobbyRoom(uint(room))),
"chatReceive", string(bytes))
resp, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
chelpers.LogChat(uint(room), player.Name, player.SteamId, message)
chelpers.AddScrollbackMessage(uint(room), string(bytes))
return string(resp)
})
}
示例11: PlayerNotReady
func PlayerNotReady(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, playerUnreadyFilter,
func(_ map[string]interface{}) string {
player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
lobbyid, tperr := player.GetLobbyId()
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
lobby, tperr := models.GetLobbyById(lobbyid)
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
if lobby.State != models.LobbyStateReadyingUp {
bytes, _ := helpers.NewTPError("Lobby hasn't been filled up yet.", 4).ErrorJSON().Encode()
return string(bytes)
}
helpers.LockRecord(lobby.ID, lobby)
tperr = lobby.UnreadyPlayer(player)
lobby.RemovePlayer(player)
helpers.UnlockRecord(lobby.ID, lobby)
if tperr != nil {
bytes, _ := tperr.ErrorJSON().Encode()
return string(bytes)
}
lobby.UnreadyAllPlayers()
bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}
示例12: PlayerSettingsSet
func PlayerSettingsSet(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, playerSettingsSetFilter,
func(params map[string]interface{}) string {
player, _ := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
key := params["key"].(string)
value := params["value"].(string)
err := player.SetSetting(key, value)
if err != nil {
bytes, _ := chelpers.BuildFailureJSON(err.Error(), 0).Encode()
return string(bytes)
}
resp, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(resp)
})
}
示例13: ServerVerify
func ServerVerify(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, serverVerifyFilters,
func(params map[string]interface{}) string {
info := models.ServerRecord{
Host: params["server"].(string),
RconPassword: params["rconpwd"].(string),
}
err := models.VerifyInfo(info)
if err != nil {
bytes, _ := chelpers.BuildFailureJSON(err.Error(), -1).Encode()
return string(bytes)
}
bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}
示例14: GetConstant
func (Global) GetConstant(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
var args struct {
Constant string `json:"constant"`
}
if err := chelpers.GetParams(data, &args); err != nil {
return helpers.NewTPErrorFromError(err).Encode()
}
output := simplejson.New()
switch args.Constant {
case "lobbySettingsList":
output = models.LobbySettingsToJson()
default:
return helpers.NewTPError("Unknown constant.", -1).Encode()
}
bytes, _ := chelpers.BuildSuccessJSON(output).Encode()
return bytes
}
示例15: DebugLobbyReady
func DebugLobbyReady(so socketio.Socket) func(string) string {
return chelpers.FilterRequest(so, debugLobbyReadyFilter,
func(param map[string]interface{}) string {
id := param["id"].(uint)
lobby, _ := models.GetLobbyById(id)
var slots []models.LobbySlot
db.DB.Where("lobby_id = ?", lobby.ID).Find(&slots)
for _, slot := range slots {
slot.Ready = true
db.DB.Save(slot)
}
lobby.OnChange(true)
bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
return string(bytes)
})
}