def test_close_lobby(self): self.client1.emit("create_game", "player1") access_code = self.client1.get_received()[0]['args'] self.client2.emit("join_game", access_code, "player2") game = db_util.get_game(access_code) self.assertIsNone(game['players'][0]['start_room']) self.client1.emit('close_lobby', access_code) game = db_util.get_game(access_code) self.assertIsNotNone(game['players'][0]['start_room'])
def join_game(access_code, player_name): """ Find a game with the access code, and add the player to it """ # find the game or error game = db_util.get_game(access_code) if game is None: send('game not found') return if game['state'] != 'waiting_for_players': send('game is in progress') return # create a player player = db_util.create_player(player_name) # add to the list of players db.games.update_one({'access_code': access_code}, {'$push': { 'players': player }}) join_room(access_code) emit('lobby_update', {'players': db_util.get_players_in_lobby(access_code)}, room=access_code)
def _view_game(game_id, init_path, game_edit_form=None, comment_form=None): """ Views existing game on given (comment) path. When there are errors with comment for instance of this one can be passed in and errors displayed. """ game = db.get_game(game_id) if not game: abort(404) # fetch games owner db.annotate(game) comments=list(db.get_comments_for_game(game_id)) for comment in comments: db.annotate(comment) # forms if not comment_form: comment_form = forms.CommentForm() user = db.get_user_by_username(session.get("username", None)) typeahead_players = [] if user and game.is_owner(user) and not game_edit_form: game_edit_form = forms.GameEditForm.form_factory(game.type)() # hint for inputting players names typeahead_players = dict([(player.name, player.rank) for player in db.get_players()]) print game.nodes return render_template("view_game.html", game=game, init_path=init_path, comments=comments, comment_paths=[(str(c["_id"]), c["path"]) for c in comments], comment_form=comment_form, typeahead_players=typeahead_players, game_edit_form=game_edit_form)
def message(roomid, person): print(request.form) game = db.get_game(roomid) for person_ in game.players: if "".join(person_.searchname.split(" ")).lower() == person.lower( ) or person_.dm or person.lower() == "all": person_.messages.append((request.form["msg"], request.form["from"], request.form["to_"])) return ("", 204)
def view_sgf(game_id): """Views sgf for game identified by game_id""" game = db.get_game(game_id) if not game: abort(404) annotated_comments = [db.annotate(c) for c in db.get_comments_for_game(game_id)] # this changes the game itself # but that is fine since we don't intend to save it game = db.patch_game_with_comments(game, annotated_comments) response = make_response(sgf.makeSgf([game.export()])) response.headers["Content-type"] = "text/plain" return response
def post_update(game_id): """Posts game tree updates for given game. Requires login. Called via ajax.""" game = db.get_game(game_id) if not game: abort(404) username = session["username"] user = db.get_user_by_username(username) try: update_data = json.loads(request.form.get("update_data")) except: return jsonify(err="Invalid encoding.") if not db.sync_game_update(game, update_data, user): return jsonify(err="You don't have permission to perform this action.") return jsonify(err=None)
def edit_game(game_id): """Edit game meta information.""" game = db.get_game(game_id) if not game: abort(404) user = db.get_user_by_username(session["username"]) if not user: abort(500) # protection if not game.is_owner(user): app.logger.warning("Unauthorized game edit: user(%s) game(%s)" % (user.username, game._id)) abort(500) form = forms.GameEditForm.form_factory(game.type)(request.form) if form.validate_on_submit(): form.update_game(game) return redirect(url_for("view_game", game_id=game._id)) return _view_game(game._id, [], game_edit_form=form)
def post_comment(game_id): """Posts comment for given game and path. Requires login. Called via ajax.""" game = db.get_game(game_id) if not game: abort(404) form = forms.CommentForm(request.form) if form.validate_on_submit(): username = session["username"] user = db.get_user_by_username(username) if not user: app.logger.warning("comment without user") abort(500) comment = db.create_comment(user._id, game_id, form.short_path, form.comment.data) db.annotate(comment) can_delete_comment = comment.is_owner(user) comment_template = app.jinja_env.from_string( """ {%% from 'macros.html' import render_comment_in_game %%} {{ render_comment_in_game(comment, %s) }} """ % can_delete_comment) return jsonify(err=None, comment_id = str(comment["_id"]), comment_path=comment["path"], comment_html = comment_template.render(comment=comment)) return jsonify(err="Invalid comment.")
def close_lobby(access_code): """ Close the lobby and direct each player to their starting room """ game = db_util.get_game(access_code) num_players = len(game['players']) rooms = gl.get_shuffled_rooms(num_players) cards = gl.get_shuffled_card_indices(num_players) for room, card, player in zip(rooms, cards, game['players']): player['start_room'] = room player['card'] = card # update the players with their rooms and change the game state game = db.games.find_one_and_update( {'access_code': access_code}, {'$set': { 'players': game['players'], 'state': 'readying_rooms' }}, return_document=ReturnDocument.AFTER) emit('game_update', game, room=access_code)
def update(roomid, name): game = db.get_game(roomid).getJSONUpdate(name) return game
def __init__(self): """Create game object from data in db""" self.load_data(db.get_game()) self.comment_units = self.load_units("comment") self.link_units = self.load_units("link")