Exemplo n.º 1
0
    def __init__(self):
        self.controller = SimulatorController()
        self.server_factory = ServerProtocolFactory(self.on_connection, self.on_received)

        reactor.listenTCP(1079, self.server_factory)

        logging.getLogger().setLevel(logging.INFO)
Exemplo n.º 2
0
    def __init__(self):
        self.TIME = 5
        self.controller = BaseController()
        self.server_factory = ServerProtocolFactory(self.on_connection, self.on_received)

        reactor.listenTCP(1079, self.server_factory)

        self.go = False
        self.end = False
        logging.getLogger().setLevel(logging.INFO)
Exemplo n.º 3
0
class SimulatorServer:
    def __init__(self):
        self.controller = SimulatorController()
        self.server_factory = ServerProtocolFactory(self.on_connection, self.on_received)

        reactor.listenTCP(1079, self.server_factory)

        logging.getLogger().setLevel(logging.INFO)

    def start(self):
        reactor.run()

    def on_connection(self, connection):
        print "New Connection", connection.transport.getPeer()
        game_data = self._get_game_data()
        connection.sendString(game_data)

    def on_received(self, data):
        data = pickle.loads(data)
        if isinstance(data, PlayBuildingCard):
            self.controller.play_building_card(data.card.name, data.player_id, data.slot)
        elif isinstance(data, PlaySpellCard):
            self.controller.play_spell_card(data.card.name, data.player_id, data.slot)
        elif isinstance(data, PlayUnitCard):
            self.controller.play_unit_card(data.card.name, data.player_id, data.location)
        elif isinstance(data, Reset):
            self.controller.clear()
        elif isinstance(data, Next):
            self.controller.next()

        self.broadcast_game()

    def broadcast_game(self):
        game_data = self._get_game_data()
        self.server_factory.broadcast(game_data)

    def _get_game_data(self):
        game = self.controller.game
        pickled_game = pickle.dumps(game)
        return pickled_game
Exemplo n.º 4
0
class Server:
    def __init__(self):
        self.TIME = 5
        self.controller = BaseController()
        self.server_factory = ServerProtocolFactory(self.on_connection, self.on_received)

        reactor.listenTCP(1079, self.server_factory)

        self.go = False
        self.end = False
        logging.getLogger().setLevel(logging.INFO)

    def setTime(self, time):
        self.TIME = time

    def start(self):
        reactor.callLater(self.TIME, self.loop)
        reactor.run()

    def loop(self):
        if self.server_factory.getConnectionCount() >= 2:
            if self.go:
                result = self.controller.advance()
                if result != None:
                    self.go = False
                    self.end = True
                    print result
                self.broadcast_game()
            elif not self.end:
                self.go = True
        else:
            if self.go == True:
                print "Active Connections:"
                for conn in self.server_factory.connections:
                    print conn.transport.getPeer()

        reactor.callLater(self.TIME, self.loop)

    def on_connection(self, connection):
        print "New Connection", connection.transport.getPeer()
        game_data = self._get_game_data()
        connection.sendString(game_data)

    def on_received(self, data):
        data = pickle.loads(data)
        if isinstance(data, PlayBuildingCard):
            self.controller.play_building_card(data.card.name, data.player_id, data.slot)
        elif isinstance(data, PlaySpellCard):
            self.controller.play_spell_card(data.card.name, data.player_id, data.slot)
        elif isinstance(data, PlayUnitCard):
            self.controller.play_unit_card(data.card.name, data.player_id, data.location)
        elif isinstance(data, BuyCard):
            self.controller.buy_card(data.player_id, data.card_name)

        self.broadcast_game()

    def broadcast_game(self):
        game_data = self._get_game_data()
        self.server_factory.broadcast(game_data)

    def _get_game_data(self):
        game = self.controller.game
        pickled_game = pickle.dumps(game)
        return pickled_game