Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 149 additions & 27 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package main

import (
"context"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/ncruces/zenity"
rlbot "github.com/swz-git/go-interface"
"github.com/swz-git/go-interface/flat"
)

type RawReleaseInfo struct {
repo string
content GhRelease
}

// App struct
type App struct {
ctx context.Context
latest_release_json []RawReleaseInfo
rlbot_address string
}

func (a *App) IgnoreMe(
Expand All @@ -22,37 +32,119 @@ func (a *App) IgnoreMe(
) {
}

// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
func (a *App) GetDefaultPath() string {
if runtime.GOOS == "windows" {
localappdata := os.Getenv("LOCALAPPDATA")
return filepath.Join(localappdata, "RLBotGUI")
}

home := os.Getenv("HOME")
return filepath.Join(home, ".rlbotgui")
}

// 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) GetLatestReleaseData(repo string) (*GhRelease, error) {
latest_release_url := "https://api.github.com/repos/" + repo + "/releases/latest"

resp, err := http.Get(latest_release_url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

content, err := ParseReleaseData(body)
if err != nil {
return nil, err
}

a.latest_release_json = append(a.latest_release_json, RawReleaseInfo{repo, content})

return &a.latest_release_json[len(a.latest_release_json)-1].content, nil
}

// // Greet returns a greeting for the given name
// func (a *App) Greet(name string) string {
// return fmt.Sprintf("Hello %s, It's show time!", name)
// }
func (a *App) DownloadBotpack(repo string, installPath string) (string, error) {
var latest_release *GhRelease

for _, release := range a.latest_release_json {
if release.repo == repo {
latest_release = &release.content
break
}
}

if latest_release == nil {
content, err := a.GetLatestReleaseData(repo)
if err != nil {
return "", err
}

latest_release = content
}

var file_name string
if runtime.GOOS == "windows" {
file_name = "botpack_x86_64-windows.tar.xz"
} else {
file_name = "botpack_x86_64-linux.tar.xz"
}

var download_url string
for _, asset := range latest_release.Assets {
if asset.Name == file_name {
download_url = asset.BrowserDownloadURL
break
}
}

err := DownloadExtractArchive(download_url, installPath)
if err != nil {
return "", err
}

return latest_release.TagName, nil
}

func recursiveFileSearch(root, pattern string) ([]string, error) {
// NewApp creates a new App application struct
func NewApp() *App {
ip := os.Getenv("RLBOT_SERVER_IP")
if ip == "" {
ip = "127.0.0.1"
}

port := os.Getenv("RLBOT_SERVER_PORT")
if port == "" {
port = "23234"
}

rlbot_address := ip + ":" + port

var latest_release_json []RawReleaseInfo

return &App{
latest_release_json,
rlbot_address,
}
}

func recursiveTomlSearch(root, tomlType string) ([]string, error) {
var matches []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && (info.Name() == "bot.toml" || filepath.Ext(info.Name()) == ".bot.toml") {
matched, err := filepath.Match(pattern, info.Name())
if err != nil {
return err
}
if matched {
matches = append(matches, path)
}

if info.IsDir() || filepath.Ext(info.Name()) != ".toml" {
return nil
}

if info.Name() == tomlType+".toml" || strings.HasSuffix(info.Name(), "."+tomlType+".toml") {
matches = append(matches, path)
}

return nil
})
return matches, err
Expand Down Expand Up @@ -85,10 +177,9 @@ type StartMatchOptions struct {

func (a *App) StartMatch(options StartMatchOptions) Result {
// TODO: Save this in App struct
// TODO: Make dynamic, pull from env var?
conn, err := rlbot.Connect("127.0.0.1:23234")
conn, err := rlbot.Connect(a.rlbot_address)
if err != nil {
return Result{false, "Failed to connect to rlbot"}
return Result{false, "Failed to connect to RLBotServer at " + a.rlbot_address}
}

var gameMode flat.GameMode
Expand All @@ -107,6 +198,8 @@ func (a *App) StartMatch(options StartMatchOptions) Result {
gameMode = flat.GameModeHeatseeker
case "Gridiron":
gameMode = flat.GameModeGridiron
case "Knockout":
gameMode = flat.GameModeKnockout
default:
println("No mode chosen, defaulting to soccer")
gameMode = flat.GameModeSoccer
Expand Down Expand Up @@ -137,8 +230,6 @@ func (a *App) StartMatch(options StartMatchOptions) Result {
playerConfigs = append(playerConfigs, playerInfo.ToPlayer().ToPlayerConfig(1))
}

println(playerConfigs)

conn.SendPacket(&flat.MatchConfigurationT{
AutoStartBots: true,
GameMapUpk: options.Map,
Expand Down Expand Up @@ -180,3 +271,34 @@ func (a *App) PickFolder() string {
}
return path
}

func (a *App) ShowPathInExplorer(path string) error {
fileInfo, err := os.Stat(path)
if err != nil {
return err
}

// if is dir
var folder string
if fileInfo.IsDir() {
folder = path
} else {
folder = filepath.Dir(path)
}

if runtime.GOOS == "windows" {
cmd := exec.Command("explorer", folder)
err := cmd.Run()
if err != nil {
return err
}
} else {
cmd := exec.Command("xdg-open", folder)
err := cmd.Run()
if err != nil {
return err
}
}

return nil
}
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"@rsbuild/core": "1.0.5",
"@rsbuild/plugin-svelte": "1.0.5",
"oxlint": "^0.13.2",
"typescript": "^5.7.3"
"typescript": "^5.8.2"
}
}
20 changes: 10 additions & 10 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading