Skip to content

Commit e6ca55b

Browse files
committed
fixing tests from graylinkim/ggtracker merge
1 parent d5eb61a commit e6ca55b

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

sc2reader/engine/plugins/creeptracker.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
from sets import Set
66
except ImportError:
77
Set = set
8-
from PIL.Image import open as PIL_open
9-
from PIL.Image import ANTIALIAS
8+
try:
9+
# required for CreepTracker, but CreepTracker is optional
10+
from PIL.Image import open as PIL_open
11+
from PIL.Image import ANTIALIAS
12+
except ImportError:
13+
pass
1014
try:
1115
from StringIO import StringIO
1216
except ImportError:
@@ -43,7 +47,7 @@ def handleUnitDiedEvent(self, event, replay):
4347
except Exception as e:
4448
print("Whoa! {}".format(e))
4549
pass
46-
50+
4751

4852
def handleUnitInitEvent(self,event,replay):
4953
try:
@@ -53,7 +57,7 @@ def handleUnitInitEvent(self,event,replay):
5357
except Exception as e:
5458
print("Whoa! {}".format(e))
5559
pass
56-
60+
5761
def handleUnitBornEvent(self,event,replay):
5862
try:
5963
if event.unit_type_name== "Hatchery":
@@ -81,14 +85,14 @@ def handleEndGame(self, event, replay):
8185
pass
8286

8387

84-
## The class used to used to calculate the creep spread
88+
## The class used to used to calculate the creep spread
8589
class creep_tracker():
8690
def __init__(self,replay):
8791
#if the debug option is selected, minimaps will be printed to a file
88-
##and a stringIO containing the minimap image will be saved for
89-
##every minite in the game and the minimap with creep highlighted
92+
##and a stringIO containing the minimap image will be saved for
93+
##every minite in the game and the minimap with creep highlighted
9094
## will be printed out.
91-
self.debug = replay.opt.debug
95+
self.debug = replay.opt['debug']
9296
##This list contains creep spread area for each player
9397
self.creep_spread_by_minute = dict()
9498
## this list contains a minimap highlighted with creep spread for each player
@@ -125,7 +129,7 @@ def __init__(self,replay):
125129
# resize height to MAPHEIGHT, and compute new width that
126130
# would preserve aspect ratio
127131
self.map_width = int(cropsize[0] * (float(self.map_height) / cropsize[1]))
128-
self.mapSize =self.map_height * self.map_width
132+
self.mapSize =self.map_height * self.map_width
129133

130134
## the following parameters are only needed if minimaps have to be printed
131135
# minimapSize = ( self.map_width,int(self.map_height) )
@@ -138,10 +142,10 @@ def __init__(self,replay):
138142
imageCenter = [(self.map_width/2), self.map_height/2]
139143
# this is the scaling factor to go from the SC2 coordinate
140144
# system to pixel coordinates
141-
self.image_scale = float(self.map_height) / cropsize[1]
145+
self.image_scale = float(self.map_height) / cropsize[1]
142146
self.transX =imageCenter[0] + self.image_scale * (mapCenter[0])
143147
self.transY = imageCenter[1] + self.image_scale * (mapCenter[1])
144-
148+
145149
def radius_to_map_positions(self,radius):
146150
## this function converts all radius into map coordinates
147151
## centred around the origin that the creep can exist
@@ -180,7 +184,7 @@ def add_to_list(self,player_id,unit_id,unit_location,unit_type,event_time):
180184
def remove_from_list(self,unit_id,time_frame):
181185
## This function searches is given a unit ID for every unit who died
182186
## the unit id will be searched in cgu_gen_units for matches
183-
## if there are any, that unit will be removed from active CGUs
187+
## if there are any, that unit will be removed from active CGUs
184188
## and appended as a new time frame
185189
for player_id in self.creep_gen_units:
186190
length_cgu_list = len(self.creep_gen_units[player_id])
@@ -194,7 +198,7 @@ def remove_from_list(self,unit_id,time_frame):
194198
self.creep_gen_units_times[player_id].append(time_frame)
195199

