diff --git a/.gitignore b/.gitignore index c7042f8..7ba9571 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ WoWChatLog.txt -.idea \ No newline at end of file +.idea +config.toml \ No newline at end of file diff --git a/go.mod b/go.mod index 0605659..6108f66 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module gamboupload go 1.24 + +require ( + github.com/BurntSushi/toml v1.4.0 // indirect + github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf // indirect + github.com/sqweek/dialog v0.0.0-20240226140203-065105509627 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..78a2ae8 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf h1:FPsprx82rdrX2jiKyS17BH6IrTmUBYqZa/CXT4uvb+I= +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I= +github.com/sqweek/dialog v0.0.0-20240226140203-065105509627 h1:2JL2wmHXWIAxDofCK+AdkFi1KEg3dgkefCsm7isADzQ= +github.com/sqweek/dialog v0.0.0-20240226140203-065105509627/go.mod h1:/qNPSY91qTz/8TgHEMioAUc6q7+3SOybeKczHMXFcXw= diff --git a/main.go b/main.go index 35f3417..9665b46 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,52 @@ package main import ( "bytes" "encoding/json" + "errors" "fmt" + "github.com/BurntSushi/toml" + "github.com/sqweek/dialog" "log" + "net/http" "os" "strings" ) +type Config struct { + Log string + Apikey string +} + +var config Config + func main() { - b, err := os.ReadFile("WoWChatLog.txt") + if _, statErr := os.Stat("config.toml"); os.IsNotExist(statErr) { + path, err := dialog.File().Title("Select your WoWChatLog.txt").Load() + if err != nil { + if errors.Is(err, dialog.ErrCancelled) { + log.Fatalf("Cancelled dialog box, exiting") + } + } + + newConfig := &Config{ + Log: path, + Apikey: "12345", + } + + file, err := os.Create("config.toml") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + encoder := toml.NewEncoder(file) + if err := encoder.Encode(newConfig); err != nil { + log.Fatal(err) + } + } + + _, err := toml.DecodeFile("config.toml", &config) + + b, err := os.ReadFile(config.Log) if err != nil { log.Fatal(err) } @@ -26,10 +64,36 @@ func main() { } for _, game := range games { - j, err := json.Marshal(game) + err = upload(game, "https://forcek.in/game") if err != nil { log.Fatal(err) } - fmt.Println(string(j)) } } + +func upload(game Game, endpoint string) error { + marshalled, err := json.Marshal(game) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(marshalled)) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-API-KEY", config.Apikey) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + fmt.Println(resp.Status) + return nil +} + +func uploadTest(game Game) error { + return upload(game, "http://localhost:3000/game") +} diff --git a/parser.go b/parser.go index ceae200..8e1b284 100644 --- a/parser.go +++ b/parser.go @@ -22,6 +22,7 @@ type Game struct { HighRoll int `json:"high_roll"` LowRoll int `json:"low_roll"` Payout int `json:"payout"` + Players []string `json:"players"` } var ( @@ -139,5 +140,10 @@ func parse(lines []string, i int) (Game, int, error) { g.Loser = endMatches[1] g.LowRoll = rolls[g.Loser] + g.Players = make([]string, 0) + for pl, _ := range rolls { + g.Players = append(g.Players, pl) + } + return g, i, nil }