-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
312 lines (261 loc) · 7.06 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/golang/geo/r3"
"github.com/healeycodes/compress-cs2-demo/github.com/healeycodes/compress-cs2-demo/optimal"
dem "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs"
"google.golang.org/protobuf/proto"
)
func main() {
path := "pera-vs-system5-m1-vertigo.dem"
t := time.Now()
naive(path)
fmt.Printf("naive done %s\n", time.Since(t))
t = time.Now()
better(path)
fmt.Printf("optimal done %s\n", time.Since(t))
}
func naive(path string) {
type Player struct {
Id int
Name string
Position r3.Vector
Equipment []string
}
type Frame struct {
Players []Player
}
type Game struct {
Frames []Frame
}
f, err := os.Open("pera-vs-system5-m1-vertigo.dem")
if err != nil {
log.Panic("failed to open demo file: ", err)
}
defer f.Close()
p := dem.NewParser(f)
defer p.Close()
game := Game{
Frames: []Frame{},
}
moreFrames, err := p.ParseNextFrame()
for ; moreFrames && err == nil; moreFrames, err = p.ParseNextFrame() {
frame := Frame{Players: []Player{}}
for _, player := range p.GameState().Participants().Playing() {
equipment := []string{}
for _, equip := range player.Weapons() {
equipment = append(equipment, equip.String())
}
frame.Players = append(frame.Players, Player{
Id: int(player.SteamID64),
Name: player.Name,
Position: player.Position(),
Equipment: equipment,
})
}
game.Frames = append(game.Frames, frame)
}
if err != nil {
log.Panicf("Failed to parse: %s\n", err)
}
jsonData, err := json.Marshal(game)
if err != nil {
log.Panicf("Failed to marshal JSON: %s\n", err)
}
f, err = os.Create("./naive.json")
if err != nil {
log.Panicf("Failed to write JSON: %s\n", err)
}
f.Write(jsonData)
}
func better(path string) {
type Player = struct {
Id uint64 `json:"id"`
IdShort uint32 `json:"idShort"`
Name string `json:"name"`
}
type PlayerMeta map[uint64]Player
type EquipmentMeta map[string]int32
type Frame struct {
PlayerSpawn []uint32 `json:"playerSpawn,omitempty"`
PlayerDeath []uint32 `json:"playerDeath,omitempty"`
PositionChange map[uint32]r3.Vector `json:"positionChange,omitempty"`
EquipmentChange map[uint32][]int32 `json:"equipmentChange,omitempty"`
}
type Game struct {
PlayerMeta `json:"playerMeta"`
EquipmentMeta `json:"equipmentMeta"`
Frames []Frame `json:"frames"`
}
f, err := os.Open(path)
if err != nil {
log.Panicf("Failed to open demo file: %s\n", err)
}
defer f.Close()
p := dem.NewParser(f)
defer p.Close()
game := Game{
PlayerMeta: make(PlayerMeta),
EquipmentMeta: make(EquipmentMeta),
Frames: make([]Frame, 0),
}
type FrameInfo struct {
Position r3.Vector
Equipment []int32
}
var playerIds uint32 = 1
var equipIds int32 = 1
var curFrame map[uint32]FrameInfo
lastFrame := map[uint32]FrameInfo{}
moreFrames, err := p.ParseNextFrame()
for ; moreFrames && err == nil; moreFrames, err = p.ParseNextFrame() {
curFrame = map[uint32]FrameInfo{}
for _, player := range p.GameState().Participants().Playing() {
var idShort uint32
if meta, ok := game.PlayerMeta[player.SteamID64]; !ok {
idShort = playerIds
playerIds++
game.PlayerMeta[player.SteamID64] = Player{
Id: player.SteamID64,
IdShort: idShort,
Name: player.Name,
}
} else {
idShort = meta.IdShort
}
equipment := []int32{}
for _, equip := range player.Weapons() {
if idEquip, ok := game.EquipmentMeta[equip.String()]; !ok {
idEquip = equipIds
equipIds++
game.EquipmentMeta[equip.String()] = idEquip
equipment = append(equipment, idEquip)
} else {
equipment = append(equipment, idEquip)
}
}
curFrame[idShort] = FrameInfo{
Position: player.Position(),
Equipment: equipment,
}
}
frame := Frame{
PlayerSpawn: []uint32{},
PlayerDeath: []uint32{},
PositionChange: map[uint32]r3.Vector{},
EquipmentChange: map[uint32][]int32{},
}
for id := range lastFrame {
if _, ok := curFrame[id]; !ok {
frame.PlayerDeath = append(frame.PlayerDeath, id)
}
}
for id, info := range curFrame {
if lastInfo, ok := lastFrame[id]; !ok {
frame.PlayerSpawn = append(frame.PlayerSpawn, id)
frame.PositionChange[id] = info.Position
frame.EquipmentChange[id] = info.Equipment
} else {
if lastInfo.Position.Cmp(info.Position) != 0 {
frame.PositionChange[id] = info.Position
}
// Add equipment
for _, idEquip := range info.Equipment {
if !contains[int32](lastInfo.Equipment, idEquip) {
if frame.EquipmentChange[id] == nil {
frame.EquipmentChange[id] = []int32{}
}
frame.EquipmentChange[id] = append(frame.EquipmentChange[id], idEquip)
}
}
// Remove equipment
for _, idEquip := range lastInfo.Equipment {
if contains[int32](lastInfo.Equipment, idEquip) && !contains[int32](info.Equipment, idEquip) {
if frame.EquipmentChange[id] == nil {
frame.EquipmentChange[id] = []int32{}
}
frame.EquipmentChange[id] = append(frame.EquipmentChange[id], idEquip)
}
}
}
}
lastFrame = curFrame
game.Frames = append(game.Frames, frame)
}
if err != nil {
log.Panicf("Failed to parse: %s\n", err)
}
jsonData, err := json.Marshal(game)
if err != nil {
log.Panicf("Failed to marshal JSON: %s\n", err)
}
f, err = os.Create("./better.json")
if err != nil {
log.Panicf("Failed to write JSON: %s\n", err)
}
f.Write(jsonData)
protoGame := &optimal.Game{
PlayerMeta: &optimal.PlayerMeta{
Players: map[uint64]*optimal.Player{},
},
EquipmentMeta: &optimal.EquipmentMeta{
Equipment: map[string]int32{},
},
Frames: []*optimal.Frame{},
}
for id, meta := range game.PlayerMeta {
protoGame.PlayerMeta.Players[id] = &optimal.Player{
Id: meta.Id,
IdShort: uint32(meta.IdShort),
Name: meta.Name,
}
}
for id, equip := range game.EquipmentMeta {
protoGame.EquipmentMeta.Equipment[id] = int32(equip)
}
for _, frame := range game.Frames {
protoGame.Frames = append(protoGame.Frames, &optimal.Frame{
PlayerSpawn: frame.PlayerSpawn,
PlayerDeath: frame.PlayerDeath,
PositionChange: convertVectorMap(frame.PositionChange),
EquipmentChange: convertEquipmentList(frame.EquipmentChange),
})
}
data, err := proto.Marshal(protoGame)
if err != nil {
log.Fatalf("Failed to marshal protobuf: %v", err)
}
if err := os.WriteFile("better.proto", data, 0644); err != nil {
log.Fatalf("Failed to write to file: %v", err)
}
}
func convertVectorMap(m map[uint32]r3.Vector) map[uint32]*optimal.Vector {
m2 := map[uint32]*optimal.Vector{}
for id, v := range m {
m2[id] = &optimal.Vector{
X: v.X,
Y: v.Y,
Z: v.Z,
}
}
return m2
}
func convertEquipmentList(l map[uint32][]int32) map[uint32]*optimal.EquipmentList {
el := map[uint32]*optimal.EquipmentList{}
for k, v := range l {
el[k] = &optimal.EquipmentList{Equipment: v}
}
return el
}
func contains[T comparable](elems []T, v T) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}