From 9ff452d862f834a856bc5c7c9139728cc5283972 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Wed, 12 Mar 2025 12:13:01 -0700 Subject: [PATCH] loot --- .gitignore | 3 +- gambo.go | 63 ++++++++++++++++++++++++++++ loot.go | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 71 +++++++++++--------------------- 4 files changed, 206 insertions(+), 49 deletions(-) create mode 100644 gambo.go create mode 100644 loot.go diff --git a/.gitignore b/.gitignore index 7ba9571..b2b4cad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ WoWChatLog.txt .idea -config.toml \ No newline at end of file +config.toml +testloot.csv \ No newline at end of file diff --git a/gambo.go b/gambo.go new file mode 100644 index 0000000..99b1106 --- /dev/null +++ b/gambo.go @@ -0,0 +1,63 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "strings" +) + +func gambo() { + b, err := os.ReadFile(config.Log) + if err != nil { + log.Fatal(err) + } + + // Strip carriage returns because Windows is retarded + b = bytes.ReplaceAll(b, []byte("\r"), []byte("")) + + lines := strings.Split(string(b), "\n") + + games, err := parseGames(lines) + if err != nil { + log.Fatal(err) + } + + for _, game := range games { + //err = uploadGambo(game, "https://forcek.in/game") + err = uploadGamboTest(game) + if err != nil { + log.Fatal(err) + } + } +} + +func uploadGambo(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 uploadGamboTest(game Game) error { + return uploadGambo(game, "http://localhost:3000/game") +} diff --git a/loot.go b/loot.go new file mode 100644 index 0000000..8049d30 --- /dev/null +++ b/loot.go @@ -0,0 +1,118 @@ +package main + +import ( + "bytes" + "encoding/csv" + "encoding/json" + "errors" + "fmt" + "github.com/sqweek/dialog" + "log" + "net/http" + "os" + "strconv" + "strings" + "time" +) + +type LootRecord struct { + Name string `json:"player"` + ItemID int `json:"item_id"` + ItemName string `json:"item_name"` + RollType string `json:"roll_type"` + Timestamp time.Time `json:"timestamp"` +} + +func loot() { + csvPath, err := dialog.File().Title("Select the csv file").Load() + if err != nil { + if errors.Is(err, dialog.ErrCancelled) { + log.Fatalf("Cancelled dialog box, exiting") + } else { + log.Fatal(err) + } + } + + csvFile, err := os.Open(csvPath) + if err != nil { + log.Fatal(err) + } + defer csvFile.Close() + + csvReader := csv.NewReader(csvFile) + records, err := csvReader.ReadAll() + if err != nil { + log.Fatalf("Failed to parse csv: %v\n", err) + } + + loots := make([]LootRecord, 0) + for i := 1; i < len(records); i++ { + if strings.Contains(records[i][7], "Personal Loot") { + continue + } + + var l = LootRecord{ + Name: strings.Split(records[i][0], "-")[0], + ItemName: strings.Trim(records[i][4], "[]"), + } + l.ItemID, err = strconv.Atoi(records[i][5]) + if err != nil { + log.Fatal(err) + } + l.Timestamp, err = time.Parse("2006/1/2 15:04:05", fmt.Sprintf("%s %s", records[i][1], records[i][2])) + if err != nil { + log.Fatal(err) + } + + switch records[i][7] { + case "Mainspec/Need": + l.RollType = "mainspec" + case "Offspec": + l.RollType = "offspec" + case "Minor Upgrade": + l.RollType = "minor" + case "Transmog": + l.RollType = "transmog" + default: + l.RollType = "unknown" + } + + loots = append(loots, l) + } + + for _, l := range loots { + err = uploadLoot(l, "https://forcek.in/loot") + //err = uploadLootTest(l) + if err != nil { + log.Fatal(err) + } + } +} + +func uploadLoot(l LootRecord, endpoint string) error { + marshalled, err := json.Marshal(l) + if err != nil { + return err + } + fmt.Println(string(marshalled)) + + 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 uploadLootTest(l LootRecord) error { + return uploadLoot(l, "http://localhost:3000/loot") +} diff --git a/main.go b/main.go index 9665b46..b961efa 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,12 @@ package main import ( - "bytes" - "encoding/json" "errors" "fmt" "github.com/BurntSushi/toml" "github.com/sqweek/dialog" "log" - "net/http" "os" - "strings" ) type Config struct { @@ -20,13 +16,15 @@ type Config struct { var config Config -func main() { +func setupConfig() { 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") } + } else { + log.Fatal(err) } newConfig := &Config{ @@ -47,53 +45,30 @@ func main() { } _, err := toml.DecodeFile("config.toml", &config) + if err != nil { + log.Fatal(err) + } +} - b, err := os.ReadFile(config.Log) +func main() { + setupConfig() + + fmt.Println("Select an option (1 or 2):") + fmt.Println("1) Upload a chat log for gambo tracking") + fmt.Println("2) Upload an RCLootCouncil export in csv format") + + var choice int + _, err := fmt.Scanf("%d", &choice) if err != nil { log.Fatal(err) } - // Strip carriage returns because Windows is retarded - b = bytes.ReplaceAll(b, []byte("\r"), []byte("")) - - lines := strings.Split(string(b), "\n") - - games, err := parseGames(lines) - if err != nil { - log.Fatal(err) - } - - for _, game := range games { - err = upload(game, "https://forcek.in/game") - if err != nil { - log.Fatal(err) - } + switch choice { + case 1: + gambo() + case 2: + loot() + default: + log.Fatalf("%d is not a valid choice, retard\n", choice) } } - -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") -}