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
|
# File 'lib/openra/cli/commands/replay_data.rb', line 21
def call(replay:, **options)
replay = Openra::Replays::Replay.new(replay)
players = replay.metadata.each_with_object([]) do |(key, value), arr|
next unless key.start_with?('Player')
arr << value
end
player_mapping = players.each_with_object({}) do |player, mapping|
mapping[player['ClientIndex']] = player
end
player_teams = players.map { |player| player['Team'] }
team_alignment = player_teams.each_with_object({}) do |team, hash|
if team == 0
hash[SecureRandom.uuid] = 1
else
hash[team] ||= 0
hash[team] += 1
end
end
replay_data = {
mod: replay.mod,
version: replay.version,
server_name: nil,
map: {
name: utf8(replay.map_title),
hash: replay.map_id
},
game: {
type: team_alignment.values.join('v'),
start_time: replay.start_time,
end_time: replay.end_time,
duration: replay.duration,
options: {}
},
clients: [],
chat: []
}
timestep = nil
sync_info_orders = replay.orders.select do |order|
order.command == 'SyncInfo'
end
sync_info_orders.reverse.each.with_index do |sync_info_order, index|
sync_info = Openra::YAML.load(sync_info_order.target)
sync_info.each_pair do |key, data|
case key
when /^Client@/
replay_data[:clients] << {
index: data['Index'],
name: utf8(data['Name']),
preferred_color: data['PreferredColor'],
color: data['Color'],
faction: data['Faction'],
ip: data['IpAddress'],
team: data['Team'].to_s == '0' ? nil : data['Team'],
is_bot: data['Bot'].nil? ? false : true,
is_admin: data['IsAdmin'] == 'True',
is_player: player_mapping.fetch(data['Index'], false) != false,
is_winner: player_mapping.fetch(data['Index'], {}).fetch('Outcome', nil) == 'Won',
build: []
} unless replay_data[:clients].any? { |client| client[:index] == data['Index'] }
when 'GlobalSettings'
next unless index.zero?
timestep = Integer(data['Timestep']) * ORDER_LATENCY_MAPPING.fetch(
data['Options']['gamespeed']['Value'],
ORDER_LATENCY_MAPPING['default']
)
replay_data[:server_name] = data['ServerName']
replay_data[:game][:options] = {
explored_map: data['Options']['explored']['Value'] == 'True',
speed: data['Options']['gamespeed']['Value'],
starting_cash: data['Options']['startingcash']['Value'],
starting_units: data['Options']['startingunits']['Value'],
fog_enabled: data['Options']['fog']['Value'] == 'True',
cheats_enabled: data['Options']['cheats']['Value'] == 'True',
kill_bounty_enabled: data['Options']['bounty']['Value'] == 'True',
allow_undeploy: data['Options']['factundeploy']['Value'] == 'True',
crates_enabled: data['Options']['crates']['Value'] == 'True',
build_off_allies: data['Options']['allybuild']['Value'] == 'True',
restrict_build_radius: data['Options']['buildradius']['Value'] == 'True',
short_game: data['Options']['shortgame']['Value'] == 'True',
techlevel: data['Options']['techlevel']['Value']
}
end
end
end
replay.orders.each do |order|
case order.command
when 'PlaceBuilding'
client = replay_data[:clients].find do |candidate|
candidate[:index] == order.client_index.to_s
end
current_time = order.frame * timestep
current_time_seconds = current_time / 1000
mm, ss = current_time_seconds.divmod(60)
hh, mm = mm.divmod(60)
client[:build] << {
structure: order.target_string,
game_time: {
formatted: '%02d:%02d:%02d' % [hh, mm, ss],
msec: current_time
},
placement: {
x: order.target_x,
y: order.target_y
}
}
when 'Message'
replay_data[:chat] << {
channel: :server,
name: nil,
message: utf8(order.target)
}
when 'Chat'
client = replay_data[:clients].find do |candidate|
candidate[:index] == order.client_index.to_s
end
replay_data[:chat] << {
channel: :global,
name: client[:name],
message: utf8(order.target)
}
when 'TeamChat'
client = replay_data[:clients].find do |candidate|
candidate[:index] == order.client_index.to_s
end
replay_data[:chat] << {
channel: client[:team],
name: client[:name],
message: utf8(order.target)
}
end
end
case options[:format]
when 'json'
puts JSON.dump(replay_data)
when 'pretty-json'
puts JSON.pretty_generate(replay_data)
end
end
|