From 13ddcf2a585051ec9517057ab0f1e82b06811e11 Mon Sep 17 00:00:00 2001 From: Art_Sakura <1754798088@qq.com> Date: Wed, 19 Mar 2025 18:00:50 +0800 Subject: [PATCH] =?UTF-8?q?feat=E2=9C=A8:=20=E6=96=B0=E5=A2=9E=E7=A7=8D?= =?UTF-8?q?=E5=AD=90=E5=95=86=E5=BA=97=E5=8A=9F=E8=83=BD=20feat=E2=9C=A8:?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=BF=87=E5=A4=9A=E7=9A=84py=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20fix=F0=9F=90=9B:=20=E5=AF=B9=E9=83=A8=E5=88=86json?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BF=9B=E8=A1=8C=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 3 +- command.py | 49 ++++++++++------ config/level.json | 22 ++++++-- config/plant.json | 108 +++++++++++++++++++++++++++++++++--- config/soil.json | 2 +- database.py | 138 ++++++++++++++++++++++------------------------ drawImage.py | 43 --------------- farm/farm.py | 99 ++++++++++++++++++++++++++++++--- farm/shop.py | 104 ++++++++++++++++++++++++++++++---- globalClass.py | 9 --- 10 files changed, 405 insertions(+), 172 deletions(-) delete mode 100644 drawImage.py delete mode 100644 globalClass.py diff --git a/__init__.py b/__init__.py index 48a306c..5ddc680 100644 --- a/__init__.py +++ b/__init__.py @@ -2,12 +2,13 @@ from nonebot import get_driver from nonebot.plugin import PluginMetadata from zhenxun.configs.utils import PluginExtraData +from zhenxun.services.log import logger from zhenxun.utils.message import MessageUtils from .command import diuse_farm, diuse_register from .config import g_pJsonManager from .database import g_pSqlManager -from .drawImage import g_pDrawImage +from .farm.shop import g_pShopManager # from .globalClass import g_pDrawImage, g_pJsonManager, g_pSqlManager diff --git a/command.py b/command.py index 1393ef9..e5640ad 100644 --- a/command.py +++ b/command.py @@ -3,10 +3,12 @@ from nonebot_plugin_alconna import (Alconna, AlconnaQuery, Args, Match, Option, Query, Subcommand, on_alconna, store_true) from nonebot_plugin_uninfo import Uninfo +from zhenxun.services.log import logger from zhenxun.utils.message import MessageUtils from .database import g_pSqlManager -from .drawImage import g_pDrawImage +from .farm.farm import g_pFarmManager +from .farm.shop import g_pShopManager diuse_register = on_alconna( Alconna("开通农场"), @@ -24,9 +26,7 @@ async def _(session: Uninfo): if user: await MessageUtils.build_message("你已经有啦").send(reply_to=True) else: - info = {"uid": uid, "name": "测试", "exp": 0, "point": 100} - - aaa = await g_pSqlManager.appendUserByUserInfo(info) + aaa = await g_pSqlManager.initUserInfoByUid(uid, str(session.user.name), 0, 100) await MessageUtils.build_message(str(aaa)).send(reply_to=True) @@ -51,16 +51,14 @@ diuse_farm = on_alconna( @diuse_farm.assign("$main") async def _(session: Uninfo): uid = str(session.user.id) + point = await g_pSqlManager.getUserPointByUid(uid) - level = await g_pSqlManager.getUserLevelByUid(uid) - if level <= 0: + if point < 0: await MessageUtils.build_message("尚未开通农场").send() - return None - image = await g_pDrawImage.drawMyFarm(uid) - - await MessageUtils.build_message(image).send() + image = await g_pFarmManager.drawFarmByUid(uid) + await MessageUtils.build_message(image).send(reply_to=True) diuse_farm.shortcut( "我的农场币", @@ -74,6 +72,10 @@ async def _(session: Uninfo): uid = str(session.user.id) point = await g_pSqlManager.getUserPointByUid(uid) + if point < 0: + await MessageUtils.build_message("尚未开通农场").send() + return None + await MessageUtils.build_message(f"你的当前农场币为: {point}").send(reply_to=True) diuse_farm.shortcut( @@ -88,7 +90,12 @@ async def _(session: Uninfo): uid = str(session.user.id) point = await g_pSqlManager.getUserPointByUid(uid) - await MessageUtils.build_message(f"你的当前农场币为: {point}").send(reply_to=True) + if point < 0: + await MessageUtils.build_message("尚未开通农场").send() + return None + + image = await g_pShopManager.getPlantShopImage() + await MessageUtils.build_message(image).send() diuse_farm.shortcut( "购买种子(?P.*?)", @@ -104,19 +111,20 @@ async def _(session: Uninfo, name: Match[str], num: Query[int] = AlconnaQuery("n "请在指令后跟需要购买的种子名称" ).finish(reply_to=True) - # result = await ShopManage.buy_prop(session.user.id, name.result, num.result) - uid = str(session.user.id) point = await g_pSqlManager.getUserPointByUid(uid) - await MessageUtils.build_message(f"你的当前农场币为: {point}").send(reply_to=True) - + if point < 0: + await MessageUtils.build_message("尚未开通农场").send() + return None + result = await g_pShopManager.buyPlant(uid, name.result, num.result) + await MessageUtils.build_message(result).send(reply_to=True) diuse_farm.shortcut( - "种子商店", + "我的种子", command="我的农场", - arguments=["plant-shop"], + arguments=["my-plant"], prefix=True, ) @@ -125,4 +133,9 @@ async def _(session: Uninfo): uid = str(session.user.id) point = await g_pSqlManager.getUserPointByUid(uid) - await MessageUtils.build_message(f"你的当前农场币为: {point}").send(reply_to=True) + if point < 0: + await MessageUtils.build_message("尚未开通农场").send() + return None + + result = await g_pFarmManager.getUserPlantByUid(uid) + await MessageUtils.build_message(result).send(reply_to=True) diff --git a/config/level.json b/config/level.json index 7ec4659..7a9df1c 100644 --- a/config/level.json +++ b/config/level.json @@ -2,10 +2,24 @@ "level": { "1": 0, - "2": 500, - "3": 1250, - "4": 3000 + "2": 50, + "3": 75, + "4": 125, + "5": 185, + "6": 200, + "7": 275, + "8": 350, + "9": 500, + "10": 850, + "11": 1250, + "12": 2250, + "13": 3500, + "14": 4500, + "15": 5800 }, - "soil":[1, 1, 1, 5, 10, 15, 25], + "soil":[1, 1, 1, 2, 3, 5, 7, 9, 12, 15, + 18, 21, 25, 29, 33, 38, 43, 48, 54, 60, + 66, 72, 78, 85, 92, 99, 106, 114, 120, 130], + "point":[0, 2000, 5000, 10000, 30000, 50000] } diff --git a/config/plant.json b/config/plant.json index e1793f4..fa223d2 100644 --- a/config/plant.json +++ b/config/plant.json @@ -1,7 +1,6 @@ { "zhuShi": [ - "name: 名称", "level: 解锁等级", "limit: 限制等级 0:普通土地 1:红土地 2:黄土地 3:黑土地", "experience: 收获经验", @@ -16,9 +15,8 @@ ], "plant": { - "daBaiCai": + "大白菜": { - "name": "大白菜", "level": 0, "limit": 0, "experience": 5, @@ -31,9 +29,8 @@ "general": true, "sell": false }, - "baiLuoBo": + "白萝卜": { - "name": "白萝卜", "level": 0, "limit": 0, "experience": 5, @@ -46,9 +43,106 @@ "general": true, "sell": false }, - "huLuoBo": + "胡萝卜": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "牧草": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "大蒜": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "水稻": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "小麦": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "玉米": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "油菜": + { + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "生菜": { - "name": "胡萝卜", "level": 0, "limit": 0, "experience": 5, diff --git a/config/soil.json b/config/soil.json index 4197087..e67a174 100644 --- a/config/soil.json +++ b/config/soil.json @@ -1,5 +1,5 @@ { - "size":[234.378, 156.252], + "size":[234, 156], "soil": { "1": diff --git a/database.py b/database.py index f08b3e9..82525f6 100644 --- a/database.py +++ b/database.py @@ -127,6 +127,34 @@ class CSqlManager: logger.error(f"数据库执行失败: {e}") return None + @classmethod + async def initUserInfoByUid(cls, + uid: str, name: str = "", exp: int = 0, point: int = 100): + #用户信息 + userInfo = f""" + INSERT INTO user (uid, name, exp, point) VALUES ({uid}, '{name}', {str(exp)}, {str(point)}) + """ + + #用户仓库 + userStorehouse = f""" + INSERT INTO storehouse (uid) VALUES ({uid}); + """ + + userSoilInfo = f""" + INSERT INTO soil (uid) VALUES ({uid}); + """ + + if not await cls.executeDB(userInfo): + return False + + if not await cls.executeDB(userStorehouse): + return False + + if not await cls.executeDB(userSoilInfo): + return False + + return True + @classmethod async def getUserInfoByUid(cls, uid: str) -> list[dict]: """根据用户Uid获取用户信息 @@ -157,7 +185,7 @@ class CSqlManager: return results except Exception as e: - logger.warning(f"查询失败: {e}") + logger.warning(f"getUserInfoByUid查询失败: {e}") return [] @classmethod @@ -175,12 +203,14 @@ class CSqlManager: try: async with cls.m_pDB.execute( - "SELECT point FROM user WHERE uid = ?", (uid,) + f"SELECT point FROM user WHERE uid = {uid}" ) as cursor: async for row in cursor: return int(row[0]) + + return -1 except Exception as e: - logger.warning(f"查询失败: {e}") + logger.warning(f"getUserPointByUid查询失败: {e}") return -1 @classmethod @@ -231,14 +261,12 @@ class CSqlManager: return -1 try: - async with cls.m_pDB.execute( - "SELECT exp FROM user WHERE uid = ?", (uid,) - ) as cursor: + async with cls.m_pDB.execute(f"SELECT exp FROM user WHERE uid = '{uid}'") as cursor: async for row in cursor: exp = int(row[0]) #获取等级列表 - levelDict = g_pJsonManager.m_pLevel['level'] + levelDict = g_pJsonManager.m_pLevel['level'] # type: ignore sorted_keys = sorted(levelDict.keys(), key=lambda x: int(x), reverse=True) for key in sorted_keys: @@ -247,7 +275,7 @@ class CSqlManager: return -1 except Exception as e: - logger.warning(f"查询失败: {e}") + logger.warning(f"getUserLevelByUid查询失败: {e}") return -1 @classmethod @@ -275,79 +303,47 @@ class CSqlManager: return soilNumber @classmethod - async def appendUserByUserInfo(cls, info: list[dict]) -> bool: - """添加用户信息 + async def getUserPlantByUid(cls, uid: str) -> str: + """获取用户仓库种子信息 Args: info (list[dict]): 用户信息 + Returns: + str: 仓库种子信息 + """ + + if len(uid) <= 0: + return "" + + try: + async with cls.m_pDB.execute( + "SELECT plant FROM storehouse WHERE uid = ?", (uid,) + ) as cursor: + async for row in cursor: + return row[0] + + return "" + except Exception as e: + logger.warning(f"getUserPlantByUid查询失败: {e}") + return "" + + @classmethod + async def updateUserPlantByUid(cls, uid: str, plant: str) -> bool: + """添加用户仓库种子信息 + + Args: + info (list[dict]): 种子信息 + Returns: bool: 是否添加成功 """ - try: - await cls.m_pDB.execute( - """ - INSERT INTO user (uid, name, exp, point) VALUES (?, ?, ?, ?) - """, - (info["uid"], info["name"], info["exp"], info["point"]), - ) - await cls.m_pDB.commit() - - return True - except Exception as e: - logger.warning(f"添加失败: {e}") + if len(uid) <= 0: return False - # @classmethod - # async def getUserStorehousePlant(cls, info: list[dict]) -> str: - # """获取用户仓库种子信息 - - # Args: - # info (list[dict]): 用户信息 - - # Returns: - # str: 仓库种子信息 - # """ - - # try: - # await cls.m_pDB.execute( - # """ - # INSERT INTO user (uid, name, exp, point) VALUES (?, ?, ?, ?) - # """, - # (info["uid"], info["name"], info["exp"], info["point"]), - # ) - # await cls.m_pDB.commit() - - # return True - # except Exception as e: - # logger.warning(f"添加失败: {e}") - # return False - - # @classmethod - # async def appendUserByUserInfo(cls, info: list[dict]) -> bool: - # """添加用户信息 - - # Args: - # info (list[dict]): 用户信息 - - # Returns: - # bool: 是否添加成功 - # """ - - # try: - # await cls.m_pDB.execute( - # """ - # INSERT INTO user (uid, name, exp, point) VALUES (?, ?, ?, ?) - # """, - # (info["uid"], info["name"], info["exp"], info["point"]), - # ) - # await cls.m_pDB.commit() - - # return True - # except Exception as e: - # logger.warning(f"添加失败: {e}") - # return False + sql = f"UPDATE storehouse SET plant = '{plant}' WHERE uid = '{uid}'" + return await cls.executeDB(sql) g_pSqlManager = CSqlManager() diff --git a/drawImage.py b/drawImage.py deleted file mode 100644 index c85d64f..0000000 --- a/drawImage.py +++ /dev/null @@ -1,43 +0,0 @@ -from zhenxun.services.log import logger -from zhenxun.utils._build_image import BuildImage - -from .config import g_pJsonManager, g_sResourcePath -from .database import g_pSqlManager - - -class CDrawImageManager: - - @classmethod - async def drawMyFarm(cls, uid: str) -> bytes: - """绘制我的农场 - - Args: - uid (str): 用户UID - - Returns: - bytes: 返回绘制结果 - """ - soilNumber = await g_pSqlManager.getUserLevelByUid(uid) - - img = BuildImage(background=g_sResourcePath / "background/background.jpg") - - soilSize = g_pJsonManager.m_pSoil['size'] # type: ignore - - #TODO 缺少判断用户土地资源状况 - soil = BuildImage(background=g_sResourcePath / "soil/普通土地.png") - await soil.resize(0, soilSize[0], soilSize[1]) - - grass = BuildImage(background=g_sResourcePath / "soil/草土地.png") - await grass.resize(0, soilSize[0], soilSize[1]) - - soilPos = g_pJsonManager.m_pSoil['soil'] # type: ignore - - for key, value in soilPos.items(): - if soilNumber >= int(key): - await img.paste(soil, (value['x'], value['y'])) - else: - await img.paste(grass, (value['x'], value['y'])) - - return img.pic2bytes() - -g_pDrawImage = CDrawImageManager() diff --git a/farm/farm.py b/farm/farm.py index b14f51c..3ee1cf9 100644 --- a/farm/farm.py +++ b/farm/farm.py @@ -1,5 +1,6 @@ from zhenxun.services.log import logger from zhenxun.utils._build_image import BuildImage +from zhenxun.utils.image_utils import ImageTemplate from ..config import g_pJsonManager, g_sResourcePath from ..database import g_pSqlManager @@ -8,8 +9,8 @@ from ..database import g_pSqlManager class CFarmManager: @classmethod - async def drawFarm(cls, uid: str) -> bytes: - """绘制农场 + async def drawFarmByUid(cls, uid: str) -> bytes: + """绘制用户农场 Args: uid (str): 用户UID @@ -31,11 +32,95 @@ class CFarmManager: await grass.resize(0, soilSize[0], soilSize[1]) soilPos = g_pJsonManager.m_pSoil['soil'] # type: ignore + soilUnlock = g_pJsonManager.m_pLevel['soil'] # type: ignore - for key, value in soilPos.items(): - if soilNumber >= int(key): - await img.paste(soil, (value['x'], value['y'])) + x = 0 + y = 0 + 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)) + + #缺少判断土地上是否有农作物 + plant = BuildImage(background=g_sResourcePath / "plant/basic/0.png") + await plant.resize(0, 35, 58) + await img.paste(plant, (x + 3, y + 3)) else: - await img.paste(grass, (value['x'], value['y'])) + await img.paste(grass, (x, y)) - return img.pic2bytes() \ No newline at end of file + return img.pic2bytes() + + @classmethod + async def getUserPlantByUid(cls, uid: str) -> bytes: + data_list = [] + column_name = [ + "-", + "种子名称", + "数量" + "收获经验", + "收获数量", + "成熟时间(分钟)", + "收获次数", + "再次成熟时间(分钟)", + "是否可以上架交易行" + ] + + plant = await g_pSqlManager.getUserPlantByUid(uid) + + if plant == None: + result = await ImageTemplate.table_page( + "种子仓库", + "播种示例:@小真寻 播种 大白菜", + column_name, + data_list, + ) + + return result.pic2bytes() + + sell = "" + + for item in plant.split(','): + if '|' in item: + plant_name, count = item.split('|', 1) # 分割一次,避免多竖线问题 + try: + plantInfo = g_pJsonManager.m_pPlant['plant'][plant_name] # type: ignore + + icon = "" + icon_path = g_sResourcePath / f"plant/{plant_name}/icon.png" + if icon_path.exists(): + icon = (icon_path, 33, 33) + + if plantInfo['again'] == True: + sell = "可以" + else: + sell = "不可以" + + data_list.append( + [ + icon, + plant_name, + count, + plantInfo['experience'], + plantInfo['harvest'], + plantInfo['time'], + plantInfo['crop'], + plantInfo['again'], + sell + ] + ) + + except Exception as e: + continue + + result = await ImageTemplate.table_page( + "种子商店", + "购买示例:@小真寻 购买种子 大白菜 5", + column_name, + data_list, + ) + + return result.pic2bytes() + +g_pFarmManager = CFarmManager() diff --git a/farm/shop.py b/farm/shop.py index d601247..bb6b76c 100644 --- a/farm/shop.py +++ b/farm/shop.py @@ -1,5 +1,6 @@ from zhenxun.services.log import logger from zhenxun.utils._build_image import BuildImage +from zhenxun.utils.image_utils import ImageTemplate from ..config import g_pJsonManager, g_sResourcePath from ..database import g_pSqlManager @@ -9,23 +10,104 @@ class CShopManager: @classmethod async def getPlantShopImage(cls) -> bytes: - return bytes() + """by ATTomato + + Returns: + bytes: _description_ + """ + + data_list = [] + column_name = [ + "-", + "种子名称", + "解锁等级", + "种子单价", + "收获经验", + "收获数量", + "成熟时间(分钟)", + "收获次数", + "再次成熟时间(分钟)", + "是否可以上架交易行" + ] + + sell = "" + plants = g_pJsonManager.m_pPlant['plant'] # type: ignore + for key, plant in plants.items(): + icon = "" + icon_path = g_sResourcePath / f"plant/{key}/icon.png" + if icon_path.exists(): + icon = (icon_path, 33, 33) + + if plant['again'] == True: + sell = "可以" + else: + sell = "不可以" + + data_list.append( + [ + icon, + key, + plant['level'], + plant['price'], + plant['experience'], + plant['harvest'], + plant['time'], + plant['crop'], + plant['again'], + sell + ] + ) + + result = await ImageTemplate.table_page( + "种子商店", + "购买示例:@小真寻 购买种子 大白菜 5", + column_name, + data_list, + ) + + return result.pic2bytes() @classmethod async def buyPlant(cls, uid: str, name: str, num: int = 1) -> str: if num <= 0: return "请输入购买数量!" - plants = g_pJsonManager.m_pPlant['plant'] # type: ignore + plantInfo = None - for key, plant in plants.items(): - if plant['name'] == name: - point = g_pSqlManager.getUserPointByUid(uid) - total = int(plant['price']) * num + try: + plantInfo = g_pJsonManager.m_pPlant['plant'][name] # type: ignore + except Exception as e: + return "购买出错!请检查需购买的种子名称!" - if point < total - return "你的农场币不够哦~ 快速速氪金吧!" - else: - await g_pSqlManager.updateUserPointByUid(uid, point - total) + userPlants = {} - pass \ No newline at end of file + point = await g_pSqlManager.getUserPointByUid(uid) + total = int(plantInfo['price']) * num + + logger.debug(f"用户:{uid}购买{name},数量为{num}。用户农场币为{point},购买需要{total}") + + if point < total: + return "你的农场币不够哦~ 快速速氪金吧!" + else: + p = await g_pSqlManager.getUserPlantByUid(uid) + + if not p == None: + for item in p.split(','): + if '|' in item: + plant_name, count = item.split('|', 1) # 分割一次,避免多竖线问题 + userPlants[plant_name] = int(count) + + if name in userPlants: + userPlants[name] += num + else: + userPlants[name] = num + + plant_list = [f"{k}|{v}" for k, v in userPlants.items()] + + await g_pSqlManager.updateUserPointByUid(uid, point - total) + await g_pSqlManager.updateUserPlantByUid(uid, ','.join(plant_list)) + + return f"成功购买{name},当前仓库数量为:{userPlants[name]},花费{total}农场币, 剩余{point - total}农场币" + + +g_pShopManager = CShopManager() diff --git a/globalClass.py b/globalClass.py deleted file mode 100644 index 7864e39..0000000 --- a/globalClass.py +++ /dev/null @@ -1,9 +0,0 @@ -# from .config import CJsonManager -# from .database import CSqlManager -# from .drawImage import CDrawImageManager - -# g_pJsonManager = CJsonManager() - -# g_pSqlManager = CSqlManager() - -# g_pDrawImage = CDrawImageManager(g_pJsonManager, g_pSqlManager)