def test_region_autocapital(self): """A region that's a capital is automatically owned by the same team""" cap = Region.capital_for(0, self.sess) self.assertEqual(cap.capital, cap.owner) cap = Region.capital_for(1, self.sess) self.assertEqual(cap.capital, cap.owner)
def test_extract_cancels_movement(self): """Emergency movement shouldn't rubberband""" sess = self.sess cap = Region.capital_for(0, sess) # Move Alice somewhere else londo = self.get_region("Orange Londo") self.alice.move(100, londo, 0) # Should be in londo self.assertEqual(self.alice.region, londo) # Bloodless coup of Sapphire sapp = self.get_region("Sapphire") sapp.owner = self.alice.team sess.commit() # Start wandering that way order = self.alice.move(100, sapp, 60 * 60 * 24) self.assert_(order) orders = self.sess.query(MarchingOrder).count() self.assertEqual(orders, 1) self.alice.extract() self.assertEqual(self.alice.region, cap) orders = self.sess.query(MarchingOrder).count() self.assertEqual(orders, 0)
def create_user(self, name, team): newbie = User(name=name, team=team, loyalists=100, leader=True) self.sess.add(newbie) cap = Region.capital_for(team, self.sess) newbie.region = cap self.sess.commit() return newbie
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 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 test_movement(self): """Move Alice from the Orangered capital to an adjacent region""" sess = self.sess cap = Region.capital_for(0, sess) # First of all, make sure alice is actually there self.assertEqual(self.alice.region.id, cap.id) londo = self.get_region("Orange Londo") self.assertIsNotNone(londo) self.alice.move(100, londo, 0) # Now she should be there self.assertEqual(self.alice.region.id, londo.id)
def test_extract(self): """Emergency movement back to capital""" sess = self.sess cap = Region.capital_for(0, sess) # Move Alice somewhere else londo = self.get_region("Orange Londo") self.alice.move(100, londo, 0) # Should be in londo self.assertEqual(self.alice.region, londo) # Emergency move! self.alice.extract() # Should be back in capital self.assertEqual(self.alice.region, cap)
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