Example #1
0
 def gameStart(self):
     self.displayText.setText("Starting game...")
     if CLIENTS.__len__() > 1:
         ranValPkg = PyDatagram()
         ranValPkg.addUint16(GAME_INITIALIZE)
         ranValPkg.addUint32(self.playerCount)
         for client in CLIENTS:
             x = random.randint(1, 5)
             y = random.randint(1, 5)
             self.gameEngine.players.append(
                 Player(x, y, 20, CLIENTS_ID[client]))
             self.gameEngine.world.attachCharacter(self.gameEngine.players[
                 CLIENTS_ID[client]].playerNP.node())
             ranValPkg.addUint32(CLIENTS_ID[client])
             ranValPkg.addFloat32(x)
             ranValPkg.addFloat32(y)
         for client in CLIENTS:
             temp = ranValPkg.__copy__()
             temp.addUint32(CLIENTS_ID[client])
             self.cWriter.send(temp, client)
         taskMgr.add(self.update, 'update')
     else:
         self.broadcastMsg("/info no_clients")
         GameUI.createWhiteBgUI("Not enough clients connected.")
     self.displayText.destroy()
Example #2
0
 def __init__(self, setup, hands):
     """
     Properties
     """
     self._setup = setup  ### Game setup data
     self._hands = hands  ### List of all played hands in the game
     self._ui = GameUI()  ### Interface object to display game history
Example #3
0
 def __init__(self):
     """
     Properties
     """
     self._blinds_maxed_out = False  ### Are the blinds at the highest value?
     self._board_name_idx = 0  ### List address for the name of the board
     self._game_data = GameData()  ### Poker Data Interface Object
     self._game_play_code = GameCode.NEW_GAME  ### What kind of game are we playing or playing back (new/load)?
     self._game_save = None  ### Object that manages saving a game finished or in progress
     self._game_theater = None  ### Plays back a finished game
     self._hand_actions = list(
     )  ### List of actions that were made in a hand
     self._ui = GameUI()  ### User Interface Object
     self._num_remaining_players = 0  ### Number of remaining players in the game
Example #4
0
    def __init__(self, network):
        self.gameEngine = ClientGameEngine()
        self.network = network
        # self.accept("escape", self.sendMsgDisconnectReq)

        self.gameStart = False
        self.my_clock = 0
        self.player_count = 0
        self.heading = 0
        self.pitch = 40
        self.skip = 0
        self.loss = 0
        self.id = 0
        self.win = False
        self.lose = False

        inputState.watchWithModifiers('forward', 'w')
        inputState.watchWithModifiers('left', 'a')
        inputState.watchWithModifiers('reverse', 's')
        inputState.watchWithModifiers('right', 'd')
        inputState.watchWithModifiers('shoot', 'mouse1')

        # GameUI
        self.healthUI = None
        self.displayUI = GameUI.createDisplayUI("")

        self.serverWait = True
Example #5
0
	def __init__(self):
		self.game_over = False
		self.__game_controller = GameController()
		self.game_vars = self.__game_controller.start_game()
		self.available_columns = self.game_vars['available_columns']
		self.current_player = self.game_vars['current_player']
		self.__gui = GameUI()
		self.__gui.display(self.game_vars)
Example #6
0
class GameView:
	def __init__(self):
		self.game_over = False
		self.__game_controller = GameController()
		self.game_vars = self.__game_controller.start_game()
		self.available_columns = self.game_vars['available_columns']
		self.current_player = self.game_vars['current_player']
		self.__gui = GameUI()
		self.__gui.display(self.game_vars)

	def make_move(self,move):
		self.game_vars = self.__game_controller.player_move(move)
		self.number_of_moves = self.game_vars['number_of_moves']
		self.current_player = self.game_vars['current_player']
		if self.game_vars['message']:
			self.game_over = True
		self.__gui.display(self.game_vars)
Example #7
0
 def gameInitialize(self, msgID, data):
     self.displayUI.destroy()
     playerCount = data.getUint32()
     for i in range(0, playerCount):
         playerId = data.getUint32()
         x = data.getFloat32()
         y = data.getFloat32()
         self.gameEngine.players.append(Player(x, y, 20, playerId))
         self.gameEngine.world.attachCharacter(self.gameEngine.players[playerId].playerNP.node())
     self.gameEngine.showPointer()
     self.id = data.getUint32()
     self.healthUI = GameUI.createWhiteBgUI("")
     self.serverWait = False
     taskMgr.add(self.update, 'update')
