Exemplo n.º 1
0
func TestPlayerSettings(t *testing.T) {
	migrations.TestCleanup()

	player, _ := models.NewPlayer("76561197999073985")

	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))
}
Exemplo n.º 2
0
func TestLobbyRemove(t *testing.T) {
	migrations.TestCleanup()
	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	lobby.Save()

	player, playErr := models.NewPlayer("1235")
	assert.Nil(t, playErr)
	player.Save()

	// 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)
}
Exemplo n.º 3
0
func TestPlayerInfoFetching(t *testing.T) {
	migrations.TestCleanup()

	if config.Constants.SteamDevApiKey == "your steam dev api key" {
		return
	}

	// disable mock mode because we're actually testing it
	config.Constants.SteamApiMockUp = false

	player, playErr := models.NewPlayer("76561197999073985")
	assert.Nil(t, playErr)

	assert.Equal(t, "http://steamcommunity.com/id/nonagono/", player.Profileurl)
	assert.Regexp(t, "(.*)steamcommunity/public/images/avatars/(.*).jpg", player.Avatar)

	assert.True(t, player.GameHours >= 3000)

	player.Stats.PlayedCountSet(models.LobbyTypeSixes, 3)
	player.Stats.PlayedCountSet(models.LobbyTypeHighlander, 7)
	player.Stats.PlayedCountIncrease(models.LobbyTypeSixes) // sixes: 3 -> 4

	assert.Equal(t, 4, player.Stats.PlayedCountGet(models.LobbyTypeSixes))
	assert.Equal(t, 7, player.Stats.PlayedCountGet(models.LobbyTypeHighlander))

	database.DB.Save(player)

	player2, err := models.GetPlayerWithStats(player.SteamId)
	assert.Nil(t, err)

	assert.Equal(t, 4, player2.Stats.PlayedCountGet(models.LobbyTypeSixes))
	assert.Equal(t, 7, player2.Stats.PlayedCountGet(models.LobbyTypeHighlander))
	assert.Equal(t, "http://steamcommunity.com/id/nonagono/", player2.Profileurl)
}
Exemplo n.º 4
0
func TestSpectators(t *testing.T) {
	migrations.TestCleanup()

	player, playErr := models.NewPlayer("apple")
	assert.Nil(t, playErr)

	player.Save()

	player2, playErr2 := models.NewPlayer("testing1")
	assert.Nil(t, playErr2)
	player2.Save()

	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	lobby.Save()

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

	var specs []models.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)
	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))

	// players in lobby should be removed from it if added as spectator
	lobby.AddPlayer(player, 10)
	err = lobby.AddSpectator(player)
	assert.Nil(t, err)
	_, terr := lobby.GetPlayerSlot(player)
	assert.NotNil(t, terr)

	// adding a player should remove them from spectators
	lobby.AddPlayer(player2, 11)
	specs = nil
	db.DB.Model(lobby).Association("Spectators").Find(&specs)
	assert.Equal(t, 0, len(specs))
}
Exemplo n.º 5
0
func TestLobbyAdd(t *testing.T) {
	migrations.TestCleanup()
	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	lobby.Save()

	var players []*models.Player

	for i := 0; i < 12; i++ {
		player, playErr := models.NewPlayer("p" + fmt.Sprint(i))
		assert.Nil(t, playErr)

		player.Save()
		players = append(players, player)
	}

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

	slot, err2 := lobby.GetPlayerSlot(players[0])
	assert.Equal(t, slot, 0)
	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 := models.NewLobby("cp_granary", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	lobby2.Save()

	// try to add a player while they're in another lobby
	err = lobby.AddPlayer(players[0], 1)
	assert.NotNil(t, err)
}
Exemplo n.º 6
0
func TestUnreadyPlayer(t *testing.T) {
	migrations.TestCleanup()
	player, playErr := models.NewPlayer("testing")
	assert.Nil(t, playErr)

	player.Save()
	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	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)
}
Exemplo n.º 7
0
func TestLobbiesPlayed(t *testing.T) {
	migrations.TestCleanup()
	stats1 := &models.PlayerStats{}

	stats1.PlayedCountSet(models.LobbyTypeSixes, 5)
	stats1.PlayedCountSet(models.LobbyTypeHighlander, 8)
	stats1.PlayedCountIncrease(models.LobbyTypeSixes) // sixes: 5 -> 6

	assert.Equal(t, 6, stats1.PlayedCountGet(models.LobbyTypeSixes))
	assert.Equal(t, 8, stats1.PlayedCountGet(models.LobbyTypeHighlander))
	database.DB.Save(stats1)

	// can load the record
	var stats2 models.PlayerStats
	err := database.DB.First(&stats2, stats1.ID).Error
	assert.Nil(t, err)

	assert.Equal(t, 6, stats2.PlayedCountGet(models.LobbyTypeSixes))
	assert.Equal(t, 8, stats2.PlayedCountGet(models.LobbyTypeHighlander))
}
Exemplo n.º 8
0
func TestIsSpectating(t *testing.T) {
	migrations.TestCleanup()

	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{}, 1)
	database.DB.Save(lobby)
	player, _ := models.NewPlayer("asdf")
	database.DB.Save(player)

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

	lobby.AddSpectator(player)

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

	lobby.RemoveSpectator(player)
	isSpectating = player.IsSpectatingId(lobby.ID)
	assert.False(t, isSpectating)
}
Exemplo n.º 9
0
func TestIsEveryoneReady(t *testing.T) {
	migrations.TestCleanup()
	player, playErr := models.NewPlayer("0")
	assert.Nil(t, playErr)

	player.Save()
	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	lobby.Save()
	lobby.AddPlayer(player, 0)
	lobby.ReadyPlayer(player)
	assert.Equal(t, lobby.IsEveryoneReady(), false)

	for i := 1; i < 12; i++ {
		player, playErr = models.NewPlayer(strconv.Itoa(i))
		assert.Nil(t, playErr)
		player.Save()
		lobby.AddPlayer(player, i)
		lobby.ReadyPlayer(player)
	}
	assert.Equal(t, lobby.IsEveryoneReady(), true)
}
Exemplo n.º 10
0
func TestLobbyCreation(t *testing.T) {
	migrations.TestCleanup()
	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "testip", ""}, 0)
	lobby.Save()

	lobby2, _ := models.GetLobbyById(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)
	//testing password creation
	assert.Equal(t, len(lobby.Server.ServerPassword), 8)
	lobby3 := models.NewLobby("cp_process_final", models.LobbyTypeSixes, models.ServerRecord{0, "testip", ""}, 0)
	lobby3.Save()
	assert.NotEqual(t, lobby.Server.ServerPassword, lobby3.Server.ServerPassword)

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

	db.DB.First(lobby2)
	assert.Equal(t, "cp_granary", lobby2.MapName)
}
Exemplo n.º 11
0
func TestLobbyBan(t *testing.T) {
	migrations.TestCleanup()
	lobby := models.NewLobby("cp_badlands", models.LobbyTypeSixes, models.ServerRecord{0, "", ""}, 0)
	lobby.Save()

	player, playErr := models.NewPlayer("1235")
	assert.Nil(t, playErr)
	player.Save()

	// 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)
}