Skip to content

Commit 2286f11

Browse files
committed
additional formatting fix
1 parent 1ffd9a2 commit 2286f11

5 files changed

Lines changed: 215 additions & 213 deletions

File tree

example_live_parser.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
from time import sleep
21
import traceback
2+
from time import sleep
33

44
from hslog.live.parser import LiveLogParser
55

6+
67
"""
7-
----------------------------------------------------------------------
8-
LiveLogParser assumes that you"ve configured Power.log to be a symlink
8+
----------------------------------------------------------------------
9+
LiveLogParser assumes that you"ve configured Power.log to be a symlink
910
10-
in "SOME_PATH/Hearthstone/Logs" folder:
11-
ln -s Power.log /tmp/hearthstone-redirected.log
11+
in "SOME_PATH/Hearthstone/Logs" folder:
12+
ln -s Power.log /tmp/hearthstone-redirected.log
1213
13-
this will redirect all data coming into Power.log
14-
so we can access it from a RAM disk
15-
----------------------------------------------------------------------
16-
For better performance make /tmp of type tmpfs (or another location)
14+
this will redirect all data coming into Power.log
15+
so we can access it from a RAM disk
16+
----------------------------------------------------------------------
17+
For better performance make /tmp of type tmpfs (or another location)
1718
18-
in /etc/fstab add line:
19-
tmpfs /tmp tmpfs nodev,nosuid,size=1G 0 0
19+
in /etc/fstab add line:
20+
tmpfs /tmp tmpfs nodev,nosuid,size=1G 0 0
2021
21-
this will create in-memory storage which is faster then SSD
22-
you need to restart the computer for this to take effect
23-
----------------------------------------------------------------------
22+
this will create in-memory storage which is faster then SSD
23+
you need to restart the computer for this to take effect
24+
----------------------------------------------------------------------
2425
"""
2526

2627

2728
def main():
28-
try:
29-
file = "/tmp/hearthstone-redirected.log"
30-
liveParser = LiveLogParser(file)
31-
liveParser.start()
29+
try:
30+
file = "/tmp/hearthstone-redirected.log"
31+
liveParser = LiveLogParser(file)
32+
liveParser.start()
3233

33-
while True:
34-
sleep(1)
34+
while True:
35+
sleep(1)
3536

36-
except Exception as e:
37-
print(traceback.format_exc())
38-
liveParser.stop()
37+
except Exception as e:
38+
print(traceback.format_exc())
39+
liveParser.stop()
3940

4041

4142
if __name__ == "__main__":
42-
main()
43+
main()

hslog/live/entities.py

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,70 @@
11
from hearthstone.entities import Card, Entity
22
from hearthstone.enums import GameTag
33

4-
"""
5-
* Card is called on export from game
6-
* LiveCard replaces Card and inserts update_callback
7-
* The point is to become able to route update events towards an API end-point
8-
"""
9-
104

115
class LiveEntity(Entity):
126

13-
def __init__(self, entity_id, parent, **kwargs):
14-
""" Entity requires an ID, store everything else in kwargs """
15-
self.parent = parent
16-
self.game_index = self.parent.parser.games.index(self.parent)
17-
super(LiveEntity, self).__init__(entity_id, **kwargs)
7+
def __init__(self, entity_id, parent, **kwargs):
8+
""" Entity requires an ID, store everything else in kwargs """
9+
self.parent = parent
10+
self.game_index = self.parent.parser.games.index(self.parent)
11+
super(LiveEntity, self).__init__(entity_id, **kwargs)
12+
13+
# push data to an end-point
14+
print(f"GAME {self.game_index} --- ENTITY CREATED:", self)
1815

19-
# push data to an end-point
20-
print(f"GAME {self.game_index} --- ENTITY CREATED:", self)
16+
def tag_change(self, tag, value):
17+
if tag == GameTag.CONTROLLER and not self._initial_controller:
18+
self._initial_controller = self.tags.get(GameTag.CONTROLLER, value)
19+
self.tags[tag] = value
2120

22-
def tag_change(self, tag, value):
23-
if tag == GameTag.CONTROLLER and not self._initial_controller:
24-
self._initial_controller = self.tags.get(GameTag.CONTROLLER, value)
25-
self.tags[tag] = value
21+
# update notify
22+
self.update_callback()
2623

27-
# update notify
28-
self.update_callback()
24+
def update_callback(self):
25+
# push data to an end-point
26+
print(f"GAME {self.game_index} --- ENTITY UPDATED:", self)
2927

30-
def update_callback(self):
31-
# push data to an end-point
32-
print(f"GAME {self.game_index} --- ENTITY UPDATED:", self)
28+
29+
"""
30+
* Card is called on export from game
31+
* LiveCard replaces Card and inserts update_callback
32+
* The point is to become able to route update events towards an API end-point
33+
"""
3334

3435

3536
class LiveCard(Card, LiveEntity):
3637

