added matches

This commit is contained in:
schreifuchs 2025-02-19 14:03:57 +01:00
parent 8b5bb78764
commit fccdcc176e
11 changed files with 224 additions and 50 deletions

15
.vscode/launch.json vendored Normal file
View File

@ -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": {}
}
]
}

23
.vscode/tasks.json vendored Normal file
View File

@ -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"
],
}
]
}

70
app.go
View File

@ -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)
}

View File

@ -43,9 +43,7 @@
<TableBodyCell>
{t.Game.Name}
</TableBodyCell>
<TableBodyCell>
{t.WinnierParticipant.Name}
</TableBodyCell>
<TableBodyCell>{t.WinnerParticipant.Name}</TableBodyCell>
<TableBodyCell>
<Link class="text-primary-500 underline" to={`/tournament/${t.ID}`}
@ -56,7 +54,6 @@
{/each}
</TableBody>
<div class="flex-row justify-evenly p-5">
<Button>View</Button>
<Button on:click={(_) => (newThing = true)}>New Tournament</Button>
</div>
<Modal bind:open={newThing}>

View File

@ -1,24 +1,49 @@
<script lang="ts">
import { onMount } from "svelte";
import {
CreateStage,
GetMatches,
GetTournament,
SaveMatch,
SaveTournament,
StartTournament,
} from "../../wailsjs/go/main/App";
import { model } from "../../wailsjs/go/models";
import { Button, TabItem, Tabs } from "flowbite-svelte";
import {
Button,
TabItem,
Table,
TableBody,
TableBodyCell,
TableBodyRow,
TableHead,
TableHeadCell,
Tabs,
Tooltip,
} from "flowbite-svelte";
let { tournamentID }: { tournamentID: number } = $props();
let tournament: model.Tournament = $state(new model.Tournament());
let matches: model.Match[][] = $state([]);
function saveMatch(m: model.Match): Promise<void> {
if (matches[matches.length - 1].length == 1) {
tournament.WinnerParticipantID = m.WinnerParticipantID;
SaveTournament(tournament).then(update);
}
return SaveMatch(m);
}
function update() {
GetTournament(tournamentID).then((t) => {
console.log(t);
tournament = t;
});
GetMatches(tournamentID).then((ms) => (matches = ms));
GetMatches(tournamentID).then((ms) => {
console.log("ms:", ms);
matches = ms;
});
}
$effect(update);
onMount(update);
@ -26,14 +51,67 @@
{#if tournament.TournamentState == 0}
<Button
disabled={tournament.Participants.length < tournament.Size}
onclick={() => {
StartTournament(tournament).then(update);
}}>Start</Button
>
{#if tournament.Participants.length < tournament.Size}
<Tooltip>Tournament must be filled</Tooltip>
{/if}
{:else}
<Tabs>
{#each matches as ms, i}
<TabItem title={`Stage: ${i}`}></TabItem>
<TabItem open={i + 1 == matches.length} title={`Stage: ${i + 1}`}>
<Table>
<TableHead>
<TableHeadCell>Participant1</TableHeadCell>
<TableHeadCell>Participant2</TableHeadCell>
<TableHeadCell>Winner</TableHeadCell>
</TableHead>
<TableBody>
{#each ms as m}
<TableBodyRow>
<TableBodyCell>
{m.Participant1.Name}
</TableBodyCell>
<TableBodyCell>
{m.Participant2.Name}
</TableBodyCell>
<TableBodyCell>
{#if m.WinnerParticipant.Name == ""}
<Button
class="m-1"
onclick={() => {
m.WinnerParticipant = m.Participant1;
saveMatch(m).then(update);
}}
>
{m.Participant1.Name}
</Button>
<Button
onclick={() => {
m.WinnerParticipantID = m.Participant2ID;
saveMatch(m).then(update);
}}
>
{m.Participant2.Name}
</Button>
{:else}
{m.WinnerParticipant.Name}
{/if}
</TableBodyCell>
</TableBodyRow>
{/each}
</TableBody>
</Table>
</TabItem>
{/each}
{#if matches.length > 0 && matches[matches.length - 1].length > 1 && matches[matches.length - 1].reduce( (acc, m) => {
return acc && m.WinnerParticipant.Name != "";
}, )}
<Button onclick={() => CreateStage(tournamentID).then(update)}>new</Button
>
{/if}
</Tabs>
{/if}

View File

@ -26,12 +26,11 @@
</script>
<form onsubmit={submit}>
<div>
<div class="m-5">
<Label>Title</Label>
<Input type="text" bind:value={tournament.Title} />
</div>
<div>
e
<div class="m-5">
<Label>Game</Label>
<Select
items={games.map((g) => {
@ -40,9 +39,11 @@
bind:value={tournament.GameID}
/>
</div>
<div>
<div class="m-5">
<Label>Size</Label>
<Input type="number" step="1" min="1" bind:value={tournament.Size} />
</div>
<Button type="submit">Save</Button>
<div class="m-5">
<Button type="submit">Save</Button>
</div>
</form>

View File

@ -2,6 +2,8 @@
// This file is automatically generated. DO NOT EDIT
import {model} from '../models';
export function CreateStage(arg1:number):Promise<void>;
export function DeleteParticipat(arg1:model.Participant):Promise<void>;
export function ExportTournament(arg1:model.Tournament):Promise<void>;
@ -20,6 +22,8 @@ export function GetTournaments():Promise<Array<model.Tournament>>;
export function RemoveParticipantFromTournament(arg1:model.Participant,arg2:model.Tournament):Promise<void>;
export function SaveMatch(arg1:model.Match):Promise<void>;
export function SaveParticipant(arg1:model.Participant):Promise<void>;
export function SaveTournament(arg1:model.Tournament):Promise<void>;

View File

@ -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);
}

View File

@ -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 {

View File

@ -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"`
}

Binary file not shown.