Example #8
0
    def __init__(self):
        DirectObject.__init__(self)
        self.gameEngine = GameEngine()

        # If you press Escape @ the server window, the server will quit.
        self.accept("escape", self.quit)
        self.lastConnection = None
        self.serverClock = 0
        self.lobbyWaitTime = 6
        self.randomValue = {}

        self.playerCount = 0
        self.listenStat = 1
        self.listPkg = PyDatagram()
        self.clientInputList = PyDatagram()
        self.clientInputList.addUint16(SERVER_INPUT)
        self.clientInputList.addUint64(self.serverClock)
        self.clientsAlive = {}

        self.displayText = GameUI.createDisplayUI("Loading...")
        # Create network layer objects

        # Deals with the basic network stuff
        self.cManager = QueuedConnectionManager()

        # Listens for new connections and queue's them
        self.cListener = QueuedConnectionListener(self.cManager, 0)

        # Reads data send to the server
        self.cReader = QueuedConnectionReader(self.cManager, 0)

        # Writes / sends data to the client
        self.cWriter = ConnectionWriter(self.cManager, 0)

        # open a server socket on the given port. Args: (port,timeout)
        self.tcpSocket = self.cManager.openTCPServerRendezvous(PORT, 1)

        # Tell the listener to listen for new connections on this socket
        self.cListener.addConnection(self.tcpSocket)

        # Start Listener task
        taskMgr.add(self.listenTask, "serverListenTask", -40)

        # Start Read task

        taskMgr.add(self.readTask, "serverReadTask", -39)
Example #9
0
    def __init__(self, network):
        self.gameEngine = GameEngine()
        self.network = network

        self.lastConnection = None
        self.serverClock = 0
        self.count_down_time = 0

        self.client_received_list = []
        self.listPkg = PyDatagram()
        self.clientInputList = PyDatagram()
        self.clientInputList.addUint16(SERVER_INPUT)
        self.clientInputList.addUint64(self.serverClock)
        self.clientsAlive = []
        self.clientsDead = []
        self.display_text = GameUI.createDisplayUI("Loading...")
        self.count = 0
Example #10
0
import time
import keyboard
import logging
import os
import pytesseract
from fuzzywuzzy import fuzz
from playerstate import PlayerState
from gameui import GameUI

from utils import do_mousepoll, get_dock_uielement, get_route_root_uielement, get_route_blocks, read_num_jumps, DEFAULT_DOCKED_ROUTE_BLOCKS_REGION, get_box_center, read_warp_status

SCRIPT_START = time.time()

draw_buffer = []

GAMEUI = GameUI()
PLAYERSTATE = PlayerState(game_ui=GAMEUI)
LAST_FRAME_TIME = time.time()


def get_logger():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    consolechannel = logging.StreamHandler()
    formatter = logging.Formatter('[%(levelname)s]: %(message)s')
    consolechannel.setFormatter(formatter)
    logger.addHandler(consolechannel)
    # logger.info('THIS IS AN INFO LOG')
    return logger

Example #11
0
File: ui.py Project: valgma/ds_hw2
 def draw_game(self):
     self.game_frame = GameUI(self, self.connector)
     self.game_frame.show()
