Exemplo n.º 1
0
Arquivo: lobby.go Projeto: mcapu/Helen
func BroadcastLobby(lobby *Lobby) {
	bytes, _ := DecorateLobbyDataJSON(lobby, true).Encode()
	room := strconv.FormatUint(uint64(lobby.ID), 10)

	broadcaster.SendMessageToRoom(room, "lobbyData", string(bytes))
	broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public", room), "lobbyData", string(bytes))
}
Exemplo n.º 2
0
func BroadcastLobby(lobby *Lobby) {
	//db.DB.Preload("Spectators").First(&lobby, lobby.ID)
	bytes, _ := json.Marshal(DecorateLobbyData(lobby, true))
	room := strconv.FormatUint(uint64(lobby.ID), 10)

	broadcaster.SendMessageToRoom(room, "lobbyData", string(bytes))
	broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public", room), "lobbyData", string(bytes))
}
Exemplo n.º 3
0
//BroadcastLobbyList broadcasts the lobby list to all users
func BroadcastLobbyList() {
	lobbies := []Lobby{}
	db.DB.Where("state = ?", LobbyStateWaiting).Order("id desc").Find(&lobbies)
	broadcaster.SendMessageToRoom(
		fmt.Sprintf("%s_public", config.GlobalChatRoom),
		"lobbyListData", DecorateLobbyListData(lobbies))
}
Exemplo n.º 4
0
Arquivo: debug.go Projeto: N1xx1/Helen
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)

		})
}
Exemplo n.º 5
0
func BroadcastLobbyList() {
	var lobbies []Lobby
	db.DB.Where("state = ?", LobbyStateWaiting).Order("id desc").Find(&lobbies)
	bytes, _ := json.Marshal(DecorateLobbyListData(lobbies))
	broadcaster.SendMessageToRoom(
		fmt.Sprintf("%s_public", config.Constants.GlobalChatRoom),
		"lobbyListData", string(bytes))

}
Exemplo n.º 6
0
//Close closes the lobby, which has the following effects:
//
//  All unfilled substitutes for the lobby are "filled" (ie, their filled field is set to true)
//  The corresponding ServerRecord is deleted
//
//If rpc == true, the log listener in Pauling for the corresponding server is stopped, this is
//used when the lobby is closed manually by a player
func (lobby *Lobby) Close(doRPC, matchEnded bool) {
	var count int

	db.DB.Preload("ServerInfo").First(lobby, lobby.ID)
	db.DB.Model(&gameserver.ServerRecord{}).Where("host = ?", lobby.ServerInfo.Host).Count(&count)
	if count != 0 {
		gameserver.PutStoredServer(lobby.ServerInfo.Host)
	}

	lobby.SetState(Ended)
	db.DB.First(lobby).UpdateColumn("match_ended", matchEnded)
	//db.DB.Exec("DELETE FROM spectators_players_lobbies WHERE lobby_id = ?", lobby.ID)
	if doRPC {
		rpc.End(lobby.ID)
	}
	if matchEnded {
		lobby.UpdateStats()
	}
	if lobby.ServemeID != 0 {
		context := helpers.GetServemeContext(lobby.ServerInfo.Host)
		err := context.Delete(lobby.ServemeID, lobby.CreatedBySteamID)
		if err != nil {
			logrus.Error(err)
		}
		if matchEnded {
			time.AfterFunc(10*time.Second, func() {
				lobby.DownloadDemo(context)
			})
		}
	}

	privateRoom := fmt.Sprintf("%d_private", lobby.ID)
	broadcaster.SendMessageToRoom(privateRoom, "lobbyLeft", LobbyEvent{ID: lobby.ID})

	publicRoom := fmt.Sprintf("%d_public", lobby.ID)
	broadcaster.SendMessageToRoom(publicRoom, "lobbyClosed", DecorateLobbyClosed(lobby))

	db.DB.Model(&gameserver.ServerRecord{}).Where("id = ?", lobby.ServerInfoID).Delete(&gameserver.ServerRecord{})
	BroadcastSubList()
	BroadcastLobby(lobby)
	BroadcastLobbyList() // has to be done manually for now
	rpc.FumbleLobbyEnded(lobby.ID)
	lobby.deleteLock()
}
Exemplo n.º 7
0
func (Chat) ChatSend(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
	reqerr := chelpers.FilterRequest(so, 0, true)

	if reqerr != nil {
		return reqerr.Encode()
	}
	var args struct {
		Message *string `json:"message"`
		Room    *int    `json:"room"`
	}

	err := chelpers.GetParams(data, &args)
	if err != nil {
		return helpers.NewTPErrorFromError(err).Encode()
	}

	player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
	if tperr != nil {
		return tperr.Encode()
	}

	//helpers.Logger.Debug("received chat message: %s %s", *args.Message, player.Name)

	if *args.Room > 0 {
		spec := player.IsSpectatingId(uint(*args.Room))
		//Check if player has either joined, or is spectating lobby
		lobbyId, tperr := player.GetLobbyId()

		if tperr != nil && !spec && lobbyId != uint(*args.Room) {
			return helpers.NewTPError("Player is not in the lobby.", 5).Encode()
		}
	} else {
		// else room is the lobby list room
		*args.Room, _ = strconv.Atoi(config.Constants.GlobalChatRoom)
	}
	if (*args.Message)[0] == '\n' {
		return helpers.NewTPError("Cannot send messages prefixed with newline", 4).Encode()
	}

	if len(*args.Message) > 120 {
		return helpers.NewTPError("Message too long", 4).Encode()
	}

	message := models.NewChatMessage(*args.Message, *args.Room, player)
	db.DB.Save(message)
	bytes, _ := json.Marshal(message)
	broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public",
		chelpers.GetLobbyRoom(uint(*args.Room))),
		"chatReceive", string(bytes))

	if strings.HasPrefix(*args.Message, "!admin") {
		chelpers.SendToSlack(*args.Message, player.Name, player.SteamId)
	}

	return chelpers.EmptySuccessJS
}
Exemplo n.º 8
0
Arquivo: lobby.go Projeto: mcapu/Helen
func BroadcastLobbyList() {
	var lobbies []Lobby
	db.DB.Where("state = ?", LobbyStateWaiting).Order("id desc").Find(&lobbies)
	list, err := DecorateLobbyListData(lobbies)
	if err != nil {
		helpers.Logger.Warning("Failed to send lobby list: %s", err.Error())
	} else {
		broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public", config.Constants.GlobalChatRoom), "lobbyListData", list)
	}
}
Exemplo n.º 9
0
func (lobby *Lobby) Close(rpc bool) {
	db.DB.Table("substitutes").Where("lobby_id = ?", lobby.ID).UpdateColumn("filled", true)
	db.DB.First(&lobby).UpdateColumn("state", LobbyStateEnded)
	db.DB.Delete(&lobby.ServerInfo)
	db.DB.Exec("DELETE FROM spectators_players_lobbies WHERE lobby_id = ?", lobby.ID)
	if rpc {
		End(lobby.ID)
	}

	privateRoom := fmt.Sprintf("%d_private", lobby.ID)
	bytesLobbyLeft, _ := json.Marshal(DecorateLobbyLeave(lobby))
	broadcaster.SendMessageToRoom(privateRoom, "lobbyLeft", string(bytesLobbyLeft))

	publicRoom := fmt.Sprintf("%d_public", lobby.ID)
	bytesLobbyClosed, _ := json.Marshal(DecorateLobbyClosed(lobby))
	broadcaster.SendMessageToRoom(publicRoom, "lobbyClosed", string(bytesLobbyClosed))

	BroadcastLobby(lobby)
}
Exemplo n.º 10
0
//Close closes the lobby, which has the following effects:
//
//  All unfilled substitutes for the lobby are "filled" (ie, their filled field is set to true)
//  The corresponding ServerRecord is deleted
//
//If rpc == true, the log listener in Pauling for the corresponding server is stopped, this is
//used when the lobby is closed manually by a player
func (lobby *Lobby) Close(rpc bool) {
	db.DB.First(&lobby).UpdateColumn("state", LobbyStateEnded)
	db.DB.Table("server_records").Where("id = ?", lobby.ServerInfoID).Delete(&ServerRecord{})
	db.DB.Table("requirements").Where("lobby_id = ?", lobby.ID).Delete(&Requirement{})
	//db.DB.Exec("DELETE FROM spectators_players_lobbies WHERE lobby_id = ?", lobby.ID)
	if rpc {
		End(lobby.ID)
	}

	privateRoom := fmt.Sprintf("%d_private", lobby.ID)
	broadcaster.SendMessageToRoom(privateRoom, "lobbyLeft", DecorateLobbyLeave(lobby))

	publicRoom := fmt.Sprintf("%d_public", lobby.ID)
	broadcaster.SendMessageToRoom(publicRoom, "lobbyClosed", DecorateLobbyClosed(lobby))

	BroadcastSubList()
	BroadcastLobby(lobby)
	BroadcastLobbyList() // has to be done manually for now
	FumbleLobbyEnded(lobby)
}
Exemplo n.º 11
0
Arquivo: debug.go Projeto: N1xx1/Helen
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)
		})
}
Exemplo n.º 12
0
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)
		})
}
Exemplo n.º 13
0
Arquivo: chat.go Projeto: N1xx1/Helen
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)
		})
}
Exemplo n.º 14
0
func matchEnded(lobbyID uint, logsID int) {
	lobby, err := lobbypackage.GetLobbyByIDServer(lobbyID)
	if err != nil {
		logrus.Error(err)
		return
	}
	lobby.Close(false, true)

	logs := fmt.Sprintf("http://logs.tf/%d", logsID)
	msg := fmt.Sprintf("Lobby Ended. Logs: %s", logs)
	chat.SendNotification(msg, int(lobby.ID))

	room := fmt.Sprintf("%d_private", lobby.ID)
	broadcaster.SendMessageToRoom(room, "lobbyLogs", struct {
		LobbyID uint   `json:"lobbyID"`
		Logs    string `json:"logs"`
	}{lobby.ID, logs})

	lobby.UpdateHours(logsID)
}
Exemplo n.º 15
0
func (Lobby) LobbyShuffle(so *wsevent.Client, args struct {
	Id uint `json:"id"`
}) interface{} {
	player := chelpers.GetPlayer(so.Token)

	lob, err := lobby.GetLobbyByID(args.Id)
	if err != nil {
		return err
	}

	if player.SteamID != lob.CreatedBySteamID && (player.Role != helpers.RoleAdmin && player.Role != helpers.RoleMod) {
		return errors.New("You aren't authorized to shuffle this lobby.")
	}

	if err = lob.ShuffleAllSlots(); err != nil {
		return err
	}

	room := fmt.Sprintf("%s_private", hooks.GetLobbyRoom(args.Id))
	broadcaster.SendMessageToRoom(room, "lobbyShuffled", args)
	chat.NewBotMessage(fmt.Sprintf("Lobby shuffled by %s", player.Alias()), int(args.Id)).Send()
	return emptySuccess
}
Exemplo n.º 16
0
func (Lobby) LobbyJoin(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
	reqerr := chelpers.FilterRequest(so, authority.AuthAction(0), true)

	if reqerr != nil {
		return reqerr.Encode()

	}

	var args struct {
		Id    *uint   `json:"id"`
		Class *string `json:"class"`
		Team  *string `json:"team" valid:"red,blu"`
	}

	if err := chelpers.GetParams(data, &args); err != nil {
		return helpers.NewTPErrorFromError(err).Encode()
	}
	//helpers.Logger.Debug("id %d class %s team %s", *args.Id, *args.Class, *args.Team)

	player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))

	if tperr != nil {
		return tperr.Encode()
	}

	lob, tperr := models.GetLobbyById(*args.Id)
	if tperr != nil {
		return tperr.Encode()
	}

	if lob.State == models.LobbyStateEnded {
		return helpers.NewTPError("Cannot join a closed lobby.", -1).Encode()

	}

	//Check if player is in the same lobby
	var sameLobby bool
	if id, err := player.GetLobbyId(); err == nil && id == *args.Id {
		sameLobby = true
	}

	slot, tperr := models.LobbyGetPlayerSlot(lob.Type, *args.Team, *args.Class)
	if tperr != nil {
		return tperr.Encode()

	}

	if prevId, err := player.GetLobbyId(); err != nil {
		server.RemoveClient(so.Id(), fmt.Sprintf("%d_public", prevId))
		server.RemoveClient(so.Id(), fmt.Sprintf("%d_private", prevId))
	}

	tperr = lob.AddPlayer(player, slot, *args.Team, *args.Class)

	if tperr != nil {
		return tperr.Encode()
	}

	if !sameLobby {
		chelpers.AfterLobbyJoin(server, so, lob, player)
	}

	if lob.IsFull() {
		lob.State = models.LobbyStateReadyingUp
		lob.ReadyUpTimestamp = time.Now().Unix() + 30
		lob.Save()

		tick := time.After(time.Second * 30)
		id := lob.ID
		stop := make(chan struct{})

		go func() {
			select {
			case <-tick:
				lobby := &models.Lobby{}
				db.DB.First(lobby, id)

				if lobby.State != models.LobbyStateInProgress {
					err := lobby.RemoveUnreadyPlayers()
					if err != nil {
						helpers.Logger.Error("RemoveUnreadyPlayers: ", err.Error())
						err = nil
					}

					err = lobby.UnreadyAllPlayers()
					if err != nil {
						helpers.Logger.Error("UnreadyAllPlayers: ", err.Error())
					}

					lobby.State = models.LobbyStateWaiting
					lobby.Save()
				}

			case <-stop:
				return
			}
		}()

		room := fmt.Sprintf("%s_private",
			chelpers.GetLobbyRoom(lob.ID))
		broadcaster.SendMessageToRoom(room, "lobbyReadyUp",
			`{"timeout":30}`)
		models.BroadcastLobbyList()
	}

	err := models.AllowPlayer(*args.Id, player.SteamId, *args.Team+*args.Class)
	if err != nil {
		helpers.Logger.Error(err.Error())
	}

	if lob.State == models.LobbyStateInProgress {
		bytes, _ := json.Marshal(models.DecorateLobbyConnect(lob, player.Name, *args.Class))
		broadcaster.SendMessage(player.SteamId, "lobbyStart", string(bytes))
	}

	return chelpers.EmptySuccessJS
}
Exemplo n.º 17
0
func (Lobby) LobbyJoin(so *wsevent.Client, args struct {
	Id       *uint   `json:"id"`
	Class    *string `json:"class"`
	Team     *string `json:"team" valid:"red,blu"`
	Password *string `json:"password" empty:"-"`
}) interface{} {

	player := chelpers.GetPlayerFromSocket(so.ID)
	if banned, until := player.IsBannedWithTime(models.PlayerBanJoin); banned {
		str := fmt.Sprintf("You have been banned from joining lobbies till %s", until.Format(time.RFC822))
		return helpers.NewTPError(str, -1)
	}

	//logrus.Debug("id %d class %s team %s", *args.Id, *args.Class, *args.Team)
	lob, tperr := models.GetLobbyByID(*args.Id)
	if tperr != nil {
		return tperr
	}

	if lob.State == models.LobbyStateEnded {
		return helpers.NewTPError("Cannot join a closed lobby.", -1)

	}

	//Check if player is in the same lobby
	var sameLobby bool
	if id, err := player.GetLobbyID(false); err == nil && id == *args.Id {
		sameLobby = true
	}

	slot, tperr := models.LobbyGetPlayerSlot(lob.Type, *args.Team, *args.Class)
	if tperr != nil {
		return tperr
	}

	if prevId, _ := player.GetLobbyID(false); prevId != 0 && !sameLobby {
		lobby, _ := models.GetLobbyByID(prevId)
		hooks.AfterLobbyLeave(lobby, player)
	}

	tperr = lob.AddPlayer(player, slot, *args.Password)

	if tperr != nil {
		return tperr
	}

	if !sameLobby {
		hooks.AfterLobbyJoin(so, lob, player)
	}

	//check if lobby isn't already in progress (which happens when the player is subbing)
	if lob.IsFull() && lob.State != models.LobbyStateInProgress {
		lob.State = models.LobbyStateReadyingUp
		lob.ReadyUpTimestamp = time.Now().Unix() + 30
		lob.Save()

		time.AfterFunc(time.Second*30, func() {
			lobby := &models.Lobby{}
			db.DB.First(lobby, lob.ID)

			//if all player's haven't readied up,
			//remove unreadied players and unready the
			//rest.
			if lobby.State != models.LobbyStateInProgress && lobby.State != models.LobbyStateEnded {
				removeUnreadyPlayers(lobby)

				lobby.State = models.LobbyStateWaiting
				lobby.Save()
			}
		})

		room := fmt.Sprintf("%s_private",
			hooks.GetLobbyRoom(lob.ID))
		broadcaster.SendMessageToRoom(room, "lobbyReadyUp",
			struct {
				Timeout int `json:"timeout"`
			}{30})
		models.BroadcastLobbyList()
	}

	if lob.State == models.LobbyStateInProgress { //this happens when the player is a substitute
		db.DB.Preload("ServerInfo").First(lob, lob.ID)
		so.EmitJSON(helpers.NewRequest("lobbyStart", models.DecorateLobbyConnect(lob, player.Name, slot)))
	}

	return chelpers.EmptySuccessJS
}
Exemplo n.º 18
0
func eventListener(eventChanMap map[string](chan map[string]interface{})) {
	for {
		select {
		case event := <-eventChanMap["playerDisc"]:
			lobbyid := event["lobbyId"].(uint)
			steamId := event["steamId"].(string)

			player, _ := models.GetPlayerBySteamId(steamId)
			lobby, _ := models.GetLobbyById(lobbyid)

			lobby.SetNotInGame(player)

			helpers.Logger.Debug("#%d, player %s<%s> disconnected",
				lobby.ID, player.Name, player.SteamId)

			room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
			broadcaster.SendMessageToRoom(room,
				"sendNotification",
				fmt.Sprintf(`{"notification": "%s has disconected from the server."}`,
					player.Name))
			t := time.After(time.Minute * 2)
			go func() {
				<-t
				lobby, _ := models.GetLobbyById(lobbyid)
				ingame, err := lobby.IsPlayerInGame(player)
				if err != nil {
					helpers.Logger.Error(err.Error())
				}
				if !ingame {
					sub, _ := models.NewSub(lobby.ID, player.SteamId)
					db.DB.Save(sub)
					models.BroadcastSubList()
					lobby.RemovePlayer(player)
				}

			}()

		case event := <-eventChanMap["playerConn"]:
			lobbyid := event["lobbyId"].(uint)
			steamId := event["steamId"].(string)

			player, _ := models.GetPlayerBySteamId(steamId)
			lobby, _ := models.GetLobbyById(lobbyid)

			lobby.SetInGame(player)

		case event := <-eventChanMap["playerSub"]:
			lobbyid := event["lobbyId"].(uint)
			steamId := event["steamId"].(string)

			sub, err := models.NewSub(lobbyid, steamId)
			if err != nil {
				helpers.Logger.Error(err.Error())
				continue
			}
			db.DB.Save(sub)

			models.BroadcastSubList()

			player, _ := models.GetPlayerBySteamId(steamId)
			room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
			broadcaster.SendMessageToRoom(room,
				"sendNotification",
				fmt.Sprintf(`{"notification": "%s has been reported."}`,
					player.Name))

			//helpers.Logger.Debug("#%d: Reported player %s<%s>",
			//	lobbyid, player.Name, player.SteamId)

		case event := <-eventChanMap["discFromServer"]:
			lobbyid := event["lobbyId"].(uint)

			lobby, _ := models.GetLobbyByIdServer(lobbyid)

			helpers.Logger.Debug("#%d: Lost connection to %s", lobby.ID, lobby.ServerInfo.Host)

			lobby.Close(false)
			room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
			broadcaster.SendMessageToRoom(room,
				"sendNotification", `{"notification": "Lobby Closed (Connection to server lost)."}`)

		case event := <-eventChanMap["matchEnded"]:
			lobbyid := event["lobbyId"].(uint)

			lobby, _ := models.GetLobbyByIdServer(lobbyid)

			helpers.Logger.Debug("#%d: Match Ended", lobbyid)

			lobby.UpdateStats()
			lobby.Close(false)
			room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
			broadcaster.SendMessageToRoom(room,
				"sendNotification", `{"notification": ""Lobby Ended."}`)

		case <-eventChanMap["getServers"]:
			var lobbies []*models.Lobby
			var activeStates = []models.LobbyState{models.LobbyStateWaiting, models.LobbyStateInProgress}
			db.DB.Preload("ServerInfo").Model(&models.Lobby{}).Where("state IN (?)", activeStates).Find(&lobbies)
			for _, lobby := range lobbies {
				info := models.ServerBootstrap{
					LobbyId: lobby.ID,
					Info:    lobby.ServerInfo,
				}
				for _, player := range lobby.BannedPlayers {
					info.BannedPlayers = append(info.BannedPlayers, player.SteamId)
				}
				for _, slot := range lobby.Slots {
					var player = &models.Player{}
					db.DB.Find(player, slot.PlayerId)
					info.Players = append(info.Players, player.SteamId)
				}
				models.Pauling.Call("Pauling.SetupVerifier", &info, &struct{}{})
			}
		}
	}
}
Exemplo n.º 19
0
func (Lobby) LobbyJoin(so *wsevent.Client, args struct {
	Id       *uint   `json:"id"`
	Class    *string `json:"class"`
	Team     *string `json:"team" valid:"red,blu"`
	Password *string `json:"password" empty:"-"`
}) interface{} {

	p := chelpers.GetPlayer(so.Token)
	if banned, until := p.IsBannedWithTime(player.BanJoin); banned {
		ban, _ := p.GetActiveBan(player.BanJoin)
		return fmt.Errorf("You have been banned from joining lobbies till %s (%s)", until.Format(time.RFC822), ban.Reason)
	}

	//logrus.Debug("id %d class %s team %s", *args.Id, *args.Class, *args.Team)
	lob, tperr := lobby.GetLobbyByID(*args.Id)

	if tperr != nil {
		return tperr
	}

	if lob.Mumble {
		if banned, until := p.IsBannedWithTime(player.BanJoinMumble); banned {
			ban, _ := p.GetActiveBan(player.BanJoinMumble)
			return fmt.Errorf("You have been banned from joining Mumble lobbies till %s (%s)", until.Format(time.RFC822), ban.Reason)
		}
	}

	if lob.State == lobby.Ended {
		return errors.New("Cannot join a closed lobby.")

	}
	if lob.State == lobby.Initializing {
		return errors.New("Lobby is being setup right now.")
	}

	if lob.RegionLock {
		region, _ := helpers.GetRegion(chelpers.GetIPAddr(so.Request))
		if region != lob.RegionCode {
			return errors.New("This lobby is region locked.")
		}
	}

	//Check if player is in the same lobby
	var sameLobby bool
	if id, err := p.GetLobbyID(false); err == nil && id == *args.Id {
		sameLobby = true
	}

	slot, tperr := format.GetSlot(lob.Type, *args.Team, *args.Class)
	if tperr != nil {
		return tperr
	}

	if prevId, _ := p.GetLobbyID(false); prevId != 0 && !sameLobby {
		lob, _ := lobby.GetLobbyByID(prevId)
		hooks.AfterLobbyLeave(lob, p, false, false)
	}

	tperr = lob.AddPlayer(p, slot, *args.Password)

	if tperr != nil {
		return tperr
	}

	if !sameLobby {
		hooks.AfterLobbyJoin(so, lob, p)
	}

	playersCnt := lob.GetPlayerNumber()
	lastNotif, timerExists := lobbyJoinLastNotif[lob.ID]
	if playersCnt >= notifThreshold[lob.Type] && !lob.IsEnoughPlayers(playersCnt) && (!timerExists || time.Since(lastNotif).Minutes() > 5) {
		lob.DiscordNotif(fmt.Sprintf("Almost ready [%d/%d]", playersCnt, lob.RequiredPlayers()))
		lobbyJoinLastNotif[lob.ID] = time.Now()
	}

	//check if lobby isn't already in progress (which happens when the player is subbing)
	lob.Lock()
	if lob.IsEnoughPlayers(playersCnt) && lob.State != lobby.InProgress && lob.State != lobby.ReadyingUp {
		lob.State = lobby.ReadyingUp
		lob.ReadyUpTimestamp = time.Now().Unix() + 30
		lob.Save()

		helpers.GlobalWait.Add(1)
		time.AfterFunc(time.Second*30, func() {
			state := lob.CurrentState()
			//if all player's haven't readied up,
			//remove unreadied players and unready the
			//rest.
			//don't do this when:
			//  lobby.State == Waiting (someone already unreadied up, so all players have been unreadied)
			// lobby.State == InProgress (all players have readied up, so the lobby has started)
			// lobby.State == Ended (the lobby has been closed)
			if state != lobby.Waiting && state != lobby.InProgress && state != lobby.Ended {
				lob.SetState(lobby.Waiting)
				removeUnreadyPlayers(lob)
				lob.UnreadyAllPlayers()
				//get updated lobby object
				lob, _ = lobby.GetLobbyByID(lob.ID)
				lobby.BroadcastLobby(lob)
			}
			helpers.GlobalWait.Done()
		})

		room := fmt.Sprintf("%s_private",
			hooks.GetLobbyRoom(lob.ID))
		broadcaster.SendMessageToRoom(room, "lobbyReadyUp",
			struct {
				Timeout int `json:"timeout"`
			}{30})
		lobby.BroadcastLobbyList()
	}
	lob.Unlock()

	if lob.State == lobby.InProgress { //this happens when the player is a substitute
		db.DB.Preload("ServerInfo").First(lob, lob.ID)
		so.EmitJSON(helpers.NewRequest("lobbyStart", lobby.DecorateLobbyConnect(lob, p, slot)))
	}

	return emptySuccess
}
Exemplo n.º 20
0
func SendNotification(message string, room int) {
	pub := fmt.Sprintf("%d_public", room)
	broadcaster.SendMessageToRoom(pub, "chatReceive", NewBotMessage(message, room))
}
Exemplo n.º 21
0
func handleEvent(event map[string]interface{}) {
	switch event["name"] {
	case "playerDisc":
		slot := &models.LobbySlot{}
		lobbyid := event["lobbyId"].(uint)
		steamId := event["steamId"].(string)

		player, _ := models.GetPlayerBySteamId(steamId)

		db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).First(slot)
		helpers.LockRecord(slot.ID, slot)
		slot.InGame = false
		db.DB.Save(slot)
		helpers.UnlockRecord(slot.ID, slot)
		room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
		broadcaster.SendMessageToRoom(room,
			"sendNotification", fmt.Sprintf("%s has disconected from the server .",
				player.Name))
		go func() {
			t := time.After(time.Minute * 2)
			<-t
			lobby, _ := models.GetLobbyById(lobbyid)
			slot := &models.LobbySlot{}
			db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).First(slot)
			if !slot.InGame {
				helpers.LockRecord(lobby.ID, lobby)
				defer helpers.UnlockRecord(lobby.ID, lobby)
				lobby.RemovePlayer(player)
				broadcaster.SendMessage(player.SteamId, "sendNotification",
					"You have been removed from the lobby.")
			}

		}()

	case "playerConn":
		slot := &models.LobbySlot{}
		lobbyid := event["lobbyId"].(uint)
		steamId := event["steamId"].(string)

		player, _ := models.GetPlayerBySteamId(steamId)
		err := db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).First(slot).Error
		if err == nil { //else, player isn't in the lobby, will be kicked by Pauling
			helpers.LockRecord(slot.ID, slot)
			slot.InGame = true
			db.DB.Save(slot)
			helpers.UnlockRecord(slot.ID, slot)
		}

	case "playerRep":
		lobbyid := event["lobbyId"].(uint)
		steamId := event["steamId"].(string)

		player, _ := models.GetPlayerBySteamId(steamId)

		var slot *models.LobbySlot
		db.DB.Where("player_id = ? AND lobby_id = ?", player.ID, lobbyid).Find(slot)
		slot.NeedSub = true
		db.DB.Save(slot)
		room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
		broadcaster.SendMessageToRoom(room,
			"sendNotification", fmt.Sprintf("%s has been reported.",
				player.Name))

	case "discFromServer":
		lobbyid := event["lobbyId"].(uint)

		lobby, _ := models.GetLobbyById(lobbyid)
		helpers.LockRecord(lobby.ID, lobby)
		lobby.Close(false)
		helpers.UnlockRecord(lobby.ID, lobby)
		room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
		broadcaster.SendMessageToRoom(room,
			"sendNotification", "Disconnected from Server.")

	case "matchEnded":
		lobbyid := event["lobbyId"].(uint)

		lobby, _ := models.GetLobbyById(lobbyid)
		helpers.LockRecord(lobby.ID, lobby)
		lobby.UpdateStats()
		lobby.Close(false)
		helpers.UnlockRecord(lobby.ID, lobby)
		room := fmt.Sprintf("%s_public", chelpers.GetLobbyRoom(lobbyid))
		broadcaster.SendMessageToRoom(room,
			"sendNotification", "Lobby Ended.")

	case "getServers":
		var lobbies []*models.Lobby
		var activeStates = []models.LobbyState{models.LobbyStateWaiting, models.LobbyStateInProgress}
		db.DB.Model(&models.Lobby{}).Where("state IN (?)", activeStates).Find(&lobbies)
		for _, lobby := range lobbies {
			info := models.ServerBootstrap{
				LobbyId: lobby.ID,
				Info:    lobby.ServerInfo,
			}
			for _, player := range lobby.BannedPlayers {
				info.BannedPlayers = append(info.BannedPlayers, player.SteamId)
			}
			for _, slot := range lobby.Slots {
				var player *models.Player
				db.DB.Find(player, slot.PlayerId)
				info.Players = append(info.Players, player.SteamId)
			}
			models.Pauling.Call("Pauling.SetupVerifier", &info, &struct{}{})
		}
	}
}
Exemplo n.º 22
0
func (m *ChatMessage) Send() {
	broadcaster.SendMessageToRoom(fmt.Sprintf("%d_public", m.Room), "chatReceive", m)
	if m.Room != 0 {
		broadcaster.SendMessageToRoom(fmt.Sprintf("%d_private", m.Room), "chatReceive", m)
	}
}
Exemplo n.º 23
0
func (l LobbyData) Send() {
	broadcaster.SendMessageToRoom(fmt.Sprintf("%d_public", l.ID), "lobbyData", l)
}
Exemplo n.º 24
0
//BroadcastSubList broadcasts a the subtitute list to the room 0_public
func BroadcastSubList() {
	subList := DecorateSubstituteList()
	broadcaster.SendMessageToRoom("0_public", "subListData", subList)
}
Exemplo n.º 25
0
//BroadcastLobbyList broadcasts the lobby list to all users
func BroadcastLobbyList() {
	broadcaster.SendMessageToRoom(
		"0_public",
		"lobbyListData", DecorateLobbyListData(GetWaitingLobbies(), false))
}
Exemplo n.º 26
0
//BroadcastLobby broadcasts the lobby to the lobby's public room (id_public)
func BroadcastLobby(lobby *Lobby) {
	room := strconv.FormatUint(uint64(lobby.ID), 10)

	broadcaster.SendMessageToRoom(fmt.Sprintf("%s_public", room), "lobbyData", DecorateLobbyData(lobby, true))
}
Exemplo n.º 27
0
func LobbyJoin(so socketio.Socket) func(string) string {
	return chelpers.FilterRequest(so, lobbyJoinFilters,
		func(params map[string]interface{}) string {
			player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))

			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			lobbyid := params["id"].(uint)
			classString := params["class"].(string)
			teamString := params["team"].(string)

			lob, tperr := models.GetLobbyById(uint(lobbyid))
			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			//Check if player is in the same lobby
			var sameLobby bool
			if id, err := player.GetLobbyId(); err == nil && id == lobbyid {
				sameLobby = true
			}

			slot, tperr := models.LobbyGetPlayerSlot(lob.Type, teamString, classString)
			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			helpers.LockRecord(lob.ID, lob)
			defer helpers.UnlockRecord(lob.ID, lob)
			tperr = lob.AddPlayer(player, slot)

			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			if !sameLobby {
				chelpers.AfterLobbyJoin(so, lob, player)
			}

			if lob.IsFull() {
				lob.State = models.LobbyStateReadyingUp
				lob.Save()
				lob.ReadyUpTimeoutCheck()
				room := fmt.Sprintf("%s_private",
					chelpers.GetLobbyRoom(lob.ID))
				broadcaster.SendMessageToRoom(room, "lobbyReadyUp",
					`{"timeout":30}`)
				models.BroadcastLobbyList()
			}

			models.BroadcastLobbyToUser(lob, player.SteamId)
			bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()
			return string(bytes)
		})
}
Exemplo n.º 28
0
func BroadcastSubList() {
	allSubs := GetSubList()

	bytes, _ := json.Marshal(allSubs)
	broadcaster.SendMessageToRoom("0_public", "subListData", string(bytes))
}