generated from schreifuchs/wails-template
53 lines
776 B
Go
53 lines
776 B
Go
package model
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Game struct {
|
|
gorm.Model
|
|
TeamA int `gorm:"-"`
|
|
TeamB int `gorm:"-"`
|
|
|
|
Steps []Step `gorm:"foreignKey:GameID"`
|
|
}
|
|
|
|
func (g *Game) SumPoints() {
|
|
g.TeamA = 0
|
|
g.TeamB = 0
|
|
|
|
for _, s := range g.Steps {
|
|
g.TeamA += s.PointsTeamA + s.AdderTeamA
|
|
g.TeamB += s.PointsTeamB + s.AdderTeamB
|
|
}
|
|
}
|
|
|
|
type Step struct {
|
|
gorm.Model
|
|
GameID uint
|
|
Game Game `gorm:"foreignKey:GameID"`
|
|
|
|
PointsTeamA int
|
|
AdderTeamA int
|
|
PointsTeamB int
|
|
AdderTeamB int
|
|
}
|
|
|
|
func InitDB() *gorm.DB {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
db, err := gorm.Open(sqlite.Open(path.Join(home, "tichu.db")))
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
db.AutoMigrate(Game{}, Step{})
|
|
return db
|
|
}
|