Esempio n. 1
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{}{})
		}
	}
}
Esempio n. 2
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{}{})
			}
		}
	}
}