예제 #1
0
func TestNewChatMessage(t *testing.T) {
	testhelpers.CleanupDB()

	lobby := testhelpers.CreateLobby()
	lobby.Save()

	player := testhelpers.CreatePlayer()
	player.Save()

	for i := 0; i < 3; i++ {
		time.Sleep(time.Second)

		message := models.NewChatMessage(strconv.Itoa(i), 0, player)
		assert.NotNil(t, message)

		err := db.DB.Save(message).Error
		assert.Nil(t, err)
	}

	messages, err := models.GetMessages(player, 0)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 3)

	for i := 1; i < len(messages); i++ {
		assert.True(t, messages[i].CreatedAt.Unix() > messages[i-1].CreatedAt.Unix())
	}

	for i := 0; i < 3; i++ {
		time.Sleep(time.Second)

		message := models.NewChatMessage(strconv.Itoa(i), 1, player)
		assert.NotNil(t, message)

		err := db.DB.Save(message).Error
		assert.Nil(t, err)
	}

	messages, err = models.GetMessages(player, 1)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 3)

	for i := 1; i < len(messages); i++ {
		assert.True(t, messages[i].CreatedAt.Unix() > messages[i-1].CreatedAt.Unix())
	}

	messages, err = models.GetAllMessages(player)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 6)
}
예제 #2
0
파일: chat.go 프로젝트: demipixel/Helen
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
}
예제 #3
0
파일: chat.go 프로젝트: gpittarelli/Helen
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
}