def joinGame(): content = request.json print(content) email = content['email'] gameID = content['gameID'] foundGame = GameService.get_game_by_id(gameID) if foundGame is None: return {"Success": False, "Message": "Could not find game"} game = chessGame() game.gameID = gameID board = foundGame['chessBoard'] game.chessBoard = chessBoard(board['size']) game.chessBoard.piecesWhite = board['piecesWhite'] game.chessBoard.piecesBlack = board['piecesBlack'] game.chessBoard.whoseTurn = 'White' game.chessBoard.gameID = gameID game.chessBoard.whiteQueen = 5 game.chessBoard.blackQueen = 5 game.blackPlayer = foundGame['blackPlayer'] game.completed = False game.winner = '' if not game.playerJoin(email): return {"success": False, "message": "Could not join game"} success, message = GameService.update_game(gameID, game.todict()) return {"success": success, "message": message}
def createGame(): content = request.json print(content) # REMOVE email = content['email'] color = content['color'] size = content['size'] newGame = chessGame(email, color, size) success, message = GameService.create_new_game(newGame.todict()) return {"success": success, "message": message, "gameID": newGame.gameID}
def resign(): content = request.json print(content) gameID = content['gameID'] email = content['email'] foundGame = GameService.get_game_by_id(gameID) if foundGame is None: return {"Success": False, "Message": "Could not find game"} if foundGame.playerResign(email): return {"Success": True, "Message": "Player Resigned"} return {"Success": False, "Message": "Could not resign game"}
def playerDashboard(): content = request.json print(content) email = content['email'] foundPlayer = PlayerService.read_player(email) del foundPlayer['_id'] if foundPlayer is None: newPlayer = Player(email, email) PlayerService.add_player(newPlayer.todict()) return {"success": True, "message": "created new player", "player": newPlayer, "openGames": []} allGames = GameService.read_all_open_games_for_player(foundPlayer['email']) return {"success": True, "message": "Found player", "player": foundPlayer, "openGames": allGames}
def makeMove(): content = request.json print(content) gameID = content['gameID'] email = content['email'] pieceID = content['pieceID'] location = content['location'] foundGame = GameService.get_game_by_id(gameID) if foundGame is None: return {"Success": False, "Message": "Could not find game"} out = foundGame.makeMove(email, pieceID, location) return {"Success": out[0], "Message": out[1]}
def updateServer(self, source, target, action, data): try: # Make socket transport = TSocket.TSocket(self.gameServer, self.gameServerPort) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = GameService.Client(protocol) # Connect! transport.open() result = client.update(source, target, action, data) logging.info(str(result)) # Close! transport.close() except Thrift.TException, tx: logging.info(tx.message)
def getGameBoardState(): content = request.json print(content) gameID = content['gameID'] email = content['email'] foundGame = GameService.get_game_by_id(gameID) if foundGame is None: return {"Success": False, "Message": "Could not find game"} out = foundGame.getGameBoard(email) if out == -1: return {"Success": False, "Message": "Could not find player"} return { "Success": True, "Message": "Got Board State", "Board": out[0], "Size": out[1], "Turn": out[2], "Player": out[3] }
transport = TSocket.TSocket(ip, port) logging.info("creating protocol") transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) logging.info("creating client") client = GamePlayer.Client(protocol) return (transport, client) if __name__ == '__main__': homedir = os.environ['HOME'] logging.basicConfig(filename=homedir + '/Server.log',level=logging.DEBUG) logging.getLogger().addHandler(logging.StreamHandler()) logging.info("Main server method") handler = GameServerHandler() handler.port = 9090 processor = GameService.Processor(handler) if len(sys.argv) > 1: handler.hostName = sys.argv[1] logging.info("My hostname is " + handler.hostName) transport = TSocket.TServerSocket(handler.hostName, handler.port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) handler.updatePlayerList() logging.info("Created handler") logging.info('Starting the server...')
from Services import GamePlayer from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer import socket #ipTable = {'voyager':'192.168.0.34', 'viking':'192.168.0.42'} if __name__ == "__main__": ip = sys.argv[1] #ip = ipTable[ip] port = 9090 source = int(sys.argv[2]) target = int(sys.argv[3]) action = int(sys.argv[4]) data = int(sys.argv[5]) # Make socket transport = TSocket.TSocket(ip, port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = GameService.Client(protocol) transport.open() client.update(source, target, action, data) transport.close()
def random_pick(update, context): update.message.reply_text(GameService.random_pick(context))
def roll_dice(update, context): update.message.reply_text(GameService.roll_dice(context))
def russian_roulette(update, context): update.message.reply_text(GameService.russian_roulette())
def should_i(update, context): update.message.reply_text(GameService.should_I(context))