async def rebuild_match_managers(app): session = db.DBSession() qs = session.query(Match).filter(Match.state == MatchState.NOT_STARTED) for match in qs: manager = MatchManager(match) app['match_managers'][match.id] = manager
async def patch(self): _id = await self.get_object_id() data = await self.request.json() # HACK: exactly the same thing as schema.validate (validate against marshmallow without serializing) # but directly raise marshmallow ValidationError instead of returning a dict of validation errors self.schema._do_load(data, partial=True, postprocess=False) try: session = db.DBSession() match = session.query(Match).filter(Match.id == _id).one() if not _id in self.request.app['match_managers']: raise Exception( "Match manager does not exist for match id {}".format(_id)) manager = self.request.app['match_managers'][_id] for key, value in data.items(): await self._process_update(manager, match, key, value) setattr(match, key, value) session.commit() except: session.rollback() raise return web.json_response(self.schema.dump(match))
async def auth_middleware(request, handler): try: if isfunction(handler): auth_req = hasattr(handler, "isAuth") else: try: auth_req = hasattr(getattr(handler, request.method.lower()), "isAuth") except AttributeError: auth_req = False if auth_req: db_session = db.DBSession() session = await get_session(request) user = db_session.query(ApiUser).filter_by( token=session['session_token']).first() if user is not None: response = await handler(request) return response db_session.commit() raise AuthorizationException("Unknown token") else: response = await handler(request) return response except AuthorizationException: raise AuthorizationException("No bearer provided") except KeyError: raise AuthorizationException("No token provided")
async def cleanup_matches(app): session = db.DBSession() qs = session.query(Match).filter(Match.state != MatchState.NOT_STARTED, Match.state != MatchState.ENDED) for match in qs: await app['match_managers'][match.id].end() match.state = MatchState.ENDED session.commit()
async def post(self): data = await self.request.json() user = self.creation_request_schema.load(data) try: session = db.DBSession() session.add(user) session.commit() except: session.rollback() raise return web.json_response({"success": "The user was successfully created"},status=200)
async def post(self): data = await self.request.json() team = self.schema.load(data) try: session = db.DBSession() session.add(team) session.commit() except: session.rollback() raise return web.json_response(self.schema.dump(team))
async def delete(self): _id = await self.get_object_id() try: session = db.DBSession() server = session.query(Server).filter(Server.id == _id).one() session.delete(server) session.commit() except: session.rollback() raise raise web.HTTPNoContent
async def delete(self): _id = await self.get_object_id() try: session = db.DBSession() team = session.query(Team).filter(Team.id == _id).one() session.delete(team) session.commit() except: session.rollback() raise return web.json_response({"success": "the team has been deleted"}, status=200)
async def patch(self): _id = await self.get_object_id() data = await self.request.json() # HACK: exactly the same thing as schema.validate (validate against marshmallow without serializing) # but directly raise marshmallow ValidationError instead of returning a dict of validation errors self.schema._do_load(data, partial=True, postprocess=False) try: session = db.DBSession() team = session.query(Team).filter(Team.id == _id).one() common.patch_object(team, data) session.commit() except: session.rollback() raise return web.json_response(self.schema.dump(team))
def handle(self, match: Match): self.session = db.DBSession() is_bot = False if self.player_steam_id == 'BOT': self.player_steam_id = match.id * 1000 + self.player_uid is_bot = True self.logger.debug( "Handling EnteredGame event for player {} (id = {})".format( self.player_name, self.player_steam_id)) if not self.session.query(Player).filter( Player.id == self.player_steam_id).count() > 0: pl = Player(id=self.player_steam_id, username=self.player_name, isBot=is_bot) self.session.add(pl) self.session.commit()
async def login(request): userForm = ApiUserAuthSchema() data = await request.json() db_session = db.DBSession() try: userData = userForm.load(data) user = db_session.query(ApiUser).filter_by(username=userData['username']).first() if user is None: raise UserDoestNotExists if user.password != userData['password']: raise PasswordDoesNotMatch user.token = secrets.token_hex(50) # TODO: enforce this secret db_session.commit() session = await new_session(request) session['session_token'] = user.token print(session) return web.json_response({'success':'logged'}) except: db_session.rollback() raise
def handle(self, match: Match): # the SwitchTeamEvent allows us to link the Player to a Team object according to the chosen team : CT or T self.logger.debug("Handling SwitchTeamEvent for player id = {}".format( self.player_steam_id)) self.session = db.DBSession() # XXX: For the moment we take care of the initial team switch only and # we do not consider players that Spectate ('Spectator' team) # We decided that if the player changes team because for example # he made a mistake during initial assignation, it will require a # manual action from tournament admin to change it in the confinebot if self.src_team == 'Unassigned': if self.player_steam_id == 'BOT': self.player_steam_id = match.id * 1000 + self.player_uid qs = self.session.query(Player).filter( Player.id == self.player_steam_id) if qs.count() == 0: # XXX: this can happen if the EnteredGameEvent was not yet handled or if it was not received by log handler. # Although the first part seem unlikely to happen, the second can happen in practice as the log protocol is over UDP ! self.logger.error( "SwitchTeamEvent with non existant player id = {}".format( self.player_steam_id)) raise ReferenceToNonexistentPlayer player = qs.one() if self.dst_team == 'TERRORIST': player.idTeam = match.teamFirstSideT.id elif dst_team == 'CT': player.idTeam = match.teamFirstSideCT.id self.session.commit()
def handle(self, match: Match): self.session = db.DBSession() killer = self.get_player_from_pterm(self.pterm_killer) victim = self.get_player_from_pterm(self.pterm_victim) self.logger.debug("Handling Kill event: {} killed by {}".format( self.pterm_victim[0][0], self.pterm_killer[0][0])) k = Kill( killPosition='{}', # FIXME killerPosition='{}', # FIXME weaponId=None, # FIXME killedId=victim.id, killerId=killer.id, matchId=match.id, roundId=1, # FIXME isHeadshot=self.is_headshot, ) self.session.add(k) self.session.commit()
async def post(self): data = await self.request.json() match = self.schema.load(data) match.firstSideT = 0 match.firstSideCT = 0 match.secondSideT = 0 match.secondSideCT = 0 match.firstSideTerrorist = 'A' match.mapSelectionMode = 'rng' match.state = MatchState.NOT_STARTED session = db.DBSession() try: session.add(match) session.commit() except: session.rollback() raise manager = MatchManager(match) self.request.app['match_managers'][match.id] = manager return web.json_response(self.schema.dump(match))
async def delete(self): _id = await self.get_object_id() try: session = db.DBSession() match = session.query(Match).filter(Match.id == _id).one() if match.state not in [MatchState.NOT_STARTED, MatchState.ENDED]: if not _id in self.request.app['match_managers']: raise Exception( "Match manager does not exist for match id {}".format( _id)) await self.request.app['match_managers'][_id].end() session.delete(match) session.commit() except: session.rollback() raise return web.json_response({"success": "the match has been deleted"}, status=200)
async def _update_match(self): self.session = db.DBSession() self.match = self.session.query(Match).filter( Match.id == self.match.id).one()
async def get(self): _id = await self.get_object_id() session = db.DBSession() match = session.query(Match).filter(Match.id == _id).one() return web.json_response(self.schema.dump(match))
async def get(self): session = db.DBSession() qs = session.query(Match).all() return web.json_response(self.schema.dump(qs, many=True))
async def get(self): _id = await self.get_object_id() session = db.DBSession() team = session.query(Team).filter(Team.id == _id).one() return web.json_response(self.schema.dump(team))
async def get(self): _id = await self.get_object_id() session = db.DBSession() server = session.query(Server).filter(Server.id == _id).one() return web.json_response(self.schema.dump(server))