commit 599acc8a851af28f681b32ef8c2f7d642b1e48a3 Author: Art_Sakura <1754798088@qq.com> Date: Sun Mar 16 19:11:05 2025 +0800 首次提交 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..de6bc7f --- /dev/null +++ b/__init__.py @@ -0,0 +1,36 @@ +from nonebot.plugin import PluginMetadata +from nonebot import get_driver + +from zhenxun.configs.utils import PluginExtraData +from zhenxun.services.plugin_init import PluginInit + +from .gClass import( + g_pSqlManager, + g_pJsonManager +) +from .command import ( + diuse_register +) + +__plugin_meta = PluginMetadata( + name = "真寻的农场", + description = "快乐的农场时光", + usage = """ + 农场快乐时光 + """.strip(), + extra = PluginExtraData( + author="molanp", + version="1.0", + menu_type="群内小游戏", + ).dict(), +) + +#注册启动函数 +driver = get_driver() + +@driver.on_startup +async def start(): + #初始化数据库 + await g_pSqlManager.init() + + await g_pJsonManager.init() diff --git a/command.py b/command.py new file mode 100644 index 0000000..6399d9d --- /dev/null +++ b/command.py @@ -0,0 +1,31 @@ +from nonebot.rule import to_me +from nonebot_plugin_uninfo import Uninfo +from nonebot_plugin_alconna import Alconna, on_alconna, Text + +from zhenxun.services.log import logger + + +from .gClass import( + g_pSqlManager +) + +diuse_register = on_alconna( + Alconna("注册空间"), + priority = 5, + rule=to_me(), + block = True, +) + +@diuse_register.handle() +async def _(session: Uninfo): + uid = str(session.user.id) + + user = await g_pSqlManager.getUserByUid(uid) + + if user: + await diuse_register.send(Text("你已经有啦"), reply_to=True) + else: + info = {'uid' : uid, 'name' : '测试', 'level' : 1, 'point' : 0} + + aaa = await g_pSqlManager.appendUserByUserInfo(info) + await diuse_register.send(Text(str(aaa)), reply_to=True) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..91be6c8 --- /dev/null +++ b/config.py @@ -0,0 +1,55 @@ +import json +from pathlib import Path + +from zhenxun.services.log import logger +from zhenxun.configs.path_config import DATA_PATH, IMAGE_PATH, TEMPLATE_PATH + +g_sDBPath = DATA_PATH / "farm_db" +g_sDBFilePath = DATA_PATH / "farm_db/farm.db" + +class CJsonManager: + def __init__(self): + self.m_pItem = None + self.m_pPlant = None + + @classmethod + async def init(cls) -> bool: + if not await cls.initItem(): + return False + + if not await cls.initPlant(): + return False + + return True + + @classmethod + async def initItem(cls) -> bool: + current_file_path = Path(__file__) + + try: + with open(current_file_path.resolve().parent / "config/item.json", 'r', encoding='utf-8') as file: + cls.m_pItem = json.load(file) + + return True + except FileNotFoundError: + logger.warning(f"item.json 打开失败: {e}") + return False + except json.JSONDecodeError as e: + logger.warning(f"item.json JSON格式错误: {e}") + return False + + @classmethod + async def initPlant(cls) -> bool: + current_file_path = Path(__file__) + + try: + with open(current_file_path.resolve().parent / "config/plant.json", 'r', encoding='utf-8') as file: + cls.m_pPlant = json.load(file) + + return True + except FileNotFoundError: + logger.warning(f"plant.json 打开失败: {e}") + return False + except json.JSONDecodeError as e: + logger.warning(f"plant.json JSON格式错误: {e}") + return False \ No newline at end of file diff --git a/config/item.json b/config/item.json new file mode 100644 index 0000000..6056e4a --- /dev/null +++ b/config/item.json @@ -0,0 +1,21 @@ +{ + "item": + { + "zhuShi": + [ + "name: 名称", + "level: 等级", + "price: 单价", + "icon: 显示图片", + "sell: 是否可以上架交易行" + ], + "muBan": + { + "name": "木板", + "level": 0, + "price": 5000, + "icon": "Test", + "sell": true + } + } +} \ No newline at end of file diff --git a/config/plant.json b/config/plant.json new file mode 100644 index 0000000..b1d6e6b --- /dev/null +++ b/config/plant.json @@ -0,0 +1,65 @@ +{ + "plant": + { + "zhuShi": + [ + "name: 名称", + "level: 解锁等级", + "limit: 限制等级 0:普通土地 1:红土地 2:黄土地 3:黑土地", + "experience: 收获经验", + "harvest: 收获数量", + "price: 单价", + "time: 成熟时间 单位:分钟", + "crop: 作物可以收几次", + "again: 再次成熟时间 单位:分钟", + "phase: 阶段 目前为 成熟时间 / 阶段 来显示每阶段图片", + "general: 第一阶段是否为通用阶段素材", + "sell: 是否可以上架交易行" + ], + "daBaiCai": + { + "name": "大白菜", + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 780, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "baiLuoBo": + { + "name": "白萝卜", + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 600, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + }, + "huLuoBo": + { + "name": "胡萝卜", + "level": 0, + "limit": 0, + "experience": 5, + "harvest": 30, + "price": 5, + "time": 5, + "crop": 1, + "again": 0, + "phase": 5, + "general": true, + "sell": false + } + } +} \ No newline at end of file diff --git a/database.py b/database.py new file mode 100644 index 0000000..e86a506 --- /dev/null +++ b/database.py @@ -0,0 +1,108 @@ +import os +import aiosqlite +import asyncio + +from zhenxun.services.log import logger +from zhenxun.configs.path_config import DATA_PATH, IMAGE_PATH, TEMPLATE_PATH + +from .config import ( + g_sDBPath, + g_sDBFilePath +) + +class CSqlManager: + def __init__(self): + g_sDBPath.mkdir(parents=True, exist_ok=True) + + def __del__(self): + if self.m_pDB: + self.m_pDB.close() + self.m_pDB = None + + @classmethod + async def init(cls) -> bool: + bIsExist = os.path.exists(g_sDBFilePath) + + cls.m_pDB = await aiosqlite.connect(g_sDBFilePath) + + if bIsExist == False: + #TODO 缺少判断创建失败事件 + await cls.createDB() + + return True + + @classmethod + async def createDB(cls) -> bool: + """初始化数据库表 + + Returns: + bool: 是否创建成功 + """ + + try: + await cls.m_pDB.execute(''' + CREATE TABLE user ( + uid INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + level INTEGER DEFAULT 1, + point INTEGER DEFAULT 0 + ); + ''') + await cls.m_pDB.commit() + return True + except Exception as e: + print(f"An error occurred: {e}") + return False + + @classmethod + async def getUserByUid(cls, uid : str) -> list[dict]: + """根据用户Uid获取用户信息 + + Args: + uid (str): 用户Uid + + Returns: + list[dict]: 用户信息 + """ + if len(uid) <= 0: + return [] + + try: + async with cls.m_pDB.execute("SELECT * FROM user WHERE uid = ?", (uid,)) as cursor: + results = [] + + async for row in cursor: + user_dict = { + 'uid': row[0], + 'name': row[1], + 'level': row[2], + 'point': row[3] + } + results.append(user_dict) + + return results + except Exception as e: + logger.warning(f"查询失败: {e}") + return [] + + @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, level, point) VALUES (?, ?, ?, ?) + ''', (info['uid'], info['name'], info['level'], info['point'])) + await cls.m_pDB.commit() + + return True + except Exception as e: + logger.warning(f"添加失败: {e}") + return False \ No newline at end of file diff --git a/gClass.py b/gClass.py new file mode 100644 index 0000000..1167a8f --- /dev/null +++ b/gClass.py @@ -0,0 +1,6 @@ +from .config import CJsonManager +from .database import CSqlManager + +g_pJsonManager = CJsonManager() + +g_pSqlManager = CSqlManager() diff --git a/initFarm.py b/initFarm.py new file mode 100644 index 0000000..503e889 --- /dev/null +++ b/initFarm.py @@ -0,0 +1,8 @@ +from nonebot_plugin_uninfo import Uninfo +from nonebot_plugin_alconna import Alconna, At, Image, Match, Text, UniMsg, on_alconna + +import command + +@diuse_register.handle() +async def _(session: Uninfo): + uid = str(session.user.id) \ No newline at end of file diff --git a/resource/basic/0.png b/resource/basic/0.png new file mode 100644 index 0000000..aee0a5c Binary files /dev/null and b/resource/basic/0.png differ diff --git a/resource/basic/9.png b/resource/basic/9.png new file mode 100644 index 0000000..21212a7 Binary files /dev/null and b/resource/basic/9.png differ diff --git a/resource/大白菜/icon.png b/resource/大白菜/icon.png new file mode 100644 index 0000000..ff1fa65 Binary files /dev/null and b/resource/大白菜/icon.png differ diff --git a/resource/大蒜/icon.png b/resource/大蒜/icon.png new file mode 100644 index 0000000..8bf3c4d Binary files /dev/null and b/resource/大蒜/icon.png differ diff --git a/resource/小麦/icon.png b/resource/小麦/icon.png new file mode 100644 index 0000000..ab60a3d Binary files /dev/null and b/resource/小麦/icon.png differ diff --git a/resource/水稻/icon.png b/resource/水稻/icon.png new file mode 100644 index 0000000..51636b7 Binary files /dev/null and b/resource/水稻/icon.png differ diff --git a/resource/油菜/icon.png b/resource/油菜/icon.png new file mode 100644 index 0000000..ae3797a Binary files /dev/null and b/resource/油菜/icon.png differ diff --git a/resource/牧草/icon.png b/resource/牧草/icon.png new file mode 100644 index 0000000..24d6b12 Binary files /dev/null and b/resource/牧草/icon.png differ diff --git a/resource/玉米/icon.png b/resource/玉米/icon.png new file mode 100644 index 0000000..921225d Binary files /dev/null and b/resource/玉米/icon.png differ diff --git a/resource/生菜/icon.png b/resource/生菜/icon.png new file mode 100644 index 0000000..2f8eef3 Binary files /dev/null and b/resource/生菜/icon.png differ diff --git a/resource/白萝卜/icon.png b/resource/白萝卜/icon.png new file mode 100644 index 0000000..2289998 Binary files /dev/null and b/resource/白萝卜/icon.png differ diff --git a/resource/胡萝卜/1.png b/resource/胡萝卜/1.png new file mode 100644 index 0000000..810e456 Binary files /dev/null and b/resource/胡萝卜/1.png differ diff --git a/resource/胡萝卜/2.png b/resource/胡萝卜/2.png new file mode 100644 index 0000000..07680e0 Binary files /dev/null and b/resource/胡萝卜/2.png differ diff --git a/resource/胡萝卜/3.png b/resource/胡萝卜/3.png new file mode 100644 index 0000000..ba91e1b Binary files /dev/null and b/resource/胡萝卜/3.png differ diff --git a/resource/胡萝卜/4.png b/resource/胡萝卜/4.png new file mode 100644 index 0000000..a96ab40 Binary files /dev/null and b/resource/胡萝卜/4.png differ diff --git a/resource/胡萝卜/icon.png b/resource/胡萝卜/icon.png new file mode 100644 index 0000000..2e51389 Binary files /dev/null and b/resource/胡萝卜/icon.png differ