def details(self, config=None): verb = 'support' if self.hinder: if self.parent: verb = 'oppose' else: verb = "attack" team = num_to_team(self.participant.team, config) amount = self.adjusted_for_buffs() effective = amount if self.parent: effective = self.parent.adjusted_for_type(self.troop_type, effective, not self.hinder) buffs = ["*%s*" % b.name for b in self.buffs] buffs = ", ".join(buffs) if buffs: buffs = " (Buffs: %s) " % buffs wins = "" if self.is_resolved() and self.children: wins = "Victor: %s" % self.winner_str(config) data = (self.id, self.participant.name, team, verb, self.amount, self.troop_type, buffs, amount, effective, wins) command = (" \\#%d %s (%s): **%s with %d %s** %s" "(effective: %d, for above: %d) %s") % data return command
def recruit_from_comment(self, comment): session = self.session if not comment.author: # Deleted comments don't have an author return name = comment.author.name.lower() if name == self.config.username.lower(): return # Is this author already one of us? found = session.query(User).filter_by(name=name).first() if not found: # Getting the author ID triggers a lookup on the userpage. In the # case of banned users, this will 404. @failable would normally # catch that just fine, but I want to check it here so that in the # future, I can do things like e.g. add to an 'ignored' list and # save myself the lookup try: author_id = comment.author.id except NotFound: logging.warn("Ignored banned user %s" % name) return team = 0 assignment = self.config['game']['assignment'] if assignment == 'uid': base10_id = base36decode(author_id) team = base10_id % 2 elif assignment == "random": team = random.randint(0, 1) is_leader = name in self.config["game"]["leaders"] loyalists = self.config["game"].get("starting_troops", 100) newbie = User(name=name, team=team, loyalists=loyalists, leader=is_leader) session.add(newbie) cap = Region.capital_for(newbie.team, session) if not cap: logging.fatal("Could not find capital for %d" % newbie.team) newbie.region = cap session.commit() logging.info("Created combatant %s", newbie) reply = ("Welcome to Chroma! You are now a %s " "in the %s army, commanding a force of loyalists " "%d people strong. You are currently encamped at %s") % ( newbie.rank, num_to_team(newbie.team, self.config), newbie.loyalists, cap.markdown()) team = session.query(TeamInfo).filter_by(id=newbie.team).first() if team: reply = "%s\n\n%s" % (reply, team.greeting) comment.reply(reply) else: #logging.info("Already registered %s", comment.author.name) pass
def winner_str(self, config=None): if self.victor is None: postamble = "**TIE**" else: vstr = num_to_team(self.victor, config) postamble = ("**%s** by %d for **%d VP**" % (vstr, self.margin, self.vp)) return postamble
def recruit_from_comment(self, comment): session = self.session if not comment.author: # Deleted comments don't have an author return name = comment.author.name.lower() if name == self.config.username.lower(): return # Is this author already one of us? found = session.query(User).filter_by( name=name).first() if not found: # Getting the author ID triggers a lookup on the userpage. In the # case of banned users, this will 404. @failable would normally # catch that just fine, but I want to check it here so that in the # future, I can do things like e.g. add to an 'ignored' list and # save myself the lookup try: author_id = comment.author.id except NotFound: logging.warn("Ignored banned user %s" % name) return team = 0 assignment = self.config['game']['assignment'] if assignment == 'uid': base10_id = base36decode(author_id) team = base10_id % 2 elif assignment == "random": team = random.randint(0, 1) is_leader = name in self.config["game"]["leaders"] newbie = User(name=name, team=team, loyalists=100, leader=is_leader) session.add(newbie) cap = Region.capital_for(newbie.team, session) if not cap: logging.fatal("Could not find capital for %d" % newbie.team) newbie.region = cap session.commit() logging.info("Created combatant %s", newbie) reply = ("Welcome to Chroma! You are now a %s " "in the %s army, commanding a force of loyalists " "%d people strong. You are currently encamped at %s" ) % (newbie.rank, num_to_team(newbie.team, self.config), newbie.loyalists, cap.markdown()) comment.reply(reply) else: #logging.info("Already registered %s", comment.author.name) pass
def lands_status_for(cls, session, config): regions = session.query(Region).all() fmt = "* **%s**: %s%s" result = [] for region in regions: dispute = "" if region.battle: dispute = " ( %s )" % region.battle.markdown() result.append(fmt % (region.markdown(), num_to_team(region.owner, config), dispute)) lands = "\n".join(result) return "State of the Lands:\n\n" + lands
def lands_status_for(cls, session, config): regions = session.query(Region).all() fmt = "* **%s**: %s%s" result = [] for region in regions: dispute = "" if region.battle: dispute = " ( %s )" % region.battle.markdown() if region.buffs: bufflist = [buff.markdown() for buff in region.buffs] dispute += " ( %s )" % ",".join(bufflist) result.append(fmt % (region.markdown(), num_to_team(region.owner, config), dispute)) result.sort() lands = "\n".join(result) return "State of the Lands:\n\n" + lands
def recruit_from_post(self, post): post.replace_more_comments(threshold=0) flat_comments = praw.helpers.flatten_tree(post.comments) session = self.session for comment in flat_comments: if not comment.author: # Deleted comments don't have an author continue name = comment.author.name.lower() if name == self.config.username.lower(): continue # Is this author already one of us? found = session.query(User).filter_by(name=name).first() if not found: team = 0 assignment = self.config["game"]["assignment"] if assignment == "uid": base10_id = base36decode(comment.author.id) team = base10_id % 2 elif assignment == "random": team = random.randint(0, 1) is_leader = name in self.config["game"]["leaders"] newbie = User(name=name, team=team, loyalists=100, leader=is_leader) session.add(newbie) cap = Region.capital_for(newbie.team, session) if not cap: logging.fatal("Could not find capital for %d" % newbie.team) newbie.region = cap session.commit() logging.info("Created combatant %s", newbie) reply = ( "Welcome to Chroma! You are now a %s " "in the %s army, commanding a force of loyalists " "%d people strong. You are currently encamped at %s" ) % (newbie.rank, num_to_team(newbie.team, self.config), newbie.loyalists, cap.markdown()) comment.reply(reply) else: # logging.info("Already registered %s", comment.author.name) pass
def team_name(self): return num_to_team(self.player.team, self.config)
def update_game(self): session = self.session MarchingOrder.update_all(session, self.config) results = Region.update_all(session, self.config) to_add = [] for newternal in results['new_eternal']: title = "The Eternal Battle Rages On" post = InvadeCommand.post_invasion(title, newternal, self.reddit) if post: newternal.submission_id = post.name to_add.append(newternal) else: logging.warn("Couldn't submit eternal battle thread") session.rollback() if to_add: session.add_all(to_add) session.commit() results = Battle.update_all(session, self.config) for ready in results['begin']: ready.display_ends = (ready.begins + self.config["game"]["battle_time"]) # Actual ending is within battle_lockout of the end chooserange = self.config["game"]["battle_lockout"] chosen = random.randint(0, chooserange) ready.ends = ready.display_ends - (chooserange / 2) + chosen text = ("War is now at your doorstep! Mobilize your armies! " "The battle has begun now, and will end at %s.\n\n" "> Enter your commands in this thread, prefixed with " "'>'") % ready.ends_str() post = self.reddit.get_submission( submission_id=name_to_id(ready.submission_id)) post.edit(text) session.commit() self.update_skirmish_summaries(results['skirmish_ended']) for done in results['ended']: report = ["The battle is complete...\n"] report += done.report(self.config) report.append("") if done.old_buffs: report.append("Buffs in effect for Team %s\n" % num_to_team(done.old_owner, self.config)) for buff in done.old_buffs: report.append(" * %s" % buff.name) report.append("") team0_name = num_to_team(0, self.config) team1_name = num_to_team(1, self.config) # Homeland buffs? if done.homeland_buffs: report.append("Homeland buffs in effect:") for team in range(0, 2): name = num_to_team(team, self.config) report.append("%s: %d%%" % (name, done.homeland_buffs[team])) report.append(("## Final Score: Team %s: %d " "Team %s: %d") % (team0_name, done.score0, team1_name, done.score1)) if done.victor is not None: report.append("\n# The Victor: Team %s" % num_to_team(done.victor, self.config)) else: report.append("# TIE") text = "\n".join(report) post = self.reddit.get_submission( submission_id=name_to_id(done.submission_id)) post.edit(text) # Update all the skirmish summaries self.update_skirmish_summaries(done.toplevel_skirmishes()) session.delete(done) session.commit() db.Buff.update_all(session)
def create_defaults(cls, sess, config): for team_num in xrange(0, 2): t = TeamInfo(id=team_num, name=num_to_team(team_num, config)) t.greeting = "For %s!" % t.name sess.add(t) sess.commit()