-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_replay_json.py
209 lines (162 loc) · 7.51 KB
/
generate_replay_json.py
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
import json
import pickle
import sys
from pprint import pprint
from pyrope import Replay
class Generator(object):
def __init__(self):
file_path = sys.argv[1]
try:
self.replay = pickle.load(open(file_path + '.pickle', "rb"))
self.replay_id = self.replay.header['Id']
except:
self.replay = Replay(path=file_path)
self.replay_id = self.replay.header['Id']
self.replay.parse_netstream()
pickle.dump(self.replay, open(file_path + '.pickle', 'wb'))
self.players = self.get_players()
self.ballData = self.get_ball_data()
for player in self.players:
# Get their position data.
self.players[player]['position_data'] = self.get_player_position_data(player)
# Restructure the data so that it's chunkable.
frame_data = []
for frame in range(self.replay.header['NumFrames']):
frame_dict = {
'time': self.replay.netstream[frame].current,
'actors': []
}
for player in self.players:
position_data = self.players[player]['position_data']
if frame in position_data:
frame_dict['actors'].append({
'id': player,
'type': 'car',
**position_data[frame]
})
if frame in self.ballData:
frame_dict['actors'].append({
'id': 'ball',
'type': 'ball',
**self.ballData[frame]
})
frame_data.append(frame_dict)
assert len(frame_data) == self.replay.header['NumFrames'], "Missing {} frames from data output.".format(
self.replay.header['NumFrames'] - len(frame_data)
)
# Get min and max z values.
for axis in ['x', 'y', 'z']:
values = [
actor[axis]
for frame in frame_data
for actor in frame['actors']
]
print(axis, min(values), max(values))
json.dump(frame_data, open(file_path + '.json', 'w'), indent=2)
def get_players(self):
players = {}
for index, frame in self.replay.netstream.items():
pri_ta = [value for name, value in frame.actors.items() if 'e_Default__PRI_TA' in name]
for value in pri_ta:
"""
Example `value`:
{'actor_id': 2,
'actor_type': 'TAGame.Default__PRI_TA',
'data': {'Engine.PlayerReplicationInfo:Ping': 24,
'Engine.PlayerReplicationInfo:PlayerID': 656,
'Engine.PlayerReplicationInfo:PlayerName': "AvD Sub'n",
'Engine.PlayerReplicationInfo:Team': (True, 6),
'Engine.PlayerReplicationInfo:UniqueId': (1, 76561198040631598, 0),
'Engine.PlayerReplicationInfo:bReadyToPlay': True,
'TAGame.PRI_TA:CameraSettings': {'dist': 270.0,
'fov': 107.0,
'height': 110.0,
'pitch': -2.0,
'stiff': 1.0,
'swiv': 4.300000190734863},
'TAGame.PRI_TA:ClientLoadout': (11, [23, 0, 613, 39, 752, 0, 0]),
'TAGame.PRI_TA:ClientLoadoutOnline': (11, 0, 0),
'TAGame.PRI_TA:PartyLeader': (1, 76561198071203042, 0),
'TAGame.PRI_TA:ReplicatedGameEvent': (True, 1),
'TAGame.PRI_TA:Title': 0,
'TAGame.PRI_TA:TotalXP': 9341290,
'TAGame.PRI_TA:bUsingSecondaryCamera': True},
'new': False,
'startpos': 102988}
"""
if 'Engine.PlayerReplicationInfo:PlayerName' not in value['data']:
continue
team_id = None
actor_id = value['actor_id']
if 'Engine.PlayerReplicationInfo:Team' in value['data']:
team_id = value['data']['Engine.PlayerReplicationInfo:Team'][1]
player_name = value['data']['Engine.PlayerReplicationInfo:PlayerName']
if actor_id in players:
if (not players[actor_id]['team'] and team_id) or team_id == -1:
players[actor_id]['team'] = team_id
elif 'TAGame.PRI_TA:ClientLoadout' in value['data']:
players[actor_id] = {
'join': index,
'left': self.replay.header['NumFrames'],
'name': player_name,
'team': team_id,
}
return players
def get_player_position_data(self, player_id):
player = self.players[player_id]
result = {}
car_actor_obj = None
for index in range(player['join'], player['left']):
frame = self.replay.netstream[index]
# pprint(frame.actors)
# First we need to find the player's car object.
for actor in frame.actors:
actor_obj = frame.actors[actor]
if 'data' not in actor_obj:
continue
engine = actor_obj['data'].get('Engine.Pawn:PlayerReplicationInfo')
# This is the correct object for this player.
if engine and engine[1] == player_id:
car_actor_obj = actor_obj['actor_id']
# If the actor we're looking at is the car object, then get the
# position and rotation data for this frame.
if actor_obj['actor_id'] == car_actor_obj:
state_data = actor_obj['data'].get('TAGame.RBActor_TA:ReplicatedRBState')
if state_data:
x, y, z = state_data['pos']
yaw, pitch, roll = state_data['rot']
result[index] = {
'x': x,
'y': y,
'z': z,
'pitch': pitch,
'roll': roll,
'yaw': yaw
}
return result
def get_ball_data(self):
result = {}
for index, frame in self.replay.netstream.items():
for actor in frame.actors:
actor_obj = frame.actors[actor]
if 'data' not in actor_obj:
continue
if 'TAGame.RBActor_TA:ReplicatedRBState' not in actor_obj['data']:
continue
if actor_obj['actor_type'] != 'Archetypes.Ball.Ball_Default':
continue
# print(actor_obj['actor_id'])
state_data = actor_obj['data']['TAGame.RBActor_TA:ReplicatedRBState']
x, y, z = state_data['pos']
yaw, pitch, roll = state_data['rot']
result[index] = {
'x': x,
'y': y,
'z': z,
'pitch': pitch,
'roll': roll,
'yaw': yaw
}
return result
if __name__ == '__main__':
Generator()