Example #12
0
File: ui.py Project: valgma/ds_hw2
class ClientApplication(tk.Frame):
    """
    @param host - the rabbitmq server
    """
    def __init__(self, host='localhost'):
        tk.Frame.__init__(self, None)
        self.pikahost = host
        self.servers = []
        self.username = ""
        self.game_frame = None

        self.create_widgets()

        self.connector = ClientConnector(host, self)
        self.connector.setDaemon(True)
        self.connector.start()

        self.show_server_selection()

    """
    Initializing widgets
    """

    def create_widgets(self):
        # ---------- Client tab widgets ----------
        self.create_server_selection()
        self.create_lobby()
        self.pack_server_selection()
        self.hide_server_selection()
        self.pack_lobby()
        self.hide_lobby()
        self.pack(fill=tk.BOTH, expand=1)

    """
    Initializing the widgets of the server selection window
    """

    def create_server_selection(self):
        self.server_selection_frame = tk.Frame(self)
        self.username_label = tk.Label(self.server_selection_frame,
                                       text='Username')
        self.username_entry = tk.Entry(self.server_selection_frame)
        self.server_box_label = tk.Label(self.server_selection_frame,
                                         text="Available servers:")
        self.server_box = tk.Listbox(self.server_selection_frame)
        self.server_button = tk.Button(self.server_selection_frame,
                                       text="Connect",
                                       command=self.pick_server,
                                       bg='SeaGreen2')

    """
    Packing the server selection window
    """

    def pack_server_selection(self):
        self.server_selection_frame.pack(fill=tk.BOTH, expand=1)
        self.server_button.pack(fill=tk.X)
        self.username_label.pack()
        self.username_entry.pack(fill=tk.X)
        self.server_box_label.pack()
        self.server_box.pack(fill=tk.BOTH, expand=1)

    """
    Making the server selection window visible and populating it with servers.
    """

    def show_server_selection(self):
        self.pack_server_selection()
        self.connector.ping_servers()

    """
    Initializing the lobby widgets
    """

    def create_lobby(self):
        self.lobbyframe = tk.Frame(self)
        self.lobby_listframe = tk.Frame(self.lobbyframe)
        self.make_client_list(self.lobby_listframe)
        self.gamesframe = tk.Frame(self.lobbyframe)
        self.game_buttonframe = tk.Frame(self.gamesframe)
        self.lobby_joinbutton = tk.Button(self.game_buttonframe,
                                          text="Join Game",
                                          command=self.join_game)
        self.lobby_hostbutton = tk.Button(self.game_buttonframe,
                                          text="Create Game",
                                          command=self.host_game)
        self.game_name_label = tk.Label(self.game_buttonframe,
                                        text='Game name:')
        self.game_name_entry = tk.Entry(self.game_buttonframe)
        self.game_size_label = tk.Label(self.game_buttonframe,
                                        text='Game size:')
        self.game_size_entry = tk.Entry(self.game_buttonframe)
        self.game_size_entry.insert(0, '10')
        self.lobby_roomlist = tk.Listbox(self.gamesframe)

    """
    Packing the lobby widgets
    """

    def pack_lobby(self):
        self.lobby_roomlist.delete(0, tk.END)
        self.lobbyframe.pack(fill=tk.BOTH, side=tk.LEFT, expand=1)
        self.lobby_listframe.pack(fill=tk.Y, side=tk.LEFT)
        self.gamesframe.pack(fill=tk.BOTH, expand=1, side=tk.LEFT, anchor=tk.N)
        self.game_buttonframe.pack(fill=tk.X)
        self.lobby_joinbutton.pack(fill=tk.X, side=tk.LEFT, expand=1)
        self.lobby_hostbutton.pack(fill=tk.X, side=tk.LEFT, expand=1)
        self.game_name_label.pack(fill=tk.X, side=tk.LEFT, expand=1)
        self.game_name_entry.pack(fill=tk.X, side=tk.LEFT, expand=1)
        self.game_size_label.pack(fill=tk.X, side=tk.LEFT, expand=1)
        self.game_size_entry.pack(fill=tk.X, side=tk.LEFT, expand=1)
        self.lobby_roomlist.pack(fill=tk.BOTH, expand=1)

    """
    Making the lobby visible and populating the room- and playerlist.
    """

    def show_lobby(self):
        self.pack_lobby()
        self.connector.request_playerlist()
        self.connector.request_roomlist()

    """
    Initializing the client list (left) side of the lobby
    """

    def make_client_list(self, master):
        self.lobby_buttonarea = tk.Frame(master)
        self.lobby_backbutton = tk.Button(self.lobby_buttonarea,
                                          text='Leave server',
                                          command=self.lobby_back,
                                          bg='tomato')
        self.client_list_label = tk.Label(self.lobby_buttonarea,
                                          text='Lobby:',
                                          pady=5)
        self.client_list = tk.Listbox(master)
        self.lobby_buttonarea.pack(fill=tk.X)
        self.lobby_backbutton.pack(side=tk.LEFT, anchor=tk.NW)
        self.client_list_label.pack(side=tk.LEFT, fill=tk.X)
        self.client_list.pack(fill=tk.Y, expand=1, anchor=tk.NW)

    """
    The actions that occurs when the back button is pressed in the lobby
    """

    def lobby_back(self):
        self.client_list.delete(0, tk.END)
        self.hide_lobby()
        self.show_server_selection()
        self.connector.leave_server()

    """
    Unpacking the lobby
    """

    def hide_lobby(self):
        self.lobbyframe.pack_forget()

    """
    Unpacking the server selection
    """

    def hide_server_selection(self):
        self.server_selection_frame.pack_forget()

    """
    Initializing the UI of the game
    """

    def draw_game(self):
        self.game_frame = GameUI(self, self.connector)
        self.game_frame.show()

    """
    Destroying the UI of the game.
    """

    def abandon_game(self):
        self.destroy_game()
        self.show_lobby()

    """
    The action called when the player attempts to join a game
    """

    def join_game(self):
        try:
            selection = self.lobby_roomlist.curselection()[0]
            self.connector.join_room(self.lobby_roomlist.get(selection))
        except IndexError:
            return

    """
    The action called when the player attempts to host a game
    """

    def host_game(self):
        name = self.game_name_entry.get()
        size = self.game_size_entry.get()
        try:
            if name and size and "/" not in name and int(
                    size):  #TODO kontrolli, et numbriline
                self.connector.request_room(name, size)
        except:
            return

    """
    Flashes the name box orange, though now we also display errors.
    """

    def flash_name(self):
        self.username_entry.configure(bg='orange')
        Timer(0.4, self.clear_flash).start()

    """
    Removing the above flash
    """

    def clear_flash(self):
        self.username_entry.configure(bg='white')

    """
    adding/removing entries from the server box
    @param serv_name - the server name to be added
    @param add - whether we add or remove
    """

    def update_server_box(self, serv_name, add):
        self.update_listbox(self.server_box, serv_name, add)
        if add and self.server_box.size() == 1:
            self.server_box.select_set(0)

    """
    adding/removing entries from the client box
    @param client_name - the name of the client to add
    @param add - whether we add or remove
    """

    def update_client_box(self, client_name, add):
        self.update_listbox(self.client_list, client_name, add)

    """
    adding/removing entries from any listbox
    @param listbox - the listbox which we modify
    @param name - the entry to add/remove
    @param add - whether we add or remove
    """

    def update_listbox(self, listbox, name, add):
        if add:
            if name not in listbox.get(0, tk.END):
                listbox.insert(tk.END, name)
        else:
            try:
                ind = listbox.get(0, tk.END).index(name)
                listbox.delete(ind)
            except Exception as e:
                return

    """
    The function for marking the background of a listbox entry red or white
    @param listbox - the listbox to edit
    @param name - the entry to change
    @param marking - colour red if true, white if false
    """

    def mark_red(self, listbox, name, marking):
        try:
            sisu = listbox.get(0, tk.END)
            ind = listbox.get(0, tk.END).index(name)
            colour = 'thistle' if marking else 'white'
            listbox.itemconfig(ind, bg=colour)
        except Exception as e:
            Log.debug("Couldn't mark/unmark %r as red.", name)
            return

    """
    The action which is called when the user selects a server to join
    """

    def pick_server(self):
        selected = self.server_box.curselection()
        uname = self.username_entry.get()
        if selected and uname and "/" not in uname:
            serv_name = selected[0]
            self.connector.join_server(self.server_box.get(serv_name), uname)
        self.flash_name()

    """
    A disconnect wrapper
    """

    def disconnect(self):
        self.connector.disconnect()

    """
    Destroying the game ui's if need be.
    """

    def destroy_game(self):
        if self.game_frame:
            self.game_frame.destroy()
        self.game_frame = None

    """
    Popups when the username was no good
    @param servername - the server that we tried to connect to
    @param username - the user name which we werent allowed to take
    """

    def notify_rejection(self, servername, username):
        tkMessageBox.showerror(
            "Invalid name!", "Server " + servername +
            " has rejected the name " + username + ".")

    """
    Pop-up for when we try to join a closed room where we are not in the game
    @param room - the roomname which we tried to join.
    """

    def notify_closed(self, room):
        tkMessageBox.showerror(
            "Can't join game!", "Room " + room +
            " is currently in a running game which does not contain you.")
