async def handleGame(data): if (data['text'] == 'join' and game.game_on == False): playerNum = game.add_player(data['sender']) data['text'] = f'join{str(playerNum)}' await notify_public_message(data) elif (data['text'] == 'join' and game.game_on == True): data = game.add_player(data['sender']) await notify_client(data) elif (data['text'] == 'ready'): data['text'] = 'start' data['sender'] = 'game' await notify_public_message(data) cards = game.deal_3(1) await notify_client(cards) cards = game.deal_3(2) await notify_client(cards) elif (data['text'] == 'move'): is_bomb = False if (len(data['data'])>3): is_bomb = True data = game.process_message(data) await notify_public_message(data) if (not is_bomb): player = game.players.index(game.whos_turn)+1 cards = game.deal_1(player) await notify_client(cards) win_state = game.check_win_state() if (win_state[0]): print('win') data = game.create_win_message(win_state[1]) await notify_public_message(data) data = game.reset() await notify_public_message(data) else: game.switch_turn()
def on_join(data): user_id = request.sid with USERS[user_id]: create_if_not_found = data.get("create_if_not_found", True) # Retrieve current game if one exists curr_game = get_curr_game(user_id) if curr_game: # Cannot join if currently in a game return # Retrieve a currently open game if one exists game = get_waiting_game() if not game and create_if_not_found: # No available game was found so create a game params = data.get('params', {}) game_name = data.get('game_name', 'overcooked') _create_game(user_id, game_name, params) return elif not game: # No available game was found so start waiting to join one emit('waiting', { "in_game" : False }) else: # Game was found so join it with game.lock: join_room(game.id) set_curr_room(user_id, game.id) game.add_player(user_id) if game.is_ready(): # Game is ready to begin play game.activate() ACTIVE_GAMES.add(game.id) emit('start_game', { "spectating" : False, "start_info" : game.to_json()}, room=game.id) socketio.start_background_task(play_game, game) else: # Still need to keep waiting for players WAITING_GAMES.put(game.id) emit('waiting', { "in_game" : True }, room=game.id)
def get_session_player(self,game,name=''): if name=="": name = self.get_cookie_name() id = cherrypy.session.get(game.id) if id==None and name!='': p = game.add_player(name) cherrypy.session[game.id] = p.id cherrypy.response.cookie['name'] = name cherrypy.response.cookie['name']['expires'] = 60*60*24 return p elif id: return game.get_player(id)
def _create_game(user_id, game_name, params={}): game, err = try_create_game(game_name, **params) if not game: emit("creation_failed", { "error" : err.__repr__() }) return spectating = True with game.lock: if not game.is_full(): spectating = False game.add_player(user_id) else: spectating = True game.add_spectator(user_id) join_room(game.id) set_curr_room(user_id, game.id) if game.is_ready(): game.activate() ACTIVE_GAMES.add(game.id) emit('start_game', { "spectating" : spectating, "start_info" : game.to_json()}, room=game.id) socketio.start_background_task(play_game, game, fps=MAX_FPS) else: WAITING_GAMES.put(game.id) emit('waiting', { "in_game" : True }, room=game.id)
def join_game(chat_id: int, gameid:str): if not chat_id in player_gameids: game = games[gameid] player_gameids[chat_id] = game.id joined = game.add_player(chat_id) if joined: bot.send_message(chat_id = chat_id, text = "Joined game " + str(gameid)) msg = bot.send_message(chat_id = chat_id, text = "_Loading..._", parse_mode="Markdown") print(msg) game.player_data[chat_id] = {"message" : msg.message_id} game.update_single(chat_id) else: bot.send_message(chat_id = chat_id, text = "Couldn't join game, it's probably full.") else: bot.send_message(chat_id = chat_id, text = "You are already in a game!")
import game game = game.Game() game.add_player(1) game.add_player(2) game.add_player(3) game.add_player(4) game.add_player(5) game.add_player(6) game.add_player(7) game.start() game.input(1, "u", "pressed") for _ in range(50): print(game.tick()) game.input(1, "u", "r") game.input(1, "d", "pressed") game.input(1, "", KeyAction.PRESSED) for i in range(25): print(game.tick()) y_pos = 0 while y_pos > -2490: x = game.tick() y_pos = x[0][1]['position'][1] print(y_pos) print(x)