def post(self, match_id): args = request.json team = MatchTeam( match_id=match_id, name=args.get("name"), statistics=args.get("statistics"), details=args.get("details"), ) g.db.add(team) g.db.commit() team_id = team.team_id resource_uri = url_for("matches.team", match_id=match_id, team_id=team_id, _external=True) response_header = {"Location": resource_uri} log.info("Created team %s for match %s", team_id, match_id) log_match_event(match_id, None, "gameserver.match.team_created", details={"team_id": team_id}) return { "team_id": team_id, "url": resource_uri, }, httplib.CREATED, response_header
def post(self): """Register a new battle on the passed in matcheserver. Each matcheserver should always have a single battle. A matcheserver will have zero matches only when it doesn't start up. Either the celery matcheserver task (in normal EC2 mode) or the matcheserver unreal process (in local development mode) will call this endpoint to create the battle resource. """ args = request.json server_id = args.get("server_id") match = Match( server_id=server_id, num_players=args.get("num_players", 0), max_players=args.get("max_players"), map_name=args.get("map_name"), game_mode=args.get("game_mode"), status=args.get("status"), status_date=utcnow(), match_statistics=args.get("match_statistics"), details=args.get("details"), ) g.db.add(match) g.db.commit() match_id = match.match_id if args.get("num_teams"): for i in xrange(args.get("num_teams")): team = MatchTeam(match_id=match_id, name="Team %s" % (i + 1)) g.db.add(team) g.db.commit() resource_uri = url_for("matches.entry", match_id=match_id, _external=True) players_resource_uri = url_for("matches.players", match_id=match_id, _external=True) response_header = { "Location": resource_uri, } log.info("Created match %s for server %s", match_id, server_id) log_match_event(match_id, None, "gameserver.match.created", details={"server_id": server_id}) try: process_match_queue() except Exception: log.exception("Unable to process match queue") return { "match_id": match_id, "url": resource_uri, "players_url": players_resource_uri, }, httplib.CREATED, response_header
def delete(self, match_id, player_id): """ A player has left an ongoing battle """ match_player = g.db.query(MatchPlayer) \ .filter(MatchPlayer.match_id == match_id, MatchPlayer.player_id == player_id) \ .first() if not match_player: abort(httplib.NOT_FOUND) if match_player.status != "active": abort(httplib.BAD_REQUEST, description="Player status must be active, not '%s'" % match_player.status) match = g.db.query(Match).get(match_id) if not match: abort(httplib.NOT_FOUND, description="Match not found") if match.status == "completed": log.warning( "Attempting to remove player %s from battle %s which has already completed", player_id, match_id) abort( httplib.BAD_REQUEST, description="You cannot remove a player from a completed match" ) team_id = match_player.team_id match_player.status = "quit" num_seconds = (utcnow() - match_player.join_date).total_seconds() match_player.leave_date = utcnow() match_player.seconds += num_seconds num_players = g.db.query(MatchPlayer) \ .filter(MatchPlayer.match_id == match_id, MatchPlayer.status == "active") \ .count() match.num_players = num_players g.db.commit() log.info("Player %s has left battle %s", player_id, match_id) log_match_event(match_id, player_id, "gameserver.match.player_left", details={"team_id": team_id}) return {"message": "Player has left the battle"}
def post(self, match_id): """ Add a player to a match """ player_id = request.json["player_id"] team_id = request.json.get("team_id", None) match = g.db.query(Match).get(match_id) if not match: abort(httplib.NOT_FOUND, description="Match not found") if match.status == "completed": abort(httplib.BAD_REQUEST, description="You cannot add a player to a completed battle") num_players = g.db.query(MatchPlayer) \ .filter(MatchPlayer.match_id == match.match_id, MatchPlayer.status.in_(["active"])) \ .count() if num_players >= match.max_players: abort(httplib.BAD_REQUEST, description="Match is full") if team_id: team = g.db.query(MatchTeam).get(team_id) if not team: abort(httplib.NOT_FOUND, description="Team not found") if team.match_id != match_id: abort(httplib.BAD_REQUEST, description="Team %s is not in match %s" % (team_id, match_id)) match_player = g.db.query(MatchPlayer) \ .filter(MatchPlayer.match_id == match_id, MatchPlayer.player_id == player_id) \ .first() if not match_player: match_player = MatchPlayer(match_id=match_id, player_id=player_id, team_id=team_id, num_joins=0, seconds=0, status="active") g.db.add(match_player) match_player.num_joins += 1 match_player.join_date = utcnow() match_player.status = "active" # remove the player from the match queue g.db.query(MatchQueuePlayer).filter(MatchQueuePlayer.player_id == player_id).delete() if match.start_date is None: match.start_date = utcnow() g.db.commit() # prepare the response resource_uri = url_for("matches.player", match_id=match_id, player_id=player_id, _external=True) response_header = {"Location": resource_uri} log.info("Player %s has joined match %s in team %s.", player_id, match_id, team_id) log_match_event(match_id, player_id, "gameserver.match.player_joined", details={"team_id": team_id}) return {"match_id": match_id, "player_id": player_id, "team_id": team_id, "url": resource_uri, }, httplib.CREATED, response_header