Exemplo n.º 1
0
def create_battle(challenger_id, opponent_id):
    challenger = Character.query.filter_by(id=challenger_id).first()
    if challenger is None:
        return "The challenger character does not exist!", 404

    opponent = Character.query.filter_by(id=opponent_id).first()
    if opponent_id is not None and opponent is None:
        return "The opponent character does not exist!", 404

    if opponent_id is not None and challenger.user_id == opponent.user_id:
        return "The challenger and opponent belong to the same user!", 403

    if is_battling(challenger_id):
        return "The challenger is already in a battle!", 403

    if opponent_id is not None and is_battling(opponent_id):
        return "The opponent is already in a battle!", 403

    new_battle = Battle(challenger_id=challenger_id, opponent_id=opponent_id)
    db.session.add(new_battle)
    db.session.commit()

    add_battle_action(new_battle)

    starting_log = create_starter_log(challenger, opponent, new_battle.id)
    return add_log(starting_log, new_battle.id), 201
Exemplo n.º 2
0
    def end_battle(self, battle=None, conf=None):
        if battle is None:
            battle = self.battle
        sess = self.sess

        battle.ends = battle.begins
        sess.commit()
        updates = Battle.update_all(sess, conf)
        sess.commit()

        self.assertNotEqual(len(updates['ended']), 0)
        self.assertEqual(updates["ended"][0], battle)
        return battle
Exemplo n.º 3
0
    def test_orangered_victory(self):
        """Make sure orangered victories actually count"""
        self.assertEqual(None, self.sapphire.owner)
        sess = self.sess
        self.battle.create_skirmish(self.alice, 5)

        self.battle.ends = self.battle.begins
        sess.commit()
        updates = Battle.update_all(sess)
        sess.commit()

        self.assertNotEqual(len(updates['ended']), 0)
        self.assertEqual(updates["ended"][0], self.battle)
        self.assertEqual(0, self.sapphire.owner)
Exemplo n.º 4
0
    def test_full_battle(self):
        """Full battle"""
        battle = self.battle
        sess = self.sess

        oldowner = self.sapphire.owner

        # Battle should be ready and started
        self.assert_(battle.is_ready())
        self.assert_(battle.has_started())

        # Still going, right?
        self.assertFalse(battle.past_end_time())

        # Skirmish 1
        s1 = battle.create_skirmish(self.alice, 10)  # Attack 10
        s1a = s1.react(self.carol, 4, hinder=False)  # --Support 4
        s1a.react(self.bob, 3)                       # ----Attack 3
        s1.react(self.dave, 8)                       # --Attack 8
        # Winner will be team orangered, 11 VP

        # Skirmish 2
        battle.create_skirmish(self.bob, 15)         # Attack 15
        # Winner will be team periwinkle, 30 VP for unopposed

        # Skirmish 3
        s3 = battle.create_skirmish(self.carol, 10)  # Attack 10
        s3.react(self.bob, 5)                        # --Attack 5
        # Winner will be team orangered, 5 VP
        # Overall winner should be team periwinkle, 30 to 16

        # End this bad boy
        self.battle.ends = self.battle.begins
        sess.commit()
        self.assert_(battle.past_end_time())

        updates = Battle.update_all(sess)
        sess.commit()

        self.assertNotEqual(len(updates['ended']), 0)
        self.assertEqual(updates["ended"][0], battle)
        self.assertEqual(battle.victor, 1)
        self.assertEqual(battle.score0, 16)
        self.assertEqual(battle.score1, 30)

        self.assertNotEqual(oldowner, battle.region.owner)
        self.assertEqual(battle.region.owner, 1)
Exemplo n.º 5
0
    def test_decommit_after_battle(self):
        """When the battle's over, we no longer commit, right?"""
        sess = self.sess
        self.battle.submission_id = "TEST"  # So update_all will work correctly

        old = self.alice.committed_loyalists
        self.battle.create_skirmish(self.alice, 5)

        # And just like that, the battle's over
        self.battle.ends = self.battle.begins
        sess.commit()

        updates = Battle.update_all(sess)
        sess.commit()

        self.assertNotEqual(len(updates['ended']), 0)
        self.assertEqual(updates["ended"][0], self.battle)

        self.assertEqual(self.alice.committed_loyalists, old)
Exemplo n.º 6
0
    def test_ejection_after_battle(self):
        """We don't want the losers sticking around after the fight"""
        self.battle.submission_id = "TEST"  # So update_all will work correctly

        old_bob_region = self.bob.region
        old_alice_region = self.alice.region
        self.battle.create_skirmish(self.alice, 5)

        self.battle.ends = self.battle.begins
        self.sess.commit()

        updates = Battle.update_all(self.sess)
        self.sess.commit()
        self.assertNotEqual(len(updates['ended']), 0)
        self.assertEqual(updates["ended"][0], self.battle)

        self.assertEqual(self.battle.victor, self.alice.team)

        self.assertNotEqual(self.bob.region, self.alice.region)
        self.assertNotEqual(self.bob.region, old_bob_region)
        self.assertEqual(self.alice.region, old_alice_region)
Exemplo n.º 7
0
 def end_skirmish(self, skirmish):
     sess = self.sess
     skirmish.ends = 1
     Battle.update_all(sess)
     return skirmish
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)