async def init_rpg_character(self, ctx: commands.Context): if not ChannelUtil.hasChannel(ctx.guild.id, ctx.channel.id, Util.ChannelType.RPG_GUILD): return if RPGCharacterUtil.hasAdventureStared(ctx.author.id): await ctx.send("你的冒險已經啟程") return member: Member = MemberUtil.get_or_add_member(ctx.author.id) if member.coin < 5000: await ctx.send(f"看來你的硬幣不足呢, 先在群裡說說話賺取經驗吧.") return isNew = True if RPGCharacterUtil.getRPGCharacter(ctx.author.id) != None: isNew = False if RPGCharacterUtil.createNewRPGCharacter(ctx.author.id) != None: user = self.bot.get_user(ctx.author.id) name = await NicknameUtil.get_user_name(ctx.guild, user) MemberUtil.add_coin(ctx.author.id, -5000) MemberUtil.add_coin(ctx.bot.user.id, 5000) if isNew: await ctx.send(f"歡迎冒險者'{name}'的加入, 從現在開始你的冒險之旅吧!") else: await ctx.send(f"歡迎回來'{name}', 已恢復您冒險者的身分!") return await ctx.send(f"看起來你的行李好像還沒準備好, 詳情請洽冒險者公會員工.")
def test_buyShopitem_failed_noProduct(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10, hidden=True) result = InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="he", count=2) assert result == ErrorCode.CannotFindProduct result == InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hey", count=2) assert result == ErrorCode.CannotFindProduct
def buyItem(guild_id: int, user_id: int, item_id: int, count: int): shopItem = InventoryUtil.findShopItem(guild_id=guild_id, item_id=item_id) # Cannot find products if shopItem is None: return ErrorCode.CannotFindProduct member = MemberUtil.get_or_add_member(user_id) # level does not reach the restrictions if member.rank < shopItem.item.level_required: return ErrorCode.LevelDoesNotReach # Token is less then price if member.token < (shopItem.item.token_required * count): return ErrorCode.TokenDoesNotEnough # Transaction part if shopItem.amount < count: return ErrorCode.SupplyDoesNotEnough # shopItem is unlimited supply elif shopItem.amount != -1: shopItem.amount -= count shopItem.save() # Reduce member's token spend = shopItem.item.token_required * count * -1 MemberUtil.add_token(member_id=user_id, amount=spend) InventoryUtil.addItemToUserInventory(guild_id, user_id, item_id, count) return shopItem
def test_getTotalTokenSuccess(self): self.database.add_member(123) assert MemberUtil.get_total_token() == 100 self.database.add_member(321) assert MemberUtil.get_total_token() == 200
async def checkToken(self, ctx: commands.Command): member = MemberUtil.get_or_add_member(ctx.author.id) if member.token < self.__RATE: await ctx.reply(f"親愛的員工, 你的雞腿不夠不能抽籤哦! 你只有 {member.token}隻.") return False MemberUtil.add_token(member.member_id, -1 * self.__RATE) await ctx.reply(f"抽籤遊戲花費 {self.__RATE}隻雞腿, 你還剩下 {member.token}隻.") return True
def test_getTotalCoinSuccess(self): self.database.add_member(123) self.database.add_coin(123, 100) assert MemberUtil.get_total_coin() == 100 self.database.add_member(321) self.database.add_coin(321, 100) assert MemberUtil.get_total_coin() == 200
async def superchat_group(self, ctx: commands.Context, sc_money: int, user: User, *args: str): # Replace msg = args sc_msg = "" for token in msg: # user id to name result = re.findall("<@!\d+>", token) replace = token if len(result) != 0: for userID in result: member = await ctx.guild.fetch_member(userID[3:-1]) replace = member.display_name.join(replace.split(userID)) # stamp to short msg result = re.findall("<:\w+:\d+>", replace) if len(result) != 0: for stamp in result: stamp_name = re.findall(":\w+:", stamp) replace = stamp_name[0].join(replace.split(stamp)) if len(sc_msg) < 1: sc_msg += replace else: sc_msg = sc_msg + " " + replace if sc_money < 15: await ctx.send("至少15硬幣才能使用SuperChat!") return else: sc_color = SuperChatMeme._getColor(sc_money) # check msg too long or not if len(sc_msg) > SuperChatMeme._ColorWord[sc_color]: await ctx.send(f"字數過多!請限制在{SuperChatMeme._ColorWord[sc_color]}字數內!") return # check author have enough coins or not giver = MemberUtil.get_or_add_member(ctx.author.id) if giver.coin < sc_money: await ctx.send("硬幣不足!快去店外雜談區聊天賺硬幣!") return adder = MemberUtil.get_or_add_member(user.id) # transaction MemberUtil.add_coin(member_id=giver.member_id, amount=-sc_money) MemberUtil.add_coin(member_id=adder.member_id, amount=sc_money * 0.8) MemberUtil.add_coin(member_id=self.bot.user.id, amount=sc_money * 0.2) # create image avatar = self.downloadUserAvatar(ctx.author) imgPath = SuperChatUtil.createSC(ctx.author.name, avatar, sc_money, sc_msg, sc_color) img = File(imgPath, filename="result.png") if sc_money < 16: await ctx.send(f"感謝{ctx.author.display_name}很寒酸的施捨給{user.display_name}的SuperChat!") else: await ctx.send(f"感謝{ctx.author.display_name}給{user.display_name}的SuperChat!") await ctx.send(file=img)
async def exchange_token(self, ctx: commands.Command, need_token: int): member = MemberUtil.get_or_add_member(ctx.author.id) if member is None: await ctx.send("沒硬幣還想換雞腿,趕快去店外雜談區聊天賺硬幣!") else: coinspertoken = GamblingUtil.get_token_rate() spend = need_token * coinspertoken if member.coin > spend: MemberUtil.add_coin(member_id=member.id, amount=-spend) MemberUtil.add_token(member_id=member.id, amount=need_token) await ctx.send(f"成功兌換{need_token}個雞腿,目前剩下{member.coin}個硬幣") else: msg = f"兌換失敗!不足{spend - member.coin}個硬幣\n" msg += f"目前匯率為 一隻雞腿{coinspertoken}個硬幣" await ctx.send(msg)
async def bank_group(self, ctx: commands.Context): if ctx.author.bot: return if not ChannelUtil.hasChannel(ctx.guild.id, ctx.channel.id, Util.ChannelType.BANK): return member = MemberUtil.get_or_add_member(self.bot.user.id) await ctx.send(f"目前銀行有 {member.coin}枚硬幣.")
def test_buyShopitem_failed_notEnoughSupply(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=1) result = InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) assert result == ErrorCode.SupplyDoesNotEnough
def test_buyShopitem_success_shopItemchangeHidden(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=500) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=1) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=10) InventoryUtil.checkZeroAmount(guild_id=1) result = InventoryUtil.ShopMenu(guild_id=1) assert len(result) == 0 and result == []
def test_buyShopitem_success(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) itemList = InventoryUtil.getAllItemsBelongToUser(guild_id=1, user_id=123) assert itemList[0].user_id == 123 and itemList[0].item == shopItem1.item
def createNewRPGCharacter(member_id: int) -> RPGCharacter: if RPGCharacterUtil.hasAdventureStared(member_id): return None character: RPGCharacter = RPGCharacterUtil.getRPGCharacter(member_id) if character: character.retired = False character.save() return character member: Member = MemberUtil.get_member(member_id) new_hp = LevelUtil.generateNewHP(member.rank) new_mp = LevelUtil.generateNewMP(member.rank) new_attack = LevelUtil.generateAttack(member.rank) new_defense = LevelUtil.generateDefense(member.rank) return RPGCharacter.create( character=member, hp_current=new_hp, hp_max=new_hp, mp_current=new_mp, mp_max=new_mp, attack_basic=new_attack, defense_basic=new_defense, retired=False, last_attack=datetime.now().replace(year=2000))
async def bank_remove(self, ctx: commands.Context, coins: int, user: User): if not ChannelUtil.hasChannel(ctx.guild.id, ctx.channel.id, Util.ChannelType.BANK): return if coins < 1: await ctx.send("請不要來亂的好嗎?") return member = MemberUtil.get_or_add_member(self.bot.user.id) nick = await NicknameUtil.get_user_nickname_or_default(ctx.guild, user) if member.coin < coins: await ctx.send(f"'{nick}'擁有餘額: {member.coin}. 不足以扣除 {coins}") return MemberUtil.add_coin(self.bot.user.id, coins) MemberUtil.add_coin(user.id, -1 * coins) bank = MemberUtil.get_or_add_member(self.bot.user.id) message = f"扣除'{nick}'的硬幣 '{coins}'並充公成功! 目前餘額為\n" message += "```" message += f"銀行: {bank.coin}\n" message += f"{nick}: {member.coin}\n" message += "```" await ctx.send(message)
async def bank_pay(self, ctx: commands.Context, coins: int, user: User): if not ChannelUtil.hasChannel(ctx.guild.id, ctx.channel.id, Util.ChannelType.BANK): return bank = MemberUtil.get_or_add_member(self.bot.user.id) if coins < 1: await ctx.send("請不要來亂的好嗎?") return nick = await NicknameUtil.get_user_nickname_or_default(ctx.guild, user) if bank.coin < coins: await ctx.send(f"銀行餘額: {bank.coin} 不足以支付 {coins} 給 {nick}") return MemberUtil.add_coin(self.bot.user.id, -1 * coins) MemberUtil.add_coin(user.id, coins) member = MemberUtil.get_member(user.id) message = f"金額 {coins} 付款給 {nick}成功! 雙方餘額為\n" message += "```" message += f"銀行: {bank.coin}\n" message += f"{nick}: {member.coin}\n" message += "```" await ctx.send(message)
async def get_user_token(self, ctx: commands.Command): member = MemberUtil.get_or_add_member(ctx.author.id) if member is None: await ctx.send(f"你目前擁有0個雞腿") else: await ctx.send(f"你目前擁有{member.token}個雞腿")
def add_token(self, member_id: int, amount: int): MemberUtil.add_token(member_id, amount)
async def bank_add(self, ctx: commands.Context, coins: int): if not ChannelUtil.hasChannel(ctx.guild.id, ctx.channel.id, Util.ChannelType.BANK): return MemberUtil.add_coin(self.bot.user.id, coins) bank = MemberUtil.get_member(self.bot.user.id) await ctx.send(f"新增 {coins}枚硬幣至銀行: 成功!\n銀行餘額: {bank.coin}")
def test_getTotalCoinZero(self): assert MemberUtil.get_total_coin() == 0
def get_member(self, member_id: int) -> Member: return MemberUtil.get_member(member_id)
def test_getTotalTokenZero(self): assert MemberUtil.get_total_token() == 0
def add_member(self, member_id: int) -> Member: return MemberUtil.add_member(member_id)
def test_getCountSuccess(self): self.database.add_member(1) assert MemberUtil.get_member_count() == 1 self.database.add_member(2) assert MemberUtil.get_member_count() == 2
def test_getCountZero(self): assert MemberUtil.get_member_count() == 0
def test_getAllItemsBelongToUser_empty(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) itemList = InventoryUtil.getAllItemsBelongToUser(guild_id=1, user_id=123) assert len(itemList) == 0
def getRPGCharacter(member_id: int): member: Member = MemberUtil.get_or_add_member(member_id) query = RPGCharacter.select().where(RPGCharacter.character == member) if query.exists(): return query.get() return None
def getUserToken(guild_id: int, user_id: int): member = MemberUtil.get_or_add_member(user_id) return member.token
async def addToken(self, ctx: commands.Context): MemberUtil.add_token(ctx.author.id, 1) member = MemberUtil.get_member(ctx.author.id) await ctx.send(f'恭喜{ctx.author.mention}獲得1隻🍗, 目前擁有{member.token}隻🍗')
def get_token_rate(): current_token = MemberUtil.get_total_token() current_coins = MemberUtil.get_total_coin() if current_token == 0 or current_coins == 0: return GamblingUtil.DEFAULT_RATE return max(current_coins // current_token, GamblingUtil.DEFAULT_RATE)