53 lines
2.5 KiB
Python
53 lines
2.5 KiB
Python
"""Compare fabricated scoreboards to the pinned BattlegroundPackets.cpp response layout."""
|
|
from struct import pack
|
|
import unittest
|
|
|
|
from native_codec_driver import codec_library_path, require_optional_file, MODES, load, serialize
|
|
|
|
|
|
def player(low, team, race, sex, cls, kills, honor, deaths, contribution, stats):
|
|
guid = pack('<QQ', low, 0x0800000000000000)
|
|
projected = (guid + pack('<4B4IB', team, race, sex, cls, kills, honor, deaths, contribution, len(stats))
|
|
+ b''.join(pack('<ii', *stat) for stat in stats))
|
|
packed_guid = pack('<H', sum(1 << i for i, value in enumerate(guid) if value)) + bytes(filter(None, guid))
|
|
# Original PVPMatchPlayerStatistics: unrated, in world, Honor present, and no damage/healing data.
|
|
expected = (packed_guid + pack('<IiIIIi3b3i', kills, team, 0, 0, len(stats), 0, sex, race, cls, 0, 0, 0)
|
|
+ b''.join(pack('<ii', *stat) for stat in stats)
|
|
+ bytes([0xC0]) + pack('<III', honor, deaths, contribution))
|
|
return projected, expected
|
|
|
|
|
|
def scoreboards():
|
|
alliance = player(0x11, 0, 1, 0, 8, 3, 5, 2, 50, [(42, 7), (44, 2)])
|
|
horde = player(0x102, 1, 2, 1, 1, 4, 6, 3, 23, [])
|
|
for players, counts in (([], (0, 0)), ([alliance], (1, 0)), ([horde], (0, 1)),
|
|
([horde, alliance], (1, 1))):
|
|
projected = bytes([1, len(players)]) + b''.join(p[0] for p in players)
|
|
# Original PVPMatchStatistics: absent ratings, unsigned row count, two signed-byte team counts.
|
|
expected = pack('<BI2b', 0, len(players), *counts) + b''.join(p[1] for p in players)
|
|
yield projected, expected
|
|
|
|
|
|
class PvPStatisticsSerializer(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.library = load()
|
|
cls.mode = MODES.index('--pvp-statistics')
|
|
|
|
def test_original_response_bytes(self):
|
|
self.assertEqual((MODES.index('--who'), self.mode), (71, 72))
|
|
for data, expected in scoreboards():
|
|
with self.subTest(input=data):
|
|
self.assertEqual(serialize(self.library, self.mode, data), expected)
|
|
|
|
def test_rejects_invalid_framing_and_team(self):
|
|
valid = list(scoreboards())[1][0]
|
|
for bad in (valid[:-1], valid + b'\0', bytes([2, 0]), bytes([1, 81]), valid[:18] + bytes([2]) + valid[19:]):
|
|
with self.subTest(input=bad), self.assertRaises(ValueError):
|
|
serialize(self.library, self.mode, bad)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
require_optional_file(codec_library_path())
|
|
unittest.main()
|