Ejemplo n.º 1
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)
	}
}
Ejemplo n.º 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)
}
Ejemplo n.º 3
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)
}
Ejemplo n.º 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)
}
Ejemplo n.º 5
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)
	}
}
Ejemplo n.º 6
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)
}
Ejemplo n.º 7
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)
}
Ejemplo n.º 8
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)
}
Ejemplo n.º 9
0
func TestStart(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)

	lobby.Start()
	assert.Equal(t, lobby.CurrentState(), LobbyStateInProgress)
}
Ejemplo n.º 10
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)
}
Ejemplo n.º 11
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))

}
Ejemplo n.º 12
0
func TestDisconnectedFromServer(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()

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

	e.Handle(e, &struct{}{})
	assert.Equal(t, lobby.CurrentState(), models.LobbyStateEnded)
}
Ejemplo n.º 13
0
func TestDeleteUnusedServerRecords(t *testing.T) {
	var count int

	lobby := testhelpers.CreateLobby()
	lobby.Close(false)
	db.DB.Save(&ServerRecord{})

	DeleteUnusedServerRecords()

	err := db.DB.Table("server_records").Count(&count).Error
	assert.NoError(t, err)
	assert.Zero(t, count)
}
Ejemplo n.º 14
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)
}
Ejemplo n.º 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)
}
Ejemplo n.º 16
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)
}
Ejemplo n.º 17
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))
}
Ejemplo n.º 18
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)
}
Ejemplo n.º 19
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))
}
Ejemplo n.º 20
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)
}
Ejemplo n.º 21
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)
}
Ejemplo n.º 22
0
func TestLobbyCreation(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	defer lobby.Close(false)
	lobby.Save()

	lobby2, _ := GetLobbyByIDServer(lobby.ID)

	assert.Equal(t, lobby.ID, lobby2.ID)
	assert.Equal(t, lobby.ServerInfo.Host, lobby2.ServerInfo.Host)
	assert.Equal(t, lobby.ServerInfo.ID, lobby2.ServerInfo.ID)

	lobby.MapName = "cp_granary"
	lobby.Save()

	db.DB.First(lobby2)
	assert.Equal(t, "cp_granary", lobby2.MapName)
}
Ejemplo n.º 23
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)
}
Ejemplo n.º 24
0
func TestLobbyClose(t *testing.T) {
	t.Parallel()
	lobby := testhelpers.CreateLobby()
	lobby.Save()

	req := &Requirement{
		LobbyID: lobby.ID,
	}
	req.Save()
	lobby.Close(true)
	var count int
	db.DB.Table("requirements").Where("lobby_id = ?", lobby.ID).Count(&count)
	assert.Zero(t, count)

	db.DB.Table("server_records").Where("id = ?", lobby.ServerInfoID).Count(&count)
	assert.Zero(t, count)
	lobby, _ = GetLobbyByID(lobby.ID)
	assert.Equal(t, lobby.State, LobbyStateEnded)
}
Ejemplo n.º 25
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)
}
Ejemplo n.º 26
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)
}
Ejemplo n.º 27
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)
	}
}
Ejemplo n.º 28
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)
}
Ejemplo n.º 29
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)
}
Ejemplo n.º 30
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)
}