-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.go
More file actions
141 lines (123 loc) · 3.73 KB
/
Copy pathevents.go
File metadata and controls
141 lines (123 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// events.go - Kill Event Participation
//
// Fetches active kill events from the server and lets the user select
// which event to participate in. The selected event ID is sent along
// with SaveKills requests so kills count towards the event leaderboard.
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
"sync"
)
// KillEvent represents an active kill event from the server.
type KillEvent struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
Status string `json:"status"`
ScoringMetric string `json:"scoringMetric"`
GameModeFilter *string `json:"gameModeFilter"`
Prize string `json:"prize"`
ImageURL *string `json:"imageUrl"`
ParticipantCount int `json:"participantCount"`
}
// KillEventsResponse is the server response for GET /api/kill-events.
type KillEventsResponse struct {
Events []KillEvent `json:"events"`
}
var (
activeEvents []KillEvent
selectedEventID string // Empty = no event selected
eventsMutex sync.RWMutex
)
// FetchActiveEvents queries the server for currently active kill events.
func FetchActiveEvents() ([]KillEvent, error) {
url := strings.Replace(APIURL, "/api/ocr", "/api/kill-events?status=active", 1)
resp, err := apiClient.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch events: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("events API returned %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 10*1024*1024))
if err != nil {
return nil, fmt.Errorf("failed to read events response: %v", err)
}
var eventsResp KillEventsResponse
if err := json.Unmarshal(body, &eventsResp); err != nil {
return nil, fmt.Errorf("failed to parse events: %v", err)
}
return eventsResp.Events, nil
}
// RefreshEvents fetches active events and updates the global list.
func RefreshEvents() {
events, err := FetchActiveEvents()
if err != nil {
debugLog("[EVENTS] Failed to fetch: %v\n", err)
return
}
eventsMutex.Lock()
activeEvents = events
// If selected event is no longer active, clear selection
if selectedEventID != "" && !isEventActive(selectedEventID) {
selectedEventID = ""
}
eventsMutex.Unlock()
debugLog("[EVENTS] Loaded %d active events\n", len(events))
}
// GetActiveEvents returns a copy of the current active events list.
func GetActiveEvents() []KillEvent {
eventsMutex.RLock()
defer eventsMutex.RUnlock()
result := make([]KillEvent, len(activeEvents))
copy(result, activeEvents)
return result
}
// GetSelectedEventID returns the currently selected event ID (empty = none).
func GetSelectedEventID() string {
eventsMutex.RLock()
defer eventsMutex.RUnlock()
return selectedEventID
}
// SetSelectedEventID sets the active event for kill tracking.
func SetSelectedEventID(id string) {
eventsMutex.Lock()
selectedEventID = id
eventsMutex.Unlock()
if id == "" {
debugLn("[EVENTS] Event deselected")
} else {
debugLog("[EVENTS] Selected event: %s\n", id)
}
}
// isEventActive checks if an event ID is in the active list. Must hold eventsMutex.
func isEventActive(id string) bool {
for _, e := range activeEvents {
if e.ID == id {
return true
}
}
return false
}
// IsEventError checks if a save error is event-related and returns true if
// the kills should be retried without event.
func IsEventError(errMsg string) bool {
eventErrors := []string{
"Kill Event nicht gefunden",
"Kill Event ist nicht aktiv",
"Kill Event ist außerhalb des Zeitraums",
}
for _, e := range eventErrors {
if strings.Contains(errMsg, e) {
return true
}
}
return false
}