-
Notifications
You must be signed in to change notification settings - Fork 1
/
replay.go
208 lines (178 loc) · 4.44 KB
/
replay.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
// Warcrumb - Replay parser library for Warcraft 3
// Copyright (C) 2020 Dmitry Narkevich
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package warcrumb
import (
"fmt"
"image/color"
"time"
)
type Replay struct {
parseOptions
Duration time.Duration
Version int
BuildNumber int
Expac Expac
IsMultiplayer bool
isReforged bool
GameType GameType
PrivateGame bool
GameOptions GameOptions
Players map[int]*Player
Slots []Slot
RandomSeed uint32
selectMode byte
startSpotCount int
ChatMessages []ChatMessage
Saver *Player
WinnerTeam int // -1 represents a draw
Actions []Action
}
type parseOptions struct {
debugMode bool
}
type GameOptions struct {
MapName string
CreatorName string
TeamsTogether bool
LockTeams bool
FullSharedUnitControl bool
RandomHero bool
RandomRaces bool
Speed GameSpeed
Visibility Visibility
ObserverSetting ObserverSetting
GameName string
}
type ObserverSetting int
type GameSpeed int
type Expac int
type Visibility int
type GameType uint16
type Player struct {
Name string
Id int
SlotId int
// slot is currently not exposed to avoid panic (due to pointer cycle) when converting to json
// but that's probably not the best reason
slot *Slot
BattleNet *BattleNet2Account
}
func (p Player) String() string {
if p.BattleNet != nil {
return p.BattleNet.Username
} else {
return p.Name
}
}
type BattleNet2Account struct {
PlayerId int
Avatar string
Username string
Clan string
ExtraData []byte
}
type ChatMessage struct {
Timestamp time.Duration
Author Slot
Body string
Destination MsgDestination
}
type MsgDestination interface {
isMsgDest() // dummy method to emulate sum type
fmt.Stringer
}
type MsgToEveryone struct{}
func (MsgToEveryone) isMsgDest() {}
func (MsgToEveryone) String() string { return "All" }
type MsgToAllies struct{}
func (MsgToAllies) isMsgDest() {}
func (MsgToAllies) String() string { return "Allies" }
type MsgToObservers struct{}
func (MsgToObservers) isMsgDest() {}
func (MsgToObservers) String() string { return "Observers" }
type MsgToPlayer struct{ Target Slot }
func (MsgToPlayer) isMsgDest() {}
func (m MsgToPlayer) String() string { return "To " + m.Target.String() }
type Slot struct {
Id int
Player *Player
IsCPU bool
Race Race
raceSelectableOrFixed bool
SlotStatus slotStatus
TeamNumber int
Color Color
AIStrength AIStrength
Handicap int
MapDownloadPercent byte
playerId int
}
// String returns the text you'd see in-game as the name of that slot.
func (s Slot) String() string {
switch s.SlotStatus {
case EmptySlot:
return "Open"
case ClosedSlot:
return "Closed"
case UsedSlot:
if s.IsCPU {
return fmt.Sprintf("Computer (%s)", s.AIStrength.String())
} else {
return s.Player.String()
}
}
return ""
}
type Race struct {
name string
// could add an icon field
}
func (r Race) String() string {
return r.name
}
func (r Race) ShortName() string {
return string(r.name[0])
}
func (r Race) MarshalText() ([]byte, error) {
return []byte(r.String()), nil
}
type slotStatus string
type Color struct {
color.Color
name string
}
func (c Color) String() string {
return c.name
}
func (c Color) MarshalText() ([]byte, error) {
return []byte(c.String()), nil
}
type AIStrength byte
func (a AIStrength) String() string {
switch a {
case EasyAI:
return "Easy"
case NormalAI:
return "Normal"
case InsaneAI:
return "Insane"
}
//return fmt.Sprintf("n/a (0x%x)", *a)
return "n/a"
}
func (a AIStrength) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}