37-
def __init__(self, entity_id, card_id, parent):
38-
super(LiveCard, self).__init__(
39-
entity_id=entity_id,
40-
card_id=card_id,
41-
parent=parent)
42-
43-
""" if card_id doesn"t change, there"s no need to pass it as the argument.
44-
we can use self.card_id instead as it is set by Card class """
45-
def reveal(self, card_id, tags):
46-
self.revealed = True
47-
self.card_id = card_id
48-
if self.initial_card_id is None:
49-
self.initial_card_id = card_id
50-
self.tags.update(tags)
51-
52-
# update notify
53-
self.update_callback()
54-
55-
def hide(self):
56-
self.revealed = False
57-
58-
# update notify
59-
self.update_callback()
60-
61-
""" same comment as for reveal """
62-
def change(self, card_id, tags):
63-
if self.initial_card_id is None:
64-
self.initial_card_id = card_id
65-
self.card_id = card_id
66-
self.tags.update(tags)
67-
68-
# update notify
69-
self.update_callback()
38+
def __init__(self, entity_id, card_id, parent):
39+
super(LiveCard, self).__init__(
40+
entity_id=entity_id,
41+
card_id=card_id,
42+
parent=parent)
43+
44+
""" if card_id doesn"t change, there"s no need to pass it as the argument.
45+
we can use self.card_id instead as it is set by Card class """
46+
def reveal(self, card_id, tags):
47+
self.revealed = True
48+
self.card_id = card_id
49+
if self.initial_card_id is None:
50+
self.initial_card_id = card_id
51+
self.tags.update(tags)
52+
53+
# update notify
54+
self.update_callback()
55+
56+
def hide(self):
57+
self.revealed = False
58+
59+
# update notify
60+
self.update_callback()
61+
62+
""" same comment as for reveal """
63+
def change(self, card_id, tags):
64+
if self.initial_card_id is None:
65+
self.initial_card_id = card_id
66+
self.card_id = card_id
67+
self.tags.update(tags)
68+
69+
# update notify
70+
self.update_callback()

hslog/live/export.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33

44

55
class LiveEntityTreeExporter(EntityTreeExporter):
6-
card_class = LiveCard
6+
card_class = LiveCard
77

8-
def __init__(self, packet_tree):
9-
super(LiveEntityTreeExporter, self).__init__(packet_tree)
8+
def __init__(self, packet_tree):
9+
super(LiveEntityTreeExporter, self).__init__(packet_tree)
1010

11-
def handle_full_entity(self, packet):
12-
entity_id = packet.entity
11+
def handle_full_entity(self, packet):
12+
entity_id = packet.entity
1313

14-
# Check if the entity already exists in the game first.
15-
# This prevents creating it twice.
16-
# This can legitimately happen in case of GAME_RESET
17-
if entity_id <= len(self.game.entities):
18-
# That first if check is an optimization to prevent always looping over all of
19-
# the game"s entities every single FULL_ENTITY packet...
20-
# FIXME: Switching to a dict for game.entities would simplify this.
21-
existing_entity = self.game.find_entity_by_id(entity_id)
22-
if existing_entity is not None:
23-
existing_entity.card_id = packet.card_id
24-
existing_entity.tags = dict(packet.tags)
25-
return existing_entity
14+
# Check if the entity already exists in the game first.
15+
# This prevents creating it twice.
16+
# This can legitimately happen in case of GAME_RESET
17+
if entity_id <= len(self.game.entities):
18+
# That first if check is an optimization to prevent always looping over all of
19+
# the game"s entities every single FULL_ENTITY packet...
20+
# FIXME: Switching to a dict for game.entities would simplify this.
21+
existing_entity = self.game.find_entity_by_id(entity_id)
22+
if existing_entity is not None:
23+
existing_entity.card_id = packet.card_id
24+
existing_entity.tags = dict(packet.tags)
25+
return existing_entity
2626

27-
entity = self.card_class(entity_id, packet.card_id, self.packet_tree)
28-
entity.tags = dict(packet.tags)
29-
self.game.register_entity(entity)
30-
return entity
27+
entity = self.card_class(entity_id, packet.card_id, self.packet_tree)
28+
entity.tags = dict(packet.tags)
29+
self.game.register_entity(entity)
30+
return entity

hslog/live/packets.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class LivePacketTree(PacketTree):
66

7-
def __init__(self, ts, parser):
8-
self.parser = parser
9-
self.liveExporter = LiveEntityTreeExporter(self)
10-
super(LivePacketTree, self).__init__(ts)
7+
def __init__(self, ts, parser):
8+
self.parser = parser
9+
self.liveExporter = LiveEntityTreeExporter(self)
10+
super(LivePacketTree, self).__init__(ts)
1111

12-
def live_export(self, packet):
13-
return self.liveExporter.export_packet(packet)
12+
def live_export(self, packet):
13+
return self.liveExporter.export_packet(packet)

0 commit comments

Comments
 (0)