Skip to content

Commit

Permalink
additional formatting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dduric committed May 6, 2018
1 parent 1ffd9a2 commit c55b4ec
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 213 deletions.
49 changes: 25 additions & 24 deletions example_live_parser.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
from time import sleep
import traceback
from time import sleep

from hslog.live.parser import LiveLogParser


"""
----------------------------------------------------------------------
LiveLogParser assumes that you"ve configured Power.log to be a symlink
----------------------------------------------------------------------
LiveLogParser assumes that you"ve configured Power.log to be a symlink
in "SOME_PATH/Hearthstone/Logs" folder:
ln -s Power.log /tmp/hearthstone-redirected.log
in "SOME_PATH/Hearthstone/Logs" folder:
ln -s Power.log /tmp/hearthstone-redirected.log
this will redirect all data coming into Power.log
so we can access it from a RAM disk
----------------------------------------------------------------------
For better performance make /tmp of type tmpfs (or another location)
this will redirect all data coming into Power.log
so we can access it from a RAM disk
----------------------------------------------------------------------
For better performance make /tmp of type tmpfs (or another location)
in /etc/fstab add line:
tmpfs /tmp tmpfs nodev,nosuid,size=1G 0 0
in /etc/fstab add line:
tmpfs /tmp tmpfs nodev,nosuid,size=1G 0 0
this will create in-memory storage which is faster then SSD
you need to restart the computer for this to take effect
----------------------------------------------------------------------
this will create in-memory storage which is faster then SSD
you need to restart the computer for this to take effect
----------------------------------------------------------------------
"""


def main():
try:
file = "/tmp/hearthstone-redirected.log"
liveParser = LiveLogParser(file)
liveParser.start()
try:
file = "/tmp/hearthstone-redirected.log"
liveParser = LiveLogParser(file)
liveParser.start()

while True:
sleep(1)
while True:
sleep(1)

except Exception as e:
print(traceback.format_exc())
liveParser.stop()
except Exception as e:
print(traceback.format_exc())
liveParser.stop()


if __name__ == "__main__":
main()
main()
111 changes: 56 additions & 55 deletions hslog/live/entities.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
from hearthstone.entities import Card, Entity
from hearthstone.enums import GameTag

"""
* Card is called on export from game
* LiveCard replaces Card and inserts update_callback
* The point is to become able to route update events towards an API end-point
"""


class LiveEntity(Entity):

def __init__(self, entity_id, parent, **kwargs):
""" Entity requires an ID, store everything else in kwargs """
self.parent = parent
self.game_index = self.parent.parser.games.index(self.parent)
super(LiveEntity, self).__init__(entity_id, **kwargs)
def __init__(self, entity_id, parent, **kwargs):
""" Entity requires an ID, store everything else in kwargs """
self.parent = parent
self.game_index = self.parent.parser.games.index(self.parent)
super(LiveEntity, self).__init__(entity_id, **kwargs)

# push data to an end-point
print(f"GAME {self.game_index} --- ENTITY CREATED:", self)

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

def tag_change(self, tag, value):
if tag == GameTag.CONTROLLER and not self._initial_controller:
self._initial_controller = self.tags.get(GameTag.CONTROLLER, value)
self.tags[tag] = value
# update notify
self.update_callback()

# update notify
self.update_callback()
def update_callback(self):
# push data to an end-point
print(f"GAME {self.game_index} --- ENTITY UPDATED:", self)

def update_callback(self):
# push data to an end-point
print(f"GAME {self.game_index} --- ENTITY UPDATED:", self)

"""
* Card is called on export from game
* LiveCard replaces Card and inserts update_callback
* The point is to become able to route update events towards an API end-point
"""


class LiveCard(Card, LiveEntity):

def __init__(self, entity_id, card_id, parent):
super(LiveCard, self).__init__(
entity_id=entity_id,
card_id=card_id,
parent=parent)

""" if card_id doesn"t change, there"s no need to pass it as the argument.
we can use self.card_id instead as it is set by Card class """
def reveal(self, card_id, tags):
self.revealed = True
self.card_id = card_id
if self.initial_card_id is None:
self.initial_card_id = card_id
self.tags.update(tags)

# update notify
self.update_callback()

def hide(self):
self.revealed = False

# update notify
self.update_callback()

""" same comment as for reveal """
def change(self, card_id, tags):
if self.initial_card_id is None:
self.initial_card_id = card_id
self.card_id = card_id
self.tags.update(tags)

# update notify
self.update_callback()
def __init__(self, entity_id, card_id, parent):
super(LiveCard, self).__init__(
entity_id=entity_id,
card_id=card_id,
parent=parent)

""" if card_id doesn"t change, there"s no need to pass it as the argument.
we can use self.card_id instead as it is set by Card class """
def reveal(self, card_id, tags):
self.revealed = True
self.card_id = card_id
if self.initial_card_id is None:
self.initial_card_id = card_id
self.tags.update(tags)

# update notify
self.update_callback()

def hide(self):
self.revealed = False

# update notify
self.update_callback()

""" same comment as for reveal """
def change(self, card_id, tags):
if self.initial_card_id is None:
self.initial_card_id = card_id
self.card_id = card_id
self.tags.update(tags)

# update notify
self.update_callback()
42 changes: 21 additions & 21 deletions hslog/live/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@


class LiveEntityTreeExporter(EntityTreeExporter):
card_class = LiveCard
card_class = LiveCard

def __init__(self, packet_tree):
super(LiveEntityTreeExporter, self).__init__(packet_tree)
def __init__(self, packet_tree):
super(LiveEntityTreeExporter, self).__init__(packet_tree)

def handle_full_entity(self, packet):
entity_id = packet.entity
def handle_full_entity(self, packet):
entity_id = packet.entity

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

entity = self.card_class(entity_id, packet.card_id, self.packet_tree)
entity.tags = dict(packet.tags)
self.game.register_entity(entity)
return entity
entity = self.card_class(entity_id, packet.card_id, self.packet_tree)
entity.tags = dict(packet.tags)
self.game.register_entity(entity)
return entity
12 changes: 6 additions & 6 deletions hslog/live/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class LivePacketTree(PacketTree):

def __init__(self, ts, parser):
self.parser = parser
self.liveExporter = LiveEntityTreeExporter(self)
super(LivePacketTree, self).__init__(ts)
def __init__(self, ts, parser):
self.parser = parser
self.liveExporter = LiveEntityTreeExporter(self)
super(LivePacketTree, self).__init__(ts)

def live_export(self, packet):
return self.liveExporter.export_packet(packet)
def live_export(self, packet):
return self.liveExporter.export_packet(packet)
Loading

0 comments on commit c55b4ec

Please sign in to comment.