-
Notifications
You must be signed in to change notification settings - Fork 0
/
raid.go
150 lines (127 loc) · 3.96 KB
/
raid.go
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
142
143
144
145
146
147
148
149
150
package wizlib
import (
"fmt"
"strconv"
"time"
)
// RaidMember represents a member of a raid.
type RaidMember struct {
RaidPosition string `json:"raid_position"`
Backup bool `json:"backup"`
}
// IsBackup checks if a raid member is a backup.
func (r *RaidMember) IsBackup() bool {
return r.Backup
}
// RaidRepository provides methods for accessing raid data.
type RaidRepository interface {
GetRaid(guildID string) (*Raid, error)
SaveRaid(raid *Raid) error
}
// RaidService provides methods for performing raid-related operations.
type RaidService struct {
repository RaidRepository
}
// NewRaidService creates a new instance of RaidService.
func NewRaidService(repository RaidRepository) *RaidService {
return &RaidService{
repository: repository,
}
}
// GetRaid retrieves a raid by guild ID.
func (s *RaidService) GetRaid(guildID string) (*Raid, error) {
return s.repository.GetRaid(guildID)
}
// SaveRaid saves a raid.
func (s *RaidService) SaveRaid(raid *Raid) error {
return s.repository.SaveRaid(raid)
}
// Raid represents a raid with multiple gates.
type Raid struct {
GuildID string `json:"guild_id"`
Gates []Gate `json:"gates"`
}
// GetGate retrieves a gate from the raid based on the date.
func (r *Raid) GetGate(date string) *Gate {
for i := range r.Gates {
if r.Gates[i].Date == date {
return &r.Gates[i]
}
}
return nil
}
// AddGate adds a new gate to the raid with the specified date if it doesn't already exist.
func (r *Raid) AddGate(date string) {
if r.GetGate(date) == nil {
r.Gates = append(r.Gates, Gate{Date: date, Status: 0xFFD700, Members: make(map[string]*RaidMember)})
}
}
// GetGate retrieves a specific gate from the raid based on the gate number.
func GetGate(raid *Raid, gateNum int) (*Gate, error) {
if gateNum < 1 || gateNum > 3 {
return nil, fmt.Errorf("invalid gate number: %d. Expected a number between 1 and 3", gateNum)
}
gateIndex := gateNum - 1
if len(raid.Gates) <= gateIndex {
return nil, fmt.Errorf("Gate %d not found in raid", gateNum)
}
return &raid.Gates[gateIndex], nil
}
// Gate represents a gate in a raid.
type Gate struct {
Status int64 `json:"status"`
Date string `json:"date"`
Members map[string]*RaidMember `json:"members"`
}
// GetMember retrieves a raid member from the gate based on the user ID.
func (g *Gate) GetMember(userID string) *RaidMember {
member, ok := g.Members[userID]
if !ok {
return nil
}
return member
}
// AddMember adds a new raid member to the gate.
func (g *Gate) AddMember(userID string, raidPosition string, backup bool) {
g.Members[userID] = &RaidMember{RaidPosition: raidPosition, Backup: backup}
}
// RemoveMember removes a raid member from the gate.
func (g *Gate) RemoveMember(userID string) {
delete(g.Members, userID)
}
// TimeFormatter provides methods for formatting time.
type TimeFormatter interface {
ParseTime(timeString string) (string, error)
}
// DefaultTimeFormatter is a default implementation of TimeFormatter.
type DefaultTimeFormatter struct {
layout string
}
// NewDefaultTimeFormatter creates a new instance of DefaultTimeFormatter.
func NewDefaultTimeFormatter(layout string) *DefaultTimeFormatter {
return &DefaultTimeFormatter{
layout: layout,
}
}
// ParseTime parses a time string into the desired format.
func (f *DefaultTimeFormatter) ParseTime(timeString string) (string, error) {
parsedTime, err := time.Parse(f.layout, timeString)
if err != nil {
return "", fmt.Errorf("invalid time format: %s", f.layout)
}
return strconv.FormatInt(parsedTime.Unix(), 10), nil
}
// TimeService provides methods for working with time.
type TimeService struct {
formatter TimeFormatter
}
// NewTimeService creates a new instance of TimeService.
func NewTimeService(formatter TimeFormatter) *TimeService {
return &TimeService{
formatter: formatter,
}
}
// ParseTime parses a time string into the desired format.
func (s *TimeService) ParseTime(timeString string) (string, error) {
return s.formatter.ParseTime(timeString)
}