def post(self, *args, **kwargs): game_objects = { "game_level": GameLevel, "corporation": Corporation, "flag": Flag, "box": Box, "hint": Hint, } obj_name = self.get_argument("obj", "") uuid = self.get_argument("uuid", "") if obj_name in game_objects.keys(): obj = game_objects[obj_name].by_uuid(uuid) if obj is not None: self.write(obj.to_dict()) else: self.write({"Error": "Invalid uuid."}) elif obj_name == "stats": flag = Flag.by_uuid(uuid) if flag is not None: if options.banking: price = "$" + str(flag.value) else: price = str(flag.value) + " points" flaginfo = [ { "name": flag.name, "description": flag.description, "token": flag.token, "price": price, } ] captures = [] for item in Flag.captures(flag.id): team = Team.by_id(item[0]) if team: captures.append({"name": team.name}) attempts = self.attempts(flag) hints = [] for item in Hint.taken_by_flag(flag.id): team = Team.by_id(item.team_id) hint = Hint.by_id(item.hint_id) if team: if options.banking: price = "$" + str(hint.price) else: price = str(hint.price) + " points" hints.append({"name": team.name, "price": price}) obj = { "flag": flaginfo, "captures": captures, "attempts": attempts, "hints": hints, } self.write(obj) else: self.write({"Error": "Invalid uuid."}) else: self.write({"Error": "Invalid object type."}) self.finish()
def post(self, *args, **kwargs): game_objects = { 'game_level': GameLevel, 'corporation': Corporation, 'flag': Flag, 'box': Box, 'hint': Hint, } obj_name = self.get_argument('obj', '') uuid = self.get_argument('uuid', '') if obj_name in game_objects.keys(): obj = game_objects[obj_name].by_uuid(uuid) if obj is not None: self.write(obj.to_dict()) else: self.write({'Error': 'Invalid uuid.'}) elif obj_name == "stats": flag = Flag.by_uuid(uuid) if flag is not None: if options.banking: flaginfo = [{"name": flag.name, "token": flag.token, "price": "$" + str(flag.value)}] else: flaginfo = [{"name": flag.name, "token": flag.token, "price": str(flag.value) + " points"}] captures = [] for item in Flag.captures(flag.id): team = Team.by_id(item[0]) if team: captures.append({"name": team.name}) attempts = [] for item in Penalty.by_flag_id(flag.id): team = Team.by_id(item.team_id) if team: attempts.append({"name": team.name, "token": item.token}) hints = [] for item in Hint.taken_by_flag(flag.id): team = Team.by_id(item.team_id) hint = Hint.by_id(item.hint_id) if team: if options.banking: hints.append({"name": team.name, "price": "$" + str(hint.price)}) else: hints.append({"name": team.name, "price": str(hint.price) + " points"}) obj = { "flag": flaginfo, "captures": captures, "attempts": attempts, "hints": hints, } self.write(obj) else: self.write({'Error': 'Invalid uuid.'}) else: self.write({'Error': 'Invalid object type.'}) self.finish()
def attempt_capture(self, flag, submission): """ Compares a user provided token to the token in the db """ user = self.get_current_user() team = user.team logging.info("%s (%s) capture the flag '%s'" % (user.handle, team.name, flag.name)) if submission is not None and flag not in team.flags: if flag.capture(submission): flag_value = flag.dynamic_value(team) if (self.config.dynamic_flag_value and self.config.dynamic_flag_type == "decay_all"): for item in Flag.captures(flag.id): tm = Team.by_id(item[0]) deduction = flag.dynamic_value(tm) - flag_value tm.money = int(tm.money - deduction) self.dbsession.add(tm) self.event_manager.flag_decayed(tm, flag) team.money += flag_value user.money += flag_value team.flags.append(flag) self.dbsession.add(user) self.dbsession.add(team) self.dbsession.commit() self.event_manager.flag_captured(team, flag) return True return False
def post(self, *args, **kwargs): ''' Called to purchase an item ''' uuid = self.get_argument('uuid', '') item = MarketItem.by_uuid(uuid) if not item is None: user = self.get_current_user() team = Team.by_id(user.team.id) # Refresh object if user.has_item(item.name): self.render('market/view.html', user=user, errors=["You have already purchased this item."]) elif team.money < item.price: message = "You only have $%d" % (team.money, ) self.render('market/view.html', user=user, errors=[message]) else: logging.info("%s (%s) purchased the market item '%s' for $%d" % (user.handle, team.name, item.name, item.price)) self.purchase_item(team, item) event = self.event_manager.create_purchased_item_event( user, item) self.new_events.append(event) self.redirect('/user/market') else: self.render('market/view.html', user=self.get_current_user(), errors=["Item does not exist."])
def post(self, *args, **kwargs): """ Called to purchase an item """ uuid = self.get_argument("uuid", "") item = MarketItem.by_uuid(uuid) if item is not None: user = self.get_current_user() team = Team.by_id(user.team.id) # Refresh object if user.has_item(item.name): self.render( "market/view.html", user=user, errors=["You have already purchased this item."], ) elif team.money < item.price: if options.banking: money = "$%d" % team.money else: money = "%d points" % team.money message = "You only have %s" % (money) self.render("market/view.html", user=user, errors=[message]) else: logging.info("%s (%s) purchased '%s' for $%d" % (user.handle, team.name, item.name, item.price)) self.purchase_item(team, item) self.event_manager.item_purchased(user, item) self.redirect("/user/market") else: self.render( "market/view.html", user=self.get_current_user(), errors=["Item does not exist."], )
def post(self, *args, **kwargs): ''' Called to purchase an item ''' uuid = self.get_argument('uuid', '') item = MarketItem.by_uuid(uuid) if not item is None: user = self.get_current_user() team = Team.by_id(user.team.id) # Refresh object if user.has_item(item.name): self.render('market/view.html', user=user, errors=["You have already purchased this item."] ) elif team.money < item.price: message = "You only have $%d" % (team.money,) self.render('market/view.html', user=user, errors=[message]) else: logging.info("%s (%s) purchased '%s' for $%d" % ( user.handle, team.name, item.name, item.price )) self.purchase_item(team, item) self.event_manager.item_purchased(user, item) self.redirect('/user/market') else: self.render('market/view.html', user=self.get_current_user(), errors=["Item does not exist."] )
def attempts(self, flag): attempts = [] teamcount = {} for item in Penalty.by_flag_id(flag.id): team = Team.by_id(item.team_id) if team.id in teamcount: teamcount.update({team.id: teamcount[team.id] + 1}) else: teamcount.update({team.id: 1}) if team: if team.id in teamcount: teamcount.update({team.id: teamcount[team.id] + 1}) else: teamcount.update({team.id: 1}) entries = { "name": team.name, "token": item.token, "flag": flag.uuid, "team": team.uuid, "type": flag.type, } if options.penalize_flag_value: penalty = "-" if options.banking: penalty += "$" if (teamcount[team.id] < options.flag_start_penalty or teamcount[team.id] > options.flag_stop_penalty): penalty += "0" else: penalty += str( int(flag.value * (options.flag_penalty_cost * 0.01))) entries.update({"penalty": penalty}) attempts.append(entries) return attempts
def dynamic_value(self, team=None): if options.dynamic_flag_value is False: return self.value elif len(self.team_captures(self.id)) == 0: return self.value elif team and self in team.flags: depreciation = float(old_div(options.flag_value_decrease, 100.0)) deduction = self.value * depreciation if options.dynamic_flag_type == "decay_all": reduction = (len(self.team_captures(self.id)) - 1) * deduction return max(options.flag_value_minimum, int(self.value - reduction)) else: for index, item in enumerate(self.team_captures(self.id)): if team == Team.by_id(item[0]): reduction = index * deduction return max(options.flag_value_minimum, int(self.value - reduction)) else: depreciation = float(old_div(options.flag_value_decrease, 100.0)) deduction = self.value * depreciation reduction = len(self.team_captures(self.id)) * deduction return max(options.flag_value_minimum, int(self.value - reduction))
def post(self, *args, **kwargs): if args[0] == "statistics" or args[0] == "game_objects": uri = { "game_objects": "admin/view/game_objects.html", "statistics": "admin/view/statistics.html", } flag_uuid = self.get_argument("flag_uuid", "") team_uuid = self.get_argument("team_uuid", "") flag = Flag.by_uuid(flag_uuid) team = Team.by_uuid(team_uuid) errors = [] success = [] if flag: point_restore = self.get_argument("point_restore", "") accept_answer = self.get_argument("accept_answer", "") answer_token = self.get_argument("answer_token", "") if point_restore == "on" and options.penalize_flag_value and team: value = int( flag.dynamic_value(team) * (options.flag_penalty_cost * 0.01)) team.money += value penalty = Penalty.by_team_token(flag, team, answer_token) if penalty: self.dbsession.delete(penalty) self.dbsession.add(team) self.dbsession.commit() self.event_manager.admin_score_update( team, "%s penalty reversed - score has been updated." % team.name, value, ) if flag not in team.flags: flag_value = flag.dynamic_value(team) if (self.config.dynamic_flag_value and self.config.dynamic_flag_type == "decay_all"): for item in Flag.captures(flag.id): tm = Team.by_id(item[0]) deduction = flag.dynamic_value(tm) - flag_value tm.money = int(tm.money - deduction) self.dbsession.add(tm) self.event_manager.flag_decayed(tm, flag) team.money += flag_value team.flags.append(flag) self.dbsession.add(team) self.dbsession.commit() self.event_manager.flag_captured(team, flag) self._check_level(flag, team) success.append("%s awarded %d" % (team.name, flag_value)) if (accept_answer == "on" and (flag.type == "static" or flag.type == "regex") and not flag.capture(answer_token)): flag.type = "regex" if flag.token.startswith("(") and flag.token.endwith(")"): token = "%s|(%s)" % (flag.token, answer_token) else: token = "(%s)|(%s)" % (flag.token, answer_token) if len(token) < 256: flag.token = token self.dbsession.add(flag) self.dbsession.commit() success.append("Token successfully added for Flag %s" % flag.name) else: errors.append( "Flag token too long. Can not expand token.") self.render(uri[args[0]], errors=errors, success=success) else: self.render("public/404.html")