Skip to content

Commit 0bcdb3b

Browse files
committed
Fix up sc2replayer and add object.__str__ methods.
Closes #137.
1 parent aeeb635 commit 0bcdb3b

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

sc2reader/objects.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def hash(self):
5353
raw_hash = ','.join(sorted(p.url for p in self.players))
5454
return hashlib.sha256(raw_hash).hexdigest()
5555

56+
def __str__(self):
57+
return "Team {0}".format(self.number)
58+
5659

5760
class Attribute(object):
5861

@@ -251,6 +254,9 @@ def __init__(self, sid, slot_data, uid, init_data, pid):
251254
#: The player id of the observer. Only meaningful in pre 2.0.4 replays
252255
self.pid = pid
253256

257+
def __str__(self):
258+
return "Observer {0} - {1}".format(self.uid, self.name)
259+
254260

255261
class Computer(Entity, Player):
256262
"""
@@ -267,6 +273,9 @@ def __init__(self, sid, slot_data, pid, detail_data, attribute_data):
267273
#: The auto-generated in-game name for this computer player
268274
self.name = detail_data.name
269275

276+
def __str__(self):
277+
return "Player {0} - {1} ({2})".format(self.pid, self.name, self.play_race)
278+
270279

271280
class Participant(Entity, User, Player):
272281
"""
@@ -283,6 +292,9 @@ def __init__(self, sid, slot_data, uid, init_data, pid, detail_data, attribute_d
283292
User.__init__(self, uid, init_data)
284293
Player.__init__(self, pid, detail_data, attribute_data)
285294

295+
def __str__(self):
296+
return "Player {0} - {1} ({2})".format(self.pid, self.name, self.play_race)
297+
286298

287299
class PlayerSummary():
288300
"""

sc2reader/scripts/sc2replayer.py

Lines changed: 60 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,69 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
import sys, os, re
5-
# import termios
6-
# import fcntl
7-
4+
try:
5+
# Assume that we are on *nix or Mac
6+
import termios
7+
import fcntl
8+
import os
9+
import sys
10+
11+
def getch():
12+
fd = sys.stdin.fileno()
13+
oldterm = termios.tcgetattr(fd)
14+
newattr = termios.tcgetattr(fd)
15+
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
16+
termios.tcsetattr(fd, termios.TCSANOW, newattr)
17+
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
18+
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
19+
try:
20+
while 1:
21+
try:
22+
sys.stdin.read(1)
23+
break
24+
except IOError:
25+
pass
26+
finally:
27+
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
28+
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
29+
30+
except ImportError as e:
31+
try:
32+
# Opps, we might be on windows, try this one
33+
from msvcrt import getch
34+
except ImportError as e:
35+
# We can't make getch happen, just dump events to the screen
36+
getch = lambda: True
37+
38+
39+
import argparse
840
import sc2reader
941
from sc2reader.events import *
1042

11-
def myGetch():
12-
sys.stdin.read(1)
13-
14-
# fd = sys.stdin.fileno()
15-
# oldterm = termios.tcgetattr(fd)
16-
# newattr = termios.tcgetattr(fd)
17-
# newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
18-
# termios.tcsetattr(fd, termios.TCSANOW, newattr)
19-
# oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
20-
# fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
21-
# try:
22-
# while 1:
23-
# try:
24-
# c = sys.stdin.read(1)
25-
# break
26-
# except IOError: pass
27-
# finally:
28-
# termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
29-
# fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
30-
31-
def get_args():
32-
import argparse
3343

44+
def main():
3445
parser = argparse.ArgumentParser(
3546
description="""Step by step replay of game events; shows only the
3647
Initialization, Ability, and Selection events by default. Press any
37-
key to advance through the events in sequential order.""",
38-
epilog="And that's all folks")
39-
40-
parser.add_argument('FILE',type=str,
41-
help="The file you would like to replay")
42-
parser.add_argument('--player',default=0, type=int,
43-
help="The number of the player you would like to watch. Defaults to 0 (All).")
44-
parser.add_argument('--bytes',default=False,action="store_true",
45-
help="Displays the byte code of the event in hex after each event.")
46-
parser.add_argument('--hotkeys',default=False,action="store_true",
47-
help="Shows the hotkey events in the event stream.")
48-
parser.add_argument('--cameras',default=False,action="store_true",
49-
help="Shows the camera events in the event stream.")
48+
key to advance through the events in sequential order."""
49+
)
5050

51-
return parser.parse_args()
51+
parser.add_argument('FILE', type=str, help="The file you would like to replay")
52+
parser.add_argument('--player', default=0, type=int, help="The number of the player you would like to watch. Defaults to 0 (All).")
53+
parser.add_argument('--bytes', default=False, action="store_true", help="Displays the byte code of the event in hex after each event.")
54+
parser.add_argument('--hotkeys', default=False, action="store_true", help="Shows the hotkey events in the event stream.")
55+
parser.add_argument('--cameras', default=False, action="store_true", help="Shows the camera events in the event stream.")
56+
args = parser.parse_args()
5257

53-
def main():
54-
args = get_args()
5558
for filename in sc2reader.utils.get_files(args.FILE):
56-
replay = sc2reader.load_replay(filename,debug=True)
59+
replay = sc2reader.load_replay(filename, debug=True)
5760
print "Release {0}".format(replay.release_string)
58-
print "{0} on {1}".format(replay.type,replay.map)
59-
for player in replay.players:
60-
print player
61+
print "{0} on {1} at {2}".format(replay.type, replay.map_name, replay.start_time)
62+
print
63+
for team in replay.teams:
64+
print team
65+
for player in team.players:
66+
print " {0}".format(player)
6167
print "\n--------------------------\n\n"
6268

6369
# Allow picking of the player to 'watch'
@@ -66,39 +72,21 @@ def main():
6672
else:
6773
events = replay.events
6874

75+
# Allow specification of events to `show`
6976
# Loop through the events
70-
#data = sc2reader.config.build_data[replay.build]
7177
for event in events:
72-
try:
73-
pass
74-
#event.apply(data)
75-
except ValueError as e:
76-
if str(e) == "Using invalid abilitiy matchup.":
77-
myGetch()
78-
else:
79-
raise e
80-
81-
# Use their options to filter the event stream
8278

83-
if isinstance(event,AbilityEvent) or\
84-
isinstance(event,SelectionEvent) or\
85-
isinstance(event,PlayerJoinEvent) or\
86-
isinstance(event, PlayerLeaveEvent) or\
87-
isinstance(event,GameStartEvent) or\
88-
(args.hotkeys and isinstance(event,HotkeyEvent)) or\
89-
(args.cameras and isinstance(event,CameraEvent)):
90-
'''
91-
if isinstance(event, SelectionEvent) or isinstance(event, HotkeyEvent):
92-
'''
79+
if isinstance(event, AbilityEvent) or \
80+
isinstance(event, SelectionEvent) or \
81+
isinstance(event, PlayerLeaveEvent) or \
82+
isinstance(event, GameStartEvent) or \
83+
(args.hotkeys and isinstance(event, HotkeyEvent)) or \
84+
(args.cameras and isinstance(event, CameraEvent)):
9385
print event
94-
#myGetch()
86+
getch()
9587
if args.bytes:
9688
print "\t"+event.bytes.encode('hex')
9789

98-
if re.search('UNKNOWN|ERROR', str(event)):
99-
myGetch()
100-
101-
10290

10391
if __name__ == '__main__':
10492
main()

0 commit comments

Comments
 (0)