-
Notifications
You must be signed in to change notification settings - Fork 22
/
godip.go
541 lines (450 loc) · 12.7 KB
/
godip.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package godip
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
"sync"
"time"
)
const (
Sea Flag = "Sea"
Land Flag = "Land"
Convoyable Flag = "Convoyable"
Army UnitType = "Army"
Fleet UnitType = "Fleet"
England Nation = "England"
France Nation = "France"
Germany Nation = "Germany"
Russia Nation = "Russia"
Austria Nation = "Austria"
Italy Nation = "Italy"
Turkey Nation = "Turkey"
Neutral Nation = "Neutral"
Spring Season = "Spring"
Fall Season = "Fall"
Movement PhaseType = "Movement"
Retreat PhaseType = "Retreat"
Adjustment PhaseType = "Adjustment"
Build OrderType = "Build"
Move OrderType = "Move"
MoveViaConvoy OrderType = "MoveViaConvoy"
Hold OrderType = "Hold"
Convoy OrderType = "Convoy"
Support OrderType = "Support"
Disband OrderType = "Disband"
ViaConvoy Flag = "ViaConvoy"
Anywhere Flag = "Anywhere"
AnyHomeCenter Flag = "AnyHomeCenter"
)
var (
Coast = []Flag{Sea, Land}
Archipelago = []Flag{Sea, Land, Convoyable}
// Invalid is not understood
// Illegal is understood but not allowed
ErrInvalidSource = fmt.Errorf("ErrInvalidSource")
ErrInvalidDestination = fmt.Errorf("ErrInvalidDestination")
ErrInvalidTarget = fmt.Errorf("ErrInvalidTarget")
ErrInvalidPhase = fmt.Errorf("ErrInvalidPhase")
ErrMissingUnit = fmt.Errorf("ErrMissingUnit")
ErrIllegalDestination = fmt.Errorf("ErrIllegalDestination")
ErrMissingConvoyPath = fmt.Errorf("ErrMissingConvoyPath")
ErrIllegalMove = fmt.Errorf("ErrIllegalMove")
ErrConvoyParadox = fmt.Errorf("ErrConvoyParadox")
ErrIllegalSupportPosition = fmt.Errorf("ErrIllegalSupportPosition")
ErrIllegalSupportDestination = fmt.Errorf("ErrIllegalSupportDestination")
ErrIllegalSupportDestinationNation = fmt.Errorf("ErrIllegalSupportDestinationNation")
ErrMissingSupportUnit = fmt.Errorf("ErrMissingSupportUnit")
ErrIllegalSupportMove = fmt.Errorf("ErrIllegalSupportMove")
ErrInvalidSupporteeOrder = fmt.Errorf("ErrInvalidSupporteeOrder")
ErrIllegalConvoyUnit = fmt.Errorf("ErrIllegalConvoyUnit")
ErrIllegalConvoyPath = fmt.Errorf("ErrIllegalConvoyPath")
ErrIllegalConvoyMove = fmt.Errorf("ErrIllegalConvoyMove")
ErrMissingConvoyee = fmt.Errorf("ErrMissingConvoyee")
ErrIllegalConvoyer = fmt.Errorf("ErrIllegalConvoyer")
ErrIllegalConvoyee = fmt.Errorf("ErrIllegalConvoyee")
ErrIllegalBuild = fmt.Errorf("ErrIllegalBuild")
ErrIllegalDisband = fmt.Errorf("ErrIllegalDisband")
ErrOccupiedSupplyCenter = fmt.Errorf("ErrOccupiedSupplyCenter")
ErrMissingSupplyCenter = fmt.Errorf("ErrMissingSupplyCenter")
ErrMissingSurplus = fmt.Errorf("ErrMissingSurplus")
ErrIllegalUnitType = fmt.Errorf("ErrIllegalUnitType")
ErrMissingDeficit = fmt.Errorf("ErrMissingDeficit")
ErrOccupiedDestination = fmt.Errorf("ErrOccupiedDestination")
ErrIllegalRetreat = fmt.Errorf("ErrIllegalRetreat")
ErrHostileSupplyCenter = fmt.Errorf("ErrHostileSupplyCenter")
InconsistencyMissingOrder = fmt.Errorf("InconsistencyMissingOrder")
)
type InconsistencyMismatchedSupporter struct {
Supportee Province
}
func (self InconsistencyMismatchedSupporter) Error() string {
return fmt.Sprintf("InconsistencyMismatchedSupporter:%v", self.Supportee)
}
type InconsistencyMismatchedConvoyee struct {
Convoyer Province
}
func (self InconsistencyMismatchedConvoyee) Error() string {
return fmt.Sprintf("InconsistencyMismatchedConvoyee:%v", self.Convoyer)
}
type InconsistencyMismatchedConvoyer struct {
Convoyee Province
}
func (self InconsistencyMismatchedConvoyer) Error() string {
return fmt.Sprintf("InconsistencyMismatchedConvoyer:%v", self.Convoyee)
}
type InconsistencyOrderTypeCount struct {
OrderType OrderType
Found int
Want int
}
func (self InconsistencyOrderTypeCount) Error() string {
return fmt.Sprintf("InconsistencyOrderTypeCount:%v:Found:%v:Want:%v", self.OrderType, self.Found, self.Want)
}
type ErrDoubleBuild struct {
Provinces []Province
}
func (self ErrDoubleBuild) Error() string {
return fmt.Sprintf("ErrDoubleBuild:%v", self.Provinces)
}
type ErrConvoyDislodged struct {
Province Province
}
func (self ErrConvoyDislodged) Error() string {
return fmt.Sprintf("ErrConvoyDislodged:%v", self.Province)
}
type ErrSupportBroken struct {
Province Province
}
func (self ErrSupportBroken) Error() string {
return fmt.Sprintf("ErrSupportBroken:%v", self.Province)
}
type ErrBounce struct {
Province Province
}
func (self ErrBounce) Error() string {
return fmt.Sprintf("ErrBounce:%v", self.Province)
}
var Debug = false
var LogIndent = []string{}
var logBuffer = new(bytes.Buffer)
var logMutex = sync.RWMutex{}
func Indent(s string) {
if Debug {
logMutex.Lock()
defer logMutex.Unlock()
LogIndent = append(LogIndent, s)
}
}
func DeIndent() {
if Debug {
logMutex.Lock()
defer logMutex.Unlock()
LogIndent = LogIndent[:len(LogIndent)-1]
}
}
func Logf(s string, o ...interface{}) {
if Debug {
logMutex.Lock()
defer logMutex.Unlock()
fmt.Fprintf(logBuffer, fmt.Sprintf("%v%v\n", strings.Join(LogIndent, ""), s), o...)
}
}
func ClearLog() {
if Debug {
logMutex.Lock()
defer logMutex.Unlock()
logBuffer = new(bytes.Buffer)
}
}
func DumpLog() {
if Debug {
logMutex.RLock()
fmt.Print(string(logBuffer.Bytes()))
logMutex.RUnlock()
ClearLog()
}
}
func Max(is ...int) (result int) {
for index, i := range is {
if index == 0 || i > result {
result = i
}
}
return
}
func Min(is ...int) (result int) {
for index, i := range is {
if index == 0 || i < result {
result = i
}
}
return
}
type UnitType string
type Nation string
func (n *Nation) String() string {
if n == nil {
return ""
}
return string(*n)
}
type Nations []Nation
func (n Nations) Equal(o Nations) bool {
if len(n) != len(o) {
return false
}
sortedN := make(Nations, len(n))
copy(sortedN, n)
sort.Sort(sortedN)
sortedO := make(Nations, len(o))
copy(sortedO, o)
sort.Sort(sortedO)
for idx, nNat := range sortedN {
if sortedO[idx] != nNat {
return false
}
}
return true
}
func (n Nations) Len() int {
return len(n)
}
func (n Nations) Less(i, j int) bool {
return bytes.Compare([]byte(n[i]), []byte(n[j])) < 0
}
func (n Nations) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
type OrderType string
type PhaseType string
type Province string
func (p *Province) String() string {
if p == nil {
return ""
}
return string(*p)
}
type Season string
func (self Province) Split() (sup Province, sub Province) {
split := strings.Split(string(self), "/")
if len(split) > 0 {
sup = Province(split[0])
}
if len(split) > 1 {
sub = Province(split[1])
}
return
}
func (self Province) Join(n Province) (result Province) {
if n != "" {
result = Province(fmt.Sprintf("%v/%v", self, n))
} else {
result = self
}
return
}
func (self Province) Super() (result Province) {
result, _ = self.Split()
return
}
func (self Province) Sub() (result Province) {
_, result = self.Split()
return
}
func (self Province) Contains(p Province) bool {
return self == p || (self.Super() == self && self == p.Super())
}
type Unit struct {
Type UnitType
Nation Nation
}
func (self Unit) Equal(o Unit) bool {
return self.Type == o.Type && self.Nation == o.Nation
}
func (self *Unit) String() string {
return fmt.Sprint(*self)
}
type Phase interface {
Year() int
Season() Season
Type() PhaseType
Next() Phase
PreProcess(State) error
PostProcess(State) error
DefaultOrder(Province) Adjudicator
Options(Validator, Nation) Options
Messages(Validator, Nation) []string
Corroborate(Validator, Nation) []Inconsistency
}
type PathFilter func(n Province, edgeFlags, provFlags map[Flag]bool, sc *Nation, trace []Province) bool
type Flag string
type Graph interface {
Has(Province) bool
Flags(Province) map[Flag]bool
AllFlags(Province) map[Flag]bool
SC(Province) *Nation
Path(src, dst Province, reverse bool, filter PathFilter) []Province
Coasts(Province) []Province
Edges(src Province, reverse bool) map[Province]map[Flag]bool
SCs(Nation) []Province
AllSCs() []Province
Provinces() []Province
Nations() []Nation
}
type Orders []Order
func (self Orders) Less(a, b int) bool {
return self[a].At().Before(self[b].At())
}
func (self Orders) Swap(a, b int) {
self[a], self[b] = self[b], self[a]
}
func (self Orders) Len() int {
return len(self)
}
type SrcProvince Province
type OptionValue interface{}
type FilteredOptionValue struct {
Filter string
Value OptionValue
}
/*
Options defines a tree of valid orders for a given situation
*/
type Options map[OptionValue]Options
func (self Options) BubbleFilters() Options {
_, result := self.bubbleFiltersHelper(false)
return result
}
func (self Options) bubbleFiltersHelper(bubbleSelf bool) (string, Options) {
lastFilter := ""
filters := map[string]bool{}
bubbledChildren := Options{}
for k, v := range self {
if filtered, ok := k.(FilteredOptionValue); ok {
lastFilter = filtered.Filter
filters[lastFilter] = true
bubbledChildren[k] = v
} else {
childCommonFilter, newChild := v.bubbleFiltersHelper(true)
lastFilter = childCommonFilter
filters[lastFilter] = true
if childCommonFilter == "" {
bubbledChildren[k] = v
} else {
bubbledChildren[FilteredOptionValue{
Filter: childCommonFilter,
Value: k,
}] = newChild
}
}
}
if !bubbleSelf || lastFilter == "" || len(filters) > 1 {
return "", bubbledChildren
}
bubbledSelf := Options{}
for k, v := range bubbledChildren {
filtered := k.(FilteredOptionValue)
bubbledSelf[filtered.Value] = v
}
return lastFilter, bubbledSelf
}
func (self Options) MarshalJSON() ([]byte, error) {
repl := map[string]interface{}{}
for k, v := range self.BubbleFilters() {
kVal := reflect.ValueOf(k)
filter := ""
if kVal.Type() == reflect.TypeOf(FilteredOptionValue{}) {
filter = kVal.FieldByName("Filter").String()
kVal = reflect.ValueOf(kVal.FieldByName("Value").Interface())
}
val := map[string]interface{}{
"Type": kVal.Type().Name(),
"Next": v,
}
if filter != "" {
val["Filter"] = filter
}
repl[fmt.Sprint(kVal.Interface())] = val
}
return json.Marshal(repl)
}
// Order is a basic order, but unable to adjudicate itself.
type Order interface {
Type() OrderType
DisplayType() OrderType
Targets() []Province
Parse([]string) (Adjudicator, error)
Validate(Validator) (Nation, error)
Options(Validator, Nation, Province) Options
At() time.Time
Flags() map[Flag]bool
Corroborate(Validator) []error
}
// Adjudicator is what orders turn into when adjudication has started.
type Adjudicator interface {
Order
Adjudicate(Resolver) error
Execute(State)
}
type BackupRule func(State, []Province) error
type StateFilter func(n Province, o Order, u *Unit) bool
// Validator is a game state able to validate orders, but not adjudicate them.
type Validator interface {
Order(Province) (Order, Province, bool)
Unit(Province) (Unit, Province, bool)
Dislodged(Province) (Unit, Province, bool)
SupplyCenter(Province) (Nation, Province, bool)
Bounce(src, dst Province) bool
Orders() map[Province]Adjudicator
Units() map[Province]Unit
Dislodgeds() map[Province]Unit
ForceDisbands() map[Province]bool
SupplyCenters() map[Province]Nation
Graph() Graph
Phase() Phase
Find(StateFilter) (provinces []Province, orders []Order, units []*Unit)
Options([]Order, Nation) (result Options)
Profile(string, time.Time)
GetProfile() (map[string]time.Duration, map[string]int)
MemoizeProvSlice(string, func() []Province) []Province
Flags() map[Flag]bool
}
// Resolver is what validators turn into when adjudication has started.
type Resolver interface {
Validator
AddBounce(src, dst Province)
Resolve(Province) error
}
type Inconsistency struct {
Province Province
Errors []error
}
func (i Inconsistency) MarshalJSON() ([]byte, error) {
errStrings := make([]string, len(i.Errors))
for idx := range i.Errors {
errStrings[idx] = i.Errors[idx].Error()
}
return json.Marshal(map[string]interface{}{
"Province": i.Province,
"Inconsistencies": errStrings,
})
}
// State is the super-user access to the entire game state.
type State interface {
Resolver
Move(src, dst Province, preventRetreat bool)
Retreat(src, dst Province) error
RemoveDislodged(Province)
RemoveUnit(Province)
ForceDisband(Province)
SetResolution(Province, error)
SetSC(Province, Nation)
SetUnit(Province, Unit) error
SetOrders(map[Province]Adjudicator)
ClearDislodgers()
ClearBounces()
Corroborate(Nation) []Inconsistency
}