diff --git a/__init__.py b/__init__.py index 6d2b440..c26b52a 100644 --- a/__init__.py +++ b/__init__.py @@ -2,9 +2,14 @@ from nonebot import get_driver from nonebot.plugin import PluginMetadata from zhenxun.configs.utils import PluginExtraData +from zhenxun.utils.message import MessageUtils from .command import diuse_farm, diuse_register -from .globalClass import g_pJsonManager, g_pSqlManager +from .config import g_pJsonManager +from .database import g_pSqlManager +from .drawImage import g_pDrawImage + +# from .globalClass import g_pDrawImage, g_pJsonManager, g_pSqlManager __plugin_meta = PluginMetadata( name="真寻的农场", @@ -26,11 +31,16 @@ driver = get_driver() @driver.on_startup async def start(): # 初始化数据库 + # await g_pSqlManager.init() + await g_pSqlManager.init() # 初始化读取Json + # await g_pJsonManager.init() await g_pJsonManager.init() + # await g_pDrawImage.drawMyFarm("11223") + await g_pDrawImage.drawMyFarm("22") # 析构函数 @driver.on_shutdown diff --git a/command.py b/command.py index 1f4a98b..1393ef9 100644 --- a/command.py +++ b/command.py @@ -5,7 +5,8 @@ from nonebot_plugin_uninfo import Uninfo from zhenxun.utils.message import MessageUtils -from .globalClass import g_pDrawImage, g_pSqlManager +from .database import g_pSqlManager +from .drawImage import g_pDrawImage diuse_register = on_alconna( Alconna("开通农场"), @@ -23,7 +24,7 @@ async def _(session: Uninfo): if user: await MessageUtils.build_message("你已经有啦").send(reply_to=True) else: - info = {"uid": uid, "name": "测试", "level": 1, "point": 0} + info = {"uid": uid, "name": "测试", "exp": 0, "point": 100} aaa = await g_pSqlManager.appendUserByUserInfo(info) @@ -50,7 +51,14 @@ diuse_farm = on_alconna( @diuse_farm.assign("$main") async def _(session: Uninfo): uid = str(session.user.id) - image = await g_pDrawImage.drawMyFarm() + + level = await g_pSqlManager.getUserLevelByUid(uid) + if level <= 0: + await MessageUtils.build_message("尚未开通农场").send() + + return None + + image = await g_pDrawImage.drawMyFarm(uid) await MessageUtils.build_message(image).send() @@ -96,6 +104,7 @@ 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) diff --git a/config.py b/config.py index 9785eef..5bc2813 100644 --- a/config.py +++ b/config.py @@ -9,24 +9,29 @@ g_sDBFilePath = DATA_PATH / "farm_db/farm.db" g_sResourcePath = Path(__file__).resolve().parent / "resource" - class CJsonManager: def __init__(self): self.m_pItem = None self.m_pPlant = None + self.m_pLevel = None + self.m_pSoil = None - @classmethod - async def init(cls) -> bool: - if not await cls.initItem(): + async def init(self) -> bool: + if not await self.initItem(): return False - if not await cls.initPlant(): + if not await self.initPlant(): + return False + + if not await self.initLevel(): + return False + + if not await self.initSoil(): return False return True - @classmethod - async def initItem(cls) -> bool: + async def initItem(self) -> bool: current_file_path = Path(__file__) try: @@ -34,7 +39,7 @@ class CJsonManager: current_file_path.resolve().parent / "config/item.json", encoding="utf-8", ) as file: - cls.m_pItem = json.load(file) + self.m_pItem = json.load(file) return True except FileNotFoundError: @@ -44,8 +49,7 @@ class CJsonManager: logger.warning(f"item.json JSON格式错误: {e}") return False - @classmethod - async def initPlant(cls) -> bool: + async def initPlant(self) -> bool: current_file_path = Path(__file__) try: @@ -53,7 +57,7 @@ class CJsonManager: current_file_path.resolve().parent / "config/plant.json", encoding="utf-8", ) as file: - cls.m_pPlant = json.load(file) + self.m_pPlant = json.load(file) return True except FileNotFoundError: @@ -62,3 +66,41 @@ class CJsonManager: except json.JSONDecodeError as e: logger.warning(f"plant.json JSON格式错误: {e}") return False + + async def initLevel(self) -> bool: + current_file_path = Path(__file__) + + try: + with open( + current_file_path.resolve().parent / "config/level.json", + encoding="utf-8", + ) as file: + self.m_pLevel = json.load(file) + + return True + except FileNotFoundError: + logger.warning("plant.json 打开失败") + return False + except json.JSONDecodeError as e: + logger.warning(f"plant.json JSON格式错误: {e}") + return False + + async def initSoil(self) -> bool: + current_file_path = Path(__file__) + + try: + with open( + current_file_path.resolve().parent / "config/soil.json", + encoding="utf-8", + ) as file: + self.m_pSoil = json.load(file) + + return True + except FileNotFoundError: + logger.warning("plant.json 打开失败") + return False + except json.JSONDecodeError as e: + logger.warning(f"plant.json JSON格式错误: {e}") + return False + +g_pJsonManager = CJsonManager() diff --git a/config/level.json b/config/level.json new file mode 100644 index 0000000..44863c1 --- /dev/null +++ b/config/level.json @@ -0,0 +1,10 @@ +{ + "level": + { + "1": 0, + "2": 3000, + "3": 7500 + }, + "soil":[1, 5, 10, 15, 25, 35], + "point":[0, 2000, 5000, 10000, 30000, 50000] +} diff --git a/config/soil.json b/config/soil.json new file mode 100644 index 0000000..3008dfa --- /dev/null +++ b/config/soil.json @@ -0,0 +1,155 @@ +{ + "soil": + { + "1": + { + "x": 1341, + "y": 571 + }, + "2": + { + "x": 1455, + "y": 627 + }, + "3": + { + "x": 1574, + "y": 686 + }, + "4": + { + "x": 1689, + "y": 745 + }, + "5": + { + "x": 1806, + "y": 804 + }, + "6": + { + "x": 1926, + "y": 863 + }, + "7": + { + "x": 1226, + "y": 627 + }, + "8": + { + "x": 1340, + "y": 683 + }, + "9": + { + "x": 1455, + "y": 739 + }, + "10": + { + "x": 1573, + "y": 798 + }, + "11": + { + "x": 1690, + "y": 857 + }, + "12": + { + "x": 1807, + "y": 916 + }, + "13": + { + "x": 1111, + "y": 683 + }, + "14": + { + "x": 1226, + "y": 739 + }, + "15": + { + "x": 1340, + "y": 795 + }, + "16": + { + "x": 1458, + "y": 855 + }, + "17": + { + "x": 1575, + "y": 912 + }, + "18": + { + "x": 1693, + "y": 972 + }, + "19": + { + "x": 997, + "y": 739 + }, + "20": + { + "x": 1111, + "y": 795 + }, + "21": + { + "x": 1225, + "y": 851 + }, + "22": + { + "x": 1341, + "y": 910 + }, + "23": + { + "x": 1461, + "y": 967 + }, + "24": + { + "x": 1578, + "y": 1025 + }, + "25": + { + "x": 882, + "y": 795 + }, + "26": + { + "x": 997, + "y": 851 + }, + "27": + { + "x": 1111, + "y": 907 + }, + "28": + { + "x": 1226, + "y": 963 + }, + "29": + { + "x": 1343, + "y": 1025 + }, + "30": + { + "x": 1460, + "y": 1084 + } + } +} diff --git a/database.py b/database.py index 0c59f8a..9592a2f 100644 --- a/database.py +++ b/database.py @@ -4,7 +4,7 @@ import aiosqlite from zhenxun.services.log import logger -from .config import g_sDBFilePath, g_sDBPath +from .config import CJsonManager, g_sDBFilePath, g_sDBPath class CSqlManager: @@ -42,7 +42,7 @@ class CSqlManager: CREATE TABLE user ( uid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, - level INTEGER DEFAULT 1, + exp INTEGER DEFAULT 0, point INTEGER DEFAULT 0 ); """) @@ -75,7 +75,7 @@ class CSqlManager: user_dict = { "uid": row[0], "name": row[1], - "level": row[2], + "exp": row[2], "point": row[3], } results.append(user_dict) @@ -87,6 +87,14 @@ class CSqlManager: @classmethod async def getUserPointByUid(cls, uid: str) -> int: + """根据用户Uid获取用户农场币 + + Args: + uid (str): 用户Uid + + Returns: + int: 用户农场币 + """ if len(uid) <= 0: return -1 @@ -100,6 +108,61 @@ class CSqlManager: logger.warning(f"查询失败: {e}") return -1 + @classmethod + async def getUserLevelByUid(cls, uid: str) -> int: + """根据用户Uid获取用户等级 + + Args: + uid (str): 用户Uid + + Returns: + int: 用户等级` + """ + if len(uid) <= 0: + return -1 + + try: + async with cls.m_pDB.execute( + "SELECT exp FROM user WHERE uid = ?", (uid,) + ) as cursor: + async for row in cursor: + exp = int(row[0]) + + #获取等级列表 + levelDict = g_pJsonManager.m_pLevel['level'] + + sorted_keys = sorted(levelDict.keys(), key=lambda x: int(x), reverse=True) + for key in sorted_keys: + if exp >= levelDict[key]: + return int(key) + except Exception as e: + logger.warning(f"查询失败: {e}") + return -1 + + @classmethod + async def getUserSoilByUid(cls, uid: str) -> int: + """根据用户Uid获取解锁地块 + + Args: + uid (str): 用户Uid + + Returns: + int: 解锁几块地 + """ + if len(uid) <= 0: + return -1 + + level = await cls.getUserLevelByUid(uid) + soilNumber = 0 + soil_list = g_pJsonManager.m_pLevel['soil'] # type: ignore + + #获取解锁地块 + for soil in soil_list: + if level >= soil: + soilNumber += 1 + + return soilNumber + @classmethod async def appendUserByUserInfo(cls, info: list[dict]) -> bool: """添加用户信息 @@ -114,9 +177,9 @@ class CSqlManager: try: await cls.m_pDB.execute( """ - INSERT INTO user (uid, name, level, point) VALUES (?, ?, ?, ?) + INSERT INTO user (uid, name, exp, point) VALUES (?, ?, ?, ?) """, - (info["uid"], info["name"], info["level"], info["point"]), + (info["uid"], info["name"], info["exp"], info["point"]), ) await cls.m_pDB.commit() @@ -124,3 +187,5 @@ class CSqlManager: except Exception as e: logger.warning(f"添加失败: {e}") return False + +g_pSqlManager = CSqlManager() diff --git a/drawImage.py b/drawImage.py index 5a1fa4c..0dd728b 100644 --- a/drawImage.py +++ b/drawImage.py @@ -1,11 +1,43 @@ +from ast import arg + +from zhenxun.services.log import logger from zhenxun.utils._build_image import BuildImage -from .config import g_sResourcePath +from .config import g_pJsonManager, g_sResourcePath +from .database import g_pSqlManager class CDrawImageManager: + @classmethod - async def drawMyFarm(cls) -> bytes: + async def drawMyFarm(cls, uid: str) -> bytes: + """绘制我的农场 + + Args: + uid (str): 用户UID + + Returns: + bytes: 返回绘制结果 + """ + # soilNumber = await self.m_pSql.getUserLevelByUid(uid) + soilNumber = 1 + img = BuildImage(background=g_sResourcePath / "background/background.jpg") + soil = BuildImage(background=g_sResourcePath / "soil/普通土地.png") + await soil.resize(0, 229, 112) + + grass = BuildImage(background=g_sResourcePath / "soil/草土地.png") + await grass.resize(0, 229, 112) + + soilPos = g_pJsonManager.m_pSoil['soil'] + + for key, value in soilPos.items(): + if soilNumber >= int(key): + await img.paste(soil, (value['x'], value['y']), center_type="center") + else: + await img.paste(grass, (value['x'], value['y']), center_type="center") + return img.pic2bytes() + +g_pDrawImage = CDrawImageManager() diff --git a/globalClass.py b/globalClass.py index ee1c492..7864e39 100644 --- a/globalClass.py +++ b/globalClass.py @@ -1,9 +1,9 @@ -from .config import CJsonManager -from .database import CSqlManager -from .drawImage import CDrawImageManager +# from .config import CJsonManager +# from .database import CSqlManager +# from .drawImage import CDrawImageManager -g_pJsonManager = CJsonManager() +# g_pJsonManager = CJsonManager() -g_pSqlManager = CSqlManager() +# g_pSqlManager = CSqlManager() -g_pDrawImage = CDrawImageManager() +# g_pDrawImage = CDrawImageManager(g_pJsonManager, g_pSqlManager) diff --git a/resource/soil/普通土地.png b/resource/soil/普通土地.png new file mode 100644 index 0000000..ac01d80 Binary files /dev/null and b/resource/soil/普通土地.png differ diff --git a/resource/soil/缺水-普通土地.png b/resource/soil/缺水-普通土地.png new file mode 100644 index 0000000..f794578 Binary files /dev/null and b/resource/soil/缺水-普通土地.png differ diff --git a/resource/soil/草土地.png b/resource/soil/草土地.png new file mode 100644 index 0000000..e8e028b Binary files /dev/null and b/resource/soil/草土地.png differ