opponent = 2 if player == 1 else 1 board = misc.get_boards(gameID)[opponent - 1] if player is None: misc.fail("Invalid name/hash/gameID") if player == 1 and game.waiting_for != 1 \ or player == 2 and game.waiting_for != 2: misc.fail("It is not your turn") if game.game_state != "G": misc.fail("The game is not in progress") if board[y][x] % misc.SHOT == 0: misc.fail("You've already shot that square") shot_was_hit = board[y][x] % misc.SHIP == 0 if not shot_was_hit: game.waiting_for = opponent board[y][x] *= misc.SHOT if misc.hasLost(board): misc.save_board(game.gameID, board, opponent) misc.save_as_score(games, game) misc.succeed("You win!") else: misc.save_board(game.gameID, board, opponent) misc.save_games(games) misc.succeed("Shot board")
#!/usr/bin/python import cgitb import misc cgitb.enable() games = misc.load_games() games = [game.to_dict() for game in games if game.game_state is "W"] for game in games: del game["player_1_hash"] del game["player_2_hash"] misc.succeed("Fetched {0} games".format(len(games)), games=games)
board_size = int(board_size) except ValueError: misc.fail("Required parameters board_size and num_ships must be integers") if None in {name, hash, num_ships, board_size}: misc.fail("Missing parameters, one of (name, hash, num_ships, board_size") if not 3 <= board_size <= 10 or not 1 <= num_ships < board_size: misc.fail("Invalid parameters, board_size must be between 3 and 10, " "num_ships must be at least 1 and less than board_size") games = misc.load_games() id = games[-1].gameID + 1 if len(games) > 0 else 1 new_game = Game( dict(gameID=id, player_1=name, player_1_hash=hash, player_2="_", player_2_hash="_", game_state="W", waiting_for=3, board_size=board_size, num_ships=num_ships, started_at=0)) games.append(new_game) misc.save_games(games) misc.succeed("Game created", **new_game.to_personalized_dict(1))
if player is None: misc.fail("Invalid name/hash/gameID") board = make_board(game.board_size, ships) board_checker.check(board, game) past_layout_phase = game.game_state != "L" and game.game_state != "W" waiting_for_opponent_only = player == 1 and game.waiting_for == 2 or game.waiting_for == 1 and player == 2 if waiting_for_opponent_only or past_layout_phase: misc.fail("You have already submitted your board") if player == game.waiting_for: misc.save_board(game.gameID, board, player) game.game_state = "G" game.started_at = misc.get_unix_timestamp() game.waiting_for = 1 if game.waiting_for == 3: misc.save_board(game.gameID, board, player) if player == 1: game.waiting_for = 2 elif player == 2: game.waiting_for = 1 misc.save_games(games) misc.succeed("Saved board")
if name != "" and name != None and name != "false" and name != "undefined": scores = [score for score in scores if name.lower() in score.player_1.lower() or name.lower() in score.player_2.lower()] if sort_by == "player_1": scores.sort(key=lambda x: x.player_1) if sort_by == "player_2": scores.sort(key=lambda x: x.player_2) if sort_by == "winner": scores.sort(key=lambda x: x.winner) if sort_by == "player_1_shots": scores.sort(key=lambda x: x.player_1_shots) if sort_by == "player_1_hits": scores.sort(key=lambda x: x.player_1_hits) if sort_by == "player_2_shots": scores.sort(key=lambda x: x.player_2_shots) if sort_by == "player_2_hits": scores.sort(key=lambda x: x.player_2_hits) if sort_by == "time": scores.sort(key=lambda x: x.time) if sort_by == "date": scores.sort(key=lambda x: x.date, reverse=True) if reverse: scores.reverse() for score in scores: score.date = score.date.replace("T", " ") misc.succeed("Got scores", scores=[s.to_dict() for s in scores][0:10])
import misc cgitb.enable() fields = cgi.FieldStorage() name = fields.getvalue("name") hash = fields.getvalue("hash") gameID = fields.getvalue("gameID") if None in {name, hash, gameID}: misc.fail("Missing parameters, one of (name, hash, gameID") games = misc.load_games() found = misc.find_game(games, gameID) if found is None: misc.fail("No game by that ID found") if found.player_1 == name and found.player_1_hash == hash: found.game_state = "X-1" elif found.player_2 == name and found.player_2_hash == hash: found.game_state = "X-2" else: misc.fail("name or hash incorrect for given game") misc.save_games(games) misc.succeed("Left game")
def obscure(board2): return [ [obscureCell(cell) for cell in line] for line in board2 ] cgitb.enable() fields = cgi.FieldStorage() name, hash, gameID = (fields.getvalue("name"), fields.getvalue("hash"), fields.getvalue("gameID")) if None in {name, hash, gameID}: misc.fail("Missing fields, one of (name, hash, gameID)") game = misc.find_game(misc.load_games(), gameID) if game is None: misc.fail("No game found") validated_player_id = misc.validate_player(game, name, hash) try: board1, board2 = misc.get_boards(gameID) your_board = board1 if validated_player_id == 1 else board2 opponent_board = obscure(board2 if validated_player_id == 1 else board1) except: your_board, opponent_board = [[]], [[]] misc.succeed("Game state loaded", game=game.to_personalized_dict(validated_player_id), your_board=your_board, opponent_board=opponent_board, validated_player_id=validated_player_id)
import cgitb import cgi import misc from misc.Game import Game cgitb.enable() fields = cgi.FieldStorage() gameID = fields.getvalue("gameID") hash = misc.make_hash() name = fields.getvalue("name") games = misc.load_games() found = misc.find_game(games, gameID) if None in {gameID, hash, name}: misc.fail("Missing parameter, one of gameID, hash, name") if found is None: misc.fail("No game with that ID found") if found.game_state != "W": misc.fail("That game is no longer open") found.player_2 = name found.player_2_hash = hash found.game_state = "L" misc.save_games(games) misc.succeed("Joined game", **found.to_personalized_dict(2))