Example #13
0
 def invalid(self, value):
     GameUI.createWhiteBgUI("Invalid command for " + value)
Example #14
0
 def info(self, value):
     if value == "no_clients":
         GameUI.createWhiteBgUI("Not enough clients connected.")
         self.displayUI.destroy()
Example #15
0
 def you_win(self):
     self.win = True
     GameUI.createDisplayUI("You Win!")
Example #16
0
class Game:
    """
    Constants
    """
    DEFAULT_BLIND_RAISING_SCHEME = lambda old_blinds, initial_blinds: old_blinds + initial_blinds  ### Default mechanism to increase blinds: add the initial blind to the new blind
    MIN_STARTING_CHIP_COUNT = 4  ### Absolute amount of starting chips per player
    NUM_CARDS_FOR_BOARD_TYPE = [3, 1,
                                1]  ### Number of cards to turn on each street
    NUM_HOLE_CARDS = 2  ### Number of hole cards per player
    NUM_PLAYERS_MIN = 2  ### Minimum number of players to start the game
    NUM_PLAYERS_MAX = 12  ### Maximum number of players to start the game
    REQUIRED_RAISE_MULTIPLE = 2  ### Required raise is this value times the current bet
    SHOW_FOLDED_HANDS = True  ### Show the remaining player's hand if folded to that player?
    """
    Constructor
    """
    def __init__(self):
        """
        Properties
        """
        self._blinds_maxed_out = False  ### Are the blinds at the highest value?
        self._board_name_idx = 0  ### List address for the name of the board
        self._game_data = GameData()  ### Poker Data Interface Object
        self._game_play_code = GameCode.NEW_GAME  ### What kind of game are we playing or playing back (new/load)?
        self._game_save = None  ### Object that manages saving a game finished or in progress
        self._game_theater = None  ### Plays back a finished game
        self._hand_actions = list(
        )  ### List of actions that were made in a hand
        self._ui = GameUI()  ### User Interface Object
        self._num_remaining_players = 0  ### Number of remaining players in the game

    """
    Public methods
    """

    def setup(self):
        """
        Set up the game structure with user-entered or loaded values
        
        Get the name of a game save and assume it is a new game
        """
        game_save_name = self._ui.get_game_save_name()
        self._game_save = GameSave(game_save_name)
        self._game_play_code = GameCode.NEW_GAME
        """
        If the save name exists, set up a saved (loaded) game
        """
        if self._game_save.save_exists():
            self._setup_saved_game()
        else:
            self._setup_new_game()
        """
        Set up the game's hot key commands
        """
        self._ui.setup_save_command(self._handle_save_game)

    def play_game(self):
        """
        Play or play back a game
        """
        if self._game_play_code == GameCode.PLAYBACK_GAME and self._game_theater is not None:
            """
            Play back a game
            """
            self._game_theater.playback_game()

        elif self._game_play_code != GameCode.NO_PLAY:
            """
            Play a game (new or loaded)
            """
            self._start_new_or_loaded_game()

    """
    Callback Methods
    """

    def _handle_save_game(self):
        """
        Serialize game save data and display a confirmation message to the user
        """
        save_successful = self._game_save.save()
        save_name = self._game_save.get_game_save_name()
        self._ui.display_game_save_confirm(save_successful, save_name)

    def _handle_time_expired(self):
        """
        Alert the user that time is up and set the next round number
        """
        self._ui.display_time_expired()
        self._game_data.mark_next_round()

    """
    Private Methods
    """

    def _start_new_or_loaded_game(self):
        """
        Start Game

        Activate hot key listeners
        """
        self._ui.listen_for_hot_keys()
        """
        Initialize the round number if this is a new game
        """
        if self._game_play_code == GameCode.NEW_GAME:
            self._game_data.mark_next_round()
        """
        Display welcome message
        """
        game_over = False
        winner = None

        self._welcome()
        """
        Game Loop
        """
        while not game_over:
            self._ui.display_round_border()
            self._setup_hand()
            self._play_hand()
            winner = self._cleanup_hand()
            game_over = winner is not None
        """
        Game Finished: Display the winner and attempt to save the game's history for playback
        """
        self._ui.display_winner(winner)
        self._attempt_save_game_history()

    def _setup_hand(self):
        """
        Check the timer, initialize hand players and pot, make blind bets, and pass out cards
        """
        current_time = self._manage_timer()
        self._game_data.setup_hand_players()
        self._game_data.setup_pot()
        self._game_data.make_blind_bets()
        self._game_data.pass_cards(Game.NUM_HOLE_CARDS)
        """
        Display all players with their positions
        """
        players_preflop = self._game_data.get_players()
        small_blind_pos, big_blind_pos = self._game_data.get_blind_positions()
        self._ui.display_player_data(players_preflop,
                                     self._game_data.get_button_positions())
        """
        Begin hand save snapshot
        """
        self._hand_actions.clear()
        round_number = self._game_data.get_round_number()
        self._game_save.begin_hand_snapshot(round_number, players_preflop,
                                            current_time)

    def _perform_showdown(self):
        """
        Find the best hand(s) and pay off the winning players
        """
        num_pots = self._game_data.get_num_pots()
        display_multiple_pots = num_pots > 1

        for pot in range(num_pots):
            """
            Determine winning players
            """
            ordered_player_hand_triples, winner_positions = self._game_data.evaluate_hands(
                pot)
            """
            Display winning players and pass the pot to them
            """
            self._ui.display_showdown_results(
                ordered_player_hand_triples,
                len(winner_positions),
                pot_idx=pot,
                display_multiple_pots=display_multiple_pots)
            self._game_data.pass_pot_to_winners(
                pot, player_pos_list=winner_positions)

    def _play_hand(self):
        """
        Execute a hand

        Preflop
        """
        round_folded = self._open_for_betting(preflop=True)

        if not round_folded:
            """
            Postflop streets
            """
            self._board_name_idx = 0

            while self._board_name_idx < len(
                    Game.NUM_CARDS_FOR_BOARD_TYPE) and round_folded is False:
                """
                Flip board cards and open the round for betting
                """
                board_cards = self._game_data.flip_board_cards(
                    Game.NUM_CARDS_FOR_BOARD_TYPE[self._board_name_idx])
                self._game_data.add_board_cards(board_cards)
                round_folded = self._open_for_betting()
                self._board_name_idx += 1

        if round_folded:
            """
            Pass pot to the only remaining player
            """
            num_pots = self._game_data.get_num_pots()

            for pot_idx in range(num_pots):
                self._game_data.pass_pot_to_winners(pot_idx)
                """
                Display results
                """
                remaining_player = self._game_data.get_hand_player(
                    pot_idx=pot_idx)
                self._ui.display_folded_round(remaining_player,
                                              Game.SHOW_FOLDED_HANDS,
                                              pot_idx=pot_idx)

        else:
            """
            Showdown
            """
            self._perform_showdown()

    def _cleanup_hand(self):
        """
        Remove any players who are out of chips
        """
        game_over = False
        winner = None
        eliminated_players = self._game_data.eliminate_busted_players()
        """
        Display eliminated players with their ranks
        And update the number of remaining players in the game
        """
        for player in eliminated_players:
            self._ui.display_player_eliminated(player,
                                               self._num_remaining_players)
            self._num_remaining_players -= 1
        """
        Get player data, current time stamp, and board cards to complete hand snapshot
        """
        players = self._game_data.get_players()
        timestamp = self._game_data.get_remaining_time()
        board = self._game_data.get_board()
        self._game_save.end_hand_snapshot(self._hand_actions, board, players,
                                          timestamp)
        """
        Check if there is a winner of the game and end the game if there is one
        """
        winner = self._game_data.get_winner()

        if winner is None:
            """
            If there are multiple players left, rotate the dealer, clear the game board, and reset cards
            """
            self._game_data.rotate_dealer()
            self._game_data.clear_board()
            self._game_data.reset_cards()
        else:
            game_over = True
        """
        Set the game code to continue to prevent reloading of saved values
        """
        self._game_play_code = GameCode.CONTINUE_GAME
        """
        Return game over status and winner if there is one
        """
        return winner

    def _setup_new_game(self):
        """
        Get setup information from the user
        """
        game_setup = GameSetup()
        game_setup.starting_num_players = self._ui.get_num_players(
            Game.NUM_PLAYERS_MIN, Game.NUM_PLAYERS_MAX)
        game_setup.starting_chip_count = self._ui.get_starting_chip_count(
            Game.MIN_STARTING_CHIP_COUNT)
        game_setup.starting_big_blind = self._ui.get_starting_big_blind(
            game_setup.starting_chip_count)
        game_setup.blind_increase_interval = self._ui.get_blind_increase_interval(
        )
        game_setup.handle_time_expired = self._handle_time_expired
        game_setup.blind_increase_scheme = Game.DEFAULT_BLIND_RAISING_SCHEME
        """
        Initialize the number of remaining players
        """
        self._num_remaining_players = game_setup.starting_num_players
        """
        Set up the game data
        """
        self._game_data.setup_game_data(game_setup)
        """
        Save the button positions
        """
        game_setup.button_positions = self._game_data.get_button_positions()
        """
        Snapshot the game setup for the game save
        """
        self._game_save.snap_game_setup(game_setup)

    def _setup_saved_game(self):
        """
        If this is a game in progress, prompt user to continue or overwrite
        If this is a complete game, prompt user for playback

        Load the existing game data to see if it is a finished or in-progress game
        """
        self._game_save.load()

        if self._game_save.is_game_complete():
            """
            Loaded game is already finished
            Prompt user to play back the completed game
            """
            load_game_history = self._ui.prompt_load_game_history()
            """
            If the user wishes to play back the loaded game, set up the theater
            Otherwise quit the game
            """
            if load_game_history:
                """
                Get the saved game data
                """
                loaded_game = self._game_save.get_game_save()
                """
                Parse the saved game data
                """
                game_setup, game_hands = loaded_game
                """
                Create a game theater and set the play protocol to playback
                """
                self._game_theater = GameTheater(game_setup, game_hands)
                self._game_play_code = GameCode.PLAYBACK_GAME
            else:
                """
                User cancellation: quit game
                """
                self._ui.display_load_game_history_cancellation()
                self._game_play_code = GameCode.NO_PLAY

        else:
            """
            Game is not complete yet
            Prompt user to load and continue this game or continue creating a new game with risk 
            of overwriting this one
            """
            load_existing_game = self._ui.prompt_load_game()

            if load_existing_game:
                """
                Get the loaded game data
                """
                loaded_game = self._game_save.get_game_save()
                """
                Parse loaded game data as setup and player state values
                """
                game_setup, game_hands = loaded_game
                round_number = GameData.INITIAL_ROUND_NUMBER
                player_state_final = None
                timestamp_final = None

                if len(game_hands) > 0:
                    """
                    If at least one hand was saved, load the player data and timestamp saved after the
                    last saved hand
                    Otherwise do not load any player or timestamp data

                    Also set the number of remaining players
                    """
                    last_hand = game_hands[-1]
                    round_number, _, _, _, _, player_state_final, timestamp_final = last_hand
                    self._num_remaining_players = len(player_state_final)
                else:
                    """
                    No hands played yet:
                        Set the number of remaining players to the initial number of players specified in setup
                    """
                    self._num_remaining_players = game_setup.starting_num_players
                """
                Package the load game values and set up the loaded game
                """
                game_setup.init_timestamp = timestamp_final
                game_setup.round_number = round_number
                game_setup.init_player_state = player_state_final
                game_setup.handle_time_expired = self._handle_time_expired
                game_setup.blind_increase_scheme = Game.DEFAULT_BLIND_RAISING_SCHEME

                self._game_data.setup_game_data(game_setup)
                self._game_play_code = GameCode.PLAY_LOAD_GAME

            else:
                """
                User decides to create and play a new game under the same name
                This means that if the user saves this new game at any point,
                the old game will be completely overwritten
                """
                self._game_save.clear_hand_history()
                self._ui.display_overwrite_warning()

    def _welcome(self):
        """
        Display the game title and hot key commands
        """
        self._ui.display_round_border()
        self._ui.display_title()
        self._ui.display_hot_key_commands()
        self._ui.display_round_border()

    def _attempt_save_game_history(self):
        """
        Prompt user for saving the game history at the end of the game
        """
        save_game_hist = self._ui.prompt_save_game_history()
        save_name = None

        if save_game_hist:
            """
            Attempt to save the game history, and if successful store the name of the file
            """
            save_success = self._game_save.save()

            if save_success:
                save_name = self._game_save.get_game_save_name()
        """
        Display confirmation message to user
        """
        self._ui.display_game_history_save_status(save_game_hist, save_name)

    def _manage_timer(self):
        """
        If the game is in load mode, start the timer now
        """
        if self._game_play_code == GameCode.PLAY_LOAD_GAME:
            self._game_data.start_timer()
        """
        Get the current round number and timestamp
        """
        round_number = self._game_data.get_round_number()
        remaining_time = self._game_data.get_remaining_time()
        """
        Reset the timer and increase the blinds if the timer is off
        """
        if remaining_time == 0:
            """
            Raise the blinds if they are not at their maximum value
            """
            if not self._blinds_maxed_out:
                """
                Raise the blinds if this is not the first round
                """
                if round_number != GameData.INITIAL_ROUND_NUMBER:
                    self._game_data.raise_blinds()
                """
                Determine if the blinds are now at the maximum
                """
                if self._game_data.blinds_maxed_out():
                    self._blinds_maxed_out = True
                """
                Set the timer and display a confirmation message
                """
                self._game_data.start_timer()
                remaining_time = self._game_data.get_remaining_time()
                big_blind = self._game_data.get_big_blind_amt()
                self._ui.display_timer_set(round_number, big_blind,
                                           remaining_time)

            else:
                """
                Display that the current big blind amount will not be raised
                Do not restart the timer
                """
                self._ui.display_blinds_maxed_out(
                    self._game_data.get_big_blind_amt())

        else:
            """
            If the timer is currently on, display the remaining time
            """
            self._ui.display_current_timer_value(round_number, remaining_time)
        """
        Return Result (remaining time)
        """
        return remaining_time

    def _get_available_moves(self, player, preflop=False):
        """
        Local Variables
        """
        available_moves = list()
        action_to_play = 0
        max_action = self._game_data.get_max_action()
        player_stack = player.get_stack_size()
        player_action = player.get_action()
        player_can_afford_to_raise = player_stack + player_action > max_action
        """
        If the player does not have any chips left, there are no options available
        """
        if player_stack > 0:
            """
            Available moves are dependent on the player's action relative to the current highest action
            (and one exception in the preflop round)
            """
            if player_action < max_action:
                available_moves.append(GameMove.CALL)
                """
                If all other players are all-in or this player cannot afford to raise the action 
                Then the player cannot raise the action
                """
                if self._has_multiple_available_betters(
                ) and player_can_afford_to_raise:
                    available_moves.append(GameMove.RAISE)
                """
                Minimum raise is a multiple of the current maximum bet in play
                """
                action_to_play = min(player_stack,
                                     Game.REQUIRED_RAISE_MULTIPLE * max_action)

            else:
                available_moves.append(GameMove.CHECK)
                """
                If all other players are all-in, this player cannot raise the action even higher
                """
                if self._has_multiple_available_betters():

                    if preflop and player_can_afford_to_raise:
                        available_moves.append(GameMove.RAISE)
                    else:
                        available_moves.append(GameMove.BET)
                """
                Minimum amount to introduce betting with is the size of the big blind
                EXCEPTION: On preflop, the big blind may raise the big blind
                """
                if preflop:
                    action_to_play = min(
                        player_stack,
                        Game.REQUIRED_RAISE_MULTIPLE *
                        self._game_data.get_big_blind_amt())
                else:
                    action_to_play = min(player_stack,
                                         self._game_data.get_big_blind_amt())
            """
            Folding is always an option if the player has chips left
            """
            available_moves.append(GameMove.FOLD)
        """
        Return result
        """
        return (available_moves, action_to_play)

    def _is_hand_over(self):
        """
        Determine if the hand is over
        Check if there is at least one side pot. In this case, the main pot must go to showdown
        Check if there are at least two players contending in the main pot. In this case, betting can still take place
        """
        num_pots = self._game_data.get_num_pots()
        num_players_in_hand = self._game_data.get_num_players_in_hand()
        return (num_pots < 2) and (num_players_in_hand < 2)

    def _has_multiple_available_betters(self):
        """
        Check if there is more than one available better in the current hand
        """
        return self._game_data.get_num_available_betters() > 1

    def _start_betting_round(self, preflop=False):
        """
        Local Variables
        """
        _, big_blind_idx = self._game_data.get_blind_positions()
        current_player_idx = None
        round_over = False
        """
        Get the position of the first player to act
        """
        if preflop:
            current_player_idx = self._game_data.get_next_player_pos(
                big_blind_idx)
        else:
            current_player_idx = self._game_data.get_next_player_pos(
                GameData.DEALER_IDX)
        """
        Game data setup for a betting round
        """
        self._game_data.init_betting_round()
        """
        Round loop: keep cycling turns until the round is over
        """
        while not round_over:
            """
            Setup: Get a copy of the acting player for UI puproses
            """
            player = self._game_data.get_hand_player(
                player_idx=current_player_idx)
            """
            Play: Determine, prompt, and play a legal move
            """
            available_moves, action_to_play = self._get_available_moves(
                player, preflop=preflop)

            if len(available_moves) > 0:
                """
                Show the pot and community cards if postflop
                """
                if preflop is False:
                    self._ui.display_board(self._game_data.get_board(),
                                           board_name_idx=self._board_name_idx)

                chosen_move, chosen_amount = self._ui.prompt_available_moves(
                    player, available_moves, action_to_play)
                adjusted_amount = self._game_data.play_move(
                    current_player_idx, chosen_move, chosen_amount)
                self._ui.display_move_made(player, chosen_move,
                                           adjusted_amount)
                """
                Store the action for the save snapshot
                """
                self._hand_actions.append(
                    (player.ID, chosen_move, adjusted_amount))

            else:
                """
                Player has no available moves: Skip player and move on
                """
                self._ui.display_no_moves_available(player)
                self._game_data.skip_player(current_player_idx)
            """
            Cleanup: Find the next player to act and determine if the round is over
            """
            current_player_idx = self._game_data.get_next_player_pos(
                current_player_idx)
            round_over = self._game_data.is_round_over()

    def _open_for_betting(self, preflop=False):
        """
        Display the current pot
        """
        self._ui.display_pot(self._game_data.get_pot())
        """
        Start a betting round if more than one player is able to bet
        """
        if preflop or self._has_multiple_available_betters():
            self._start_betting_round(preflop=preflop)
        else:

            if preflop is False:
                self._ui.display_board(self._game_data.get_board(),
                                       board_name_idx=self._board_name_idx)
            """
            Insufficient amount of remaining betters in the hand: no betting can take place
            """
            self._ui.display_no_bet_from_all_in()
        """
        Move all player action to the pot and display it
        """
        self._game_data.move_action_to_pot()
        """
        Determine if the hand has ended prematurely and return it
        """
        return self._is_hand_over()
