add option to only parse last raid or full log

This commit is contained in:
Evan Burkey 2025-04-26 20:35:56 -07:00
parent fd18343b84
commit dd00e06a27
2 changed files with 60 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
"net/http"
"os"
"strings"
"time"
)
func gambo() {
@ -21,7 +22,29 @@ func gambo() {
lines := strings.Split(string(b), "\n")
games, err := parseGames(lines)
fmt.Println("Parse entire log or just the last raid's entries?")
fmt.Println("1) Last raid")
fmt.Println("2) Full log")
var (
start int
choice int
)
_, err = fmt.Scanf("%d", &choice)
if err != nil {
log.Fatal(err)
}
switch choice {
case 1:
start = findStart(lines)
case 2:
start = 0
default:
log.Fatalf("%d is not a valid choice, retard\n", choice)
}
games, err := parseGames(lines, start)
if err != nil {
log.Fatal(err)
}
@ -35,6 +58,27 @@ func gambo() {
}
}
func findStart(lines []string) int {
now := time.Now()
t := time.Date(now.Year(), now.Month(), now.Day(), 15, 45, 0, 0, time.UTC)
for t.Weekday() != time.Sunday {
t = t.AddDate(0, 0, -1)
}
// skip ending newline
var i = len(lines) - 2
for ; i >= 0; i-- {
ts, err := timestamp(lines[i])
if err != nil {
log.Fatalf("Failed to rewind log: %v\n", err)
}
if ts.Before(t) {
break
}
}
return i + 1
}
func uploadGambo(game Game, endpoint string) error {
marshalled, err := json.Marshal(game)
if err != nil {

View File

@ -34,11 +34,11 @@ var (
reEnd = regexp.MustCompile(`([\p{L}']+) owes ([\p{L}']+) ([\d,]+) gold!`)
)
func parseGames(lines []string) ([]Game, error) {
func parseGames(lines []string, start int) ([]Game, error) {
games := make([]Game, 0)
var err error
i := 0
i := start
for i < len(lines) {
if reGameStart.MatchString(lines[i]) {
var game Game
@ -54,19 +54,25 @@ func parseGames(lines []string) ([]Game, error) {
return games, nil
}
func timestamp(line string) (time.Time, error) {
ts := reTimeStamp.FindString(line)
if ts == "" {
return time.Now(), fmt.Errorf("failed to extract timestamp from %s", line)
}
stamp, err := time.Parse("1/2 15:04:05.000", ts)
if err != nil {
return time.Now(), fmt.Errorf("failed to extract timestamp from %s", line)
}
return time.Date(time.Now().Year(), stamp.Month(), stamp.Day(), stamp.Hour(), stamp.Minute(), stamp.Second(), 0, time.UTC), nil
}
func parse(lines []string, i int) (Game, int, error) {
var (
g Game
err error
)
// Timestamp
ts := reTimeStamp.FindString(lines[i])
if ts == "" {
return g, i, fmt.Errorf("failed to extract timestamp from %s", lines[i])
}
g.Timestamp, err = time.Parse("1/2 15:04:05.000", ts)
g.Timestamp = time.Date(time.Now().Year(), g.Timestamp.Month(), g.Timestamp.Day(), g.Timestamp.Hour(), g.Timestamp.Minute(), g.Timestamp.Second(), 0, time.UTC)
g.Timestamp, err = timestamp(lines[i])
if err != nil {
return g, i, err
}