-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
215 lines (191 loc) · 3.99 KB
/
model.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
package main
import (
"gob"
"strings"
"regexp"
)
type LunchPoll struct {
Places PlaceVector
IndexCounter int
Votes map[string]*Place
}
var placeRegex = regexp.MustCompile("[^A-Z0-9]")
func init() {
RegisterTypes()
}
func NewPoll() LunchPoll {
poll := LunchPoll{
Places: make(PlaceVector, 0),
Votes: make(map[string]*Place),
IndexCounter: 1}
defaultPlace := &Place{
Id: 0,
Name: "No Where",
Votes: 0,
People: make(PersonVector, 0),
Nominator: nil}
poll.Places.Push(defaultPlace)
return poll
}
func (p *LunchPoll) addPlace(name, nominator string) (count int, success bool) {
count = p.IndexCounter
if name == "" || p.placeExists(name) {
return
}
person := p.getPerson(nominator)
if person.NominationsLeft > 0 {
place := &Place{
Id: p.IndexCounter,
Nominator: person,
Name: name,
People: make(PersonVector, 0)}
p.Places.Push(place)
p.IndexCounter++
person.NominationsLeft--
success = true
}
return
}
func (p *LunchPoll) delPlace(placeId int, person string) bool {
if placeId < 1 {
return false
}
place, ok := p.getPlace(placeId)
if ok {
if place.Votes == 0 && place.Nominator.Name == person {
if p.remove(place) {
place.Nominator.NominationsLeft++
return true
}
}
}
return false
}
func (p *LunchPoll) drive(who string, seats int) bool {
person := p.getPerson(who)
person.CanDrive = true
person.NumSeats = seats
return true
}
func (p *LunchPoll) unDrive(who string) bool {
person := p.getPerson(who)
person.CanDrive = false
person.NumSeats = 0
return true
}
func (p *LunchPoll) vote(who string, vote int) bool {
if vote < 1 {
return false
}
place, ok := p.getPlace(vote)
if ok {
person := p.getPerson(who)
if p.Votes[who] == nil {
p.goingSomewhere(who)
p.Votes[who] = place
place.Votes++
place.People.Push(person)
return true
}
}
return false
}
func (p *LunchPoll) unVote(who string) bool {
if place, voted := p.Votes[who]; voted {
place.Votes--
p.Votes[who] = nil, false
person := place.RemovePerson(who)
if person.Name != "" {
p.Places.At(0).People.Push(person)
return true
}
}
return false
}
func (p *LunchPoll) comment(comment, user string) bool {
person := p.getPerson(user)
person.Comment = comment
return true
}
// Helpers
func (p *LunchPoll) getPerson(name string) *Person {
for _, place := range p.Places {
if place == nil {
continue
}
for _, peep := range place.People {
if peep.Name == name {
return peep
}
}
}
person := &Person{
CanDrive: false,
Name: name,
NominationsLeft: 2}
p.Places.At(0).People.Push(person)
return person
}
func (p *LunchPoll) getPlace(dest int) (*Place, bool) {
for _, pl := range p.Places {
if pl.Id == dest {
return pl, true
}
}
return nil, false
}
func (p *LunchPoll) goingSomewhere(name string) {
for i, person := range p.Places.At(0).People {
if person.Name == name {
p.Places.At(0).People.Delete(i)
}
}
}
func (p *LunchPoll) remove(sp *Place) bool {
for i, place := range p.Places {
if place == nil {
return false
}
if place.Id == sp.Id {
p.Places.Delete(i)
return true
}
}
return false
}
func sanitizePlace(name string) string {
return placeRegex.ReplaceAllString(strings.ToUpper(name), "")
}
func (p *LunchPoll) placeExists(name string) bool {
check := sanitizePlace(name)
for _, place := range p.Places {
if check == sanitizePlace(place.Name) {
return true
}
}
return false
}
func RegisterTypes() {
gob.Register(make(PlaceVector, 0))
gob.Register(make(PersonVector, 0))
gob.Register(
LunchPoll{
Places: make(PlaceVector, 0),
Votes: make(map[string]*Place),
IndexCounter: 1})
gob.Register(
Place{
Id: 0,
Name: "No Where",
Votes: 0,
People: make(PersonVector, 0),
Nominator: &Person{
CanDrive: false,
Name: "",
NominationsLeft: 2}})
gob.Register(
Person{
CanDrive: false,
Name: "",
NominationsLeft: 2})
}