Пример #1
0
func TestHasPlayer(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	player := testhelpers.CreatePlayer()

	lobby.AddPlayer(player, 1, "")
	assert.True(t, lobby.HasPlayer(player))

	player2 := testhelpers.CreatePlayer()
	assert.False(t, lobby.HasPlayer(player2))

	lobby.RemovePlayer(player)
	assert.False(t, lobby.HasPlayer(player))
}
Пример #2
0
func TestMatchEnded(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	player := testhelpers.CreatePlayer()

	lobby.Type = models.LobbyTypeBball
	lobby.Save()
	lobby.AddPlayer(player, 0, "")

	e := rpc.Event{
		Name:    rpc.MatchEnded,
		LobbyID: lobby.ID,
	}

	e.Handle(e, &struct{}{})
	db.DB.Preload("Stats").First(player, player.ID)
	assert.Equal(t, player.Stats.PlayedBballCount, 1)

	lobby = testhelpers.CreateLobby()

	lobby.Type = models.LobbyTypeBball
	lobby.Save()
	lobby.AddPlayer(player, 0, "")

	e.LobbyID = lobby.ID
	e.Handle(e, &struct{}{})
	db.DB.Preload("Stats").First(player, player.ID)
	assert.Equal(t, player.Stats.PlayedBballCount, 2)
	assert.Equal(t, player.Stats.TotalLobbies(), 2)
}
Пример #3
0
func TestPlayerSettings(t *testing.T) {
	t.Parallel()

	player := testhelpers.CreatePlayer()

	settings, err := player.GetSettings()

	assert.Nil(t, err)
	assert.Equal(t, 0, len(settings))

	err = player.SetSetting("foo", "bar")
	assert.Nil(t, err)

	settings, err = player.GetSettings()
	assert.Nil(t, err)
	assert.Equal(t, "foo", settings[0].Key)
	assert.Equal(t, "bar", settings[0].Value)

	setting, err := player.GetSetting("foo")
	assert.Nil(t, err)
	assert.Equal(t, "bar", setting.Value)

	err = player.SetSetting("hello", "world")
	assert.Nil(t, err)

	settings, err = player.GetSettings()
	assert.Nil(t, err)
	assert.Equal(t, 2, len(settings))
}
Пример #4
0
func TestLobbyAdd(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	var players []*Player

	for i := 0; i < 12; i++ {
		player := testhelpers.CreatePlayer()
		players = append(players, player)
	}

	// add player
	err := lobby.AddPlayer(players[0], 0, "")
	assert.Nil(t, err)

	slot, err2 := lobby.GetPlayerSlot(players[0])
	assert.Zero(t, slot)
	assert.Nil(t, err2)

	id, err3 := lobby.GetPlayerIDBySlot(0)
	assert.Equal(t, id, players[0].ID)
	assert.Nil(t, err3)

	// try to switch slots
	err = lobby.AddPlayer(players[0], 1, "")
	assert.Nil(t, err)

	slot, err2 = lobby.GetPlayerSlot(players[0])
	assert.Equal(t, slot, 1)
	assert.Nil(t, err2)

	// this should be empty now
	id, err3 = lobby.GetPlayerIDBySlot(0)
	assert.NotNil(t, err3)

	// try to add a second player to the same slot
	err = lobby.AddPlayer(players[1], 1, "")
	assert.NotNil(t, err)

	// try to add a player to a wrong slot slot
	err = lobby.AddPlayer(players[2], 55, "")
	assert.NotNil(t, err)

	lobby2 := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby2.Save()

	// try to add a player while they're in another lobby
	//player should be substituted
	lobby.State = LobbyStateInProgress
	lobby.Save()
	err = lobby2.AddPlayer(players[0], 1, "")
	assert.Nil(t, err)

	var count int
	db.DB.Table("lobby_slots").Where("lobby_id = ? AND needs_sub = ?", lobby.ID, true).Count(&count)
	assert.Equal(t, count, 1)
}
Пример #5
0
func TestNotInGameSub(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	var (
		players                  []*Player
		ingame, subbed, subcount int
		//subs                     []int
	)

	for i := 0; i < 12; i++ {
		players = append(players, testhelpers.CreatePlayer())
	}
	for i, player := range players {
		err := lobby.AddPlayer(player, i, "")
		assert.NoError(t, err)
		r := rand.Intn(3)
		if r == 1 {
			ingame++
			lobby.SetInGame(player)
		} else if r == 2 {
			lobby.Substitute(player)
			subbed++
		}

	}

	t.Logf("%d players are in-game, %d player have been substituted", ingame, subbed)
	lobby.SubNotInGamePlayers()

	db.DB.Table("lobby_slots").Where("lobby_id = ? AND needs_sub = ?", lobby.ID, true).Count(&subcount)
	//assert.Equal(t, subcount, len(subs))
	lobby.Close(false)
}
Пример #6
0
func TestRemoveUnreadyPlayers(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	var players []*Player
	for i := 0; i < 12; i++ {
		player := testhelpers.CreatePlayer()

		lobby.AddPlayer(player, i, "")
		players = append(players, player)
	}

	err := lobby.RemoveUnreadyPlayers(true)
	assert.Nil(t, err)

	for i := 0; i < 12; i++ {
		var count int
		_, err := lobby.GetPlayerIDBySlot(i)
		assert.Error(t, err)

		db.DB.Table("spectators_players_lobbies").Where("lobby_id = ? AND player_id = ?", lobby.ID, players[i].ID).Count(&count)
		assert.Equal(t, count, 1)
	}
}
Пример #7
0
func TestGetLobbyID(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	lobby.Save()

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

	lobby.AddPlayer(player, 0, "")
	lobby.Save()

	id, err := player.GetLobbyID(false)
	assert.NoError(t, err)
	assert.Equal(t, id, lobby.ID)

	lobby.State = LobbyStateEnded
	lobby.Save()
	id, err = player.GetLobbyID(false)
	assert.Error(t, err)
	assert.Equal(t, id, uint(0))

	lobby.State = LobbyStateInProgress
	lobby.Save()

	//Exclude lobbies in progress
	id, err = player.GetLobbyID(true)
	assert.Error(t, err)
	assert.Equal(t, id, uint(0))

	//Include lobbies in progress
	id, err = player.GetLobbyID(false)
	assert.NoError(t, err)
	assert.Equal(t, id, lobby.ID)
}
Пример #8
0
func TestGetSpectatingIds(t *testing.T) {
	t.Parallel()

	player := testhelpers.CreatePlayer()

	specIds, specErr := player.GetSpectatingIds()
	assert.Nil(t, specErr)
	assert.Equal(t, len(specIds), 0)
	//assert.Equal(t, []uint{lobby.ID, lobby2.ID}, specIds)

	lobby1 := testhelpers.CreateLobby()
	database.DB.Save(lobby1)
	lobby1.AddSpectator(player)

	specIds, specErr = player.GetSpectatingIds()
	assert.Nil(t, specErr)
	assert.Equal(t, specIds[0], lobby1.ID)

	lobby2 := testhelpers.CreateLobby()
	database.DB.Save(lobby2)
	lobby2.AddSpectator(player)

	specIds, specErr = player.GetSpectatingIds()
	assert.Nil(t, specErr)
	for _, specID := range specIds {
		assert.True(t, lobby1.ID == specID || lobby2.ID == specID)
	}
}
Пример #9
0
func TestIsSpectating(t *testing.T) {
	t.Parallel()

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

	lobby2 := testhelpers.CreateLobby()
	database.DB.Save(lobby2)

	player := testhelpers.CreatePlayer()

	isSpectating := player.IsSpectatingID(lobby.ID)
	assert.False(t, isSpectating)

	lobby.AddSpectator(player)

	isSpectating = player.IsSpectatingID(lobby.ID)
	assert.True(t, isSpectating)

	lobby2.AddSpectator(player)
	isSpectating2 := player.IsSpectatingID(lobby2.ID)
	assert.True(t, isSpectating2)

	lobby.RemoveSpectator(player, false)
	isSpectating = player.IsSpectatingID(lobby.ID)
	assert.False(t, isSpectating)
}
Пример #10
0
func TestGetPlayer(t *testing.T) {
	t.Parallel()
	player := testhelpers.CreatePlayer()
	player2, err := GetPlayerByID(player.ID)
	assert.NoError(t, err)
	assert.Equal(t, player.ID, player2.ID)
}
Пример #11
0
func TestSpectators(t *testing.T) {
	t.Parallel()

	player := testhelpers.CreatePlayer()

	player2 := testhelpers.CreatePlayer()

	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	err := lobby.AddSpectator(player)
	assert.Nil(t, err)

	var specs []Player
	db.DB.Model(lobby).Association("Spectators").Find(&specs)
	assert.Equal(t, 1, len(specs))

	err = lobby.AddSpectator(player2)
	assert.Nil(t, err)

	specs = nil
	db.DB.Model(lobby).Association("Spectators").Find(&specs)
	assert.Equal(t, 2, len(specs))
	assert.Equal(t, true, specs[0].IsSpectatingID(lobby.ID))

	err = lobby.RemoveSpectator(player, false)
	assert.Nil(t, err)

	specs = nil
	db.DB.Model(lobby).Association("Spectators").Find(&specs)
	assert.Equal(t, 1, len(specs))

	// adding the same player again should not increase the count
	err = lobby.AddSpectator(player2)
	specs = nil
	db.DB.Model(lobby).Association("Spectators").Find(&specs)
	assert.Equal(t, 1, len(specs))

	// adding a player should remove them from spectators
	lobby.AddPlayer(player2, 11, "")
	specs = nil
	db.DB.Model(lobby).Association("Spectators").Find(&specs)
	assert.Zero(t, len(specs))
}
Пример #12
0
func TestIsEveryoneReady(t *testing.T) {
	t.Parallel()
	player := testhelpers.CreatePlayer()

	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()
	lobby.AddPlayer(player, 0, "")
	lobby.ReadyPlayer(player)
	assert.Equal(t, lobby.IsEveryoneReady(), false)

	for i := 1; i < 12; i++ {
		player := testhelpers.CreatePlayer()
		lobby.AddPlayer(player, i, "")
		lobby.ReadyPlayer(player)
	}
	assert.Equal(t, lobby.IsEveryoneReady(), true)
}
Пример #13
0
func TestIsSubNeeded(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	player := testhelpers.CreatePlayer()
	lobby.AddPlayer(player, 1, "")

	lobby.Substitute(player)
	assert.True(t, lobby.SlotNeedsSubstitute(1))

}
Пример #14
0
func TestSlotRequirements(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	player := testhelpers.CreatePlayer()
	req := &Requirement{
		LobbyID: lobby.ID,
		Slot:    0,
		Hours:   1,
		Lobbies: 1,
	}
	req.Save()

	assert.True(t, lobby.HasRequirements(0))
	err := lobby.AddPlayer(player, 0, "")
	assert.Equal(t, err, ReqHoursErr)

	player.GameHours = 2
	player.Save()

	err = lobby.AddPlayer(player, 0, "")
	assert.Equal(t, err, ReqLobbiesErr)

	player, _ = GetPlayerWithStats(player.SteamID)
	player.Stats.PlayedCountIncrease(lobby.Type)

	err = lobby.AddPlayer(player, 0, "")
	assert.NoError(t, err)

	//Adding a player to another slot shouldn't return any errors
	// req = &Requirement{
	// 	LobbyID: lobby.ID,
	// 	Slot:    -1,
	// 	Hours:   1,
	// 	Lobbies: 1,
	// }
	player2 := testhelpers.CreatePlayer()
	err = lobby.AddPlayer(player2, 1, "")
	assert.NoError(t, err)
}
Пример #15
0
func TestSetInGame(t *testing.T) {
	t.Parallel()
	player := testhelpers.CreatePlayer()

	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()
	lobby.AddPlayer(player, 0, "")
	lobby.SetInGame(player)

	slot, err := lobby.GetPlayerSlotObj(player)
	assert.Nil(t, err)
	assert.True(t, slot.InGame)
}
Пример #16
0
func TestIsPlayerInGame(t *testing.T) {
	t.Parallel()
	player := testhelpers.CreatePlayer()

	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()
	lobby.AddPlayer(player, 0, "")
	lobby.SetInGame(player)

	ingame, err := lobby.IsPlayerInGame(player)
	assert.NoError(t, err)
	assert.True(t, ingame)
}
Пример #17
0
func TestNewChatMessage(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

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

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

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

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

	messages, err := GetRoomMessages(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 := NewChatMessage(strconv.Itoa(i), 1, player)
		assert.NotNil(t, message)

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

	messages, err = GetPlayerMessages(player)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 6)

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

	messages, err = GetPlayerMessages(player)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 6)
}
Пример #18
0
func TestPlayerBanning(t *testing.T) {
	t.Parallel()
	player := testhelpers.CreatePlayer()

	for ban := PlayerBanJoin; ban != PlayerBanFull; ban++ {
		assert.False(t, player.IsBanned(ban))
	}

	past := time.Now().Add(time.Second * -10)
	player.BanUntil(past, PlayerBanJoin, "they suck")
	assert.False(t, player.IsBanned(PlayerBanJoin))

	future := time.Now().Add(time.Second * 10)
	player.BanUntil(future, PlayerBanJoin, "they suck")
	player.BanUntil(future, PlayerBanFull, "they suck")

	player2, _ := GetPlayerBySteamID(player.SteamID)
	assert.False(t, player2.IsBanned(PlayerBanCreate))
	assert.False(t, player2.IsBanned(PlayerBanChat))
	isBannedFull, untilFull := player2.IsBannedWithTime(PlayerBanFull)
	assert.True(t, isBannedFull)
	assert.True(t, future.Sub(untilFull) < time.Second)
	assert.True(t, untilFull.Sub(future) < time.Second)
	log.Println(future.Sub(untilFull))

	isBannedJoin, untilJoin := player2.IsBannedWithTime(PlayerBanJoin)
	assert.True(t, isBannedJoin)
	assert.True(t, future.Sub(untilJoin) < time.Second)
	assert.True(t, untilJoin.Sub(future) < time.Second)

	future2 := time.Now().Add(time.Second * 20)
	player2.BanUntil(future2, PlayerBanJoin, "they suck")
	isBannedJoin2, untilJoin2 := player2.IsBannedWithTime(PlayerBanJoin)
	assert.True(t, isBannedJoin2)
	assert.True(t, future2.Sub(untilJoin2) < time.Second)
	assert.True(t, untilJoin.Sub(future2) < time.Second)

	bans, err := player2.GetActiveBans()
	assert.Nil(t, err)
	assert.Equal(t, 3, len(bans))

	player2.Unban(PlayerBanJoin)
	player2.Unban(PlayerBanFull)

	for ban := PlayerBanJoin; ban != PlayerBanFull; ban++ {
		assert.False(t, player2.IsBanned(ban))
	}
}
Пример #19
0
func TestReportVoted(t *testing.T) {
	t.Parallel()
	p := testhelpers.CreatePlayer()
	l1 := testhelpers.CreateLobby()
	defer l1.Close(false, false)
	l2 := testhelpers.CreateLobby()
	defer l2.Close(false, false)

	// RageQuit = Vote + 1, so we don't need to test that
	p.NewReport(Vote, l1.ID)
	p.NewReport(Vote, l2.ID)

	banned, until := p.IsBannedWithTime(BanJoin)
	assert.True(t, banned, "Player should be banned from joining lobbies")
	assert.WithinDuration(t, until, time.Now(), 30*time.Minute)
}
Пример #20
0
func TestUnreadyPlayer(t *testing.T) {
	t.Parallel()
	player := testhelpers.CreatePlayer()

	player.Save()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()
	lobby.AddPlayer(player, 0, "")

	lobby.ReadyPlayer(player)
	lobby.UnreadyPlayer(player)
	ready, err := lobby.IsPlayerReady(player)
	assert.Equal(t, ready, false)
	assert.Nil(t, err)
}
Пример #21
0
func TestPlayerConn(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	player := testhelpers.CreatePlayer()

	lobby.AddPlayer(player, 0, "")
	db.DB.Table("lobby_slots").Where("lobby_id = ? AND player_id = ?", lobby.ID, player.ID).UpdateColumn("in_game", false)
	e := rpc.Event{
		Name:     rpc.PlayerConnected,
		LobbyID:  lobby.ID,
		PlayerID: player.ID,
	}
	e.Handle(e, &struct{}{})

	var count int
	db.DB.Table("lobby_slots").Where("lobby_id = ? AND player_id = ? AND in_game = ?", lobby.ID, player.ID, true).Count(&count)
	assert.Equal(t, count, 1)
}
Пример #22
0
func TestUnreadyAllPlayers(t *testing.T) {
	t.Parallel()

	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	for i := 0; i < 12; i++ {
		player := testhelpers.CreatePlayer()
		lobby.AddPlayer(player, i, "")
		lobby.ReadyPlayer(player)
	}

	err := lobby.UnreadyAllPlayers()
	assert.Nil(t, err)
	ready := lobby.IsEveryoneReady()
	assert.Equal(t, ready, false)
}
Пример #23
0
func TestReportSubs(t *testing.T) {
	t.Parallel()

	p := testhelpers.CreatePlayer()
	l1 := testhelpers.CreateLobby()
	defer l1.Close(false, false)
	l2 := testhelpers.CreateLobby()
	defer l2.Close(false, false)
	// l3 := testhelpers.CreateLobby()
	// defer l3.Close(false, false)

	p.NewReport(Substitute, l1.ID)
	p.NewReport(Substitute, l2.ID)

	banned, until := p.IsBannedWithTime(BanJoin)
	assert.True(t, banned, "Player should be banned from joining lobbies")
	assert.WithinDuration(t, until, time.Now(), 30*time.Minute)
}
Пример #24
0
func TestGeneralRequirements(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	player := testhelpers.CreatePlayer()
	req := &Requirement{
		LobbyID: lobby.ID,
		Slot:    -1,
		Hours:   1,
		Lobbies: 1,
	}
	req.Save()

	err := lobby.AddPlayer(player, 0, "")
	assert.Equal(t, err, ReqHoursErr)

	err = lobby.AddPlayer(player, 3, "")
	assert.Equal(t, err, ReqHoursErr)
}
Пример #25
0
func TestNewChatMessage(t *testing.T) {
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false, true)
	lobby.Save()

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

	for i := 0; i < 3; i++ {
		message := NewChatMessage(strconv.Itoa(i), 0, player)
		assert.NotNil(t, message)

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

	messages, err := GetRoomMessages(0)
	assert.Nil(t, err)
	assert.Equal(t, len(messages), 3)
}
Пример #26
0
func TestUpdateStats(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	var players []*Player

	for i := 0; i < 6; i++ {
		players = append(players, testhelpers.CreatePlayer())
	}
	for i, player := range players {
		err := lobby.AddPlayer(player, i, "")
		assert.NoError(t, err)
	}

	lobby.UpdateStats()
	for _, player := range players {
		db.DB.Preload("Stats").First(player, player.ID)
		assert.Equal(t, player.Stats.PlayedSixesCount, 1)
	}
}
Пример #27
0
func TestLobbyBan(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	player := testhelpers.CreatePlayer()

	// add player
	err := lobby.AddPlayer(player, 0, "")
	assert.Nil(t, err)

	// ban player
	err = lobby.RemovePlayer(player)
	lobby.BanPlayer(player)
	assert.Nil(t, err)

	// should not be able to add again
	err = lobby.AddPlayer(player, 5, "")
	assert.NotNil(t, err)
}
Пример #28
0
func TestLobbyRemove(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	player := testhelpers.CreatePlayer()

	// add player
	err := lobby.AddPlayer(player, 0, "")
	assert.Nil(t, err)

	// remove player
	err = lobby.RemovePlayer(player)
	assert.Nil(t, err)

	// this should be empty now
	_, err2 := lobby.GetPlayerIDBySlot(0)
	assert.NotNil(t, err2)

	// can add player again
	err = lobby.AddPlayer(player, 0, "")
	assert.Nil(t, err)
}