Exemple #1
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
}
Exemple #2
0
func (Chat) ChatSend(so *wsevent.Client, args struct {
	Message *string `json:"message"`
	Room    *int    `json:"room"`
}) interface{} {

	playerID := chelpers.GetPlayerID(so.ID)
	now := time.Now().Unix()
	if now-lastChatTime[playerID] == 0 {
		return helpers.NewTPError("You're sending messages too quickly", -1)
	}

	lastChatTime[playerID] = now
	player, _ := models.GetPlayerByID(playerID)

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

	if *args.Room > 0 {
		var count int
		spec := player.IsSpectatingID(uint(*args.Room))
		//Check if player has either joined, or is spectating lobby
		db.DB.Table("lobby_slots").Where("lobby_id = ? AND player_id = ?", *args.Room, player.ID).Count(&count)

		if !spec && count == 0 {
			return helpers.NewTPError("Player is not in the lobby.", 5)
		}
	} else {
		// else room is the lobby list room
		*args.Room, _ = strconv.Atoi(config.GlobalChatRoom)
	}
	switch {
	case len(*args.Message) == 0:
		return helpers.NewTPError("Cannot send an empty message", 4)

	case (*args.Message)[0] == '\n':
		return helpers.NewTPError("Cannot send messages prefixed with newline", 4)

	case len(*args.Message) > 150:
		return helpers.NewTPError("Message too long", 4)
	}

	message := models.NewChatMessage(*args.Message, *args.Room, player)
	message.Save()

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

	message.Send()

	return chelpers.EmptySuccessJS
}
Exemple #3
0
func (Chat) ChatSend(so *wsevent.Client, args struct {
	Message *string `json:"message"`
	Room    *int    `json:"room"`
}) interface{} {
	p := chelpers.GetPlayer(so.Token)
	if banned, until := p.IsBannedWithTime(player.BanChat); banned {
		ban, _ := p.GetActiveBan(player.BanChat)
		return fmt.Errorf("You've been banned from creating lobbies till %s (%s)", until.Format(time.RFC822), ban.Reason)
	}

	if *args.Room > 0 {
		//Check if player has either joined, or is spectating lobby
		var count int
		spec := p.IsSpectatingID(uint(*args.Room))
		db.DB.Model(&lobby.LobbySlot{}).Where("lobby_id = ? AND player_id = ?", *args.Room, p.ID).Count(&count)

		if !spec && count == 0 {
			return errors.New("Player is not in the lobby.")
		}
	} else {
		// else room is the lobby list room
		*args.Room = 0
	}
	switch {
	case len(*args.Message) == 0:
		return errors.New("Cannot send an empty message")

	case (*args.Message)[0] == '\n':
		return errors.New("Cannot send messages prefixed with newline")

	case len(*args.Message) > 150:
		return errors.New("Message too long")
	}

	message := chat.NewChatMessage(*args.Message, *args.Room, p)

	if strings.HasPrefix(*args.Message, "!admin") {
		chelpers.SendToSlack(*args.Message, p.Name, p.SteamID)
		return emptySuccess
	}

	message.Save()
	message.Send()

	return emptySuccess
}