def create_game_level(self): ''' Creates a new level in the database, the levels are basically a linked-list where each level points to the next, and the last points to None. This function creates a new level and sorts everything based on the 'number' attrib ''' try: new_level = GameLevel() new_level.number = self.get_argument('level_number', '') new_level.buyout = self.get_argument('buyout', 0) new_level.name = self.get_argument('name', None) new_level._type = self.get_argument('type', 'buyout') new_level._reward = self.get_argument('reward', 0) if new_level._type == "progress": new_level.buyout = min(new_level.buyout, 100) elif new_level._type == "none": new_level.buyout = 0 if new_level._type != "none" and new_level.buyout == 0: new_level._type = "none" self.dbsession.add(new_level) self.dbsession.flush() game_levels = sorted(GameLevel.all()) for index, level in enumerate(game_levels[:-1]): level.next_level_id = game_levels[index + 1].id self.dbsession.add(level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.commit() self.redirect('/admin/view/game_levels') except ValidationError as error: self.render('admin/create/game_level.html', errors=[str(error), ])
def create_team(self): ''' Admins can create teams manually ''' try: name = self.get_argument('team_name', '') if Team.by_name(name) is not None: raise ValidationError("Team already exists") team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') if not self.config.banking: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) self.dbsession.add(team) self.dbsession.commit() # Avatar avatar_select = self.get_argument('team_avatar_select', '') if avatar_select and len(avatar_select) > 0: team._avatar = avatar_select elif hasattr(self.request, 'files') and 'avatar' in self.request.files: team.avatar = self.request.files['avatar'][0]['body'] self.dbsession.add(team) self.dbsession.commit() self.redirect('/admin/users') except ValidationError as error: self.render("admin/create/team.html", errors=[str(error), ])
def create_box(self): ''' Create a box object ''' try: game_level = self.get_argument('game_level', '') corp_uuid = self.get_argument('corporation_uuid', '') if Box.by_name(self.get_argument('name', '')) is not None: raise ValidationError("Box name already exists") elif Corporation.by_uuid(corp_uuid) is None: raise ValidationError("Corporation does not exist") elif GameLevel.by_number(game_level) is None: raise ValidationError("Game level does not exist") else: corp = Corporation.by_uuid(corp_uuid) level = GameLevel.by_number(game_level) box = Box(corporation_id=corp.id, game_level_id=level.id) box.name = self.get_argument('name', '') box.description = self.get_argument('description', '') box.autoformat = self.get_argument('autoformat', '') == 'true' box.difficulty = self.get_argument('difficulty', '') self.dbsession.add(box) self.dbsession.commit() if hasattr(self.request, 'files') and 'avatar' in self.request.files: box.avatar = self.request.files['avatar'][0]['body'] self.dbsession.commit() self.redirect('/admin/view/game_objects') except ValidationError as error: self.render('admin/create/box.html', errors=[ str(error), ])
def edit_game_level(self): ''' Update game level objects ''' try: level = GameLevel.by_uuid(self.get_argument('uuid', '')) if level is None: raise ValidationError("Game level does not exist") if int(self.get_argument('number', level.number)) != level.number: level.number = self.get_argument('number') level.buyout = self.get_argument('buyout', '1') self.dbsession.add(level) self.dbsession.flush() # Fix the linked-list game_levels = sorted(GameLevel.all()) for index, game_level in enumerate(game_levels[:-1]): game_level.next_level_id = game_levels[index + 1].id self.dbsession.add(game_level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.add(level) self.dbsession.commit() self.redirect('/admin/view/game_levels') except ValueError: raise ValidationError("That was not a number ...") except ValidationError as error: self.render("admin/view/game_levels.html", errors=[ str(error), ])
def create_game_level(self): ''' Creates a new level in the database, the levels are basically a linked-list where each level points to the next, and the last points to None. This function creates a new level and sorts everything based on the 'number' attrib ''' try: new_level = GameLevel() new_level.number = self.get_argument('level_number', '') new_level.buyout = self.get_argument('buyout', '') self.dbsession.add(new_level) self.dbsession.flush() game_levels = sorted(GameLevel.all()) for index, level in enumerate(game_levels[:-1]): level.next_level_id = game_levels[index + 1].id self.dbsession.add(level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.commit() self.redirect('/admin/view/game_levels') except ValidationError as error: self.render('admin/create/game_level.html', errors=[ str(error), ])
def edit_game_level(self): ''' Update game level objects ''' try: level = GameLevel.by_uuid(self.get_argument('uuid', '')) if level is None: raise ValidationError("Game level does not exist") if int(self.get_argument('number', level.number)) != level.number: level.number = self.get_argument('number') level.buyout = self.get_argument('buyout', '1') self.dbsession.add(level) self.dbsession.flush() # Fix the linked-list game_levels = sorted(GameLevel.all()) for index, game_level in enumerate(game_levels[:-1]): game_level.next_level_id = game_levels[index + 1].id self.dbsession.add(game_level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.add(level) self.dbsession.commit() self.redirect('/admin/view/game_levels') except ValueError: raise ValidationError("That was not a number ...") except ValidationError as error: self.render("admin/view/game_levels.html", errors=[str(error), ])
def edit_game_level(self): ''' Update game level objects ''' errors = [] level = GameLevel.by_uuid(self.get_argument('uuid')) try: new_number = int(self.get_argument('number', '')) new_buyout = int(self.get_argument('buyout', '')) if level.number != new_number and GameLevel.by_number(new_number) is None: level.number = new_number self.dbsession.add(level) # Fix the linked-list game_levels = sorted(GameLevel.all()) index = 0 for game_level in game_levels[:-1]: game_level.next_level_id = game_levels[index + 1].id self.dbsession.add(game_level) index += 1 game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) if GameLevel.by_number(new_number) is not None: errors.append("GameLevel number is not unique") if level.buyout != new_buyout: level.buyout = new_buyout self.dbsession.add(level) self.dbsession.commit() except ValueError: errors.append("That was not a number ...") self.render("admin/view/game_levels.html", errors=errors)
def success_capture(self, flag, old_reward=None): if self.config.teams: teamval = "team's " else: teamval = "" user = self.get_current_user() old_reward = flag.value if old_reward is None else old_reward reward_dialog = flag.name + " answered correctly. " if self.config.banking: reward_dialog += ("$" + str(old_reward) + " has been added to your " + teamval + "account.") else: reward_dialog += (str(old_reward) + " points added to your " + teamval + "score.") success = [reward_dialog] # Check for Box Completion box = flag.box if box.is_complete(user): success.append("Congratulations! You have completed " + box.name + ".") # Check for Level Completion level = GameLevel.by_id(box.game_level_id) level_progress = old_div(len(user.team.level_flags(level.number)), float(len(level.flags))) if level_progress == 1.0 and level not in user.team.game_levels: reward_dialog = "" if level._reward > 0: user.team.money += level._reward self.dbsession.add(user.team) self.dbsession.flush() self.dbsession.commit() if self.config.banking: reward_dialog += ("$" + str(level._reward) + " has been added to your " + teamval + "account.") else: reward_dialog += (str(level._reward) + " points added to your " + teamval + "score.") success.append("Congratulations! You have completed " + str(level.name) + ". " + reward_dialog) # Unlock next level if based on Game Progress next_level = GameLevel.by_id(level.next_level_id) if (next_level and next_level._type == "progress" and level_progress * 100 >= next_level.buyout and next_level not in user.team.game_levels): logging.info("%s (%s) unlocked %s" % (user.handle, user.team.name, next_level.name)) user.team.game_levels.append(next_level) self.dbsession.add(user.team) self.dbsession.commit() self.event_manager.level_unlocked(user, next_level) success.append("Congratulations! You have unlocked " + str(next_level.name)) return success
def create_box(self): ''' Create a box object ''' try: game_level = self.get_argument('game_level', '') corp_uuid = self.get_argument('corporation_uuid', '') if Box.by_name(self.get_argument('name', '')) is not None: raise ValidationError("Box name already exists") elif Corporation.by_uuid(corp_uuid) is None: raise ValidationError("Corporation does not exist") elif GameLevel.by_number(game_level) is None: raise ValidationError("Game level does not exist") else: corp = Corporation.by_uuid(corp_uuid) level = GameLevel.by_number(game_level) box = Box(corporation_id=corp.id, game_level_id=level.id) box.name = self.get_argument('name', '') box.description = self.get_argument('description', '') box.autoformat = self.get_argument('autoformat', '') == 'true' box.difficulty = self.get_argument('difficulty', '') self.dbsession.add(box) self.dbsession.commit() if hasattr(self.request, 'files') and 'avatar' in self.request.files: box.avatar = self.request.files['avatar'][0]['body'] self.dbsession.commit() self.redirect('/admin/view/game_objects') except ValidationError as error: self.render('admin/create/box.html', errors=[str(error), ])
def post(self, *args, **kwargs): ''' Reset the Game ''' errors = [] success = None try: users = User.all() for user in users: user.money = 0 teams = Team.all() for team in teams: if options.banking: team.money = options.starting_team_money else: team.money = 0 team.flags = [] team.hints = [] team.boxes = [] team.items = [] team.purchased_source_code = [] level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels = [level_0] self.dbsession.add(team) self.dbsession.commit() self.dbsession.flush() for team in teams: for paste in team.pastes: self.dbsession.delete(paste) for shared_file in team.files: shared_file.delete_data() self.dbsession.delete(shared_file) self.dbsession.commit() self.dbsession.flush() Penalty.clear() Notification.clear() snapshot = Snapshot.all() for snap in snapshot: self.dbsession.delete(snap) self.dbsession.commit() snapshot_team = SnapshotTeam.all() for snap in snapshot_team: self.dbsession.delete(snap) self.dbsession.commit() game_history = GameHistory.instance() game_history.take_snapshot() # Take starting snapshot flags = Flag.all() for flag in flags: flag.value = flag._original_value if flag._original_value else flag.value self.dbsession.add(flag) self.dbsession.commit() self.dbsession.flush() success = "Successfully Reset Game" self.render('admin/reset.html', success=success, errors=errors) except BaseException as e: errors.append("Failed to Reset Game") logging.error(str(e)) self.render('admin/reset.html', success=None, errors=errors)
def create_team(self): """ Admins can create teams manually """ try: name = self.get_argument("team_name", "") if Team.by_name(name) is not None: raise ValidationError("Team already exists") team = Team() team.name = self.get_argument("team_name", "") team.motto = self.get_argument("motto", "") if not self.config.banking: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) self.dbsession.add(team) self.dbsession.commit() # Avatar avatar_select = self.get_argument("team_avatar_select", "") if avatar_select and len(avatar_select) > 0: team._avatar = avatar_select elif hasattr(self.request, "files") and "avatar" in self.request.files: team.avatar = self.request.files["avatar"][0]["body"] self.dbsession.add(team) self.dbsession.commit() self.event_manager.push_score_update() self.redirect("/admin/users") except ValidationError as error: self.render("admin/create/team.html", errors=[str(error)])
def create_team(self): ''' Admins can create teams manually ''' try: name = self.get_argument('team_name', '') if Team.by_name(name) is not None: raise ValidationError("Team already exists") team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') if not self.config.banking: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) self.dbsession.add(team) self.dbsession.commit() # Avatar avatar_select = self.get_argument('team_avatar_select', '') if avatar_select and len(avatar_select) > 0: team._avatar = avatar_select elif hasattr(self.request, 'files') and 'avatar' in self.request.files: team.avatar = self.request.files['avatar'][0]['body'] self.dbsession.add(team) self.dbsession.commit() self.redirect('/admin/users') except ValidationError as error: self.render("admin/create/team.html", errors=[ str(error), ])
def flag_captured(self, player, flag): """ Callback for when a flag is captured """ if isinstance(player, User): team = player.team else: team = player if isinstance(player, User) and options.teams: message = '%s (%s) has completed "%s" in %s' % ( player.handle, team.name, flag.name, flag.box.name, ) else: message = '%s has completed "%s" in %s' % ( team.name, flag.name, flag.box.name, ) if len(GameLevel.all()) > 1: message = message + " (%s)" % ( GameLevel.by_id(flag.box.game_level_id).name, ) Notification.create_broadcast(team, "Flag Capture", message, SUCCESS) self.io_loop.add_callback(self.push_broadcast) self.io_loop.add_callback(self.push_scoreboard)
def success_capture(self, flag, old_reward=None): if self.config.teams: teamval = "team's " else: teamval = "" user = self.get_current_user() old_reward = flag.value if old_reward is None else old_reward reward_dialog = flag.name + " answered correctly. " if self.config.banking: reward_dialog += "$" + str(old_reward) + " has been added to your " + teamval + "account." else: reward_dialog += str(old_reward) + " points added to your " + teamval + "score." success = [reward_dialog] # Check for Box Completion boxcomplete = True box = flag.box for boxflag in box.flags: if not boxflag in user.team.flags: boxcomplete = False break if boxcomplete: success.append("Congratulations! You have completed " + box.name + ".") # Check for Level Completion level = GameLevel.by_id(box.game_level_id) level_progress = len(user.team.level_flags(level.number)) / float(len(level.flags)) if level_progress == 1.0 and level not in user.team.game_levels: reward_dialog = "" if level._reward > 0: user.team.money += level._reward self.dbsession.add(user.team) self.dbsession.flush() self.dbsession.commit() if self.config.banking: reward_dialog += "$" + str(level._reward) + " has been added to your " + teamval + "account." else: reward_dialog += str(level._reward) + " points added to your " + teamval + "score." success.append("Congratulations! You have completed " + str(level.name) + ". " + reward_dialog) # Unlock next level if based on Game Progress next_level = GameLevel.by_id(level.next_level_id) if next_level and next_level._type == "progress" and level_progress * 100 >= next_level.buyout and next_level not in user.team.game_levels: logging.info("%s (%s) unlocked %s" % ( user.handle, user.team.name, next_level.name )) user.team.game_levels.append(next_level) self.dbsession.add(user.team) self.dbsession.commit() self.event_manager.level_unlocked(user, next_level) success.append("Congratulations! You have unlocked " + str(next_level.name)) return success
def hint_taken(self, user, hint): ''' Callback for when a hint is taken ''' if len(GameLevel.all()) > 1: message = "%s has taken a hint for %s (Lvl %s)" % ( user.team.name, hint.box.name, GameLevel.by_id(hint.box.game_level_id).number) else: message = "%s has taken a hint for %s" % (user.team.name, hint.box.name) Notification.create_team(user.team, "Hint Taken", message, INFO) self.io_loop.add_callback(self.push_team, user.team.id) self.io_loop.add_callback(self.push_scoreboard)
def flag_penalty(self, user, flag): ''' Callback for when a flag is captured ''' if len(GameLevel.all()) > 1: message = "%s was penalized on the '%s' flag in %s (Lvl %s)" % ( user.team.name, flag.name, flag.box.name, GameLevel.by_id(flag.box.game_level_id).number) else: message = "%s was penalized on the '%s' flag in %s" % ( user.team.name, flag.name, flag.box.name) Notification.create_team(user.team, "Flag Penalty", message, WARNING) self.io_loop.add_callback(self.push_team, user.team.id) self.io_loop.add_callback(self.push_scoreboard)
def export_game_objects(self, root): ''' Exports the game objects to an XML doc. For the record, I hate XML with a passion. ''' levels_elem = ET.SubElement(root, "gamelevels") levels_elem.set("count", str(GameLevel.count())) for level in GameLevel.all()[1:]: level.to_xml(levels_elem) corps_elem = ET.SubElement(root, "corporations") corps_elem.set("count", str(Corporation.count())) for corp in Corporation.all(): corp.to_xml(corps_elem)
def hint_taken(self, user, hint): """ Callback for when a hint is taken """ if options.teams: message = "%s has taken a hint for %s" % (user.handle, hint.box.name) else: message = "%s has taken a hint for %s" % (user.team.name, hint.box.name) if len(GameLevel.all()) > 1: message = message + " (%s)" % ( GameLevel.by_id(hint.box.game_level_id).name, ) Notification.create_team(user.team, "Hint Taken", message, INFO) self.io_loop.add_callback(self.push_team, user.team.id) self.io_loop.add_callback(self.push_scoreboard)
def flag_captured(self, user, flag): ''' Callback for when a flag is captured ''' if len(GameLevel.all()) > 1: message = "%s has captured the '%s' flag in %s (Lvl %s)" % ( user.team.name, flag.name, flag.box.name, GameLevel.by_id(flag.box.game_level_id).number) else: message = "%s has captured the '%s' flag in %s" % ( user.team.name, flag.name, flag.box.name) Notification.create_broadcast(user.team, "Flag Capture", message, SUCCESS) self.io_loop.add_callback(self.push_broadcast) self.io_loop.add_callback(self.push_scoreboard)
def create_team(self): """ Create a new team """ if not self.config.teams: team = Team.by_name(self.get_argument("handle", "")) if team is None: team = Team() team.name = self.get_argument("handle", "") else: logging.info( "Team %s already exists - Player Mode: reset team." % team.name ) team.flags = [] team.hints = [] team.boxes = [] team.items = [] team.game_levels = [] team.purchased_source_code = [] team.motto = self.get_argument("motto", "") team._avatar = identicon(team.name, 6) if self.config.banking: team.money = self.config.starting_team_money else: team.money = 0 levels = GameLevel.all() for level in levels: if level.type == "none": team.game_levels.append(level) elif level.type != "hidden" and level.buyout == 0: team.game_levels.append(level) return team elif self.config.public_teams: if Team.by_name(self.get_argument("team_name", "")) is not None: raise ValidationError( "This team name is already registered. Use team code to join that team." ) team = Team() team.name = self.get_argument("team_name", "") team.motto = self.get_argument("motto", "") if len(filter_avatars("team")) == 0: team._avatar = identicon(team.name, 6) if not self.config.banking: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) return team else: raise ValidationError("Public teams are not enabled")
def create_box(self): """ Create a box object """ try: game_level = self.get_argument("game_level", "") corp_uuid = self.get_argument("corporation_uuid", "") if Box.by_name(self.get_argument("name", "")) is not None: raise ValidationError("Box name already exists") elif GameLevel.by_number(game_level) is None: raise ValidationError("Game level does not exist") else: if Corporation.by_uuid(corp_uuid) is None: if len(Corporation.all()) == 0: # Create a empty Corp corporation = Corporation() corporation.name = "" self.dbsession.add(corporation) self.dbsession.commit() corp_uuid = corporation.uuid else: raise ValidationError("Corporation does not exist") corp = Corporation.by_uuid(corp_uuid) level = GameLevel.by_number(game_level) box = Box(corporation_id=corp.id, game_level_id=level.id) box.name = self.get_argument("name", "") box.description = self.get_argument("description", "") box.flag_submission_type = FlagsSubmissionType[ self.get_argument("flag_submission_type", "")] box.difficulty = self.get_argument("difficulty", "") box.operating_system = self.get_argument( "operating_system", "?") box.capture_message = self.get_argument("capture_message", "") box.value = self.get_argument("reward", 0) cat = Category.by_uuid(self.get_argument("category_uuid", "")) if cat is not None: box.category_id = cat.id else: box.category_id = None # Avatar avatar_select = self.get_argument("box_avatar_select", "") if avatar_select and len(avatar_select) > 0: box._avatar = avatar_select elif hasattr(self.request, "files") and "avatar" in self.request.files: box.avatar = self.request.files["avatar"][0]["body"] self.dbsession.add(box) self.dbsession.commit() self.redirect("/admin/view/game_objects#%s" % box.uuid) except ValidationError as error: self.render("admin/create/box.html", errors=[str(error)])
def create_team(self, user): team = Team() team.name = user.handle team.motto = "" team._avatar = identicon(team.name, 6) if self.config.banking: team.money = self.config.starting_team_money else: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) self.dbsession.add(team) return team
def buyout(self): ''' Buyout and unlock a level ''' user = self.get_current_user() level = GameLevel.by_uuid(self.get_argument('uuid', '')) if level is not None and user is not None: if level.buyout <= user.team.money: logging.info("%s (%s) payed buyout for level #%d" % ( user.handle, user.team.name, level.number )) user.team.game_levels.append(level) user.team.money -= level.buyout self.dbsession.add(user.team) self.dbsession.commit() event = self.event_manager.create_unlocked_level_event(user, level) self.new_events.append(event) self.redirect("/user/missions") else: self.render("missions/view.html", team=user.team, errors=["You do not have enough money to unlock this level"] ) else: self.render("missions/view.html", team=user.team, errors=["Level does not exist"] )
def buyout(self): ''' Buyout and unlock a level ''' user = self.get_current_user() level = GameLevel.by_uuid(self.get_argument('uuid', '')) if level is not None and user is not None: if level.buyout <= user.team.money: logging.info("%s (%s) payed buyout for level #%d" % (user.handle, user.team.name, level.number)) user.team.game_levels.append(level) user.team.money -= level.buyout self.dbsession.add(user.team) self.dbsession.commit() self.event_manager.level_unlocked(user, level) self.redirect("/user/missions") else: self.render( "missions/view.html", team=user.team, errors=[ "You do not have enough money to unlock this level" ]) else: self.render("missions/view.html", team=user.team, errors=["Level does not exist"])
def get(self, *args, **kwargs): """ Renders the box details page. """ uuid = self.get_argument("uuid", "") box = Box.by_uuid(uuid) if box is not None: user = self.get_current_user() level = GameLevel.by_id(box.game_level_id) if ( user.team and level.type != "none" and level not in user.team.game_levels ): self.redirect("/403") elif box.locked: self.render( "missions/status.html", errors=None, info=["This box is currently locked by the Admin."], ) else: self.render( "missions/box.html", box=box, user=user, team=user.team, errors=[], success=[], info=[], ) else: self.render("public/404.html")
def create_team(self): ''' Create a new team ''' if not self.config.teams: team = Team.by_name(self.get_argument('handle', '')) if team is None: team = Team() team.name = self.get_argument('handle', '') else: logging.info( "Team %s already exists - Player Mode: reset team." % team.name) team.flags = [] team.hints = [] team.boxes = [] team.items = [] team.game_levels = [] team.purchased_source_code = [] team.motto = self.get_argument('motto', '') if self.config.banking: team.money = self.config.starting_team_money else: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) return team elif self.config.public_teams: if Team.by_name(self.get_argument('team_name', '')) is not None: raise ValidationError( "This team name is already registered. Use team code to join that team." ) team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') if not self.config.banking: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) return team else: raise ValidationError("Public teams are not enabled")
def create_team(self): ''' Create a new team in the database ''' team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') level_0 = GameLevel.all()[0] team.game_levels.append(level_0) self.dbsession.add(team) self.dbsession.commit() self.redirect('/admin/view/user_objects')
def create_team(self): ''' Create a new team ''' if self.config.public_teams: team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') level_0 = GameLevel.all()[0] team.game_levels.append(level_0) return team else: raise ValidationError("Public teams are not enabled")
def del_game_level(self): ''' Deletes a game level, and fixes the linked list ''' game_level = GameLevel.by_uuid(self.get_argument('uuid', '')) if game_level is not None: game_levels = sorted(GameLevel.all()) game_levels.remove(game_level) for index, level in enumerate(game_levels[:-1]): level.next_level_id = game_levels[index + 1].id self.dbsession.add(level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.delete(game_level) self.dbsession.commit() self.redirect('/admin/view/game_levels') else: self.render('admin/view/game_levels.html', errors=["Game level does not exist in database."])
def create_box(self): ''' Create a box object ''' try: game_level = self.get_argument('game_level', '') corp_uuid = self.get_argument('corporation_uuid', '') if Box.by_name(self.get_argument('name', '')) is not None: raise ValidationError("Box name already exists") elif Corporation.by_uuid(corp_uuid) is None: raise ValidationError("Corporation does not exist") elif GameLevel.by_number(game_level) is None: raise ValidationError("Game level does not exist") else: corp = Corporation.by_uuid(corp_uuid) level = GameLevel.by_number(game_level) box = Box(corporation_id=corp.id, game_level_id=level.id) box.name = self.get_argument('name', '') box.description = self.get_argument('description', '') box.flag_submission_type = FlagsSubmissionType[ self.get_argument('flag_submission_type', '')] box.difficulty = self.get_argument('difficulty', '') box.operating_system = self.get_argument( 'operating_system', '?') cat = Category.by_uuid(self.get_argument('category_uuid', '')) if cat is not None: box.category_id = cat.id else: box.category_id = None # Avatar avatar_select = self.get_argument('box_avatar_select', '') if avatar_select and len(avatar_select) > 0: box._avatar = avatar_select elif hasattr(self.request, 'files') and 'avatar' in self.request.files: box.avatar = self.request.files['avatar'][0]['body'] self.dbsession.add(box) self.dbsession.commit() self.redirect("/admin/view/game_objects#%s" % box.uuid) except ValidationError as error: self.render('admin/create/box.html', errors=[ str(error), ])
def to_dict(self): ''' Returns editable data as a dictionary ''' corp = Corporation.by_id(self.corporation_id) game_level = GameLevel.by_id(self.game_level_id) return { 'name': self.name, 'uuid': self.uuid, 'corporation': corp.uuid, 'description': self._description, 'difficulty': self.difficulty, 'game_level': game_level.uuid, }
def del_game_level(self): ''' Deletes a game level, and fixes the linked list ''' game_level = GameLevel.by_uuid(self.get_argument('uuid', '')) if game_level is not None: game_levels = sorted(GameLevel.all()) game_levels.remove(game_level) for index, level in enumerate(game_levels[:-1]): level.next_level_id = game_levels[index + 1].id self.dbsession.add(level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.delete(game_level) self.dbsession.commit() self.redirect('/admin/view/game_levels') else: self.render('admin/view/game_levels.html', errors=["Game level does not exist in database."] )
def flag_penalty(self, user, flag): """ Callback for when a flag is captured """ if options.teams: message = "%s was penalized on '%s' in %s" % ( user.handle, flag.name, flag.box.name, ) else: message = "%s was penalized on '%s' in %s" % ( user.team.name, flag.name, flag.box.name, ) if len(GameLevel.all()) > 1: message = message + " (%s)" % ( GameLevel.by_id(flag.box.game_level_id).name, ) Notification.create_team(user.team, "Flag Penalty", message, WARNING) self.io_loop.add_callback(self.push_team, user.team.id) self.io_loop.add_callback(self.push_scoreboard)
def create_box(corp=None): if corp is None: corp = create_corp() game_level = GameLevel.all()[0] box = Box(corporation_id=corp.id, game_level_id=game_level.id) box.name = "TestBox" box.description = "Some description" box.difficuly = "Easy" corp.boxes.append(box) dbsession.add(box) dbsession.commit() return box, corp
def create_team(self): ''' Create a new team ''' if not self.config.teams: team = Team.by_name(self.get_argument('handle', '')) if team is None: team = Team() team.name = self.get_argument('handle', '') else: logging.info("Team %s already exists - Player Mode: reset team." % team.name) team.flags = [] team.hints = [] team.boxes = [] team.items = [] team.game_levels = [] team.purchased_source_code = [] team.motto = self.get_argument('motto', '') if self.config.banking: team.money = self.config.starting_team_money else: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) return team elif self.config.public_teams: if Team.by_name(self.get_argument('team_name', '')) is not None: raise ValidationError("This team name is already registered. Use team code to join that team.") team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') if not self.config.banking: team.money = 0 level_0 = GameLevel.by_number(0) if not level_0: level_0 = GameLevel.all()[0] team.game_levels.append(level_0) return team else: raise ValidationError("Public teams are not enabled")
def edit_game_level(self): """ Update game level objects """ try: level = GameLevel.by_uuid(self.get_argument("uuid", "")) if level is None: raise ValidationError("Game level does not exist") if int(self.get_argument("number", level.number)) != level.number: level.number = self.get_argument("number", "") level.buyout = self.get_argument("buyout", 1) level._type = self.get_argument("type", "buyout") level._reward = self.get_argument("reward", 0) level.name = self.get_argument("name", None) level.description = self.get_argument("description", "") if level._type == "progress": level.buyout = min(level.buyout, 100) elif level._type == "none": level.buyout = 0 if level._type != "none" and level.buyout == 0: level._type = "none" self.dbsession.add(level) self.dbsession.flush() # Fix the linked-list game_levels = sorted(GameLevel.all()) for index, game_level in enumerate(game_levels[:-1]): game_level.next_level_id = game_levels[index + 1].id self.dbsession.add(game_level) if game_levels[0].number != 0: game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.add(level) self.dbsession.commit() self.redirect("/admin/view/game_levels") except ValueError: raise ValidationError("That was not a number ...") except ValidationError as error: self.render("admin/view/game_levels.html", errors=[str(error)])
def box_level(self): """ Changes a boxs level """ errors = [] box = Box.by_uuid(self.get_argument("box_uuid", "")) level = GameLevel.by_uuid(self.get_argument("level_uuid", "")) if box is not None and level is not None: box.game_level_id = level.id self.dbsession.add(box) self.dbsession.commit() elif box is None: errors.append("Box does not exist") elif level is None: errors.append("GameLevel does not exist") self.render("admin/view/game_levels.html", errors=errors)
def _make_box(self, level_number): ''' Creates a box in the database ''' corp = Corporation.by_uuid(self.get_argument('corporation_uuid')) level = GameLevel.by_number(level_number) box = Box(corporation_id=corp.id, game_level_id=level.id) box.name = self.get_argument('name', '') box.description = self.get_argument('description', '') box.autoformat = self.get_argument('autoformat', '') == 'true' box.difficulty = self.get_argument('difficulty', '') self.dbsession.add(box) self.dbsession.commit() if 'avatar' in self.request.files: box.avatar = self.request.files['avatar'][0]['body'] return box
def _make_level(self, level_number): ''' Creates a new level in the database, the levels are basically a linked-list where each level points to the next, and the last points to None. This function creates a new level and sorts everything based on the 'number' attrib ''' new_level = GameLevel() new_level.number = level_number new_level.buyout = self.get_argument('buyout', '') game_levels = GameLevel.all() game_levels.append(new_level) game_levels = sorted(game_levels) index = 0 for level in game_levels[:-1]: level.next_level_id = game_levels[index + 1].id self.dbsession.add(level) index += 1 game_levels[0].number = 0 self.dbsession.add(game_levels[0]) game_levels[-1].next_level_id = None self.dbsession.add(game_levels[-1]) self.dbsession.commit()
def box_level(self): ''' Changes a boxs level ''' errors = [] box = Box.by_uuid(self.get_argument('box_uuid')) level = GameLevel.by_uuid(self.get_argument('level_uuid')) if box is not None and level is not None: box.game_level_id = level.id self.dbsession.add(box) self.dbsession.commit() elif box is None: errors.append("Box does not exist") elif level is None: errors.append("GameLevel does not exist") self.render("admin/view/game_levels.html", errors=errors)
def create_box(self): ''' Create a box object ''' try: game_level = self.get_argument('game_level', '') corp_uuid = self.get_argument('corporation_uuid', '') if Box.by_name(self.get_argument('name', '')) is not None: raise ValidationError("Box name already exists") elif Corporation.by_uuid(corp_uuid) is None: raise ValidationError("Corporation does not exist") elif GameLevel.by_number(game_level) is None: raise ValidationError("Game level does not exist") else: corp = Corporation.by_uuid(corp_uuid) level = GameLevel.by_number(game_level) box = Box(corporation_id=corp.id, game_level_id=level.id) box.name = self.get_argument('name', '') box.description = self.get_argument('description', '') box.flag_submission_type = FlagsSubmissionType[self.get_argument('flag_submission_type','')] box.difficulty = self.get_argument('difficulty', '') box.operating_system = self.get_argument('operating_system', '?') cat = Category.by_uuid(self.get_argument('category_uuid', '')) if cat is not None: box.category_id = cat.id else: box.category_id = None # Avatar avatar_select = self.get_argument('box_avatar_select', '') if avatar_select and len(avatar_select) > 0: box._avatar = avatar_select elif hasattr(self.request, 'files') and 'avatar' in self.request.files: box.avatar = self.request.files['avatar'][0]['body'] self.dbsession.add(box) self.dbsession.commit() self.redirect("/admin/view/game_objects#%s" % box.uuid) except ValidationError as error: self.render('admin/create/box.html', errors=[str(error), ])
def create_game_level(self): ''' Creates a game level ''' try: lvl = self.get_argument('level_number', '') game_level = abs(int(lvl)) if lvl.isdigit() else 0 if GameLevel.by_number(game_level) is not None: self.render('admin/create/game_level.html', errors=["Game level number must be unique"] ) else: self._make_level(game_level) self.redirect('/admin/view/game_levels') except ValueError: self.render('admin/create/game_level.html', errors=["Invalid level number"] )
def create_team(self): ''' Admins can create teams manually ''' try: name = self.get_argument('team_name', '') motto = self.get_argument('motto', '') if Team.by_name(name) is not None: raise ValidationError("Team already exists") team = Team() team.name = self.get_argument('team_name', '') team.motto = self.get_argument('motto', '') level_0 = GameLevel.all()[0] self.dbsession.add(team) self.dbsession.commit() self.redirect('/admin/users') except ValidationError as error: self.render("admin/create/team.html", errors=[str(error), ])
def create_box(self): ''' Create a box object ''' try: lvl = self.get_argument('game_level', '') game_level = abs(int(lvl)) if lvl.isdigit() else 0 corp_uuid = self.get_argument('corporation_uuid', '') if Box.by_name(self.get_argument('name', '')) is not None: self.render("admin/create/box.html", errors=["Box name already exists"] ) elif Corporation.by_uuid(corp_uuid) is None: self.render("admin/create/box.html", errors=["Corporation does not exist"]) elif GameLevel.by_number(game_level) is None: self.render("admin/create/box.html", errors=["Game level does not exist"]) else: box = self._make_box(game_level) self.dbsession.commit() self.redirect('/admin/view/game_objects') except ValueError as error: self.render('admin/create/box.html', errors=["%s" % error])
def to_dict(self): ''' Returns editable data as a dictionary ''' corp = Corporation.by_id(self.corporation_id) game_level = GameLevel.by_id(self.game_level_id) cat = Category.by_id(self.category_id) if cat: category = cat.uuid else: category = "" return { 'name': self.name, 'uuid': self.uuid, 'corporation': corp.uuid, 'category': category, 'operating_system': self.operating_system, 'description': self._description, 'difficulty': self.difficulty, 'game_level': game_level.uuid, 'flag_submission_type': self.flag_submission_type, 'flaglist': self.flaglist(self.id) }
def game_level(self): return GameLevel.by_id(self.game_level_id)