class BotSideAMPProtocol(amp.AMP): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.game = None @ConnectToServer.responder @defer.inlineCallbacks def connectToServer(self, port): cc = ClientCreator(reactor, TrosnothClientProtocol) client = yield cc.connectTCP(LOCALHOST, port, timeout=10) settings = yield client.getSettings() client.onConnectionLost.addListener(self.lostServerConnection) layoutDatabase = LayoutDatabase() self.game = RemoteGame(layoutDatabase) client.connectNode(self.game) self.game.connected(settings) return [] def lostServerConnection(self, reason=None): if reactor.running: reactor.stop() @StartBot.responder @defer.inlineCallbacks def startBot(self, name, fromLevel, nick, teamId, authTag): assert self.game team = self.game.world.getTeam(teamId) bot = yield self.game.addBot( name, team, fromLevel, nick, forceLocal=True, authTag=authTag) return []
def startGame(self, level=None): db = mapLayout.LayoutDatabase(pathGetter=self.theme.getPath, blocks=self.mapBlocks) gameType = 'solo' self.game = game = LocalGame( db, self.size[0], self.size[1], onceOnly=True, blockRatio=self.blockRatio, duration=self.duration * 60, level=level, gameType=gameType, ) if self.testMode: game.world.setTestMode() self.ais[:] = [] try: for i in range(self.aiCount): if self.stackTeams: ai = game.addBot(self.aiClass, team=game.world.teams[0]) else: ai = game.addBot(self.aiClass) self.ais.append(ai) except ImportError: print('AI module not found: %s' % (self.aiClass, ), file=sys.stderr) sys.exit(1) except AttributeError: print( ('AI module does not define BotClass: %s' % (self.aiClass, )), file=sys.stderr) sys.exit(1) # Create a client and an interface. if self.isolate: rgame = RemoteGame(db) hub = LocalHub(game) self.tweener = UIMsgThrottler() if self.delay: delayer = DelayNodeHub(self.delay) hub.connectNode(delayer) delayer.connectNode(self.tweener) else: hub.connectNode(self.tweener) self.tweener.connectNode(rgame) self.rgame = rgame rgame.connected(game.world.dumpEverything()) self.uiAgent = agent = PandaUIAgent(self, rgame) else: self.tweener = LocalGameTweener(game) self.uiAgent = agent = PandaUIAgent(self, game) agent.onDisconnectRequest.addListener(self.stop) agent.onConnectionLost.addListener(self.stop) agent.show()
def connectToServer(self, port): cc = ClientCreator(reactor, TrosnothClientProtocol) client = yield cc.connectTCP(LOCALHOST, port, timeout=10) settings = yield client.getSettings() client.onConnectionLost.addListener(self.lostServerConnection) layoutDatabase = LayoutDatabase() self.game = RemoteGame(layoutDatabase) client.connectNode(self.game) self.game.connected(settings) return []
def connectionEstablished(self, settings, authTag=0): 'Called when this client establishes a connection to a server.' self.trosnothClient.onConnectionLost.addListener(self.connectionLost) game = RemoteGame(self.app.layoutDatabase) self.app.tweener = UIMsgThrottler() self.trosnothClient.connectNode(self.app.tweener) self.app.tweener.connectNode(game) game.connected(settings) self.setupGame(game, authTag, local=False) self.game = game self.elements = [self.gi]
def connectToReplay(self, filename): self.trosnothClient = player = replays.ReplayPlayer(filename) game = RemoteGame(self.app.layoutDatabase) self.app.tweener = UIMsgThrottler() player.connectNode(self.app.tweener) self.app.tweener.connectNode(game) game.connected(player.popSettings()) self.setupReplay(game) player.start() self.game = game self.elements = [self.gi]
def connectedToGame(self, trosnothClient, settings, authTag): self.trosnothClient = trosnothClient trosnothClient.onConnectionLost.addListener(self._connectionLost) self.game = game = RemoteGame(self.app.layoutDatabase) self.app.tweener = UIMsgThrottler() self.trosnothClient.connectNode(self.app.tweener) self.app.tweener.connectNode(game) game.connected(settings) self.onGameOpened(game, authTag)
def openReplay(self, filename): if self.game is not None: raise RuntimeError('Already displaying a game') self.trosnothClient = replayer = replays.ReplayPlayer(filename) self.game = game = RemoteGame(self.app.layoutDatabase) self.app.tweener = UIMsgThrottler() replayer.connectNode(self.app.tweener) self.app.tweener.connectNode(game) game.connected(replayer.popSettings()) replayer.start() self.onReplayOpened(game)