def startGame(self): global started #The Start variable is the time the server waits for a game to start start = 10 #Create a board board = tiles.Board() #The game loop while True: #Server uses the startTimer function that gives a delay print("A new game is about to start!") now = time.time() server.startTimer(now, start) #Reset everything in the server #This happens after the start timer so extra games can see what happened in the previous game before starting again server.resetState() board.reset() #The pickPlayers function updates the global variables that deal with current players liveIdNums = server.pickPlayers() #The game should now start started = 1 #The function that handles the game playing server.gameStart(board, liveIdNums) print("This game is over!")
def threaded_start_game(self): # this goes in the thread while True: board = tiles.Board() #new players live_id_nums = self.select_Players_From_Connected_Clients() # spectator_id_nums = self.select_Spectators_From_Connected_Clients() # self.game_started == True self.start_game(board) board.reset() server.resetState()
def threaded_start_game(self): # this goes in the thread # global live_id_nums start = 5 print("maybe") while True: print("HELLO ??????????") board = tiles.Board() print("The GaMe Is ABoUT tO StARt!!!!!!!!!") #new players live_id_nums = self.select_Players_From_Connected_Clients() print("DID WE GET HERE ??????????") # self.game_started == True self.start_game(board) print("IS THE GAME STARTING ??????????")
def startGame(self): start = 5 print("maybe") while True: board = tiles.Board() print("Game is about to start!") now = time.time() server.startTimer(now,start) print("up to startgame") print("we got here") #new players live_id_nums = server.pickPlayers() server.gameStart(board) print("the game finished plsss") #rese start = 10
def __init__(self, parent=None): super().__init__(parent) self.parent = parent self.pack() self.sock = None self.infolock = threading.Lock() self.idnum = None self.playernames = {} # idnum -> player name self.handlock = threading.Lock() self.hand_offset = tiles.Point( (Application.CANVAS_WIDTH_PX - Application.HAND_WIDTH_PX) / 2, 2 * Application.BORDER_PX + Application.BOARD_HEIGHT_PX) self.hand = [None] * Application.HAND_SIZE self.handrotations = [0] * Application.HAND_SIZE self.boardlock = threading.Lock() self.board = tiles.Board() self.board.tile_size_px = Application.TILE_PX self.lasttilelocation = None self.location = None self.playernums = {} # idnum -> player number (turn order) self.playerlist = [] self.playerlistvar = StringVar(value=self.playerlist) self.eliminatedlist = [] self.currentplayerid = None self.boardoffset = tiles.Point(Application.BORDER_PX, Application.BORDER_PX) self.selected_hand = 0 self.handrects = [None] * Application.HAND_SIZE self.bind('<<ClearBoard>>', lambda ev: self.clear_board()) self.bind('<<RedrawBoard>>', lambda ev: self.draw_board()) self.bind('<<RedrawHand>>', lambda ev: self.draw_hand()) self.bind('<<RedrawTokens>>', lambda ev: self.draw_tokens()) self.bind('<<RedrawTurn>>', lambda ev: self.draw_turn()) self.bind('<<CloseConnection>>', lambda ev: on_quit()) self.create_widgets()
#target = server.cew,#server.client_handler, target=server.handle_client_connection, args=(readable, writeable, exceptional), # args = (lock, readable, writeable, exceptional), daemon=True) thread.start() else: print("Sorry, that's all the clients the server can support!") print( "------------------------ ALL CONNECTED CLIENTS ------------------------" ) print(server.all_connected_clients) # select the players from the current pool of connected clients print( "------------------------ CREATE THE LIST OF LIVE_ID_NUMS ------------------------" ) live_id_nums = server.select_Players_From_Connected_Clients( ) # WE GET STUCK HERE # start the game with the current players and spectators: print( "------------------------ CREATE THE BOARD OBJECT ------------------------" ) board = tiles.Board() print("------------------------ START THE GAME ------------------------") # server.start_game(board) # game starts now # server.reset_clients() server.start_game(board, live_id_nums) # game starts now
def client_handler(self, connection, address): print(f"[NEW CONNECTION] {address} connected to the server.") host, port = address name = '{}:{}'.format(host, port) idnum = 0 live_idnums = [idnum] connection.send(tiles.MessageWelcome(idnum).pack()) connection.send(tiles.MessagePlayerJoined(name, idnum).pack()) connection.send(tiles.MessageGameStart().pack()) for _ in range(tiles.HAND_SIZE): tileid = tiles.get_random_tileid() connection.send(tiles.MessageAddTileToHand(tileid).pack()) connection.send(tiles.MessagePlayerTurn(idnum).pack()) board = tiles.Board() buffer = bytearray() while True: # with lock: # connection, msg = self.task_queue.get() # added chunk = connection.recv(4096) if not chunk: print('client {} disconnected'.format(address)) return buffer.extend(chunk) while True: msg, consumed = tiles.read_message_from_bytearray(buffer) if not consumed: break buffer = buffer[consumed:] print('received message {}'.format(msg)) # sent by the player to put a tile onto the board (in all turns except # their second) if isinstance(msg, tiles.MessagePlaceTile): if board.set_tile(msg.x, msg.y, msg.tileid, msg.rotation, msg.idnum): # notify client that placement was successful connection.send(msg.pack()) # check for token movement positionupdates, eliminated = board.do_player_movement( live_idnums) for msg in positionupdates: connection.send(msg.pack()) if idnum in eliminated: connection.send( tiles.MessagePlayerEliminated(idnum).pack()) return # pickup a new tile tileid = tiles.get_random_tileid() connection.send( tiles.MessageAddTileToHand(tileid).pack()) # start next turn connection.send(tiles.MessagePlayerTurn(idnum).pack()) # sent by the player in the second turn, to choose their token's # starting path elif isinstance(msg, tiles.MessageMoveToken): if not board.have_player_position(msg.idnum): if board.set_player_start_position( msg.idnum, msg.x, msg.y, msg.position): # check for token movement positionupdates, eliminated = board.do_player_movement( live_idnums) for msg in positionupdates: connection.send(msg.pack()) if idnum in eliminated: connection.send( tiles.MessagePlayerEliminated( idnum).pack()) return # start next turn connection.send( tiles.MessagePlayerTurn(idnum).pack()) # added: self.task_queue.task_done()
def client_handler(self, lock, connection, address): # def client_handler(self, connection, address): # global turn_Queue host, port = address name = '{}:{}'.format(host, port) print(f"{name} has joined") idnum = 0 live_idnums = [idnum] with lock: connection.send(tiles.MessageWelcome(idnum).pack()) connection.send(tiles.MessagePlayerJoined(name, idnum).pack()) connection.send(tiles.MessageGameStart().pack()) # get a client from the queue: # client_socket_id = turn_Queue.get() # is this now idnum??????????????????????????????????? for _ in range(tiles.HAND_SIZE): tileid = tiles.get_random_tileid() connection.send(tiles.MessageAddTileToHand(tileid).pack()) connection.send(tiles.MessagePlayerTurn(idnum).pack()) board = tiles.Board() buffer = bytearray() # Don't do the following unless >= 2 clients # if num_live_id_nums > 1: print("Now the game can START!") while True: chunk = connection.recv(4096) if not chunk: print('client {} disconnected'.format(address)) return buffer.extend(chunk) while True: # with lock: # connection, msg = self.task_queue.get() # added msg, consumed = tiles.read_message_from_bytearray(buffer) if not consumed: break buffer = buffer[consumed:] print('received message {}'.format(msg)) # sent by the player to put a tile onto the board (in all turns except # their second) if isinstance(msg, tiles.MessagePlaceTile): if board.set_tile(msg.x, msg.y, msg.tileid, msg.rotation, msg.idnum): # notify client that placement was successful connection.send(msg.pack()) # check for token movement positionupdates, eliminated = board.do_player_movement( live_idnums) for msg in positionupdates: connection.send(msg.pack()) if idnum in eliminated: connection.send( tiles.MessagePlayerEliminated( idnum).pack()) return # pickup a new tile tileid = tiles.get_random_tileid() connection.send( tiles.MessageAddTileToHand(tileid).pack()) # start next turn connection.send( tiles.MessagePlayerTurn(idnum).pack()) # sent by the player in the second turn, to choose their token's # starting path elif isinstance(msg, tiles.MessageMoveToken): if not board.have_player_position(msg.idnum): if board.set_player_start_position( msg.idnum, msg.x, msg.y, msg.position): # check for token movement positionupdates, eliminated = board.do_player_movement( live_idnums) for msg in positionupdates: connection.send(msg.pack()) if idnum in eliminated: connection.send( tiles.MessagePlayerEliminated( idnum).pack()) return # start next turn connection.send( tiles.MessagePlayerTurn(idnum).pack())