def test_should_add_duel(self): arena = DuelArena(AlwaysFirstRandom()) duel_result = arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) self.assertEqual(duel_result.status, DuelStatus.DUEL_CREATED)
def test_target_should_not_be_challenger(self): arena = DuelArena(AlwaysSecondRandom()) duel_result = arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.challenger) self.assertEqual(duel_result.status, DuelStatus.CANNOT_DUEL_WITH_YOURSELF)
def test_should_take_prize_from_proposal(self): arena = DuelArena(AlwaysFirstRandom()) arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) duel_result = arena.add_or_make_duel("1", challenger=self.target, prize=self.prize + 1, target=self.challenger) self.assertEqual(duel_result.prize, self.prize)
def test_not_make_duel_twice(self): arena = DuelArena(AlwaysSecondRandom()) arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) duel_result = arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) self.assertEqual(duel_result.status, DuelStatus.DUEL_ALREADY_CREATED)
def test_should_make_duel(self): arena = DuelArena(AlwaysFirstRandom()) arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) duel_result = arena.add_or_make_duel("1", challenger=self.target, prize=self.prize, target=self.challenger) self.assertEqual(duel_result.status, DuelStatus.CHALLENGER_WON) self.assertEqual(duel_result.prize, self.prize)
def test_should_list_user_duels(self): arena = DuelArena(AlwaysFirstRandom()) arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) self.assertEqual(arena.list_user_open_duels("1", self.challenger), {self.target: 1}) self.assertEqual(arena.list_user_waiting_duels("1", self.target), {self.challenger: 1})
def test_should_add_duel_after_previous_id_resolved(self): arena = DuelArena(AlwaysSecondRandom()) arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) arena.add_or_make_duel("1", challenger=self.target, prize=self.prize, target=self.challenger) duel_result = arena.add_or_make_duel("1", challenger=self.challenger, prize=self.prize, target=self.target) self.assertEqual(duel_result.status, DuelStatus.DUEL_CREATED)
def test_should_list_empty(self): arena = DuelArena(AlwaysFirstRandom()) self.assertEqual(arena.list_user_open_duels("1", self.challenger), dict()) self.assertEqual(arena.list_user_waiting_duels("1", self.target), dict())
class DuelCog(commands.Cog): def __init__(self, db: DatabaseController, name_extractor): self.db = db self.arena = DuelArena(Random()) self.name_extractor = name_extractor @commands.command(name="duel-list") async def duel_list(self, ctx): challenger_id = str(ctx.author.id) server_id = str(ctx.guild.id) rivals = self.arena.list_user_waiting_duels(server_id, challenger_id) open_duels = self.arena.list_user_open_duels(server_id, challenger_id) formatted_opened = "\n".join([ f"{self.name_extractor(int(name))}: {points}" for name, points in open_duels.items() ]) formatted_rivals = "\n".join([ f"`!duel @{self.name_extractor(int(rival_name))} {points}`" for rival_name, points in rivals.items() ]) message = f"Tu czekasz: \n" \ f"{formatted_opened}\n" \ f"Tu czekają na Ciebie:\n" \ f"{formatted_rivals}" await ctx.send(message) @commands.command() async def duel(self, ctx, target: discord.User, prize: int = 0): challenger_id = str(ctx.author.id) challenger_name = str(ctx.author.name) target_name = str(target.name) server_id = str(ctx.guild.id) target_id = str(target.id) challenger_points = await self.db.fetch_user_points( challenger_id, server_id) target_points = await self.db.fetch_user_points(target_id, server_id) if challenger_points < prize: await ctx.send("Panie kolego, nie ma tak") return if target_points < prize: await ctx.send("Cel jest zabyt biedny") return if not self.arena.reverse_proposal_exists(server_id, challenger_id, target_id) and prize < 1: await ctx.send("Panie kolego, nie ma tak") return duel_result = self.arena.add_or_make_duel(server_id, challenger_id, prize, target_id) if duel_result.status == DuelStatus.DUEL_CREATED: await ctx.send("Walka czeka") return if duel_result.status == DuelStatus.CANNOT_DUEL_WITH_YOURSELF: await ctx.send("Ale weź kogoś jeszcze") return if duel_result.status == DuelStatus.DUEL_ALREADY_CREATED: await ctx.send("Daj się najpierw raz sklepać") return if duel_result.status == DuelStatus.TARGET_WON: new_target_points = target_points + duel_result.prize new_challenger_points = challenger_points - duel_result.prize await self.db.upsert_user_points(server_id, target_id, target_name, new_target_points) await self.db.upsert_user_points(server_id, challenger_id, challenger_name, new_challenger_points) await self.send_result(challenger_name, ctx, new_challenger_points, new_target_points, target_name) return if duel_result.status == DuelStatus.CHALLENGER_WON: new_target_points = target_points - duel_result.prize new_challenger_points = challenger_points + duel_result.prize await self.db.upsert_user_points(server_id, target_id, target_name, new_target_points) await self.db.upsert_user_points(server_id, challenger_id, challenger_name, new_challenger_points) await self.send_result(target_name, ctx, new_target_points, new_challenger_points, challenger_name) return await ctx.send("Nie powinno mnie tu być") pass @staticmethod async def send_result(loser_name, ctx, loser_points, winner_points, winner_name): await ctx.send( f'{winner_name} pyknął {loser_name} i ma teraz {winner_points}. Przegranemu zostało tylko {loser_points}' )
def __init__(self, db: DatabaseController, name_extractor): self.db = db self.arena = DuelArena(Random()) self.name_extractor = name_extractor