Exemple #1
0
class Game:
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not Game._instance:
            Game._instance = object.__new__(cls)
        return Game._instance

    def __init__(self):
        self.entity_manager = EntityManager()
        self.message_dispatcher = MessageDispatcher()

    def add_entity(self, entity):
        self.entity_manager.register_entity(entity)
        entity.game = self

    def update(self):
        for entity in self.entity_manager:
            entity.update()
        self.message_dispatcher.dispatch_delayed_messages()
class BaseApp:
    __metaclass__=ABCMeta
    
    def __init__(self, devices):
        self.devices = devices
        self.task_result_queue = Queue.Queue()
        self.task_dispatcher = TaskDispatcher(self.devices, self.task_result_queue)
        self.message_dispatcher = MessageDispatcher()

    def createAndAddTask(self, orders):
        new_task = self.task_dispatcher.createTask(orders)
        if new_task.canProcess():
            return self.task_dispatcher.addTask(new_task)
        raise Exception("The new task can not be executing, because the operations can NOT be found")

    def triggerTask(self, task_name, device_name):
        self.task_dispatcher.triggerTask(task_name, device_name)

    def pumpTaskStatusMessage(self):
        try:
            task_result = self.task_result_queue.get_nowait()
            return task_result
        except Queue.Empty:
            pass
        return None

    def dispatchMessage(self, topic, message):
        if message is None:
            return
        self.message_dispatcher.publishAsync(topic, message)
    
    def installBroadcastMessageHandler(self, callback, front = False):
        self.subscribe(BROADCAST_MSG_TOPICS.TASK_STATUS, 
                       callback, front)
    
    def uninstallBroadcastMessageHandler(self, callback):
        self.unsubscribe(BROADCAST_MSG_TOPICS.TASK_STATUS, 
                         callback)

    def subscribe(self, topic, callback, front = False):
        self.message_dispatcher.subscribe(topic, callback, front)

    def unsubscribe(self, topic, callback):
        self.message_dispatcher.unsubscribe(topic, callback)

    def close(self):
        self.task_dispatcher.close()
        self.message_dispatcher.close()
Exemple #3
0
 def __init__(self, devices):
     self.devices = devices
     self.task_result_queue = Queue.Queue()
     self.task_dispatcher = TaskDispatcher(self.devices, self.task_result_queue)
     self.message_dispatcher = MessageDispatcher()
Exemple #4
0
import time
from sys import stdin

import ids
from barfly import BarFly
from entities import EntityManager
from messages import MessageDispatcher
from miner import Miner
from minerswife import MinersWife

entities = EntityManager()
messages = MessageDispatcher(entities)

entities.register(Miner(messages, ids.MINER))
entities.register(MinersWife(messages, ids.ELSA))
entities.register(BarFly(messages, ids.BAR_FLY))

while True:
    for i in range(10):
        [e.update() for e in entities]
        time.sleep(1)
        messages.dispatch_delayed_messages()
    print("Enter to continue")
    stdin.readline()
Exemple #5
0
 def __init__(self):
     self.entity_manager = EntityManager()
     self.message_dispatcher = MessageDispatcher()