Skip to content

Commit

Permalink
Added demonstration of battle results parsing #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Monstrofil committed Sep 9, 2023
1 parent 02f3210 commit 2791745
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 13 deletions.
4 changes: 2 additions & 2 deletions replay_unpack/clients/wows/network/packets/BattleStats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8
import os
import json
import struct

from replay_unpack.core.pretty_print_mixin import PrettyPrintObjectMixin
Expand All @@ -8,4 +8,4 @@
class BattleStats(PrettyPrintObjectMixin):
def __init__(self, stream):
self.payloadSize, = struct.unpack('i', stream.read(4))
self.payload = stream.read(self.payloadSize)
self.serverData = json.loads(stream.read(self.payloadSize))
6 changes: 5 additions & 1 deletion replay_unpack/clients/wows/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
PlayerPosition,
Version,
PACKETS_MAPPING,
PACKETS_MAPPING_12_6
PACKETS_MAPPING_12_6, BattleStats
)

last_gap = 0
Expand Down Expand Up @@ -173,3 +173,7 @@ def _process_packet(self, time, packet):
logging.debug('nested property request for id=%s isSlice=%s packet=%s',
e.id, packet.is_slice, packet.payload.hex())
packet.read_and_apply(e)

elif isinstance(packet, BattleStats) and\
hasattr(self._battle_controller, 'onPostBattleResultsReceived'):
self._battle_controller.onPostBattleResultsReceived(packet.serverData)
34 changes: 28 additions & 6 deletions replay_unpack/clients/wows/versions/12_7_0/battle_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
except ImportError:
DEATH_TYPES = {}
from .players_info import PlayersInfo, PlayerType
from .battle_results import (
unpackCommonRes,
unpackPlayerPrivateRes,
unpackClientPublicRes,
unpackBuildinsRes,
)


class BattleController(IBattleController):
Expand All @@ -28,6 +34,7 @@ def __init__(self):
self._map = {}
self._player_id = None
self._arena_id = None
self.postBattleResult = None

self._dead_planes = {}

Expand Down Expand Up @@ -93,7 +100,8 @@ def get_info(self):
tasks=list(self._getTasksInfo()),
skills=dict(),
crew=dict(self.getCrewInformation()),
arena_id=self._arena_id
arena_id=self._arena_id,
post_battle=self.postBattleResult
)

def _getCrewInfo(self, vehicle):
Expand Down Expand Up @@ -144,7 +152,7 @@ def _getTasksInfo(self):
yield {
"category": Category.names[task['category']],
"status": Status.names[task['status']],
"name": task['name'],
"name": task['id'],
"type": TaskType.names[task['type']]
}

Expand All @@ -154,17 +162,17 @@ def onBattleEnd(self, avatar):
victory_type=self.battle_logic.properties['client']['battleResult']['finishReason'],
)

def onNewPlayerSpawnedInBattle(self, avatar, playersStates, botsStates, observersState):
def onNewPlayerSpawnedInBattle(self, avatar, playersData, botsData, observersData):
self._players.create_or_update_players(
pickle.loads(playersStates, encoding='latin1'),
pickle.loads(playersData, encoding='latin1'),
PlayerType.PLAYER
)
self._players.create_or_update_players(
pickle.loads(botsStates, encoding='latin1'),
pickle.loads(botsData, encoding='latin1'),
PlayerType.BOT
)
self._players.create_or_update_players(
pickle.loads(observersState, encoding='latin1'),
pickle.loads(observersData, encoding='latin1'),
PlayerType.OBSERVER
)

Expand Down Expand Up @@ -226,6 +234,20 @@ def receive_planeDeath(self, avatar, squadronID, planeIDs, reason, attackerId):
self._dead_planes.setdefault(attackerId, 0)
self._dead_planes[attackerId] += len(planeIDs)

def onPostBattleResultsReceived(self, serverData):
self.postBattleResult = {
'common': unpackCommonRes(serverData['commonList']),
'private': unpackPlayerPrivateRes(serverData['privateDataList']),
'public': {
playerId: unpackClientPublicRes(data)
for playerId, data in serverData['playersPublicInfo'].items()
},
'buildings': {
buildingId: unpackBuildinsRes(data)
for buildingId, data in serverData['buildings'].items()
},
}

@property
def map(self):
raise NotImplemented()
Expand Down
47 changes: 47 additions & 0 deletions replay_unpack/clients/wows/versions/12_7_0/battle_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from .constants import (
COMMON_RESULTS,
PLAYER_PRIVATE_RESULTS,
INIT_ECONOMICS,
SUBTOTAL_ECONOMICS,
COMMON_ECONOMICS,
CLIENT_PUBLIC_RESULTS,
CLIENT_VEH_INTERACTION_DETAILS,
CLIENT_BUILDING_INTERACTION_DETAILS,
BUILDINGS_FULL_RESULTS
)


def listToDict(names, l):
return dict(zip(names, l))


def unpackBuildinsRes(buildingsList):
return listToDict(BUILDINGS_FULL_RESULTS, buildingsList)


def unpackCommonRes(commonResults):
return listToDict(COMMON_RESULTS, commonResults)


def unpackPlayerPrivateRes(privateList):
result = listToDict(PLAYER_PRIVATE_RESULTS, privateList)

result['init_economics'] = listToDict(INIT_ECONOMICS, result['init_economics'])
result['common_economics'] = listToDict(COMMON_ECONOMICS, result['common_economics'])
result['subtotal_economics'] = listToDict(SUBTOTAL_ECONOMICS, result['subtotal_economics'])
return result


def unpackClientPublicRes(publicList):
result = listToDict(CLIENT_PUBLIC_RESULTS, publicList)

result['interactions'] = {
targetId: listToDict(CLIENT_VEH_INTERACTION_DETAILS, data)
for targetId, data in result['interactions'].items()
}
result['buildingInteractions'] = {
targetId: listToDict(CLIENT_BUILDING_INTERACTION_DETAILS, data)
for targetId, data in result['buildingInteractions'].items()
}

return result
Loading

0 comments on commit 2791745

Please sign in to comment.