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
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 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_searchItem_fail(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.searchItem(guild_id=1, item_name="he") assert result == None
def test_createItem_success(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.ListAllItem(guild_id=1) assert len(result) == 1 and result[0] == item1
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_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_buffTypeStorageRead_success(self): item = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=30, buff_round=3) item_from_db = ItemUtil.ListAllItem(guild_id=1)[0] item_buff: Buff = item_from_db.buff assert item_buff.buff_type == BuffType.ATTACK assert item_buff.buff_value == 30 assert item_buff.buff_round == 3
def test_deleteItem_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2, level_required=0, price=1) ItemUtil.deleteItem(guild_id=1, item_name="hello") result = ItemUtil.ListAllItem(guild_id=1) assert result == []
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_createItem_failed_itemExist(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) 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
def test_addItemToItemdb_success(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) item2 = ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) items = ItemUtil.ListAllItem(guild_id=1) assert items == [item1, item2]
def test_addItemToItemdb_failed_samename(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) item2 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) items = ItemUtil.ListAllItem(guild_id=1) assert len(items) == 1 and item2 == -1
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_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_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_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
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_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
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_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
async def list_item(self, ctx: commands.Command): result = ItemUtil.ListAllItem(ctx.guild.id) if len(result) < 1: await ctx.send('目前沒有商品') else: msg = "```" for products in result: msg += " 購買ID: {}".format(str(products.id).ljust(3, " ")) msg += " 商品名稱: {}".format(products.name).ljust(15, " ") msg += " 等級限制: {}".format( str(products.level_required).ljust(3, " ")) msg += " 價格: {}\n".format( str(products.token_required).ljust(3, " ")) msg += "```" await ctx.send(msg)
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 create_item(self, ctx: commands.Command, item_name: str, itemtype: int, buff_type: int, buff_value: int, buff_round: int, level_required: int, price: int, description: str): if len(item_name) > 15: await ctx.send(f"名稱不可超過15個中英字元!") elif price < 0: await ctx.send(f"價錢不可為負!請重新輸入!") elif not 0 < itemtype < len(ItemType) + 1: await ctx.send(f"道具類型錯誤!請重新輸入!") elif not 0 < buff_type < len(BuffType) + 1: await ctx.send(f"增幅類型只有五種!請重新輸入!") elif buff_round < -1: await ctx.send(f"增幅持續時間不可為負數(-1為永遠不毀滅)!請重新輸入!") else: result = ItemUtil.createItem(ctx.guild.id, item_name, ItemType.list()[itemtype], BuffType.list()[buff_type], buff_value, buff_round, description, level_required, price) if result == -1: await ctx.send(item_name + ' 已經存在!') else: await ctx.send(item_name + ' 新增成功!')
async def clear_all_items(self, ctx: commands.Command): ItemUtil.deleteItems(ctx.guild.id) await ctx.send("本群所有item清理結束")
def test_deleteItem_failed_noItem(self): ItemUtil.deleteItem(guild_id=1, item_name="hello") result = ItemUtil.ListAllItem(guild_id=1) assert result == []
def test_deleteItems_failed_noItem(self): ItemUtil.deleteItems(guild_id=1) result = ItemUtil.ListAllItem(guild_id=1) assert result == []