regio-ue1/app.go
2025-02-10 09:51:43 +01:00

132 lines
3.1 KiB
Go

package main
import (
"context"
"fmt"
"gegio-ue1/model"
"os"
"slices"
"github.com/wailsapp/wails/v2/pkg/runtime"
"gorm.io/gorm"
)
// App struct
type App struct {
ctx context.Context
db *gorm.DB
}
// NewApp creates a new App application struct
func NewApp(db *gorm.DB) *App {
return &App{
db: db,
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) GetGames() (gs []model.Game, err error) {
err = a.db.Find(&gs).Error
return
}
func (a *App) GetTournaments() (ts []model.Tournament, err error) {
err = a.db.Preload("Game").Find(&ts).Error
return
}
func (a *App) GetTournament(id int) (t model.Tournament, err error) {
err = a.db.Preload("Game").Preload("Participants").Preload("Matches").First(&t, id).Error
return
}
func (a *App) SaveTournament(t model.Tournament) error {
err := a.db.Save(&t).Error
return err
}
func (a *App) FillRandom(t model.Tournament) {
for range t.Size - len(t.Participants) {
t.Participants = append(t.Participants, &model.Participant{
Name: model.RandomName(),
IsTemporary: true,
IsTeam: true,
})
}
a.db.Save(&t.Participants)
a.db.Save(&t)
}
func (a *App) StartTournament(t model.Tournament) {
t.TournamentState = 1
a.SaveTournament(t)
t, _ = a.GetTournament(int(t.ID))
matches := make([]model.Match, 0, len(t.Participants)/2)
for i := range len(t.Participants) / 2 {
matches = append(matches, model.Match{
TournamentID: t.ID,
Stage: 1,
Order: i,
Participant1ID: t.Participants[i].ID,
Participant2ID: t.Participants[i+len(t.Participants)/2].ID,
})
}
if len(t.Participants)%2 != 0 {
matches = append(matches, model.Match{
TournamentID: t.ID,
Stage: 1,
Order: len(t.Participants)/2 + 1,
Participant1ID: t.Participants[len(t.Participants)-1].ID,
Participant2ID: t.Participants[len(t.Participants)-1].ID,
})
}
a.db.Save(matches)
}
func (a *App) ExportTournament(t model.Tournament) {
dirname, _ := os.UserHomeDir()
str, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{DefaultDirectory: dirname, DefaultFilename: "tournament.csv"})
if err != nil {
fmt.Println(err)
}
println(str)
}
func (a *App) GetParticipants() (ps []model.Participant, err error) {
err = a.db.Find(&ps).Error
return
}
func (a *App) SaveParticipant(p model.Participant) error {
return a.db.Save(&p).Error
}
func (a *App) DeleteParticipat(p model.Participant) {
a.db.Delete(&p)
}
func (a *App) RemoveParticipantFromTournament(p model.Participant, t model.Tournament) {
if p.IsTemporary {
a.db.Delete(&p)
return
}
a.db.Model(&t).Association("Participants").Delete(&p)
}
func (a *App) GetMatches(tID uint) [][]model.Match {
matches := make([][]model.Match, 0)
for {
ms := make([]model.Match, 0)
if err := a.db.Where("tournament_id = ? AND stage = ?", tID, len(matches)).Find(&ms).Error; err != nil || len(ms) == 0 {
fmt.Println(ms)
fmt.Println(err)
return matches
}
slices.SortFunc(ms, func(a, b model.Match) int { return a.Order - b.Order })
matches = append(matches, ms)
}
}