-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
220 lines (171 loc) · 4.39 KB
/
data.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
/*
Copyright (C) 2023 Holiday Paintboard Authors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"image"
"image/color"
"sync"
"sync/atomic"
)
type TokenStatistic struct {
Lock sync.Mutex
NextOperationTime Tick
}
func (stat *TokenStatistic) Init() {
stat.NextOperationTime = Now()
}
type Color struct {
R, G, B, A uint8
}
func (c Color) RGBA() *color.RGBA {
return &color.RGBA{R: c.R, G: c.G, B: c.B, A: c.A}
}
type Point image.Point
func (d1 Point) Greater(d Point) bool {
if d1.X > d.X || d1.Y > d.Y {
return true
}
return false
}
type Zone struct {
closed bool
Size, Range Point
Map [][]Color
Title string
TokenStats map[Token]*TokenStatistic
TokenStatsLock sync.RWMutex
PrivilegedToken Token
ReqChan chan DrawRequest
//IllegalOpChan chan Token
//BroadcastChan chan string
Freeze Tick
}
var (
zones []*Zone
zonesLock sync.RWMutex
)
func LookupZone(zoneId int) *Zone {
zonesLock.RLock()
defer zonesLock.RUnlock()
if zoneId < len(zones) || zoneId >= 0 {
return zones[zoneId]
}
return nil
}
func (z *Zone) AddToken(token Token) {
stat := new(TokenStatistic)
stat.Init()
z.TokenStatsLock.Lock()
defer z.TokenStatsLock.Unlock()
z.TokenStats[token] = stat
}
func (z *Zone) DeleteToken(token Token) {
z.TokenStatsLock.Lock()
defer z.TokenStatsLock.Unlock()
delete(z.TokenStats, token)
}
func (z *Zone) UpdatePrivilegedToken(t Token) {
z.TokenStatsLock.Lock()
defer z.TokenStatsLock.Unlock()
z.PrivilegedToken = t
}
func (z *Zone) Init(size Point) error {
if size.X <= 0 || size.Y <= 0 {
return errors.New("zone size cannot be zero")
}
z.Map = make([][]Color, size.X)
for i := range z.Map {
z.Map[i] = make([]Color, size.Y)
}
z.Size = size
z.Range.X = size.X - 1
z.Range.Y = size.Y - 1
z.closed = false
z.TokenStats = make(map[Token]*TokenStatistic, 10)
//z.IllegalOpChan = make(chan Token, 10)
z.ReqChan = make(chan DrawRequest, 1000)
return nil
}
func (z *Zone) Close() {
z.closed = true
//close(z.IllegalOpChan)
close(z.ReqChan)
}
func (z *Zone) GenImage() *image.RGBA {
img := image.NewRGBA(image.Rectangle{Min: image.Point{}, Max: image.Point{X: z.Size.X, Y: z.Size.Y}})
for x := 0; x <= z.Range.X; x++ {
for y := 0; y <= z.Range.Y; y++ {
img.Set(x, y, z.Map[x][y].RGBA())
}
}
return img
}
func (z *Zone) LoadImage(img image.Image) error {
if img.ColorModel() != color.RGBAModel {
return errors.New("RGBA required")
}
size := Point(img.Bounds().Size())
if size.Greater(z.Size) {
return errors.New("too large")
}
for x := 0; x <= z.Range.X; x++ {
for y := 0; y <= z.Range.Y; y++ {
r, g, b, a := img.At(x, y).RGBA()
if a == 0 {
continue
}
z.Map[x][y] = Color{uint8(r), uint8(g), uint8(b), uint8(a)}
}
}
return nil
}
type IPStatistic struct {
Lock sync.RWMutex
Challenges int32
}
func (s *IPStatistic) Init() {
s.Challenges = maxChallenges
}
func (s *IPStatistic) GetChallenges() int32 {
return atomic.LoadInt32(&s.Challenges)
}
func (s *IPStatistic) DecreaseChallenges() {
atomic.AddInt32(&s.Challenges, -1)
}
var (
ipStatistics map[string]*IPStatistic
ipStatisticsLock sync.RWMutex
)
func lookupIPStatistic(remoteAddr string) (*IPStatistic, bool) {
ipStatisticsLock.RLock()
stat, ok := ipStatistics[remoteAddr]
ipStatisticsLock.RUnlock()
return stat, ok
}
func addIPStatistic(remoteAddr string) *IPStatistic {
stat := new(IPStatistic) // No competition.
stat.Init()
ipStatisticsLock.Lock()
ipStatistics[remoteAddr] = stat // Pointer modification competition may cause lacks of allocation, but GC will solve it.
ipStatisticsLock.Unlock()
return stat
}
func recordIPStatistic(remoteAddr string) *IPStatistic {
stat, ok := lookupIPStatistic(remoteAddr)
if ok {
return stat
}
return addIPStatistic(remoteAddr)
}