From 8b5bb787641bdbf13e3692ea383ce3f5002984d6 Mon Sep 17 00:00:00 2001 From: schreifuchs Date: Mon, 10 Feb 2025 09:51:43 +0100 Subject: [PATCH] start on matches --- app.go | 56 +++++++++++++++++++++++- frontend/src/routes/Matches.svelte | 24 ++++++++--- frontend/src/routes/Tournament.svelte | 11 ++--- frontend/wailsjs/go/main/App.d.ts | 6 +++ frontend/wailsjs/go/main/App.js | 12 ++++++ frontend/wailsjs/go/models.ts | 60 ++++++++++++++++++++++++++ model/model.go | 9 ++-- things.db | 0 things/resource.go | 43 ------------------ tournament.db | Bin 49152 -> 49152 bytes 10 files changed, 159 insertions(+), 62 deletions(-) delete mode 100644 things.db delete mode 100644 things/resource.go diff --git a/app.go b/app.go index ceb2ced..2452682 100644 --- a/app.go +++ b/app.go @@ -2,8 +2,12 @@ package main import ( "context" + "fmt" "gegio-ue1/model" + "os" + "slices" + "github.com/wailsapp/wails/v2/pkg/runtime" "gorm.io/gorm" ) @@ -38,7 +42,7 @@ func (a *App) GetTournaments() (ts []model.Tournament, err error) { } func (a *App) GetTournament(id int) (t model.Tournament, err error) { - err = a.db.Preload("Game").Preload("Participants").First(&t, id).Error + err = a.db.Preload("Game").Preload("Participants").Preload("Matches").First(&t, id).Error return } func (a *App) SaveTournament(t model.Tournament) error { @@ -58,6 +62,42 @@ func (a *App) FillRandom(t model.Tournament) { } +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 @@ -75,3 +115,17 @@ func (a *App) RemoveParticipantFromTournament(p model.Participant, t model.Tourn } 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) + } +} diff --git a/frontend/src/routes/Matches.svelte b/frontend/src/routes/Matches.svelte index 9a6a6dc..18ebe8b 100644 --- a/frontend/src/routes/Matches.svelte +++ b/frontend/src/routes/Matches.svelte @@ -1,17 +1,24 @@ @@ -33,7 +28,7 @@ {#if tournament}
{tournament.Title} - +
diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index af3c53d..03aa673 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -4,10 +4,14 @@ import {model} from '../models'; export function DeleteParticipat(arg1:model.Participant):Promise; +export function ExportTournament(arg1:model.Tournament):Promise; + export function FillRandom(arg1:model.Tournament):Promise; export function GetGames():Promise>; +export function GetMatches(arg1:number):Promise>; + export function GetParticipants():Promise>; export function GetTournament(arg1:number):Promise; @@ -19,3 +23,5 @@ export function RemoveParticipantFromTournament(arg1:model.Participant,arg2:mode export function SaveParticipant(arg1:model.Participant):Promise; export function SaveTournament(arg1:model.Tournament):Promise; + +export function StartTournament(arg1:model.Tournament):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index 9dec93f..dba73c9 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -6,6 +6,10 @@ export function DeleteParticipat(arg1) { return window['go']['main']['App']['DeleteParticipat'](arg1); } +export function ExportTournament(arg1) { + return window['go']['main']['App']['ExportTournament'](arg1); +} + export function FillRandom(arg1) { return window['go']['main']['App']['FillRandom'](arg1); } @@ -14,6 +18,10 @@ export function GetGames() { return window['go']['main']['App']['GetGames'](); } +export function GetMatches(arg1) { + return window['go']['main']['App']['GetMatches'](arg1); +} + export function GetParticipants() { return window['go']['main']['App']['GetParticipants'](); } @@ -37,3 +45,7 @@ export function SaveParticipant(arg1) { export function SaveTournament(arg1) { return window['go']['main']['App']['SaveTournament'](arg1); } + +export function StartTournament(arg1) { + return window['go']['main']['App']['StartTournament'](arg1); +} diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index 2c01fcc..6fb34b3 100755 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -57,6 +57,7 @@ export namespace model { WinnierParticipantID: number; WinnierParticipant: Participant; Participants: Participant[]; + Matches: Match[]; static createFrom(source: any = {}) { return new Tournament(source); @@ -76,6 +77,7 @@ export namespace model { this.WinnierParticipantID = source["WinnierParticipantID"]; this.WinnierParticipant = this.convertValues(source["WinnierParticipant"], Participant); this.Participants = this.convertValues(source["Participants"], Participant); + this.Matches = this.convertValues(source["Matches"], Match); } convertValues(a: any, classs: any, asMap: boolean = false): any { @@ -143,6 +145,64 @@ export namespace model { return a; } } + export class Match { + ID: number; + // Go type: time + CreatedAt: any; + // Go type: time + UpdatedAt: any; + // Go type: gorm + DeletedAt: any; + TournamentID: number; + Stage: number; + Order: number; + Participant1ID: number; + Participant1: Participant; + Participant2ID: number; + Participant2: Participant; + WinnierParticipantID: number; + WinnierParticipant: Participant; + + static createFrom(source: any = {}) { + return new Match(source); + } + + constructor(source: any = {}) { + if ('string' === typeof source) source = JSON.parse(source); + this.ID = source["ID"]; + this.CreatedAt = this.convertValues(source["CreatedAt"], null); + this.UpdatedAt = this.convertValues(source["UpdatedAt"], null); + this.DeletedAt = this.convertValues(source["DeletedAt"], null); + this.TournamentID = source["TournamentID"]; + this.Stage = source["Stage"]; + this.Order = source["Order"]; + this.Participant1ID = source["Participant1ID"]; + this.Participant1 = this.convertValues(source["Participant1"], Participant); + this.Participant2ID = source["Participant2ID"]; + this.Participant2 = this.convertValues(source["Participant2"], Participant); + this.WinnierParticipantID = source["WinnierParticipantID"]; + this.WinnierParticipant = this.convertValues(source["WinnierParticipant"], Participant); + } + + convertValues(a: any, classs: any, asMap: boolean = false): any { + if (!a) { + return a; + } + if (a.slice && a.map) { + return (a as any[]).map(elem => this.convertValues(elem, classs)); + } else if ("object" === typeof a) { + if (asMap) { + for (const key of Object.keys(a)) { + a[key] = new classs(a[key]); + } + return a; + } + return new classs(a); + } + return a; + } + } + } diff --git a/model/model.go b/model/model.go index ea761ae..1a5eaff 100644 --- a/model/model.go +++ b/model/model.go @@ -27,17 +27,18 @@ type Tournament struct { WinnierParticipantID int WinnierParticipant Participant `gorm:"foreignKey:WinnierParticipantID"` Participants []*Participant `gorm:"many2many:partcipant_tournaments;"` + Matches []Match } type Match struct { gorm.Model - TournamentID int + TournamentID uint Stage int Order int - Participant1ID int + Participant1ID uint Participant1 Participant `gorm:"foreignKey:Participant1ID"` - Participant2ID int + Participant2ID uint Participant2 Participant `gorm:"foreignKey:Participant2ID"` - WinnierParticipantID int + WinnierParticipantID uint WinnierParticipant Participant `gorm:"foreignKey:WinnierParticipantID"` } diff --git a/things.db b/things.db deleted file mode 100644 index e69de29..0000000 diff --git a/things/resource.go b/things/resource.go deleted file mode 100644 index 6bd18ba..0000000 --- a/things/resource.go +++ /dev/null @@ -1,43 +0,0 @@ -package things - -import "slices" - -type Thing struct { - ID int - Name string -} - -type Service struct { - things map[int]Thing - maxID int -} - -func NewThingsService() *Service { - return &Service{ - things: make(map[int]Thing), - } -} - -func (s *Service) NewThing(name string) { - s.maxID++ - s.things[s.maxID] = Thing{ - Name: name, - ID: s.maxID, - } - - print(name) -} - -func (s *Service) GetThings() []Thing { - things := make([]Thing, 0, len(s.things)) - for _, t := range s.things { - things = append(things, t) - } - slices.SortFunc(things, func(a, b Thing) int { return a.ID - b.ID }) - return things -} - -func (s *Service) DeleteThing(id int) { - delete(s.things, id) - -} diff --git a/tournament.db b/tournament.db index efeac48642ef13308a718515fbedf481cc4c72ea..b917d5ae9403f35a7181c44d92e372237db057cb 100644 GIT binary patch delta 1602 zcmb_cU1%It7`=Dy%{;1Nts>e8e&iFdLVjNDt%gv@g_xI=KOZD=_%5wTEeZKjZu`g%A zEF$(TWZ!~sV7NJ$->}mUhMzs|vOJ3u&r2A0JfFv29C9(n+=Q_gi^=ITuM!=lKMs$c z+yx9{5%@fG+rZbW3zhOx=|UYL^l}D%rW?$H(B+|s{Wkc_N@Z#F6-7rcOs1Q|BVISu z3s@vvw^P4`%xqYaSA`7(_ux192{xL8>|1!+RLKzS*c>2a6fc&R>eX^}v9wTM)@_B1 zH1D1|Jh0C`h;&OJ`+IobUa^F=ZVxpF2aZ{d%UCr~u~;0Zlrt{I86V#f$Q29 zKw9x_y;Whv=yBQb{KOS<=zhexw>|V8n|ndtsFddOQ?>GJr9MSk+y@L}`y7k&u1HuY zKgWw*7Du7qnS2kVu@~m0(!%n4I3t}r_Fqdnq(S1kPUx~gMv8g6$MPf)5js*;Lz)ia zHp|3PZLV6*Pt@L(Q48cex-#)N%$Ko}Y3`2FBabC~{E?V0h=`mxlpPIxKS&o}JGkG3 ze~=EV2>yjn;B(l7FJV>Q>oTNA&-FJtN!}}is!9;nvPC~ZinwPiLDF5R-5zC>Ozxy- zM|8W5U0uwsX+}#dWFUm$I)dv`(Z?|TCN{H_AHbOQCMC#ji5rQ~6T;=Yhq`0JjU5s5 zFlG_&gsC*Md_hr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