Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions sc2reader/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
},
}

LOTV_SPEEDUP = 1.4

GAME_SECONDS_PER_SECOND = {
'WoL': 1.4,
'HotS': 1.4,
'LotV': 1
}

GATEWAY_CODES = {
'US': 'Americas',
'KR': 'Asia',
Expand Down
12 changes: 10 additions & 2 deletions sc2reader/engine/plugins/apm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import, print_function, unicode_literals, division

from collections import defaultdict
from sc2reader.constants import LOTV_SPEEDUP, GAME_SECONDS_PER_SECOND


class APMTracker(object):
Expand All @@ -24,8 +25,15 @@ def handleInitGame(self, event, replay):
human.seconds_played = replay.length.seconds

def handlePlayerActionEvent(self, event, replay):
event.player.aps[event.second] += 1.4
event.player.apm[int(event.second/60)] += 1.4
speed_multiplier = 1
if replay.expansion == 'LotV':
speed_multiplier = LOTV_SPEEDUP

game_seconds_per_second = GAME_SECONDS_PER_SECOND[replay.expansion]

game_second = int(event.second/speed_multiplier)
event.player.aps[game_second] += game_seconds_per_second
event.player.apm[int(game_second/60)] += game_seconds_per_second

def handlePlayerLeaveEvent(self, event, replay):
event.player.seconds_played = event.second
Expand Down
17 changes: 14 additions & 3 deletions sc2reader/factories/plugins/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sc2reader import log_utils
from sc2reader.utils import Length
from sc2reader.factories.plugins.utils import PlayerSelection, GameState, JSONDateEncoder, plugin
from sc2reader.constants import LOTV_SPEEDUP, GAME_SECONDS_PER_SECOND


@plugin
Expand Down Expand Up @@ -100,18 +101,28 @@ def APMTracker(replay):
above actions divided by the number of seconds played by the player (not
necessarily the whole game) multiplied by 60.
"""

speed_multiplier = 1
if replay.expansion == 'LotV':
speed_multiplier = LOTV_SPEEDUP

game_seconds_per_second = GAME_SECONDS_PER_SECOND[replay.expansion]
if replay.expansion == 'LotV':
game_seconds_per_second = 1

for player in replay.players:
player.aps = defaultdict(int)
player.apm = defaultdict(int)
player.seconds_played = replay.length.seconds

for event in player.events:
game_second = int(event.second/speed_multiplier)
if event.name == 'SelectionEvent' or 'AbilityEvent' in event.name or 'Hotkey' in event.name:
player.aps[event.second] += 1.4
player.apm[int(event.second/60)] += 1.4
player.aps[game_second] += game_seconds_per_second
player.apm[int(game_second/60)] += game_seconds_per_second

elif event.name == 'PlayerLeaveEvent':
player.seconds_played = event.second
player.seconds_played = game_second

if len(player.apm) > 0:
player.avg_apm = sum(player.aps.values())/float(player.seconds_played)*60
Expand Down
11 changes: 11 additions & 0 deletions test_replays/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@ def test_lotv_time(self):
self.assertEqual(replay.length.seconds, 1002)
self.assertEqual(replay.real_length.seconds, 1002)

def test_lotv_apm(self):
from sc2reader.factories.plugins.replay import APMTracker, SelectionTracker, toJSON
factory = sc2reader.factories.SC2Factory()
factory.register_plugin("Replay", APMTracker())
replay = factory.load_replay("test_replays/lotv/lotv1.SC2Replay")
for player in replay.players:
from pprint import pprint
pprint(player)
if player.name == 'Zenchii':
self.assertEquals(int(player.avg_apm), 56)


class TestGameEngine(unittest.TestCase):
class TestEvent(object):
Expand Down