-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
248 lines (200 loc) · 5.1 KB
/
entity.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"time"
)
type MatchMakerStatus string
const (
MatchMakerStatusPending MatchMakerStatus = "pending"
MatchMakerStatusRunning MatchMakerStatus = "running"
MatchMakerStatusFinished MatchMakerStatus = "finished"
MatchMakerStatusStopped MatchMakerStatus = "stopped"
)
type MatchMakerUserStatus string
const (
MatchMakerUserStatusPending MatchMakerUserStatus = "pending"
MatchMakerUserStatusRunning MatchMakerUserStatus = "running"
MatchMakerUserStatusFinished MatchMakerUserStatus = "finished"
MatchMakerUserStatusStopped MatchMakerUserStatus = "stopped"
)
type Person struct {
Name string
}
type People []*Person
func (p People) ToUserReferences() []string {
var userReferences []string
for _, person := range p {
if person == nil {
continue
}
userReferences = append(userReferences, person.Name)
}
return userReferences
}
func (p People) Get(i int) *Person {
if i < 0 || i >= len(p) {
return nil
}
return p[i]
}
func (p People) Print() string {
var s string
for i, person := range p {
if i > 0 {
s += ", "
}
s += person.Name
}
return s
}
type MatchMakerUserSerial string
func (m MatchMakerUserSerial) String() string {
return string(m)
}
type MatchMap map[MatchMakerUserSerial]People
func (m MatchMap) First() (MatchMakerUserSerial, People) {
for serial, match := range m {
return serial, match
}
return "", nil
}
type MatchMakerEntity struct {
Serial string
Name string
Description string
Status MatchMakerStatus
StartTime time.Time
Duration time.Duration
}
type MatchMakerEntityOption func(*MatchMakerEntity)
func WithMatchMakerEntityName(name string) MatchMakerEntityOption {
return func(m *MatchMakerEntity) {
m.Name = name
}
}
func WithMatchMakerEntityDescription(description string) MatchMakerEntityOption {
return func(m *MatchMakerEntity) {
m.Description = description
}
}
func WithMatchMakerEntityStartTime(startTime time.Time) MatchMakerEntityOption {
return func(m *MatchMakerEntity) {
m.StartTime = startTime
}
}
func WithMatchMakerEntityDuration(duration time.Duration) MatchMakerEntityOption {
return func(m *MatchMakerEntity) {
m.Duration = duration
}
}
func (m *MatchMakerEntity) Build(options ...MatchMakerEntityOption) *MatchMakerEntity {
m.Serial = GenerateSerial()
m.Status = MatchMakerStatusPending
for _, opt := range options {
opt(m)
}
if m.StartTime.IsZero() {
m.StartTime = time.Now()
}
if m.Duration == 0 {
m.Duration = 24 * time.Hour
}
if m.Name == "" {
m.Name = fmt.Sprintf("MatchMaker-%s", m.Serial)
}
return m
}
func (m *MatchMakerEntity) Error() error {
if m.Serial == "" {
return fmt.Errorf("serial is empty")
}
if m.Name == "" {
return fmt.Errorf("name is empty")
}
if m.StartTime.IsZero() {
return fmt.Errorf("start time is zero")
}
if m.Duration == 0 {
return fmt.Errorf("duration is zero")
}
return nil
}
type MatchMakerUserEntity struct {
MatchMakerSerial string
Serial string
UserReference string
Status MatchMakerUserStatus
}
type MatchMakerUserEntityOption func(*MatchMakerUserEntity)
func WithMatchMakerUserEntityStatus(status MatchMakerUserStatus) MatchMakerUserEntityOption {
return func(m *MatchMakerUserEntity) {
m.Status = status
}
}
func WithMatchMakerUserEntityUserReference(userReference string) MatchMakerUserEntityOption {
return func(m *MatchMakerUserEntity) {
m.UserReference = userReference
}
}
func WithMatchMakerUserEntityMatchMakerSerial(matchMakerSerial string) MatchMakerUserEntityOption {
return func(m *MatchMakerUserEntity) {
m.MatchMakerSerial = matchMakerSerial
}
}
func WithMatchMakerUserEntitySerial(serial string) MatchMakerUserEntityOption {
return func(m *MatchMakerUserEntity) {
m.Serial = serial
}
}
func (m *MatchMakerUserEntity) Build(options ...MatchMakerUserEntityOption) *MatchMakerUserEntity {
for _, opt := range options {
opt(m)
}
if m.Status == "" {
m.Status = MatchMakerUserStatusPending
}
return m
}
func (m *MatchMakerUserEntity) Error() error {
if m.MatchMakerSerial == "" {
return fmt.Errorf("match maker serial is empty")
}
if m.UserReference == "" {
return fmt.Errorf("user reference is empty")
}
if m.Serial == "" {
return fmt.Errorf("serial is empty")
}
return nil
}
type MatchMakerUserEntities []*MatchMakerUserEntity
func (m MatchMakerUserEntities) ToPeople() People {
var people People
for _, matchMakerUser := range m {
if matchMakerUser == nil {
continue
}
people = append(people, &Person{Name: matchMakerUser.UserReference})
}
return people
}
func (m MatchMakerUserEntities) ToMatchMap() MatchMap {
matchMap := make(MatchMap)
for _, matchMakerUser := range m {
if matchMakerUser == nil {
continue
}
match, ok := matchMap[MatchMakerUserSerial(matchMakerUser.Serial)]
if !ok {
matchMap[MatchMakerUserSerial(matchMakerUser.Serial)] = make(People, 0)
}
match = append(match, &Person{Name: matchMakerUser.UserReference})
matchMap[MatchMakerUserSerial(matchMakerUser.Serial)] = match
}
return matchMap
}
type MatchMakerInformation struct {
MatchMaker *MatchMakerEntity
Users MatchMakerUserEntities
Pairs MatchMap
}