This repository has been archived on 2022-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
shared/models/village.go

233 lines
6.7 KiB
Go

package models
import (
"fmt"
"strconv"
"strings"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type Village struct {
tableName struct{} `pg:"?SERVER.villages,alias:village"`
ID int `json:"id" pg:"type:bigint,pk" gqlgen:"id"`
Name string `json:"name" gqlgen:"name"`
Points int `json:"points" pg:",use_zero" gqlgen:"points"`
X int `json:"x" pg:",use_zero" gqlgen:"x"`
Y int `json:"y" pg:",use_zero" gqlgen:"y"`
Bonus int `json:"bonus" pg:",use_zero" gqlgen:"bonus"`
PlayerID int `json:"-" pg:",use_zero" gqlgen:"playerID"`
Player *Player `json:"player,omitempty" gqlgen:"-" pg:"rel:has-one"`
}
func (v *Village) Continent() string {
if v == nil {
return ""
}
return fmt.Sprintf("K%c%c", strconv.FormatInt(int64(v.Y), 10)[0], strconv.FormatInt(int64(v.X), 10)[0])
}
func (v *Village) FullName() string {
return fmt.Sprintf("%s (%d|%d) %s",
v.Name,
v.X,
v.Y,
v.Continent())
}
type VillageFilter struct {
ID []int `json:"id" gqlgen:"id"`
IDNEQ []int `json:"idNEQ" gqlgen:"idNEQ"`
Name []string `json:"name" gqlgen:"name"`
NameNEQ []string `json:"nameNEQ" gqlgen:"nameNEQ"`
NameMATCH string `json:"nameMATCH" gqlgen:"nameMATCH"`
NameIEQ string `json:"nameIEQ" gqlgen:"nameIEQ"`
XGT int `json:"xGT" gqlgen:"xGT"`
XGTE int `json:"xGTE" gqlgen:"xGTE"`
XLT int `json:"xLT" gqlgen:"xLT"`
XLTE int `json:"xLTE" gqlgen:"xLTE"`
YGT int `json:"yGT" gqlgen:"yGT"`
YGTE int `json:"yGTE" gqlgen:"yGTE"`
YLT int `json:"yLT" gqlgen:"yLT"`
YLTE int `json:"yLTE" gqlgen:"yLTE"`
XY []string `json:"xy" gqlgen:"xy"`
Points int `json:"points" gqlgen:"points"`
PointsGT int `json:"pointsGT" gqlgen:"pointsGT"`
PointsGTE int `json:"pointsGTE" gqlgen:"pointsGTE"`
PointsLT int `json:"pointsLT" gqlgen:"pointsLT"`
PointsLTE int `json:"pointsLTE" gqlgen:"pointsLTE"`
Bonus int `json:"bonus" gqlgen:"bonus"`
BonusGT int `json:"bonusGT" gqlgen:"bonusGT"`
BonusGTE int `json:"bonusGTE" gqlgen:"bonusGTE"`
BonusLT int `json:"bonusLT" gqlgen:"bonusLT"`
BonusLTE int `json:"bonusLTE" gqlgen:"bonusLTE"`
PlayerID []int `json:"playerID" gqlgen:"playerID"`
PlayerIDNEQ []int `json:"playerIDNEQ" gqlgen:"playerIDNEQ"`
PlayerFilter *PlayerFilter `json:"playerFilter" gqlgen:"playerFilter"`
}
func (f *VillageFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Query, error) {
if !isZero(f.ID) {
q = q.Where(buildConditionArray(addAliasToColumnName("id", alias)), pg.Array(f.ID))
}
if !isZero(f.IDNEQ) {
q = q.Where(buildConditionNotInArray(addAliasToColumnName("id", alias)), pg.Array(f.IDNEQ))
}
if !isZero(f.Name) {
q = q.Where(buildConditionArray(addAliasToColumnName("name", alias)), pg.Array(f.Name))
}
if !isZero(f.NameNEQ) {
q = q.Where(buildConditionNotInArray(addAliasToColumnName("name", alias)), pg.Array(f.NameNEQ))
}
if !isZero(f.NameMATCH) {
q = q.Where(buildConditionMatch(addAliasToColumnName("name", alias)), f.NameMATCH)
}
if !isZero(f.NameIEQ) {
q = q.Where(buildConditionIEQ(addAliasToColumnName("name", alias)), f.NameIEQ)
}
if !isZero(f.XGT) {
q = q.Where(buildConditionGT(addAliasToColumnName("x", alias)), f.XGT)
}
if !isZero(f.XGTE) {
q = q.Where(buildConditionGTE(addAliasToColumnName("x", alias)), f.XGTE)
}
if !isZero(f.XLT) {
q = q.Where(buildConditionLT(addAliasToColumnName("x", alias)), f.XLT)
}
if !isZero(f.XLTE) {
q = q.Where(buildConditionLTE(addAliasToColumnName("x", alias)), f.XLTE)
}
if !isZero(f.YGT) {
q = q.Where(buildConditionGT(addAliasToColumnName("y", alias)), f.YGT)
}
if !isZero(f.YGTE) {
q = q.Where(buildConditionGTE(addAliasToColumnName("y", alias)), f.YGTE)
}
if !isZero(f.YLT) {
q = q.Where(buildConditionLT(addAliasToColumnName("y", alias)), f.YLT)
}
if !isZero(f.YLTE) {
q = q.Where(buildConditionLTE(addAliasToColumnName("y", alias)), f.YLTE)
}
if !isZero(f.XY) {
q = q.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
for _, xy := range f.XY {
splitted := strings.Split(xy, "|")
if len(splitted) != 2 {
continue
}
x, err := strconv.Atoi(splitted[0])
if err != nil {
continue
}
y, err := strconv.Atoi(splitted[1])
if err != nil {
continue
}
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
q = q.Where("x = ?", x)
q = q.Where("y = ?", y)
return q, nil
})
}
return q, nil
})
}
if !isZero(f.Points) {
q = q.Where(buildConditionEquals(addAliasToColumnName("points", alias)), f.Points)
}
if !isZero(f.PointsGT) {
q = q.Where(buildConditionGT(addAliasToColumnName("points", alias)), f.PointsGT)
}
if !isZero(f.PointsGTE) {
q = q.Where(buildConditionGTE(addAliasToColumnName("points", alias)), f.PointsGTE)
}
if !isZero(f.PointsLT) {
q = q.Where(buildConditionLT(addAliasToColumnName("points", alias)), f.PointsLT)
}
if !isZero(f.PointsLTE) {
q = q.Where(buildConditionLTE(addAliasToColumnName("points", alias)), f.PointsLTE)
}
if !isZero(f.Bonus) {
q = q.Where(buildConditionEquals(addAliasToColumnName("bonus", alias)), f.Bonus)
}
if !isZero(f.BonusGT) {
q = q.Where(buildConditionGT(addAliasToColumnName("bonus", alias)), f.BonusGT)
}
if !isZero(f.BonusGTE) {
q = q.Where(buildConditionGTE(addAliasToColumnName("bonus", alias)), f.BonusGTE)
}
if !isZero(f.BonusLT) {
q = q.Where(buildConditionLT(addAliasToColumnName("bonus", alias)), f.BonusLT)
}
if !isZero(f.BonusLTE) {
q = q.Where(buildConditionLTE(addAliasToColumnName("bonus", alias)), f.BonusLTE)
}
if !isZero(f.PlayerID) {
q = q.Where(buildConditionArray(addAliasToColumnName("player_id", alias)), pg.Array(f.PlayerID))
}
if !isZero(f.PlayerIDNEQ) {
q = q.Where(buildConditionNotInArray(addAliasToColumnName("player_id", alias)), pg.Array(f.PlayerIDNEQ))
}
return q, nil
}
func (f *VillageFilter) Where(q *orm.Query) (*orm.Query, error) {
return f.WhereWithAlias(q, "village")
}
type VillageRelationshipAndSortAppender struct {
Filter *VillageFilter
Sort []string
}
func (a *VillageRelationshipAndSortAppender) Append(q *orm.Query) (*orm.Query, error) {
var err error
playerRequired := findStringWithPrefix(a.Sort, "player.") != ""
playerTribeRequired := findStringWithPrefix(a.Sort, "player.tribe.") != ""
if a.Filter.PlayerFilter != nil {
q, err = a.Filter.PlayerFilter.WhereWithAlias(q, "player")
if err != nil {
return q, err
}
playerRequired = true
if a.Filter.PlayerFilter.TribeFilter != nil {
q, err = a.Filter.PlayerFilter.WhereWithAlias(q, "tribe")
if err != nil {
return q, err
}
playerTribeRequired = true
}
}
if !isZero(a.Sort) {
q = q.Order(a.Sort...)
}
if playerRequired {
q = q.Relation("Player._")
}
if playerTribeRequired {
q = q.Join("LEFT JOIN ?SERVER.tribes AS tribe ON tribe.id = player.tribe_id")
}
return q, nil
}