diff --git a/__init__.py b/__init__.py index 5ddc680..2025009 100644 --- a/__init__.py +++ b/__init__.py @@ -8,10 +8,9 @@ from zhenxun.utils.message import MessageUtils from .command import diuse_farm, diuse_register from .config import g_pJsonManager from .database import g_pSqlManager +from .farm.farm import g_pFarmManager from .farm.shop import g_pShopManager -# from .globalClass import g_pDrawImage, g_pJsonManager, g_pSqlManager - __plugin_meta = PluginMetadata( name="真寻的农场", description="快乐的农场时光", @@ -37,6 +36,9 @@ async def start(): # 初始化读取Json await g_pJsonManager.init() + aaa = await g_pFarmManager.sowing("1754798088", "胡萝卜", 1) + logger.info(aaa) + # 析构函数 @driver.on_shutdown async def shutdown(): diff --git a/command.py b/command.py index e5640ad..3f74d7b 100644 --- a/command.py +++ b/command.py @@ -39,6 +39,7 @@ diuse_farm = on_alconna( Subcommand("buy-plant", Args["name?", str]["num?", int], help_text="购买种子"), Subcommand("my-plant", help_text="我的种子"), Subcommand("my-props", help_text="我的农场道具"), + Subcommand("sowing", Args["name?", str]["num?", int], help_text="播种"), Subcommand("buy", Args["name?", str]["num?", int], help_text="购买道具"), Subcommand("use", Args["name?", str]["num?", int], help_text="使用道具"), Subcommand("gold-list", Args["num?", int], help_text="金币排行"), @@ -128,7 +129,7 @@ diuse_farm.shortcut( prefix=True, ) -@diuse_farm.assign("plant-shop") +@diuse_farm.assign("my-plant") async def _(session: Uninfo): uid = str(session.user.id) point = await g_pSqlManager.getUserPointByUid(uid) @@ -139,3 +140,28 @@ async def _(session: Uninfo): result = await g_pFarmManager.getUserPlantByUid(uid) await MessageUtils.build_message(result).send(reply_to=True) + +diuse_farm.shortcut( + "播种(?P.*?)", + command="我的农场", + arguments=["sowing", "{name}"], + prefix=True, +) + +async def _(session: Uninfo, name: Match[str], num: Query[int] = AlconnaQuery("num", 1),): + if not name.available: + await MessageUtils.build_message( + "请在指令后跟需要播种的种子名称" + ).finish(reply_to=True) + + uid = str(session.user.id) + point = await g_pSqlManager.getUserPointByUid(uid) + + if point < 0: + await MessageUtils.build_message("尚未开通农场").send() + return None + + return None + + # result = await g_pShopManager.buyPlant(uid, name.result, num.result) + # await MessageUtils.build_message(result).send(reply_to=True) diff --git a/database.py b/database.py index 82525f6..ab3a0dc 100644 --- a/database.py +++ b/database.py @@ -1,4 +1,5 @@ import os +from datetime import datetime, timedelta from io import StringIO from typing import Any, List, Optional @@ -302,6 +303,47 @@ class CSqlManager: return soilNumber + @classmethod + async def getUserSoilStatusBySoilID(cls, uid: str, soil: str) -> bool: + """根据土地块获取用户土地状态 + + Args: + uid (str): 用户Uid + soil (str): 土地块 + + Returns: + bool: 是否可以播种 + """ + if len(uid) <= 0: + return False + + async with cls.m_pDB.execute(f"SELECT {soil} FROM soil WHERE uid = '{uid}'") as cursor: + async for row in cursor: + if row[0] == None: + return True + + return False + + @classmethod + async def updateUserSoilStatusBySowing(cls, uid: str, soil: str, plant: str) -> bool: + + if len(uid) <= 0: + return False + + #获取种子信息 这里能崩我吃 + plantInfo = g_pJsonManager.m_pPlant['plant'][plant] # type: ignore + + + currentTime = datetime.now() + newTime = currentTime + timedelta(minutes=int(plantInfo['time'])) + + #种子名称,当前阶段,预计长大/预计下个阶段,地状态:0:无 1:长草 2:生虫 3:缺水 4:枯萎状态 + status = f"{plant},1,{int(newTime.timestamp())},0" + + sql = f"UPDATE soil SET {soil} = '{status}' WHERE uid = '{uid}'" + + return await cls.executeDB(sql) + @classmethod async def getUserPlantByUid(cls, uid: str) -> str: """获取用户仓库种子信息 @@ -317,9 +359,7 @@ class CSqlManager: return "" try: - async with cls.m_pDB.execute( - "SELECT plant FROM storehouse WHERE uid = ?", (uid,) - ) as cursor: + async with cls.m_pDB.execute(f"SELECT plant FROM storehouse WHERE uid = '{uid}'") as cursor: async for row in cursor: return row[0] diff --git a/farm/farm.py b/farm/farm.py index 3ee1cf9..ca01ac3 100644 --- a/farm/farm.py +++ b/farm/farm.py @@ -1,3 +1,5 @@ +from numpy import arange + from zhenxun.services.log import logger from zhenxun.utils._build_image import BuildImage from zhenxun.utils.image_utils import ImageTemplate @@ -36,29 +38,48 @@ class CFarmManager: x = 0 y = 0 + isFirst = True for index, level in enumerate(soilUnlock): x = soilPos[str(index + 1)]['x'] y = soilPos[str(index + 1)]['y'] + #如果土地已经到达对应等级 if soilNumber >= int(level): await img.paste(soil, (x, y)) - #缺少判断土地上是否有农作物 + #TODO 缺少判断土地上是否有农作物 plant = BuildImage(background=g_sResourcePath / "plant/basic/0.png") await plant.resize(0, 35, 58) - await img.paste(plant, (x + 3, y + 3)) + await img.paste(plant, (x + 100, y + 50)) else: await img.paste(grass, (x, y)) + if isFirst: + isFirst = False + + #首次添加扩建图片 + expansion = BuildImage(background=g_sResourcePath / "background/expansion.png") + await expansion.resize(0, 69, 69) + await img.paste(expansion, (x + 85, y + 20)) + return img.pic2bytes() @classmethod async def getUserPlantByUid(cls, uid: str) -> bytes: + """获取用户种子仓库 + + Args: + uid (str): 用户Uid + + Returns: + bytes: 返回图片 + """ + data_list = [] column_name = [ "-", "种子名称", - "数量" + "数量", "收获经验", "收获数量", "成熟时间(分钟)", @@ -80,15 +101,14 @@ class CFarmManager: return result.pic2bytes() sell = "" - for item in plant.split(','): if '|' in item: - plant_name, count = item.split('|', 1) # 分割一次,避免多竖线问题 + plantName, count = item.split('|', 1) # 分割一次,避免多竖线问题 try: - plantInfo = g_pJsonManager.m_pPlant['plant'][plant_name] # type: ignore + plantInfo = g_pJsonManager.m_pPlant['plant'][plantName] # type: ignore icon = "" - icon_path = g_sResourcePath / f"plant/{plant_name}/icon.png" + icon_path = g_sResourcePath / f"plant/{plantName}/icon.png" if icon_path.exists(): icon = (icon_path, 33, 33) @@ -100,7 +120,7 @@ class CFarmManager: data_list.append( [ icon, - plant_name, + plantName, count, plantInfo['experience'], plantInfo['harvest'], @@ -123,4 +143,43 @@ class CFarmManager: return result.pic2bytes() + @classmethod + async def sowing(cls, uid: str, name: str, num: int = 1) -> str: + """播种 + + Args: + uid (str): 用户Uid + name (str): 播种种子名称 + num (int, optional): 播种数量 + + Returns: + str: + """ + plant = await g_pSqlManager.getUserPlantByUid(uid) + + if plant == None: + return "你的种子仓库是空的,快去买点吧!" + + for item in plant.split(','): + if '|' in item: + plantName, count = item.split('|', 1) # 分割一次,避免多竖线问题 + + #判断仓库是否有当前播种种子 + if plantName == name: + count = int(count) + + #获取用户解锁多少块地 + soilName = "" + soilNumber = await g_pSqlManager.getUserSoilByUid(uid) + #遍历地块,查看地块是否可以播种 + for i in arange(1, soilNumber + 1): + if count > 0: + soilName = f"soil{str(i)}" + #如果可以播种 + if await g_pSqlManager.getUserSoilStatusBySoilID(uid, soilName): + count -= 1 + await g_pSqlManager.updateUserSoilStatusBySowing(uid, soilName, plantName) + return f"播种{plantName}成功!" + return "播种失败" + g_pFarmManager = CFarmManager() diff --git a/resource/background/expansion.png b/resource/background/expansion.png new file mode 100644 index 0000000..f056606 Binary files /dev/null and b/resource/background/expansion.png differ diff --git a/resource/background/gains.png b/resource/background/gains.png new file mode 100644 index 0000000..4f5b5db Binary files /dev/null and b/resource/background/gains.png differ