This commit is contained in:
Evan Burkey 2025-03-12 12:13:01 -07:00
parent 999b4f884b
commit 9ff452d862
4 changed files with 206 additions and 49 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
WoWChatLog.txt WoWChatLog.txt
.idea .idea
config.toml config.toml
testloot.csv

63
gambo.go Normal file
View File

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

118
loot.go Normal file
View File

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

71
main.go
View File

@ -1,16 +1,12 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/sqweek/dialog" "github.com/sqweek/dialog"
"log" "log"
"net/http"
"os" "os"
"strings"
) )
type Config struct { type Config struct {
@ -20,13 +16,15 @@ type Config struct {
var config Config var config Config
func main() { func setupConfig() {
if _, statErr := os.Stat("config.toml"); os.IsNotExist(statErr) { if _, statErr := os.Stat("config.toml"); os.IsNotExist(statErr) {
path, err := dialog.File().Title("Select your WoWChatLog.txt").Load() path, err := dialog.File().Title("Select your WoWChatLog.txt").Load()
if err != nil { if err != nil {
if errors.Is(err, dialog.ErrCancelled) { if errors.Is(err, dialog.ErrCancelled) {
log.Fatalf("Cancelled dialog box, exiting") log.Fatalf("Cancelled dialog box, exiting")
} }
} else {
log.Fatal(err)
} }
newConfig := &Config{ newConfig := &Config{
@ -47,53 +45,30 @@ func main() {
} }
_, err := toml.DecodeFile("config.toml", &config) _, 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Strip carriage returns because Windows is retarded switch choice {
b = bytes.ReplaceAll(b, []byte("\r"), []byte("")) case 1:
gambo()
lines := strings.Split(string(b), "\n") case 2:
loot()
games, err := parseGames(lines) default:
if err != nil { log.Fatalf("%d is not a valid choice, retard\n", choice)
log.Fatal(err)
}
for _, game := range games {
err = upload(game, "https://forcek.in/game")
if err != nil {
log.Fatal(err)
}
} }
} }
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")
}