Ejemplo n.º 1
0
func TestListPlayerMatchStat(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	NewPlayerMatchStat(store, PlayerMatchStat{
		Score:   1,
		AliasID: 1,
		MatchID: 2,
	})
	NewPlayerMatchStat(store, PlayerMatchStat{
		Score:   -1,
		AliasID: 2,
		MatchID: 2,
	})
	NewPlayerMatchStat(store, PlayerMatchStat{
		Score:   -1,
		AliasID: 2,
		MatchID: 1,
	})

	pls := ListPlayerMatchStat(store, 2)
	assert.Len(t, pls, 2)

	pls = ListPlayerMatchStat(store, 1)
	assert.Len(t, pls, 1)

	pls = ListPlayerMatchStat(store, 5)
	assert.Len(t, pls, 0)
}
Ejemplo n.º 2
0
func TestListPlayers(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	NewPlayer(store, "foo", "123")
	NewPlayer(store, "bar", "123")
	NewPlayer(store, "baz", "123")

	players := ListPlayers(store)

	assert.Len(t, players, 3)

	d := map[string]int{
		"foo": 0,
		"bar": 0,
		"baz": 0,
	}
	for _, p := range players {
		d[p.Name] = 1
	}
	for k, v := range d {
		if v != 1 {
			t.Fatalf("player %s not seen", k)
		}
	}
}
Ejemplo n.º 3
0
func TestNewPlayerMatchStat(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	pms := PlayerMatchStat{
		Score:   10,
		Kills:   10,
		AliasID: 2,
		MatchID: 3,
	}

	pmsid := NewPlayerMatchStat(store, pms)

	var fpms PlayerMatchStat
	nf := db.Find(&fpms, pmsid).RecordNotFound()
	assert.False(t, nf, "expected to find player-match stat of ID %u", pmsid)

	assert.Equal(t, pms.AliasID, fpms.AliasID)
	assert.Equal(t, pms.MatchID, fpms.MatchID)
	assert.Equal(t, pms.Score, fpms.Score)
}
Ejemplo n.º 4
0
func TestNewAlias(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	// add bogus alias first
	aidfoo := NewAliasOrCurrent(store, Alias{Alias: "foo"})
	// now the one we're going to track
	aidbar := NewAliasOrCurrent(store, Alias{Alias: "bar"})

	if aidfoo == aidbar {
		t.Fatalf("expected IDs to be different, got %u %u",
			aidfoo, aidbar)
	}

	// try again, we should get the same alias
	aidbar2 := NewAliasOrCurrent(store, Alias{Alias: "bar"})
	if aidbar2 != aidbar {
		t.Fatalf("expected the same ID, got %u %u",
			aidbar, aidbar2)
	}
}
Ejemplo n.º 5
0
func TestHasPlayer(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	NewPlayer(store, "foo", "123")

	assert.True(t, HasPlayer(store, "foo"))
	assert.False(t, HasPlayer(store, "bar"))
}
Ejemplo n.º 6
0
func TestGetAlias(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	// add bogus alias first
	aidfoo := NewAliasOrCurrent(store, Alias{Alias: "foo"})

	afoo := GetAlias(store, aidfoo)
	assert.NotNil(t, afoo)

	assert.Nil(t, GetAlias(store, 999))
}
Ejemplo n.º 7
0
func TestGetPlayer(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	pid1 := NewPlayer(store, "foo", "123")

	p := GetPlayer(store, pid1)

	assert.Equal(t, "foo", p.Name)
	assert.Equal(t, "123", p.PasswordHash)
	assert.Equal(t, pid1, p.ID)
}
Ejemplo n.º 8
0
func TestPlayer(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	id := NewPlayer(store, "foo", "123")

	var p Player
	nf := db.First(&p, id).RecordNotFound()
	assert.False(t, nf)

	assert.Equal(t, "foo", p.Name)
	assert.Equal(t, "123", p.PasswordHash)
}
Ejemplo n.º 9
0
func TestListItemStat(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	is := ItemStat{
		Type:              MegaHealth,
		Pickups:           1,
		PlayerMatchStatID: 2,
	}
	NewItemStat(store, is)

	iss := ListItemStats(store, 2)
	assert.Len(t, iss, 1)

	assert.Len(t, ListItemStats(store, 9999), 0)
}
Ejemplo n.º 10
0
func TestClaimAlias(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	db.Create(&Alias{Alias: "foo"})
	db.Create(&Alias{Alias: "bar"})
	db.Create(&Alias{Alias: "baz"})

	ClaimAliasesByPlayer(store, 1, []string{"foo", "bar"})

	player1_aliases := GetAliases(store, 1)
	if len(player1_aliases) != 2 {
		t.Fatalf("expected 2 aliases, got %u",
			len(player1_aliases))
	}

	for _, a := range player1_aliases {
		if a.Alias != "foo" && a.Alias != "bar" {
			t.Fatalf("unexpected alias: %s", a.Alias)
		}
		if a.PlayerID != 1 {
			t.Fatalf("alias with incorrect player ID %u",
				a.PlayerID)
		}
	}

	unclaimed := GetAliases(store, NoUser)
	if len(unclaimed) != 1 {
		t.Fatal("expected 1 unclaimed alias, got %u",
			len(unclaimed))
	}
	ua := unclaimed[0]
	if ua.Alias != "baz" {
		t.Fatalf("expected 'baz' to be unclaimed, got %s",
			ua.Alias)
	}
}
Ejemplo n.º 11
0
func TestListWeaponStat(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	w := WeaponStat{
		Hits:              10,
		Shots:             100,
		PlayerMatchStatID: 2,
	}

	NewWeaponStat(store, w)

	wfs := ListWeaponStats(store, 2)
	assert.Len(t, wfs, 1)

	assert.Len(t, ListWeaponStats(store, 9999), 0)
}
Ejemplo n.º 12
0
func TestGetAliases(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	db.Create(&Alias{Alias: "foo"})
	db.Create(&Alias{Alias: "bar"})

	aliases := GetAliases(store, NoUser)

	if len(aliases) != 2 {
		t.Fatalf("returned aliases %u expected 2",
			len(aliases))
	}

	// create another one, assigned to player 1
	db.Create(&Alias{Alias: "baz", PlayerID: 1})

	aliases = GetAliases(store, NoUser)
	// still expecting 2
	if len(aliases) != 2 {
		t.Fatalf("returned aliases %u expected 2",
			len(aliases))
	}

	aliases = GetAliases(store, 1)
	// still expecting 2
	if len(aliases) != 1 {
		t.Fatalf("returned aliases %u expected 1",
			len(aliases))
	}
	al := aliases[0]
	if al.Alias != "baz" {
		t.Fatalf("expected baz, got %s", al.Alias)
	}

}
Ejemplo n.º 13
0
func TestNewItemStat(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	is := ItemStat{
		Type:              MegaHealth,
		Pickups:           1,
		PlayerMatchStatID: 2,
	}
	isid := NewItemStat(store, is)

	var fis ItemStat
	nf := db.Find(&fis, isid).RecordNotFound()
	assert.False(t, nf)

	assert.Equal(t, is.Type, fis.Type)
	assert.Equal(t, is.Pickups, fis.Pickups)
	assert.Equal(t, is.PlayerMatchStatID, fis.PlayerMatchStatID)
}
Ejemplo n.º 14
0
func TestNewWeaponStat(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	w := WeaponStat{
		Hits:              10,
		Shots:             100,
		PlayerMatchStatID: 2,
	}

	wid := NewWeaponStat(store, w)

	var fw WeaponStat
	nf := db.Find(&fw, wid).RecordNotFound()
	assert.False(t, nf)

	assert.Equal(t, w.Hits, fw.Hits)
	assert.Equal(t, w.Shots, fw.Shots)
	assert.Equal(t, w.PlayerMatchStatID, fw.PlayerMatchStatID)
}
Ejemplo n.º 15
0
func TestNewMatchInfo(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	ws := WeaponStat{
		Type:  RocketLauncher,
		Shots: 10,
		Hits:  10,
		Kills: 1,
	}
	is := ItemStat{
		Type:    MegaHealth,
		Pickups: 10,
	}
	pmsfoo := PlayerMatchStat{
		Kills:    10,
		Deaths:   10,
		Suicides: 1,
		Score:    -2,
	}
	pmsbar := PlayerMatchStat{
		Kills:    10,
		Deaths:   10,
		Suicides: 1,
		Score:    2,
	}

	matchtime := time.Now()
	t.Logf("time: %v", matchtime.Format(time.RFC822Z))
	m := MatchInfo{
		Match: Match{
			DataHash: "foobar",
			Map:      "q3dm17",
			Duration: 100,
			DateTime: matchtime,
		},
		PlayerData: []PlayerDataItem{
			{
				Alias{Alias: "foo"},
				[]WeaponStat{
					ws,
				},
				[]ItemStat{
					is,
				},
				pmsfoo,
			},
			{
				Alias{Alias: "bar"},
				[]WeaponStat{
					ws,
				},
				[]ItemStat{},
				pmsbar,
			},
		},
	}

	id, hash := NewMatchInfo(store, &m)

	assert.Equal(t, "foobar", hash, "should be equal")

	match := FindMatchByHash(store, "foobar")
	assert.NotNil(t, match)
	assert.Equal(t, id, match.ID, "different match ID")
	assert.Equal(t, "q3dm17", match.Map, "incorrect map")
	assert.Equal(t, uint(100), match.Duration, "incorrect duration")
	assert.Equal(t, matchtime.Format(time.RFC822Z),
		match.DateTime.Format(time.RFC822Z), "incorrect match datetime")

	aliases := GetAliases(store, NoUser)
	assert.Len(t, aliases, 2)

	var afooid, abarid uint
	for _, a := range aliases {
		if a.Alias != "foo" && a.Alias != "bar" {
			assert.Fail(t, "unexpected alias: %s", a.Alias)
		}
		if a.Alias == "foo" {
			afooid = a.ID
		} else if a.Alias == "bar" {
			abarid = a.ID
		}
	}

	// find player match stats for this match
	pms := ListPlayerMatchStat(store, id)
	assert.Len(t, pms, 2)

	for _, p := range pms {
		if p.AliasID == afooid {
			// player foo
			// 1 weapon stat
			// 1 item stats
			assert.Equal(t, pmsfoo.Kills, p.Kills)
			assert.Len(t, ListWeaponStats(store, p.ID), 1)
			assert.Len(t, ListItemStats(store, p.ID), 1)
		} else if p.AliasID == abarid {
			// player bar
			// 1 weapon stat
			// 0 item stats
			assert.Equal(t, pmsbar.Kills, p.Kills)
			assert.Len(t, ListWeaponStats(store, p.ID), 1)
			assert.Len(t, ListItemStats(store, p.ID), 0)
		}
	}

}
Ejemplo n.º 16
0
func TestGetMatchinfo(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	m := GetMatchInfo(store, "fooo")
	assert.Nil(t, m)

	// start with match
	mid := NewMatch(store, Match{
		DataHash: "foobar",
		Map:      "q3dm17",
		Duration: 100,
	})

	// alias foo
	aidfoo := NewAliasOrCurrent(store, Alias{
		Alias: "foo",
	})
	// match stats for foo
	pmsfoo := NewPlayerMatchStat(store, PlayerMatchStat{
		Score:    1,
		Kills:    2,
		Suicides: 1,
		AliasID:  aidfoo,
		MatchID:  mid,
	})
	// weapon stats for foo's stats in match
	NewWeaponStat(store, WeaponStat{
		Type:              RocketLauncher,
		Shots:             1,
		PlayerMatchStatID: pmsfoo,
	})
	// item stats for foo's stats in match
	NewItemStat(store, ItemStat{
		Type:              MegaHealth,
		Pickups:           1,
		PlayerMatchStatID: pmsfoo,
	})

	aidbar := NewAliasOrCurrent(store, Alias{
		Alias: "bar",
	})
	pmsbar := NewPlayerMatchStat(store, PlayerMatchStat{
		Score:    -2,
		Kills:    2,
		Suicides: 1,
		AliasID:  aidbar,
		MatchID:  mid,
	})
	NewWeaponStat(store, WeaponStat{
		Type:              Plasmagun,
		Shots:             10,
		PlayerMatchStatID: pmsbar,
	})

	mi := GetMatchInfo(store, "foobar")
	assert.NotNil(t, mi)
	assert.Equal(t, "q3dm17", mi.Match.Map)
	assert.Equal(t, "foobar", mi.Match.DataHash)
	assert.Equal(t, uint(100), mi.Match.Duration)
	assert.Len(t, mi.PlayerData, 2)

	for _, p := range mi.PlayerData {
		if p.Alias.Alias == "foo" {
			assert.Equal(t, 1, p.Stats.Score)
			assert.Equal(t, aidfoo, p.Stats.AliasID)
			assert.Equal(t, mid, p.Stats.MatchID)

			assert.Len(t, p.Weapons, 1)
			assert.Len(t, p.Items, 1)

			w := p.Weapons[0]
			assert.Equal(t, RocketLauncher, w.Type)
			assert.Equal(t, uint(1), w.Shots)
			assert.Equal(t, pmsfoo, w.PlayerMatchStatID)

			i := p.Items[0]
			assert.Equal(t, MegaHealth, i.Type)
			assert.Equal(t, uint(1), i.Pickups)
			assert.Equal(t, pmsfoo, i.PlayerMatchStatID)
		} else if p.Alias.Alias == "bar" {
			assert.Len(t, p.Weapons, 1)
			assert.Len(t, p.Items, 0)
		} else {
			assert.Fail(t, "unexpected alias: %s",
				p.Alias.Alias)
		}
	}
}
Ejemplo n.º 17
0
func TestGetPlayerGlobalStats(t *testing.T) {
	store := test.GetStore(t)

	db := store.Conn()
	defer db.Close()

	CreateSchema(store)

	m1 := NewMatch(store, Match{Map: "q3dm17", Duration: 10})
	m2 := NewMatch(store, Match{Map: "q3dm4", Duration: 100})

	a1 := NewAliasOrCurrent(store, Alias{Alias: "foo"})
	p1 := NewPlayer(store, "fooplayer", "")

	// claim aliases
	ClaimAliasesByPlayer(store, p1, []string{"foo"})

	pms1 := NewPlayerMatchStat(store, PlayerMatchStat{
		Score:    10,
		Kills:    12,
		Deaths:   33,
		Suicides: 2,
		MatchID:  m1,
		AliasID:  a1,
	})
	NewWeaponStat(store, WeaponStat{
		Type:              RocketLauncher,
		Shots:             100,
		Hits:              10,
		PlayerMatchStatID: pms1,
	})

	NewWeaponStat(store, WeaponStat{
		Type:              GrenadeLauncher,
		Shots:             25,
		Hits:              5,
		PlayerMatchStatID: pms1,
	})

	NewItemStat(store, ItemStat{
		Type:              MegaHealth,
		Pickups:           12,
		PlayerMatchStatID: pms1,
	})

	NewItemStat(store, ItemStat{
		Type:              RedArmor,
		Pickups:           5,
		PlayerMatchStatID: pms1,
	})
	NewItemStat(store, ItemStat{
		Type:              QuadDamage,
		Pickups:           5,
		Time:              time.Duration(30) * time.Second,
		PlayerMatchStatID: pms1,
	})

	pms2 := NewPlayerMatchStat(store, PlayerMatchStat{
		Score:    5,
		Kills:    5,
		Deaths:   3,
		Suicides: 2,
		MatchID:  m2,
		AliasID:  a1,
	})
	NewWeaponStat(store, WeaponStat{
		Type:              RocketLauncher,
		Shots:             10,
		Hits:              5,
		PlayerMatchStatID: pms2,
	})
	NewWeaponStat(store, WeaponStat{
		Type:              Shotgun,
		Shots:             10,
		Hits:              5,
		PlayerMatchStatID: pms2,
	})
	NewItemStat(store, ItemStat{
		Type:              GreenArmor,
		Pickups:           3,
		PlayerMatchStatID: pms2,
	})
	NewItemStat(store, ItemStat{
		Type:              QuadDamage,
		Pickups:           1,
		Time:              time.Duration(5) * time.Second,
		PlayerMatchStatID: pms2,
	})

	pgs := GetPlayerGlobaStats(store, p1)

	assert.Equal(t, uint(12+5), pgs.Kills)
	assert.Equal(t, uint(2+2), pgs.Suicides)
	assert.Equal(t, uint(33+3), pgs.Deaths)
	assert.Equal(t, uint(2), pgs.Matches)

	assert.Len(t, pgs.Weapons, 3)
	assert.Len(t, pgs.Items, 4)

	// these should be aggregate values from all weapons
	for _, w := range pgs.Weapons {
		switch w.Type {
		case RocketLauncher:
			assert.Equal(t, uint(110), w.Shots)
			assert.Equal(t, uint(15), w.Hits)
		case GrenadeLauncher:
			assert.Equal(t, uint(25), w.Shots)
		case Shotgun:
			assert.Equal(t, uint(10), w.Shots)
			assert.Equal(t, uint(5), w.Hits)
		default:
			assert.Fail(t, "unexpected weapon type: %s", w.Type)
		}
	}

	// aggregate values for all items
	for _, i := range pgs.Items {
		switch i.Type {
		case MegaHealth:
			assert.Equal(t, uint(12), i.Pickups)
		case RedArmor:
			assert.Equal(t, uint(5), i.Pickups)
		case GreenArmor:
			assert.Equal(t, uint(3), i.Pickups)
		case QuadDamage:
			assert.Equal(t, uint(5+1), i.Pickups)
			assert.Equal(t, time.Duration(30+5)*time.Second,
				i.Time)
		}
	}
}