Example #17
0
    def gameover(self):
        taskMgr.remove('update')
        taskMgr.add(self.gameEngine.deathCamTask, "DeathCameraTask")
        self.gameEngine.myId = self.id

        gameoverDisplay = GameUI.createDisplayUI("Game Over!")
import sys
from gameui import GameUI
from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = GameUI()
    sys.exit(app.exec_())
Example #19
0
    def game_over(self):
        self.lose = True
        taskMgr.add(self.gameEngine.deathCamTask, "DeathCameraTask")
        self.gameEngine.myId = self.id

        gameoverDisplay = GameUI.createDisplayUI("Game Over!")
Example #20
0
class GameTheater:
    """
    Constructor
    """
    def __init__(self, setup, hands):
        """
        Properties
        """
        self._setup = setup  ### Game setup data
        self._hands = hands  ### List of all played hands in the game
        self._ui = GameUI()  ### Interface object to display game history

    """
    Public Methods
    """

    def playback_game(self):
        """
        Game Playback:
            * Display how the game was set up
            * Show the hand history of the game in order
            * Display which player won the game
        """
        self._announce_setup()
        self._display_hands()
        self._display_winner()

    """
    Private Methods
    """

    def _announce_setup(self):
        """
        Display how the game was set up
        """
        self._ui.display_game_setup(self._setup.starting_num_players,
                                    self._setup.starting_chip_count,
                                    self._setup.starting_big_blind,
                                    self._setup.blind_increase_interval)
        """
        Pause interface and wait for user acknowledgement
        """
        self._ui.get_user_acknowledgement()

    def _display_hands(self):
        """
        Show the hand history of the game in order
        """
        for hand in self._hands:
            """
            Separate each hand
            """
            self._ui.display_round_border()
            """
            Gather all data from the hand
            """
            round_number, init_player_state, timestamp_init, actions, cards, _, _ = hand
            starting_big_blind = self._setup.starting_big_blind
            current_big_blind = starting_big_blind
            """
            Determine the current big blind size
            """
            for _ in range(1, round_number):
                current_big_blind = self._setup.blind_increase_scheme(
                    current_big_blind, starting_big_blind)
            """
            Display all hand data
            """
            self._ui.display_timer_set(round_number,
                                       current_big_blind,
                                       timestamp_init,
                                       past=True)
            self._ui.display_player_data(init_player_state,
                                         self._setup.button_positions)
            self._ui.display_board(cards)
            self._ui.display_actions(actions)
            """
            Wait for user acknowledgement
            """
            self._ui.display_round_border()
            self._ui.get_user_acknowledgement()

    def _display_winner(self):
        """
        Make the following assumptions:
            * The game has had at least one completed hand
            * There is one and only one player left after the most recent hand
        """
        last_hand = self._hands[-1]
        _, _, _, _, _, player_state_final, _ = last_hand
        winner = player_state_final[0]
        """
        Display the winner
        """
        self._ui.display_winner(winner)
Example #21
0
 def game_end(self, value):
     if int(value) == self.id:
         GameUI.createDisplayUI("You Win!")