diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..827cc07 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Wails: Debug myproject", + "type": "go", + "request": "launch", + "mode": "exec", + "program": "${workspaceFolder}/build/bin/vscode", + "preLaunchTask": "build", + "cwd": "${workspaceFolder}", + "env": {} + } + ] + } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..3360ab3 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,23 @@ +{ + "version": "2.0.0", + "tasks": [ + + { + "label": "build", + "type": "shell", + "options": { + "cwd": "${workspaceFolder}" + }, + "command": "go", + "args": [ + "build", + "-tags", + "dev", + "-gcflags", + "all=-N -l", + "-o", + "build/bin/vscode" + ], + } + ] + } \ No newline at end of file diff --git a/app.go b/app.go index 2452682..9460bc4 100644 --- a/app.go +++ b/app.go @@ -37,7 +37,7 @@ func (a *App) GetGames() (gs []model.Game, err error) { } func (a *App) GetTournaments() (ts []model.Tournament, err error) { - err = a.db.Preload("Game").Find(&ts).Error + err = a.db.Preload("Game").Preload("WinnerParticipant").Find(&ts).Error return } @@ -117,15 +117,63 @@ func (a *App) RemoveParticipantFromTournament(p model.Participant, t model.Tourn } 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) + rawMatches := make([]model.Match, 0) + a.db.Preload("Participant1").Preload("Participant2").Preload("WinnerParticipant").Where("tournament_id = ?", tID).Find(&rawMatches) + slices.SortFunc(rawMatches, func(a model.Match, b model.Match) int { return a.Stage - b.Stage }) + + if len(rawMatches) < 1 { + return [][]model.Match{} } + + matches := make([][]model.Match, rawMatches[len(rawMatches)-1].Stage) + for _, match := range rawMatches { + matches[match.Stage-1] = append(matches[match.Stage-1], match) + } + return matches +} + +func (a *App) CreateStage(tID uint) { + oldMatches := a.GetMatches(tID) + + if len(oldMatches) < 1 { + return + } + + participants := make([]model.Participant, 0, len(oldMatches[len(oldMatches)-1])) + + for _, m := range oldMatches[len(oldMatches)-1] { + participants = append(participants, m.WinnerParticipant) + } + + if len(participants) == 0 { + return + } + + stage := oldMatches[len(oldMatches)-1][len(oldMatches[len(oldMatches)-1])-1].Stage + 1 + + matches := make([]model.Match, 0, len(participants)/2) + for i := range len(participants) / 2 { + matches = append(matches, model.Match{ + TournamentID: tID, + Order: i, + Stage: stage, + Participant1ID: participants[i].ID, + Participant2ID: participants[i+len(participants)/2].ID, + }) + } + if len(participants)%2 != 0 { + matches = append(matches, model.Match{ + TournamentID: tID, + Order: len(participants)/2 + 1, + Stage: stage, + Participant1ID: participants[len(participants)-1].ID, + Participant2ID: participants[len(participants)-1].ID, + WinnerParticipantID: participants[len(participants)-1].ID, + }) + } + a.db.Save(matches) +} + +func (a *App) SaveMatch(m *model.Match) { + a.db.Save(m) } diff --git a/frontend/src/routes/Home.svelte b/frontend/src/routes/Home.svelte index 5286e49..ebcdd31 100644 --- a/frontend/src/routes/Home.svelte +++ b/frontend/src/routes/Home.svelte @@ -43,9 +43,7 @@ {t.Game.Name} - - {t.WinnierParticipant.Name} - + {t.WinnerParticipant.Name}
-
diff --git a/frontend/src/routes/Matches.svelte b/frontend/src/routes/Matches.svelte index 18ebe8b..4ba14d2 100644 --- a/frontend/src/routes/Matches.svelte +++ b/frontend/src/routes/Matches.svelte @@ -1,24 +1,49 @@
-
+
-
- e +
- +
+ +
diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 03aa673..3fc226e 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -2,6 +2,8 @@ // This file is automatically generated. DO NOT EDIT import {model} from '../models'; +export function CreateStage(arg1:number):Promise; + export function DeleteParticipat(arg1:model.Participant):Promise; export function ExportTournament(arg1:model.Tournament):Promise; @@ -20,6 +22,8 @@ export function GetTournaments():Promise>; export function RemoveParticipantFromTournament(arg1:model.Participant,arg2:model.Tournament):Promise; +export function SaveMatch(arg1:model.Match):Promise; + export function SaveParticipant(arg1:model.Participant):Promise; export function SaveTournament(arg1:model.Tournament):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index dba73c9..20f31b0 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -2,6 +2,10 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT +export function CreateStage(arg1) { + return window['go']['main']['App']['CreateStage'](arg1); +} + export function DeleteParticipat(arg1) { return window['go']['main']['App']['DeleteParticipat'](arg1); } @@ -38,6 +42,10 @@ export function RemoveParticipantFromTournament(arg1, arg2) { return window['go']['main']['App']['RemoveParticipantFromTournament'](arg1, arg2); } +export function SaveMatch(arg1) { + return window['go']['main']['App']['SaveMatch'](arg1); +} + export function SaveParticipant(arg1) { return window['go']['main']['App']['SaveParticipant'](arg1); } diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index 6fb34b3..6c011d9 100755 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -54,8 +54,8 @@ export namespace model { Game: Game; Size: number; TournamentState: number; - WinnierParticipantID: number; - WinnierParticipant: Participant; + WinnerParticipantID: number; + WinnerParticipant: Participant; Participants: Participant[]; Matches: Match[]; @@ -74,8 +74,8 @@ export namespace model { this.Game = this.convertValues(source["Game"], Game); this.Size = source["Size"]; this.TournamentState = source["TournamentState"]; - this.WinnierParticipantID = source["WinnierParticipantID"]; - this.WinnierParticipant = this.convertValues(source["WinnierParticipant"], Participant); + this.WinnerParticipantID = source["WinnerParticipantID"]; + this.WinnerParticipant = this.convertValues(source["WinnerParticipant"], Participant); this.Participants = this.convertValues(source["Participants"], Participant); this.Matches = this.convertValues(source["Matches"], Match); } @@ -160,8 +160,8 @@ export namespace model { Participant1: Participant; Participant2ID: number; Participant2: Participant; - WinnierParticipantID: number; - WinnierParticipant: Participant; + WinnerParticipantID: number; + WinnerParticipant: Participant; static createFrom(source: any = {}) { return new Match(source); @@ -180,8 +180,8 @@ export namespace model { 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); + this.WinnerParticipantID = source["WinnerParticipantID"]; + this.WinnerParticipant = this.convertValues(source["WinnerParticipant"], Participant); } convertValues(a: any, classs: any, asMap: boolean = false): any { diff --git a/model/model.go b/model/model.go index 1a5eaff..2e18935 100644 --- a/model/model.go +++ b/model/model.go @@ -19,26 +19,26 @@ type Participant struct { type Tournament struct { gorm.Model - Title string - GameID int - Game Game `gorm:"foreignKey:GameID"` - Size int // number of prarticipants - TournamentState int - WinnierParticipantID int - WinnierParticipant Participant `gorm:"foreignKey:WinnierParticipantID"` - Participants []*Participant `gorm:"many2many:partcipant_tournaments;"` - Matches []Match + Title string + GameID int + Game Game `gorm:"foreignKey:GameID"` + Size int // number of prarticipants + TournamentState int + WinnerParticipantID int + WinnerParticipant Participant `gorm:"foreignKey:WinnerParticipantID"` + Participants []*Participant `gorm:"many2many:partcipant_tournaments;"` + Matches []Match } type Match struct { gorm.Model - TournamentID uint - Stage int - Order int - Participant1ID uint - Participant1 Participant `gorm:"foreignKey:Participant1ID"` - Participant2ID uint - Participant2 Participant `gorm:"foreignKey:Participant2ID"` - WinnierParticipantID uint - WinnierParticipant Participant `gorm:"foreignKey:WinnierParticipantID"` + TournamentID uint + Stage int + Order int + Participant1ID uint + Participant1 Participant `gorm:"foreignKey:Participant1ID"` + Participant2ID uint + Participant2 Participant `gorm:"foreignKey:Participant2ID"` + WinnerParticipantID uint + WinnerParticipant Participant `gorm:"foreignKey:WinnerParticipantID"` } diff --git a/tournament.db b/tournament.db index b917d5a..75dc330 100644 Binary files a/tournament.db and b/tournament.db differ