196200
def cgu_gen_times_to_chunks(self,cgu_time_list):
197-
## this function returns the index and value of every cgu time
201+
## this function returns the index and value of every cgu time
198202
maximum_cgu_time = max(cgu_time_list)
199203
for i in range(0, maximum_cgu_time):
200204
a = list(filter(lambda x_y: x_y[1]//60==i , enumerate(cgu_time_list)))
@@ -211,7 +215,7 @@ def cgu_in_min_to_cgu_units(self,player_id,cgu_in_minutes):
211215
cgu_units.append(self.creep_gen_units[player_id][index])
212216
cgu_max_in_minute = max(cgu_units,key = len)
213217
yield cgu_max_in_minute
214-
218+
215219
def reduce_cgu_per_minute(self,player_id):
216220
#the creep_gen_units_lists contains every single time frame
217221
#where a CGU is added,
@@ -224,7 +228,7 @@ def reduce_cgu_per_minute(self,player_id):
224228
self.creep_gen_units_times[player_id] = list(minutes)
225229

226230
def get_creep_spread_area(self,player_id):
227-
## iterates through all cgus and and calculate the area
231+
## iterates through all cgus and and calculate the area
228232
for index,cgu_per_player in enumerate(self.creep_gen_units[player_id]):
229233
# convert cgu list into centre of circles and radius
230234
cgu_radius = map(lambda x: (x[1], self.unit_name_to_radius[x[2]]),\
@@ -234,7 +238,7 @@ def get_creep_spread_area(self,player_id):
234238
creep_area_positions = self.cgu_radius_to_map_positions(cgu_radius,self.radius_to_coordinates)
235239
cgu_event_time = self.creep_gen_units_times[player_id][index]
236240
cgu_event_time_str=str(int(cgu_event_time//60))+":"+str(cgu_event_time%60)
237-
if self.debug:
241+
if self.debug:
238242
self.print_image(creep_area_positions,player_id,cgu_event_time_str)
239243
creep_area = len(creep_area_positions)
240244
self.creep_spread_by_minute[player_id][cgu_event_time]=\
@@ -251,7 +255,7 @@ def cgu_radius_to_map_positions(self,cgu_radius,radius_to_coordinates):
251255
point = cgu[0]
252256
radius = cgu[1]
253257
## subtract all radius_to_coordinates with centre of
254-
## cgu radius to change centre of circle
258+
## cgu radius to change centre of circle
255259
cgu_map_position = map( lambda x:(x[0]+point[0],x[1]+point[1])\
256260
,self.radius_to_coordinates[radius])
257261
total_points_on_map= total_points_on_map | Set(cgu_map_position)

test_replays/test_all.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import unittest
1414

1515
import sc2reader
16+
from sc2reader.exceptions import CorruptTrackerFileError
1617

1718
sc2reader.log_utils.log_to_console("INFO")
1819

@@ -395,8 +396,10 @@ def test_creepTracker(self):
395396
assert replay.player[2].creep_spread_by_minute[780] == 22.42
396397

397398
def test_bad_unit_ids(self):
398-
replay = sc2reader.load_replay("test_replays/2.0.11.26825/bad_unit_ids_1.SC2Replay", load_level=4)
399-
replay = sc2reader.load_replay("test_replays/2.0.9.26147/bad_unit_ids_2.SC2Replay", load_level=4)
399+
with self.assertRaises(CorruptTrackerFileError):
400+
replay = sc2reader.load_replay("test_replays/2.0.11.26825/bad_unit_ids_1.SC2Replay", load_level=4)
401+
with self.assertRaises(CorruptTrackerFileError):
402+
replay = sc2reader.load_replay("test_replays/2.0.9.26147/bad_unit_ids_2.SC2Replay", load_level=4)
400403

401404
def test_daedalus_point(self):
402405
replay = sc2reader.load_replay("test_replays/2.0.11.26825/DaedalusPoint.SC2Replay")

0 commit comments

Comments
 (0)