def test_changeSupplyAmount_failed(self): 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) # item cannot find result1 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="he", newAmount=-1) assert result1 == -1 # shopitem cannot find result2 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="hey", newAmount=-1) assert result2 == -2
def test_addItemTOShop_success_addAmount(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result[0].amount == 20 and len(result) == 1
def test_addItemTOShop_failed_unlimitedSupply(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=-1) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result[0].amount == -1
def test_checkShopitemStatus_success(self): 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=10) result = InventoryUtil.checkShopitemStatus(guild_id=1, item_name="hello") assert result.hidden == False
def test_changeShopitemHiddenStatus_failed_noitem(self): 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=10) result = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="he", hidden=True) assert result == -1
def test_addItemTOShop_failed_limitedToUnlimited(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) shopitem = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=-1) assert result == -3 and shopitem.amount == 10
async def buy_item(self, ctx: commands.Command, count: int, item_name: str): result = InventoryUtil.buyShopitem(ctx.guild.id, ctx.author.id, item_name, count) if result == ErrorCode.CannotFindProduct: await ctx.send("沒有該項商品,請確認!") elif result == ErrorCode.LevelDoesNotReach: await ctx.send("等級不夠,無法購買!") elif result == ErrorCode.TokenDoesNotEnough: await ctx.send("雞腿不夠,無法購買!") elif result == ErrorCode.SupplyDoesNotEnough: await ctx.send("商品數量不足,無法購買!") else: await ctx.send(f"{count}個{result.item.name} 購買成功!") InventoryUtil.checkZeroAmount(ctx.guild.id)
def test_addItemTOShop_failed_noitem(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="he", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result == []
def test_findShopItem_failed(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) result = InventoryUtil.findShopItem(guild_id=1, item=item1) assert result == None
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_checkShopitemStatus_failed(self): # no item result1 = InventoryUtil.checkShopitemStatus(guild_id=1, item_name="hello") assert result1 == -1 # no shopitem 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) result2 = InventoryUtil.checkShopitemStatus(guild_id=1, item_name="hello") assert result2 == -2
def test_changeSupplyAmount_success_limitedToUnlimited(self): 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) assert shopItem1.amount == 10 shopItem2 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="hello", newAmount=-1) assert shopItem2.amount == -1
async def show_item(self, ctx: commands.Context, item_name: str): result = InventoryUtil.getAllItemsBelongToUser(ctx.guild.id, ctx.author.id) if len(result) < 1: await ctx.send('`你尚未擁有任何物品!`') else: # for-else: for for result_item in result: if result_item.item.name == item_name: embed_specific_item = Embed( title=f"物品名稱: {result_item.item.name}", description=f"描述: {result_item.item.description}", color=0xff8000 ) # 內嵌方塊: 內容第1列之1,物品ID embed_specific_item.add_field( name = 'ID:', value = '{}'.format(str(result_item.item.id).ljust(3, " ")), inline = True ) # 內嵌方塊: 內容第1列之2,持有數量 embed_specific_item.add_field( name = '持有數量:', value = '{}'.format(str(result_item.amount).ljust(3, " ")), inline = True ) # 內嵌方塊: 內容第2列之1,等級限制 embed_specific_item.add_field( name = '等級限制:', value = '{}'.format(str(result_item.item.level_required).ljust(3, " ")), inline = True ) # 內嵌方塊: 內容第2列之2,價格 embed_specific_item.add_field( name = '價格:', value = '{}'.format(str(result_item.item.token_required).ljust(3, " ")), inline = True ) # 內嵌方塊: 內容第3列之1,物品類型 embed_specific_item.add_field( name = '物品類型:', value = '{}'.format(str(result_item.item.type).ljust(3, " ")), #顯示錯誤 e.g. ItemType.RECOVER inline = True ) # 內嵌方塊: 內容第3列之2,增幅類型 embed_specific_item.add_field( name = '增幅類型:', value = '{}'.format(str(result_item.item.buff.buff_type).ljust(3, " ")), #顯示錯誤 e.g. HITPOINT inline = True ) # 暫時放一張圖代替 (set_thumbnail()) embed_specific_item.set_thumbnail(url='https://images.heb.com/is/image/HEBGrocery/001584756') await ctx.send(embed = embed_specific_item) break; # for-else: else else: await ctx.send('`你的物品清單裡沒有此物品!`')
async def list_hidden_shopItem(self, ctx: commands.Command): result = InventoryUtil.listHiddenShopItem(ctx.guild.id) if len(result) < 1: await ctx.send("沒有任何商品隱藏") else: msg = "```\n" for product in result: msg += product.item.name + "\n" msg += "```" await ctx.send(msg)
def test_changeShopitemHiddenStatus_success(self): 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=10) result = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hello", hidden=True) assert result.hidden == True result2 = InventoryUtil.ShopMenu(guild_id=1) assert len(result2) == 0 result3 = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hello", hidden=False) assert result3.hidden == False
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
async def change_shopitemHidden_status(self, ctx: commands.Command, item_name: str, hidden: bool): result = InventoryUtil.changeShopitemHiddenStatus( ctx.guild.id, item_name, hidden) if result == -1: await ctx.send("查無此項目,請確認商品名稱是否輸入錯誤!") elif result == -2: await ctx.send("該商品存在但尚未上架!") else: if hidden == True: await ctx.send(f"修改成功! 目前{item_name}狀態改為隱藏") else: await ctx.send(f"修改成功! 目前{item_name}供給數量為{result.amount}")
async def check_Shopitem_status(self, ctx: commands.Command, item_name: str): result = InventoryUtil.checkShopitemStatus(ctx.guild.id, item_name) if result == -1: await ctx.send("查無此項目,請確認商品名稱是否輸入錯誤!") elif result == -2: await ctx.send(f"{item_name}存在但尚未上架!") else: msg = f"{item_name}已上架,狀態為" if result.hidden is True: msg += "隱藏" else: msg += "顯示" await ctx.send(msg)
async def show_items_command(self, ctx: commands.Context): if ctx.author.bot: return # ignore bot records = InventoryUtil.getAllItemsBelongToUser( ctx.guild.id, ctx.author.id) msg = "" if len(records) > 0: msg += "你現在有以下物品:\n" record: InventoryRecord for record in records: msg += f"{record.item.name} x {record.amount}\n" else: msg += "你目前沒有任何物品" await ctx.author.send(msg)
async def change_shopitem_amount(self, ctx: commands.Command, amount: int, item_name: str): if amount < -1: await ctx.send("商品供應數量不能為負數!") else: result = InventoryUtil.changeSupplyAmount(ctx.guild.id, item_name, amount) if result == -1: await ctx.send("查無此項目,請確認商品名稱是否輸入錯誤!") elif result == -2: await ctx.send("該商品存在但尚未上架!") else: if amount == 0: await ctx.send("數量為0,建議下架商品實在點") else: await ctx.send(f"修改成功! 目前{item_name}供給數量已改成{result.amount}" )
def test_deleteShopItems(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=100) list = InventoryUtil.ShopMenu(guild_id=1) assert len(list) == 2 InventoryUtil.deleteShopItems(guild_id=1) list = InventoryUtil.ShopMenu(guild_id=1) assert len(list) == 0
async def showShopitemDetail(self, ctx: commands.Command, item_name: str): item = ItemUtil.searchItem(guild_id=ctx.guild.id, item_name=item_name) if item is None: await ctx.send("找不到該商品,請確認名稱是否輸入錯誤!") result = InventoryUtil.findShopItem(ctx.guild.id, item) if result is None: await ctx.send("找不到該商品,請確認名稱是否輸入錯誤!") else: if result.item.type == ItemType.ATTACK: itemtype = "攻擊道具" elif result.item.type == ItemType.DEFENCE: itemtype = "防禦道具" elif result.item.type == ItemType.RECOVER: itemtype = "恢復道具" elif result.item.type == ItemType.STATUS: itemtype = "狀態道具" else: itemtype = "一般道具" if result.item.buff.buff_type == BuffType.ATTACK: bufftype = "攻擊力" elif result.item.buff.buff_type == BuffType.DEFENCE: bufftype = "防禦力" elif result.item.buff.buff_type == BuffType.MAGIC: bufftype = "魔法力" elif result.item.buff.buff_type == BuffType.HIT_POINT: bufftype = "生命力" else: bufftype = "無屬性" msg = "```" msg += f"商品名稱: {result.item.name}\n" msg += f"商品價錢: {result.item.token_required}\n" msg += f"等級限制: {result.item.level_required}\n" msg += f"道具種類: {itemtype}\n" msg += f"增幅種類: {bufftype}\n" msg += f"增幅數值: {result.item.buff.buff_value}\n" msg += "增幅時間: {}\n".format(result.item.buff.buff_round if result. item.buff.buff_round > 0 else "永不毀滅") msg += f"商品描述: {result.item.description}\n" msg += "```" await ctx.send(msg)
async def show_menu(self, ctx: commands.Command): result = InventoryUtil.ShopMenu(ctx.guild.id) if len(result) < 1: await ctx.send('目前商品都被買光了!') else: msg = "```\n" msg += "{:^63}".format("===商品價目表===") msg += "\n" for products in result: msg += " 購買ID: {}".format(str(products.item.id).ljust(3, " ")) msg += " 商品名稱: {}".format(products.item.name).ljust(15, " ") msg += " 等級限制: {}".format( str(products.item.level_required).ljust(3, " ")) msg += " 價格: {}".format( str(products.item.token_required).ljust(3, " ")) msg += " 供應數量: {}\n".format( str(products.amount).ljust(3, " ") if products.amount > 0 else "無限".ljust(3, " ")) msg += "```" await ctx.send(msg)
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 == []
async def show_inventory(self, ctx: commands.Context): result = InventoryUtil.getAllItemsBelongToUser(ctx.guild.id, ctx.author.id) result_count = len(result) # 資料個數 = 資料輸出串列總長 if result_count < 1: await ctx.send('`你尚未擁有任何物品!`') else: # 內嵌方塊: 內容初始化 embed_inventory = Embed( title=f"{ctx.author.name}的物品清單", description="清單每頁20筆資料", color=0xff8000 ) # 內嵌方塊: 顯示全部物品 # 這裡要用set,不然重複的物品會出現在顯示欄位 for item_index, item_display in enumerate(result[:], start=1): embed_inventory.add_field(name=None, value = f'{item_index}. {item_display.item.name}', inline=False) # 暫時放一張圖代替 (set_thumbnail()) embed_inventory.set_thumbnail(url="https://s1.zerochan.net/Takanashi.Kiara.600.3145979.jpg") embed_inventory.set_footer(text=f"一共{result_count}個物品") await ctx.send(embed = embed_inventory)
async def add_item(self, ctx: commands.Command, item_count: int, item_name: str): if item_count == 0: await ctx.send(f"新增數量為0?那你新增幹嘛?浪費我的時間") elif item_count < -1: await ctx.send(f"新增不能為負數,請重新輸入!") else: result = InventoryUtil.addItemToShop(ctx.guild.id, item_name, item_count) if result == -1: await ctx.send(f"{item_name}上架失敗!請確認商品名字是否正確!") elif result == -2: await ctx.send(f"{item_name}上架失敗!該物品目前無限量供應!") elif result == -3: await ctx.send( f"{item_name}目前限量存在,若需更改為無限量供應,請使用!shop change指令") else: if result.amount != item_count: await ctx.send( f"{item_name}已存在{result.amount - item_count}個,已更新提供數量至{result.amount}個!" ) else: await ctx.send(f"{item_count}個{item_name} 商品上架成功!")
def test_buyShopitems_success(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=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) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) shopItem2 = InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hey", count=4) itemList = InventoryUtil.getAllItemsBelongToUser(guild_id=1, user_id=123) assert len(itemList) == 2 and itemList[0].user_id == 123 and itemList[ 1].user_id == 123
async def clear_inventory(self, ctx: commands.Context): # result = InventoryUtil.getAllItemsBelongToUser(ctx.guild.id, ctx.author.id) InventoryUtil.removeUserItems_TEST(ctx.guild.id, ctx.author.id)
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 test_listHiddenitem_success(self): result = InventoryUtil.listHiddenShopItem(guild_id=1) assert len(result) == 0 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) 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=1) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hello", hidden=True) result = InventoryUtil.listHiddenShopItem(guild_id=1) assert len(result) == 1 # items InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hey", hidden=True) result = InventoryUtil.listHiddenShopItem(guild_id=1) assert len(result) == 2