refactor: remove other servers - player
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
4d176b2f10
commit
df9410ee45
|
@ -7,34 +7,27 @@ import (
|
|||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type PlayerServer struct {
|
||||
Key string `json:"key"`
|
||||
ProfileURL string `json:"profileUrl"`
|
||||
DeletedAt time.Time `json:"deletedAt"`
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
bun.BaseModel `bun:"base_model,table:players,alias:player"`
|
||||
|
||||
ID int64 `bun:"id,nullzero,pk"`
|
||||
ServerKey string `bun:"server_key,nullzero,pk,type:varchar(100)"`
|
||||
Name string `bun:"name,nullzero,notnull,type:varchar(150)"`
|
||||
NumVillages int64 `bun:"num_villages,default:0"`
|
||||
Points int64 `bun:"points,default:0"`
|
||||
Rank int64 `bun:"rank,default:0"`
|
||||
TribeID int64 `bun:"tribe_id,nullzero"`
|
||||
Tribe Tribe `bun:"tribe,rel:belongs-to,join:tribe_id=id,join:server_key=server_key"`
|
||||
ProfileURL string `bun:"profile_url,nullzero,type:varchar(150)"`
|
||||
BestRank int64 `bun:"best_rank,default:999999"`
|
||||
BestRankAt time.Time `bun:"best_rank_at,nullzero,notnull,default:current_timestamp"`
|
||||
MostPoints int64 `bun:"most_points,default:0"`
|
||||
MostPointsAt time.Time `bun:"most_points_at,nullzero,notnull,default:current_timestamp"`
|
||||
MostVillages int64 `bun:"most_villages,default:0"`
|
||||
MostVillagesAt time.Time `bun:"most_villages_at,nullzero,notnull,default:current_timestamp"`
|
||||
LastActivityAt time.Time `bun:"last_activity_at,nullzero,notnull,default:current_timestamp"`
|
||||
OtherServers []PlayerServer `bun:"other_servers,scanonly"`
|
||||
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp"`
|
||||
DeletedAt time.Time `bun:"deleted_at,nullzero"`
|
||||
ID int64 `bun:"id,nullzero,pk"`
|
||||
ServerKey string `bun:"server_key,nullzero,pk,type:varchar(100)"`
|
||||
Name string `bun:"name,nullzero,notnull,type:varchar(150)"`
|
||||
NumVillages int64 `bun:"num_villages,default:0"`
|
||||
Points int64 `bun:"points,default:0"`
|
||||
Rank int64 `bun:"rank,default:0"`
|
||||
TribeID int64 `bun:"tribe_id,nullzero"`
|
||||
Tribe Tribe `bun:"tribe,rel:belongs-to,join:tribe_id=id,join:server_key=server_key"`
|
||||
ProfileURL string `bun:"profile_url,nullzero,type:varchar(150)"`
|
||||
BestRank int64 `bun:"best_rank,default:999999"`
|
||||
BestRankAt time.Time `bun:"best_rank_at,nullzero,notnull,default:current_timestamp"`
|
||||
MostPoints int64 `bun:"most_points,default:0"`
|
||||
MostPointsAt time.Time `bun:"most_points_at,nullzero,notnull,default:current_timestamp"`
|
||||
MostVillages int64 `bun:"most_villages,default:0"`
|
||||
MostVillagesAt time.Time `bun:"most_villages_at,nullzero,notnull,default:current_timestamp"`
|
||||
LastActivityAt time.Time `bun:"last_activity_at,nullzero,notnull,default:current_timestamp"`
|
||||
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp"`
|
||||
DeletedAt time.Time `bun:"deleted_at,nullzero"`
|
||||
|
||||
OpponentsDefeated
|
||||
}
|
||||
|
@ -86,12 +79,7 @@ func (p Player) ToDomain() domain.Player {
|
|||
|
||||
func (p Player) ToDomainWithRelations() domain.PlayerWithRelations {
|
||||
res := domain.PlayerWithRelations{
|
||||
Player: p.ToDomain(),
|
||||
OtherServers: make([]domain.PlayerServer, 0, len(p.OtherServers)),
|
||||
}
|
||||
|
||||
for _, srv := range p.OtherServers {
|
||||
res.OtherServers = append(res.OtherServers, domain.PlayerServer(srv))
|
||||
Player: p.ToDomain(),
|
||||
}
|
||||
|
||||
if p.Tribe.ID > 0 {
|
||||
|
|
|
@ -72,28 +72,12 @@ func TestPlayer(t *testing.T) {
|
|||
result.Tribe = model.Tribe{
|
||||
ID: params.TribeID,
|
||||
}
|
||||
result.OtherServers = []model.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-999",
|
||||
DeletedAt: time.Time{},
|
||||
},
|
||||
{
|
||||
Key: "pl155",
|
||||
ProfileURL: "profile-999",
|
||||
DeletedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
playerWithRelations := result.ToDomainWithRelations()
|
||||
assert.Equal(t, player, playerWithRelations.Player)
|
||||
assert.WithinDuration(t, time.Now(), playerWithRelations.CreatedAt, 10*time.Millisecond)
|
||||
assert.True(t, playerWithRelations.Tribe.Valid)
|
||||
assert.Equal(t, params.TribeID, playerWithRelations.Tribe.Tribe.ID)
|
||||
assert.Len(t, playerWithRelations.OtherServers, len(result.OtherServers))
|
||||
for i, srv := range result.OtherServers {
|
||||
assert.EqualValues(t, srv, playerWithRelations.OtherServers[i])
|
||||
}
|
||||
|
||||
metaWithRelations := result.ToMetaWithRelations()
|
||||
assert.Equal(t, meta, metaWithRelations.PlayerMeta)
|
||||
|
|
|
@ -114,27 +114,15 @@ func (p *Player) ListCountWithRelations(ctx context.Context, params domain.ListP
|
|||
Model(&model.Player{}).
|
||||
Apply(paramsApplier.applyFilters)
|
||||
|
||||
otherServersQ := p.db.NewSelect().
|
||||
Model(&model.Player{}).
|
||||
ExcludeColumn("*").
|
||||
ColumnExpr("jsonb_agg(jsonb_build_object('key', player2.server_key, 'profileUrl', player2.profile_url, 'deletedAt', player2.deleted_at))").
|
||||
ModelTableExpr("?TableName as player2").
|
||||
Join("INNER JOIN servers AS server2 ON server2.key = player2.server_key").
|
||||
Where("player2.id = player.id").
|
||||
Where("server2.version_code = server.version_code").
|
||||
Where("server2.key != server.key")
|
||||
|
||||
q := p.db.NewSelect().
|
||||
Model(&players).
|
||||
ColumnExpr("?TableColumns").
|
||||
ColumnExpr("(?) AS other_servers", otherServersQ).
|
||||
Order(playerOrders...).
|
||||
Apply(paramsApplier.applySort).
|
||||
Where("(player.id, player.server_key) IN (?)", subQ).
|
||||
Relation("Tribe", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
return q.Column(tribeMetaColumns...)
|
||||
}).
|
||||
Join("INNER JOIN servers AS server ON server.key = player.server_key")
|
||||
})
|
||||
|
||||
count, err := scanAndCount(ctx, cntQ, q)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
|
|
|
@ -27,17 +27,10 @@ type Player struct {
|
|||
DeletedAt time.Time
|
||||
}
|
||||
|
||||
type PlayerServer struct {
|
||||
Key string
|
||||
ProfileURL string
|
||||
DeletedAt time.Time
|
||||
}
|
||||
|
||||
type PlayerWithRelations struct {
|
||||
Player
|
||||
|
||||
Tribe NullTribeMeta
|
||||
OtherServers []PlayerServer
|
||||
Tribe NullTribeMeta
|
||||
}
|
||||
|
||||
type PlayerMeta struct {
|
||||
|
|
|
@ -7,38 +7,31 @@ import (
|
|||
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
||||
)
|
||||
|
||||
type PlayerServer struct {
|
||||
Key string `json:"key"`
|
||||
ProfileURL string `json:"profileUrl"`
|
||||
DeletedAt NullTime `json:"deletedAt" swaggertype:"string" format:"date-time" extensions:"x-nullable"`
|
||||
} // @name PlayerServer
|
||||
|
||||
type Player struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
NumVillages int64 `json:"numVillages"`
|
||||
Points int64 `json:"points"`
|
||||
Rank int64 `json:"rank"`
|
||||
Tribe NullTribeMeta `json:"tribe" extensions:"x-nullable"`
|
||||
RankAtt int64 `json:"rankAtt"`
|
||||
ScoreAtt int64 `json:"scoreAtt"`
|
||||
RankDef int64 `json:"rankDef"`
|
||||
ScoreDef int64 `json:"scoreDef"`
|
||||
RankSup int64 `json:"rankSup"`
|
||||
ScoreSup int64 `json:"scoreSup"`
|
||||
RankTotal int64 `json:"rankTotal"`
|
||||
ScoreTotal int64 `json:"scoreTotal"`
|
||||
ProfileURL string `json:"profileUrl"`
|
||||
BestRank int64 `json:"bestRank"`
|
||||
BestRankAt time.Time `json:"bestRankAt" format:"date-time"`
|
||||
MostPoints int64 `json:"mostPoints"`
|
||||
MostPointsAt time.Time `json:"mostPointsAt" format:"date-time"`
|
||||
MostVillages int64 `json:"mostVillages"`
|
||||
MostVillagesAt time.Time `json:"mostVillagesAt" format:"date-time"`
|
||||
LastActivityAt time.Time `json:"lastActivityAt" format:"date-time"`
|
||||
OtherServers []PlayerServer `json:"otherServers"`
|
||||
CreatedAt time.Time `json:"createdAt" format:"date-time"`
|
||||
DeletedAt NullTime `json:"deletedAt" swaggertype:"string" format:"date-time" extensions:"x-nullable"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
NumVillages int64 `json:"numVillages"`
|
||||
Points int64 `json:"points"`
|
||||
Rank int64 `json:"rank"`
|
||||
Tribe NullTribeMeta `json:"tribe" extensions:"x-nullable"`
|
||||
RankAtt int64 `json:"rankAtt"`
|
||||
ScoreAtt int64 `json:"scoreAtt"`
|
||||
RankDef int64 `json:"rankDef"`
|
||||
ScoreDef int64 `json:"scoreDef"`
|
||||
RankSup int64 `json:"rankSup"`
|
||||
ScoreSup int64 `json:"scoreSup"`
|
||||
RankTotal int64 `json:"rankTotal"`
|
||||
ScoreTotal int64 `json:"scoreTotal"`
|
||||
ProfileURL string `json:"profileUrl"`
|
||||
BestRank int64 `json:"bestRank"`
|
||||
BestRankAt time.Time `json:"bestRankAt" format:"date-time"`
|
||||
MostPoints int64 `json:"mostPoints"`
|
||||
MostPointsAt time.Time `json:"mostPointsAt" format:"date-time"`
|
||||
MostVillages int64 `json:"mostVillages"`
|
||||
MostVillagesAt time.Time `json:"mostVillagesAt" format:"date-time"`
|
||||
LastActivityAt time.Time `json:"lastActivityAt" format:"date-time"`
|
||||
CreatedAt time.Time `json:"createdAt" format:"date-time"`
|
||||
DeletedAt NullTime `json:"deletedAt" swaggertype:"string" format:"date-time" extensions:"x-nullable"`
|
||||
} // @name Player
|
||||
|
||||
func NewPlayer(p domain.PlayerWithRelations) Player {
|
||||
|
@ -50,18 +43,6 @@ func NewPlayer(p domain.PlayerWithRelations) Player {
|
|||
}
|
||||
}
|
||||
|
||||
otherServers := make([]PlayerServer, 0, len(p.OtherServers))
|
||||
for _, srv := range p.OtherServers {
|
||||
otherServers = append(otherServers, PlayerServer{
|
||||
Key: srv.Key,
|
||||
ProfileURL: srv.ProfileURL,
|
||||
DeletedAt: NullTime{
|
||||
Time: srv.DeletedAt,
|
||||
Valid: !srv.DeletedAt.IsZero(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return Player{
|
||||
ID: p.ID,
|
||||
Name: p.Name,
|
||||
|
@ -85,7 +66,6 @@ func NewPlayer(p domain.PlayerWithRelations) Player {
|
|||
MostVillages: p.MostVillages,
|
||||
MostVillagesAt: p.MostVillagesAt,
|
||||
LastActivityAt: p.LastActivityAt,
|
||||
OtherServers: otherServers,
|
||||
CreatedAt: p.CreatedAt,
|
||||
DeletedAt: NullTime{
|
||||
Time: p.DeletedAt,
|
||||
|
|
|
@ -40,18 +40,6 @@ func TestNewPlayer(t *testing.T) {
|
|||
ServerKey: "pl151",
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
OtherServers: []domain.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: time.Time{},
|
||||
},
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: time.Now(),
|
||||
},
|
||||
},
|
||||
}
|
||||
assertPlayer(t, player, model.NewPlayer(player))
|
||||
|
||||
|
@ -292,18 +280,6 @@ func TestNewListPlayersResp(t *testing.T) {
|
|||
ServerKey: "pl151",
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
OtherServers: []domain.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: time.Time{},
|
||||
},
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: time.Now(),
|
||||
},
|
||||
},
|
||||
Tribe: domain.NullTribeMeta{
|
||||
Valid: true,
|
||||
Tribe: domain.TribeMeta{
|
||||
|
@ -414,23 +390,11 @@ func assertPlayer(tb testing.TB, dp domain.PlayerWithRelations, rp model.Player)
|
|||
assert.Equal(tb, dp.MostVillages, rp.MostVillages)
|
||||
assert.Equal(tb, dp.MostVillagesAt, rp.MostVillagesAt)
|
||||
assert.Equal(tb, dp.LastActivityAt, rp.LastActivityAt)
|
||||
assert.Len(tb, rp.OtherServers, len(dp.OtherServers))
|
||||
for i, srv := range dp.OtherServers {
|
||||
assertPlayerServer(tb, srv, rp.OtherServers[i])
|
||||
}
|
||||
assert.Equal(tb, dp.CreatedAt, rp.CreatedAt)
|
||||
assertNullTribeMeta(tb, dp.Tribe, rp.Tribe)
|
||||
assertNullTime(tb, dp.DeletedAt, rp.DeletedAt)
|
||||
}
|
||||
|
||||
func assertPlayerServer(tb testing.TB, dps domain.PlayerServer, rps model.PlayerServer) {
|
||||
tb.Helper()
|
||||
|
||||
assert.Equal(tb, dps.Key, rps.Key)
|
||||
assert.Equal(tb, dps.ProfileURL, rps.ProfileURL)
|
||||
assertNullTime(tb, dps.DeletedAt, rps.DeletedAt)
|
||||
}
|
||||
|
||||
func assertPlayerMeta(tb testing.TB, dp domain.PlayerMetaWithRelations, rp model.PlayerMeta) {
|
||||
tb.Helper()
|
||||
|
||||
|
|
|
@ -104,18 +104,6 @@ func TestPlayer_list(t *testing.T) {
|
|||
ProfileURL: "profile-1234",
|
||||
},
|
||||
},
|
||||
OtherServers: []domain.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: time.Time{},
|
||||
},
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: now,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Player: domain.Player{
|
||||
|
@ -191,21 +179,6 @@ func TestPlayer_list(t *testing.T) {
|
|||
ProfileURL: "profile-1234",
|
||||
},
|
||||
},
|
||||
OtherServers: []model.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: model.NullTime{},
|
||||
},
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: model.NullTime{
|
||||
Time: now,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
CreatedAt: now,
|
||||
},
|
||||
{
|
||||
|
@ -230,7 +203,6 @@ func TestPlayer_list(t *testing.T) {
|
|||
MostVillages: 1114,
|
||||
MostVillagesAt: now.Add(time.Hour),
|
||||
LastActivityAt: now.Add(time.Minute),
|
||||
OtherServers: []model.PlayerServer{},
|
||||
Tribe: model.NullTribeMeta{
|
||||
Valid: false,
|
||||
},
|
||||
|
@ -336,7 +308,6 @@ func TestPlayer_list(t *testing.T) {
|
|||
MostVillages: 1114,
|
||||
MostVillagesAt: now.Add(time.Hour),
|
||||
LastActivityAt: now.Add(time.Minute),
|
||||
OtherServers: []model.PlayerServer{},
|
||||
Tribe: model.NullTribeMeta{
|
||||
Valid: false,
|
||||
},
|
||||
|
@ -449,7 +420,6 @@ func TestPlayer_list(t *testing.T) {
|
|||
MostVillages: 1114,
|
||||
MostVillagesAt: now.Add(time.Hour),
|
||||
LastActivityAt: now.Add(time.Minute),
|
||||
OtherServers: []model.PlayerServer{},
|
||||
Tribe: model.NullTribeMeta{
|
||||
Valid: false,
|
||||
},
|
||||
|
@ -727,18 +697,6 @@ func TestPlayer_getByID(t *testing.T) {
|
|||
ProfileURL: "profile-1234",
|
||||
},
|
||||
},
|
||||
OtherServers: []domain.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: time.Time{},
|
||||
},
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: now,
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
versionCode: version.Code,
|
||||
|
@ -770,21 +728,6 @@ func TestPlayer_getByID(t *testing.T) {
|
|||
MostVillagesAt: now.Add(time.Minute),
|
||||
LastActivityAt: now.Add(time.Second),
|
||||
CreatedAt: now,
|
||||
OtherServers: []model.PlayerServer{
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: model.NullTime{},
|
||||
},
|
||||
{
|
||||
Key: "pl153",
|
||||
ProfileURL: "profile-997",
|
||||
DeletedAt: model.NullTime{
|
||||
Time: now,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Tribe: model.NullTribeMeta{
|
||||
Tribe: model.TribeMeta{
|
||||
ID: 1234,
|
||||
|
|
Reference in New Issue
Block a user