📝 继续对数据库进行优化操作
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import aiosqlite
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from ..config import g_sDBFilePath, g_sDBPath
|
||||
from ..dbService import g_pDBService
|
||||
|
||||
|
||||
class CSqlManager:
|
||||
def __init__(self):
|
||||
g_sDBPath.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@classmethod
|
||||
async def cleanup(cls):
|
||||
if cls.m_pDB:
|
||||
await cls.m_pDB.close()
|
||||
|
||||
@classmethod
|
||||
async def init(cls) -> bool:
|
||||
bIsExist = os.path.exists(g_sDBFilePath)
|
||||
|
||||
cls.m_pDB = await aiosqlite.connect(g_sDBFilePath)
|
||||
cls.m_pDB.row_factory = aiosqlite.Row
|
||||
|
||||
await g_pDBService.init()
|
||||
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
@asynccontextmanager
|
||||
async def _transaction(cls):
|
||||
await cls.m_pDB.execute("BEGIN;")
|
||||
try:
|
||||
yield
|
||||
except:
|
||||
await cls.m_pDB.execute("ROLLBACK;")
|
||||
raise
|
||||
else:
|
||||
await cls.m_pDB.execute("COMMIT;")
|
||||
|
||||
@classmethod
|
||||
async def getTableInfo(cls, tableName: str) -> list:
|
||||
if not re.match(r'^[A-Za-z_][A-Za-z0-9_]*$', tableName):
|
||||
raise ValueError(f"Illegal table name: {tableName}")
|
||||
try:
|
||||
cursor = await cls.m_pDB.execute(f'PRAGMA table_info("{tableName}")')
|
||||
rows = await cursor.fetchall()
|
||||
return [{"name": row[1], "type": row[2]} for row in rows]
|
||||
except aiosqlite.Error:
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
async def ensureTableSchema(cls, tableName: str, columns: dict) -> bool:
|
||||
"""由AI生成
|
||||
创建表或为已存在表添加缺失字段。
|
||||
返回 True 表示有变更(创建或新增列),False 则无操作
|
||||
|
||||
Args:
|
||||
tableName (_type_): 表名
|
||||
columns (_type_): 字典
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
|
||||
info = await cls.getTableInfo(tableName)
|
||||
existing = {col['name']: col['type'].upper() for col in info}
|
||||
desired = {k: v.upper() for k, v in columns.items() if k != "PRIMARY KEY"}
|
||||
primaryKey = columns.get("PRIMARY KEY", "")
|
||||
|
||||
if not existing:
|
||||
colsDef = ", ".join(f'"{k}" {v}' for k, v in desired.items())
|
||||
if primaryKey:
|
||||
colsDef += f", PRIMARY KEY {primaryKey}"
|
||||
await cls.m_pDB.execute(f'CREATE TABLE "{tableName}" ({colsDef});')
|
||||
return True
|
||||
|
||||
toAdd = [k for k in desired if k not in existing]
|
||||
toRemove = [k for k in existing if k not in desired]
|
||||
typeMismatch = [k for k in desired if k in existing and existing[k] != desired[k]]
|
||||
|
||||
if toAdd and not toRemove and not typeMismatch:
|
||||
for col in toAdd:
|
||||
await cls.m_pDB.execute(
|
||||
f'ALTER TABLE "{tableName}" ADD COLUMN "{col}" {columns[col]}'
|
||||
)
|
||||
return True
|
||||
|
||||
async with cls._transaction():
|
||||
tmpTable = f"{tableName}_new"
|
||||
colsDef = ", ".join(f'"{k}" {v}' for k, v in desired.items())
|
||||
if primaryKey:
|
||||
colsDef += f", PRIMARY KEY {primaryKey}"
|
||||
await cls.m_pDB.execute(f'CREATE TABLE "{tmpTable}" ({colsDef});')
|
||||
|
||||
commonCols = [k for k in desired if k in existing]
|
||||
if commonCols:
|
||||
colsStr = ", ".join(f'"{c}"' for c in commonCols)
|
||||
await cls.m_pDB.execute(
|
||||
f'INSERT INTO "{tmpTable}" ({colsStr}) SELECT {colsStr} FROM "{tableName}";'
|
||||
)
|
||||
await cls.m_pDB.execute(f'DROP TABLE "{tableName}";')
|
||||
await cls.m_pDB.execute(f'ALTER TABLE "{tmpTable}" RENAME TO "{tableName}";')
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def executeDB(cls, command: str) -> bool:
|
||||
"""执行自定义SQL
|
||||
|
||||
Args:
|
||||
command (str): SQL语句
|
||||
|
||||
Returns:
|
||||
bool: 是否执行成功
|
||||
"""
|
||||
if len(command) <= 0:
|
||||
logger.warning("数据库语句长度为空!")
|
||||
return False
|
||||
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(command)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("数据库语句执行出错:" + command, e=e)
|
||||
return False
|
||||
|
||||
g_pSqlManager = CSqlManager()
|
||||
@@ -0,0 +1,400 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import List, Union
|
||||
|
||||
from database import CSqlManager
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class CUserDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
"""初始化用户表结构,确保user表存在且字段完整"""
|
||||
# 用户Uid
|
||||
# 农场名称
|
||||
# 经验值
|
||||
# 金币
|
||||
# 解锁土地数量
|
||||
# 偷菜时间字符串
|
||||
# 剩余偷菜次数
|
||||
userInfo = {
|
||||
"uid": "INTEGER PRIMARY KEY AUTOINCREMENT",
|
||||
"name": "TEXT NOT NULL",
|
||||
"exp": "INTEGER DEFAULT 0",
|
||||
"point": "INTEGER DEFAULT 0",
|
||||
"soil": "INTEGER DEFAULT 3",
|
||||
"stealTime": "TEXT DEFAULT NULL",
|
||||
"stealCount": "INTEGER DEFAULT 0"
|
||||
}
|
||||
await cls.ensureTableSchema("user", userInfo)
|
||||
|
||||
@classmethod
|
||||
async def initUserInfoByUid(cls, uid: str, name: str = "", exp: int = 0, point: int = 500) -> Union[bool, str]:
|
||||
"""初始化用户信息,包含初始偷菜时间字符串与次数
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 农场名称
|
||||
exp (int): 农场经验
|
||||
point (int): 农场币
|
||||
|
||||
Returns:
|
||||
Union[bool, str]: False 表示失败,字符串表示成功信息
|
||||
"""
|
||||
nowStr = date.today().strftime('%Y-%m-%d')
|
||||
sql = (
|
||||
f"INSERT INTO user (uid, name, exp, point, soil, stealTime, stealCount) "
|
||||
f"VALUES ({uid}, '{name}', {exp}, {point}, 3, '{nowStr}', 5)"
|
||||
)
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(sql)
|
||||
return "开通农场成功"
|
||||
except Exception as e:
|
||||
logger.warning("initUserInfoByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getAllUsers(cls) -> List[str]:
|
||||
"""获取所有用户UID列表
|
||||
|
||||
Returns:
|
||||
List[str]: 用户UID列表
|
||||
"""
|
||||
cursor = await cls.m_pDB.execute("SELECT uid FROM user")
|
||||
rows = await cursor.fetchall()
|
||||
return [row[0] for row in rows]
|
||||
|
||||
@classmethod
|
||||
async def getUserInfoByUid(cls, uid: str) -> dict:
|
||||
"""获取指定用户完整信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
dict: 包含所有用户字段的字典
|
||||
"""
|
||||
if not uid:
|
||||
return {}
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT * FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
async for row in cursor:
|
||||
return {
|
||||
"uid": row[0],
|
||||
"name": row[1],
|
||||
"exp": row[2],
|
||||
"point": row[3],
|
||||
"soil": row[4],
|
||||
"stealTime": row[5] or "",
|
||||
"stealCount": int(row[6])
|
||||
}
|
||||
return {}
|
||||
except Exception as e:
|
||||
logger.warning("getUserInfoByUid 查询失败!", e=e)
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
async def getUserNameByUid(cls, uid: str) -> str:
|
||||
"""根据用户Uid获取用户名
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
str: 用户名,失败返回空字符串
|
||||
"""
|
||||
if not uid:
|
||||
return ""
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT name FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return row["name"] if row else ""
|
||||
except Exception as e:
|
||||
logger.warning("getUserNameByUid 查询失败!", e=e)
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def updateUserNameByUid(cls, uid: str, name: str) -> bool:
|
||||
"""根据用户Uid更新用户名
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
name (str): 新用户名
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
if not uid or not name:
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE user SET name = ? WHERE uid = ?", (name, uid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("updateUserNameByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getUserPointByUid(cls, uid: str) -> int:
|
||||
"""获取指定用户农场币
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
int: 农场币数量,失败返回 -1
|
||||
"""
|
||||
if not uid:
|
||||
return -1
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT point FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return int(row[0]) if row and row[0] is not None else -1
|
||||
except Exception as e:
|
||||
logger.warning("getUserPointByUid 查询失败!", e=e)
|
||||
return -1
|
||||
|
||||
@classmethod
|
||||
async def updateUserPointByUid(cls, uid: str, point: int) -> bool:
|
||||
"""根据用户Uid更新农场币数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
point (int): 新农场币数量
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
if not uid or point < 0:
|
||||
logger.warning("updateUserPointByUid 参数校验失败!")
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE user SET point = ? WHERE uid = ?", (point, uid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error("updateUserPointByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getUserExpByUid(cls, uid: str) -> int:
|
||||
"""获取指定用户经验值
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
int: 经验值,失败返回 -1
|
||||
"""
|
||||
if not uid:
|
||||
return -1
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT exp FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return int(row[0]) if row and row[0] is not None else -1
|
||||
except Exception as e:
|
||||
logger.warning("getUserExpByUid 查询失败!", e=e)
|
||||
return -1
|
||||
|
||||
@classmethod
|
||||
async def updateUserExpByUid(cls, uid: str, exp: int) -> bool:
|
||||
"""根据用户Uid更新经验值
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
exp (int): 新经验值
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
if not uid:
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE user SET exp = ? WHERE uid = ?", (exp, uid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("updateUserExpByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getUserLevelByUid(cls, uid: str) -> tuple[int, int, int]:
|
||||
"""获取用户等级信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
tuple[int, int, int]: (当前等级, 下级所需经验, 当前等级剩余经验),失败返回(-1, -1, -1)
|
||||
"""
|
||||
if not uid:
|
||||
return -1, -1, -1
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT exp FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
if row and row[0] is not None:
|
||||
expVal = int(row[0])
|
||||
level = expVal // 200
|
||||
nextLevelExp = 200 * (level + 1)
|
||||
currentLevelExp = level * 200
|
||||
remainingExp = expVal - currentLevelExp
|
||||
return level, nextLevelExp, remainingExp
|
||||
return -1, -1, -1
|
||||
except Exception as e:
|
||||
logger.warning("getUserLevelByUid 查询失败!", e=e)
|
||||
return -1, -1, -1
|
||||
|
||||
@classmethod
|
||||
async def getUserSoilByUid(cls, uid: str) -> int:
|
||||
"""获取解锁土地数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
int: 解锁土地块数,失败返回0
|
||||
"""
|
||||
if not uid:
|
||||
return 0
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT soil FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return int(row[0]) if row and row[0] is not None else 0
|
||||
except Exception as e:
|
||||
logger.warning("getUserSoilByUid 查询失败!", e=e)
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
async def updateUserSoilByUid(cls, uid: str, soil: int) -> bool:
|
||||
"""更新指定用户解锁土地数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
soil (int): 新土地数量
|
||||
|
||||
Returns:
|
||||
bool: 更新成功返回True,否则False
|
||||
"""
|
||||
if not uid or soil < 0:
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE user SET soil = ? WHERE uid = ?", (soil, uid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("updateUserSoilByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getStealTimeByUid(cls, uid: str) -> str:
|
||||
"""根据用户Uid获取偷菜时间字符串
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
str: 偷菜时间字符串,失败返回空字符串
|
||||
"""
|
||||
if not uid:
|
||||
return ""
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT stealTime FROM user WHERE uid = ?", (uid,
|
||||
)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row and row[0] else ""
|
||||
except Exception as e:
|
||||
logger.warning("getStealTimeByUid 查询失败!", e=e)
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def updateStealTimeByUid(cls, uid: str, stealTime: str) -> bool:
|
||||
"""根据用户Uid更新偷菜时间字符串
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
stealTime (str): 新偷菜时间字符串
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
if not uid or not stealTime:
|
||||
logger.warning("updateStealTimeByUid 参数校验失败!")
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE user SET stealTime = ? WHERE uid = ?", (stealTime, uid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("updateStealTimeByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getStealCountByUid(cls, uid: str) -> int:
|
||||
"""根据用户Uid获取剩余偷菜次数
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
|
||||
Returns:
|
||||
int: 剩余偷菜次数,失败返回 -1
|
||||
"""
|
||||
if not uid:
|
||||
return -1
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT stealCount FROM user WHERE uid = ?", (uid,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return int(row[0]) if row and row[0] is not None else 0
|
||||
except Exception as e:
|
||||
logger.warning("getStealCountByUid 查询失败!", e=e)
|
||||
return -1
|
||||
|
||||
@classmethod
|
||||
async def updateStealCountByUid(cls, uid: str, stealCount: int) -> bool:
|
||||
"""根据用户Uid更新剩余偷菜次数
|
||||
|
||||
Args:
|
||||
uid (str): 用户Uid
|
||||
stealCount (int): 新剩余偷菜次数
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
if not uid or stealCount < 0:
|
||||
logger.warning("updateStealCountByUid 参数校验失败!")
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE user SET stealCount = ? WHERE uid = ?", (stealCount, uid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("updateStealCountByUid 事务执行失败!", e=e)
|
||||
return False
|
||||
@@ -0,0 +1,168 @@
|
||||
from typing import Optional
|
||||
|
||||
from database import CSqlManager
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class CUserItemDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
#用户Uid
|
||||
#物品名称
|
||||
#数量
|
||||
userItem = {
|
||||
"uid": "INTEGER NOT NULL",
|
||||
"item": "TEXT NOT NULL",
|
||||
"count": "INTEGER NOT NULL DEFAULT 0",
|
||||
"PRIMARY KEY": "(uid, item)"
|
||||
}
|
||||
|
||||
await cls.ensureTableSchema("userItem", userItem)
|
||||
|
||||
@classmethod
|
||||
async def getUserItemByName(cls, uid: str, item: str) -> Optional[int]:
|
||||
"""根据道具名称查询某一项数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
item (str): 道具名称
|
||||
|
||||
Returns:
|
||||
Optional[int]: 数量(不存在返回None)
|
||||
"""
|
||||
if not uid or not item:
|
||||
return None
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT count FROM userItem WHERE uid = ? AND item = ?",
|
||||
(uid, item)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row else None
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场getUserItemByName查询失败!", e=e)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def getUserItemByUid(cls, uid: str) -> dict:
|
||||
"""根据用户Uid获取全部道具信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
|
||||
Returns:
|
||||
dict: {itemName: count, ...}
|
||||
"""
|
||||
if not uid:
|
||||
return {}
|
||||
try:
|
||||
cursor = await cls.m_pDB.execute(
|
||||
"SELECT item, count FROM userItem WHERE uid = ?",
|
||||
(uid,)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return {row["item"]: row["count"] for row in rows}
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场getUserItemByUid查询失败!", e=e)
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
async def deleteUserItemByName(cls, uid: str, item: str) -> bool:
|
||||
"""根据道具名删除道具
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
item (str): 道具名称
|
||||
|
||||
Returns:
|
||||
bool: 是否删除成功
|
||||
"""
|
||||
if not uid or not item:
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userItem WHERE uid = ? AND item = ?",
|
||||
(uid, item)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场deleteUserItemByName失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def updateUserItemByName(cls, uid: str, item: str, count: int) -> bool:
|
||||
"""根据道具名直接更新道具数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
item (str): 道具名称
|
||||
count (int): 要更新的新数量
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
if not uid or not item:
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
if count <= 0:
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userItem WHERE uid = ? AND item = ?",
|
||||
(uid, item)
|
||||
)
|
||||
else:
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userItem SET count = ? WHERE uid = ? AND item = ?",
|
||||
(count, uid, item)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场updateUserItemByName失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def addUserItemByUid(cls, uid: str, item: str, count: int = 1) -> bool:
|
||||
"""根据用户uid添加道具信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
item (str): 道具名称
|
||||
count (int, optional): 数量.Defaults to 1.
|
||||
|
||||
Returns:
|
||||
bool: 是否添加成功
|
||||
"""
|
||||
if not uid or not item:
|
||||
return False
|
||||
try:
|
||||
async with cls._transaction():
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT count FROM userItem WHERE uid = ? AND item = ?",
|
||||
(uid, item)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
|
||||
if row:
|
||||
newCount = row[0] + count
|
||||
if newCount <= 0:
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userItem WHERE uid = ? AND item = ?",
|
||||
(uid, item)
|
||||
)
|
||||
else:
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userItem SET count = ? WHERE uid = ? AND item = ?",
|
||||
(newCount, uid, item)
|
||||
)
|
||||
else:
|
||||
if count > 0:
|
||||
await cls.m_pDB.execute(
|
||||
"INSERT INTO userItem (uid, item, count) VALUES (?, ?, ?)",
|
||||
(uid, item, count)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场addUserItemByUid失败!", e=e)
|
||||
return False
|
||||
@@ -0,0 +1,147 @@
|
||||
from typing import Dict, Optional
|
||||
|
||||
from database import CSqlManager
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class CUserPlantDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
#用户UiD
|
||||
#作物名称
|
||||
#数量
|
||||
userPlant = {
|
||||
"uid": "INTEGER NOT NULL",
|
||||
"plant": "TEXT NOT NULL",
|
||||
"count": "INTEGER NOT NULL DEFAULT 0",
|
||||
"PRIMARY KEY": "(uid, plant)"
|
||||
}
|
||||
|
||||
await cls.ensureTableSchema("userPlant", userPlant)
|
||||
|
||||
@classmethod
|
||||
async def addUserPlantByUid(cls, uid: str, plant: str, count: int = 1) -> bool:
|
||||
"""根据用户uid添加作物信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
plant (str): 作物名称
|
||||
count (int): 数量
|
||||
|
||||
Returns:
|
||||
bool: 是否添加成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
#检查是否已存在该作物
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT count FROM userPlant WHERE uid = ? AND plant = ?",
|
||||
(uid, plant)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
|
||||
if row:
|
||||
#如果作物已存在,则更新数量
|
||||
new_count = row[0] + count
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userPlant SET count = ? WHERE uid = ? AND plant = ?",
|
||||
(new_count, uid, plant)
|
||||
)
|
||||
else:
|
||||
#如果作物不存在,则插入新记录
|
||||
await cls.m_pDB.execute(
|
||||
"INSERT INTO userPlant (uid, plant, count) VALUES (?, ?, ?)",
|
||||
(uid, plant, count)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场addUserPlantByUid 失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getUserPlantByUid(cls, uid: str) -> Dict[str, int]:
|
||||
"""根据用户uid获取全部作物信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
|
||||
Returns:
|
||||
Dict[str, int]: 作物名称和数量
|
||||
"""
|
||||
cursor = await cls.m_pDB.execute(
|
||||
"SELECT plant, count FROM userPlant WHERE uid=?",
|
||||
(uid,)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return {row["plant"]: row["count"] for row in rows}
|
||||
|
||||
@classmethod
|
||||
async def getUserPlantByName(cls, uid: str, plant: str) -> Optional[int]:
|
||||
"""根据作物名称获取用户的作物数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
plant (str): 作物名称
|
||||
|
||||
Returns:
|
||||
Optional[int]: 作物数量
|
||||
"""
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT count FROM userPlant WHERE uid = ? AND plant = ?",
|
||||
(uid, plant)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row else None
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场getUserPlantByName 查询失败!", e=e)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def updateUserPlantByName(cls, uid: str, plant: str, count: int) -> bool:
|
||||
"""更新 userPlant 表中某个作物的数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
plant (str): 作物名称
|
||||
count (int): 新的作物数量
|
||||
|
||||
Returns:
|
||||
bool: 是否更新成功
|
||||
"""
|
||||
try:
|
||||
if count <= 0:
|
||||
return await cls.deleteUserPlantByName(uid, plant)
|
||||
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userPlant SET count = ? WHERE uid = ? AND plant = ?",
|
||||
(count, uid, plant)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场updateUserPlantByName失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def deleteUserPlantByName(cls, uid: str, plant: str) -> bool:
|
||||
"""从 userPlant 表中删除某个作物记录
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
plant (str): 作物名称
|
||||
|
||||
Returns:
|
||||
bool: 是否删除成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userPlant WHERE uid = ? AND plant = ?",
|
||||
(uid, plant)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场deleteUserPlantByName 失败!", e=e)
|
||||
return False
|
||||
@@ -0,0 +1,157 @@
|
||||
from typing import Optional
|
||||
|
||||
from database import CSqlManager
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class CUserSeedDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
#用户Uid
|
||||
#种子名称
|
||||
#数量
|
||||
userSeed = {
|
||||
"uid": "INTEGER NOT NULL",
|
||||
"seed": "TEXT NOT NULL",
|
||||
"count": "INTEGER NOT NULL DEFAULT 0",
|
||||
"PRIMARY KEY": "(uid, seed)"
|
||||
}
|
||||
|
||||
await cls.ensureTableSchema("userSeed", userSeed)
|
||||
|
||||
@classmethod
|
||||
async def addUserSeedByUid(cls, uid: str, seed: str, count: int = 1) -> bool:
|
||||
"""根据用户uid添加种子信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
seed (str): 种子名称
|
||||
count (int): 数量
|
||||
|
||||
Returns:
|
||||
bool: 是否添加成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
#检查是否已存在该种子
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT count FROM userSeed WHERE uid = ? AND seed = ?",
|
||||
(uid, seed)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
|
||||
if row:
|
||||
#如果种子已存在,则更新数量
|
||||
newCount = row[0] + count
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userSeed SET count = ? WHERE uid = ? AND seed = ?",
|
||||
(newCount, uid, seed)
|
||||
)
|
||||
else:
|
||||
#如果种子不存在,则插入新记录
|
||||
newCount = count
|
||||
await cls.m_pDB.execute(
|
||||
"INSERT INTO userSeed (uid, seed, count) VALUES (?, ?, ?)",
|
||||
(uid, seed, count)
|
||||
)
|
||||
|
||||
#如果种子数量为 0,删除记录
|
||||
if newCount <= 0:
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userSeed WHERE uid = ? AND seed = ?",
|
||||
(uid, seed)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场addUserSeedByUid 失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getUserSeedByName(cls, uid: str, seed: str) -> Optional[int]:
|
||||
"""根据种子名称获取种子数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
seed (str): 种子名称
|
||||
|
||||
Returns:
|
||||
Optional[int]: 种子数量
|
||||
"""
|
||||
|
||||
try:
|
||||
async with cls.m_pDB.execute(
|
||||
"SELECT count FROM userSeed WHERE uid = ? AND seed = ?",
|
||||
(uid, seed)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row else None
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场getUserSeedByName 查询失败!", e=e)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def getUserSeedByUid(cls, uid: str) -> dict:
|
||||
"""根据用户Uid获取仓库全部种子信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
|
||||
Returns:
|
||||
dict: 种子信息
|
||||
"""
|
||||
|
||||
cursor = await cls.m_pDB.execute(
|
||||
"SELECT seed, count FROM userSeed WHERE uid=?",
|
||||
(uid,)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return {row["seed"]: row["count"] for row in rows}
|
||||
|
||||
@classmethod
|
||||
async def updateUserSeedByName(cls, uid: str, seed: str, count: int) -> bool:
|
||||
"""根据种子名称更新种子数量
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
seed (str): 种子名称
|
||||
count (int): 种子数量
|
||||
|
||||
Returns:
|
||||
bool: 是否成功
|
||||
"""
|
||||
try:
|
||||
if count <= 0:
|
||||
return await cls.deleteUserSeedByName(uid, seed)
|
||||
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userSeed SET count = ? WHERE uid = ? AND seed = ?",
|
||||
(count, uid, seed)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场updateUserSeedByName失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def deleteUserSeedByName(cls, uid: str, seed: str) -> bool:
|
||||
"""根据种子名称从种子仓库中删除种子
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
seed (str): 种子名称
|
||||
|
||||
Returns:
|
||||
bool: 是否成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userSeed WHERE uid = ? AND seed = ?",
|
||||
(uid, seed)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"真寻农场deleteUserSeedByName 删除失败!", e=e)
|
||||
return False
|
||||
@@ -0,0 +1,261 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from database import CSqlManager
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from ..config import g_pJsonManager
|
||||
from ..dbService import g_pDBService
|
||||
|
||||
|
||||
class CUserSoilDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
#用户Uid
|
||||
#地块索引从1开始
|
||||
#作物名称
|
||||
#播种时间
|
||||
#成熟时间
|
||||
#土地等级 0=普通地,1=红土地,2=黑土地,3=金土地
|
||||
#枯萎状态 0=未枯萎,1=枯萎
|
||||
#施肥状态 0=未施肥,1=施肥
|
||||
#虫害状态 0=无虫害,1=有虫害
|
||||
#杂草状态 0=无杂草,1=有杂草
|
||||
#缺水状态 0=不缺水,1=缺水
|
||||
userSoil = {
|
||||
"uid": "TEXT NOT NULL",
|
||||
"soilIndex": "INTEGER NOT NULL",
|
||||
"plantName": "TEXT DEFAULT ''",
|
||||
"plantTime": "INTEGER DEFAULT 0",
|
||||
"matureTime": "INTEGER DEFAULT 0",
|
||||
"soilLevel": "INTEGER DEFAULT 0",
|
||||
"wiltStatus": "INTEGER DEFAULT 0",
|
||||
"fertilizerStatus": "INTEGER DEFAULT 0",
|
||||
"bugStatus": "INTEGER DEFAULT 0",
|
||||
"weedStatus": "INTEGER DEFAULT 0",
|
||||
"waterStatus": "INTEGER DEFAULT 0",
|
||||
"PRIMARY KEY": "(uid, soilIndex)"
|
||||
}
|
||||
|
||||
await cls.ensureTableSchema("userSoil", userSoil)
|
||||
|
||||
@classmethod
|
||||
async def getUserFarmByUid(cls, uid: str) -> dict:
|
||||
"""获取指定用户的旧农场数据
|
||||
|
||||
Args:
|
||||
uid (str): 用户ID
|
||||
|
||||
Returns:
|
||||
dict: 包含字段名-值的字典; 若无数据则返回空字典
|
||||
"""
|
||||
cursor = await cls.m_pDB.execute("SELECT * FROM soil WHERE uid = ?", (uid,))
|
||||
row = await cursor.fetchone()
|
||||
if not row:
|
||||
return {}
|
||||
columns = [description[0] for description in cursor.description]
|
||||
return dict(zip(columns, row))
|
||||
|
||||
@classmethod
|
||||
async def migrateOldFarmData(cls):
|
||||
"""迁移旧土地数据到新表 userSoil 并删除旧表
|
||||
|
||||
Raises:
|
||||
aiosqlite.Error: 数据库操作失败时抛出
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
async with cls._transaction():
|
||||
users = await g_pDBService.user.getAllUsers()
|
||||
for uid in users:
|
||||
farmInfo = await cls.getUserFarmByUid(uid)
|
||||
for i in range(1, 31):
|
||||
soilName = f"soil{i}"
|
||||
if soilName not in farmInfo:
|
||||
continue
|
||||
soilData = farmInfo[soilName]
|
||||
if not soilData:
|
||||
continue
|
||||
|
||||
fields = soilData.split(',')
|
||||
if len(fields) < 6:
|
||||
continue
|
||||
|
||||
plantName = fields[0]
|
||||
plantTime = int(fields[1])
|
||||
matureTime = int(fields[2])
|
||||
soilLevel = int(fields[5])
|
||||
|
||||
# 构建新的土地数据
|
||||
soilInfo = {
|
||||
"uid": uid,
|
||||
"soilIndex": i,
|
||||
"plantName": plantName,
|
||||
"plantTime": plantTime,
|
||||
"matureTime": matureTime,
|
||||
"soilLevel": soilLevel,
|
||||
"wiltStatus":0,
|
||||
"fertilizerStatus": 0,
|
||||
"bugStatus": 0,
|
||||
"weedStatus": 0,
|
||||
"waterStatus": 0
|
||||
}
|
||||
|
||||
await cls.insertUserSoil(soilInfo)
|
||||
|
||||
# 彻底清除旧表
|
||||
await cls.m_pDB.execute("DROP TABLE soil")
|
||||
|
||||
@classmethod
|
||||
async def insertUserSoil(cls, soilInfo: dict):
|
||||
"""插入一条新的 userSoil 记录
|
||||
|
||||
Args:
|
||||
soilInfo (dict): 新土地数据
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"INSERT INTO userSoil (uid, soilIndex, plantName, plantTime, matureTime, soilLevel, fertilizerStatus, bugStatus, weedStatus, waterStatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
soilInfo["uid"],
|
||||
soilInfo["soilIndex"],
|
||||
soilInfo.get("plantName", ""),
|
||||
soilInfo.get("plantTime", 0),
|
||||
soilInfo.get("matureTime", 0),
|
||||
soilInfo.get("soilLevel", 0),
|
||||
soilInfo.get("fertilizerStatus", 0),
|
||||
soilInfo.get("bugStatus", 0),
|
||||
soilInfo.get("weedStatus", 0),
|
||||
soilInfo.get("waterStatus", 0)
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def getUserSoil(cls, uid: str, soilIndex: int) -> Optional[dict]:
|
||||
"""获取指定用户某块土地的详细信息
|
||||
|
||||
Args:
|
||||
uid (str): 用户ID
|
||||
soilIndex (int): 土地索引
|
||||
|
||||
Returns:
|
||||
Optional[dict]: 记录存在返回字段-值字典,否则返回 None
|
||||
"""
|
||||
async with cls._transaction():
|
||||
cursor = await cls.m_pDB.execute(
|
||||
"SELECT * FROM userSoil WHERE uid = ? AND soilIndex = ?",
|
||||
(uid, soilIndex)
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
columns = [description[0] for description in cursor.description]
|
||||
return dict(zip(columns, row))
|
||||
|
||||
@classmethod
|
||||
async def updateUserSoil(cls, uid: str, soilIndex: int, field: str, value):
|
||||
"""更新指定用户土地的单个字段
|
||||
|
||||
Args:
|
||||
uid (str): 用户ID
|
||||
soilIndex (int): 土地索引
|
||||
field (str): 需更新的字段名
|
||||
value: 新值
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
f"UPDATE userSoil SET {field} = ? WHERE uid = ? AND soilIndex = ?",
|
||||
(value, uid, soilIndex)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def deleteUserSoil(cls, uid: str, soilIndex: int):
|
||||
"""删除指定用户的土地记录
|
||||
|
||||
Args:
|
||||
uid (str): 用户ID
|
||||
soilIndex (int): 土地索引
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userSoil WHERE uid = ? AND soilIndex = ?",
|
||||
(uid, soilIndex)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def isSoilPlanted(cls, uid: str, soilIndex: int) -> bool:
|
||||
"""判断指定用户的指定土地是否已种植
|
||||
|
||||
Args:
|
||||
uid (str): 用户ID
|
||||
soilIndex (int): 土地索引
|
||||
|
||||
Returns:
|
||||
bool: 如果 plantName 不为空且 plantTime 大于 0,则视为已种植,返回 True;否则 False
|
||||
"""
|
||||
soilInfo = await cls.getUserSoil(uid, soilIndex)
|
||||
if not soilInfo:
|
||||
return False
|
||||
|
||||
return bool(soilInfo.get("plantName")) and soilInfo.get("plantTime", 0) > 0
|
||||
|
||||
@classmethod
|
||||
async def sowingByPlantName(cls, uid: str, soilIndex: int, plantName: str) -> bool:
|
||||
"""播种指定作物到用户土地区
|
||||
|
||||
Args:
|
||||
uid (str): 用户ID
|
||||
soilIndex (int): 土地区索引
|
||||
plantName (str): 植物名
|
||||
|
||||
Returns:
|
||||
bool: 播种成功返回 True,否则返回 False
|
||||
"""
|
||||
# 校验土地区是否已种植
|
||||
soilRecord = await cls.getUserSoil(uid, soilIndex)
|
||||
if soilRecord and soilRecord.get("plantName"):
|
||||
return False
|
||||
|
||||
# 获取植物配置
|
||||
plantCfg = g_pJsonManager.m_pPlant.get("plant", {}).get(plantName)
|
||||
if not plantCfg:
|
||||
logger.error(f"未知植物: {plantName}")
|
||||
return False
|
||||
|
||||
nowTs = int(datetime.now().timestamp())
|
||||
matureTs = nowTs + int(plantCfg.get("time", 0)) * 3600
|
||||
|
||||
try:
|
||||
async with cls._transaction():
|
||||
# 复用原有记录字段,保留土壤状态等信息
|
||||
prev = soilRecord or {}
|
||||
await cls.deleteUserSoil(uid, soilIndex)
|
||||
await cls.insertUserSoil({
|
||||
"uid": uid,
|
||||
"soilIndex": soilIndex,
|
||||
"plantName": plantName,
|
||||
"plantTime": nowTs,
|
||||
"matureTime": matureTs,
|
||||
# 保留之前的土壤等级和状态字段,避免数据丢失
|
||||
"soilLevel": prev.get("soilLevel", 0),
|
||||
"wiltStatus": prev.get("wiltStatus", 0),
|
||||
"fertilizerStatus": prev.get("fertilizerStatus", 0),
|
||||
"bugStatus": prev.get("bugStatus", 0),
|
||||
"weedStatus": prev.get("weedStatus", 0),
|
||||
"waterStatus": prev.get("waterStatus", 0)
|
||||
})
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"真寻农场播种失败!", e=e)
|
||||
return False
|
||||
@@ -0,0 +1,228 @@
|
||||
from database import CSqlManager
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
|
||||
class CUserStealDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
# 被偷用户Uid
|
||||
# 被偷的地块索引 从1开始
|
||||
# 偷菜用户Uid
|
||||
# 被偷数量
|
||||
# 被偷时间
|
||||
userSteal = {
|
||||
"uid": "TEXT NOT NULL",
|
||||
"soilIndex": "INTEGER NOT NULL",
|
||||
"stealerUid": "TEXT NOT NULL",
|
||||
"stealCount": "INTEGER NOT NULL",
|
||||
"stealTime": "INTEGER NOT NULL",
|
||||
"PRIMARY KEY": "(uid, soilIndex, stealerUid)"
|
||||
}
|
||||
await cls.ensureTableSchema("userSteal", userSteal)
|
||||
|
||||
@classmethod
|
||||
async def addStealRecord(cls, uid: str, soilIndex: int, stealerUid: str, stealCount: int, stealTime: int) -> bool:
|
||||
"""添加偷菜记录
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
stealerUid (str): 偷菜用户Uid
|
||||
stealCount (int): 被偷数量
|
||||
stealTime (int): 被偷时间(时间戳)
|
||||
|
||||
Returns:
|
||||
bool: 操作是否成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
'INSERT INTO "userSteal"(uid, soilIndex, stealerUid, stealCount, stealTime) VALUES(?, ?, ?, ?, ?);',
|
||||
(uid, soilIndex, stealerUid, stealCount, stealTime)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("添加偷菜记录失败", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getStealRecordsByUid(cls, uid: str) -> list:
|
||||
"""根据用户Uid获取所有偷菜记录
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
|
||||
Returns:
|
||||
list: 偷菜记录字典列表,每条包含 soilIndex, stealerUid, stealCount, stealTime
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
cursor = await cls.m_pDB.execute(
|
||||
'SELECT soilIndex, stealerUid, stealCount, stealTime FROM "userSteal" WHERE uid=?;',
|
||||
(uid,)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return [
|
||||
{
|
||||
"uid": uid,
|
||||
"soilIndex": row[0],
|
||||
"stealerUid": row[1],
|
||||
"stealCount": row[2],
|
||||
"stealTime": row[3]
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
except Exception as e:
|
||||
logger.warning("获取偷菜记录失败", e=e)
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
async def getStealRecord(cls, uid: str, soilIndex: int) -> list:
|
||||
"""获取指定地块的所有偷菜记录
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
|
||||
Returns:
|
||||
list: 偷菜记录字典列表,每条包含 stealerUid, stealCount, stealTime
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
cursor = await cls.m_pDB.execute(
|
||||
'SELECT stealerUid, stealCount, stealTime FROM "userSteal" WHERE uid=? AND soilIndex=?;',
|
||||
(uid, soilIndex)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return [
|
||||
{
|
||||
"uid": uid,
|
||||
"soilIndex": soilIndex,
|
||||
"stealerUid": row[0],
|
||||
"stealCount": row[1],
|
||||
"stealTime": row[2]
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
except Exception as e:
|
||||
logger.warning("获取单地块偷菜记录失败", e=e)
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
async def getTotalStolenCount(cls, uid: str, soilIndex: int) -> int:
|
||||
"""计算指定地块被偷的总数量(所有用户偷取数量之和)
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
|
||||
Returns:
|
||||
int: 被偷的总数量,如果无记录则返回 0
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
cursor = await cls.m_pDB.execute(
|
||||
'SELECT SUM(stealCount) FROM "userSteal" WHERE uid=? AND soilIndex=?;',
|
||||
(uid, soilIndex)
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row[0] or 0 # type: ignore
|
||||
except Exception as e:
|
||||
logger.warning("计算总偷菜数量失败", e=e)
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
async def getStealerCount(cls, uid: str, soilIndex: int) -> int:
|
||||
"""计算指定地块被多少人偷过(不同偷菜用户数量)
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
|
||||
Returns:
|
||||
int: 偷菜者总数,如果无记录则返回 0
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
cursor = await cls.m_pDB.execute(
|
||||
'SELECT COUNT(DISTINCT stealerUid) FROM "userSteal" WHERE uid=? AND soilIndex=?;',
|
||||
(uid, soilIndex)
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row[0] or 0 # type: ignore
|
||||
except Exception as e:
|
||||
logger.warning("计算偷菜者数量失败", e=e)
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
async def hasStealed(cls, uid: str, soilIndex: int, stealerUid: str) -> bool:
|
||||
"""判断指定用户是否曾偷取过该地块
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
stealerUid (str): 偷菜用户Uid
|
||||
|
||||
Returns:
|
||||
bool: 若存在记录返回 True,否则返回 False
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
cursor = await cls.m_pDB.execute(
|
||||
'SELECT 1 FROM "userSteal" WHERE uid=? AND soilIndex=? AND stealerUid=? LIMIT 1;',
|
||||
(uid, soilIndex, stealerUid)
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return bool(row)
|
||||
except Exception as e:
|
||||
logger.warning("检查偷菜记录失败", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def updateStealRecord(cls, uid: str, soilIndex: int, stealerUid: str, stealCount: int, stealTime: int) -> bool:
|
||||
"""更新偷菜记录的数量和时间
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
stealerUid (str): 偷菜用户Uid
|
||||
stealCount (int): 新的偷菜数量
|
||||
stealTime (int): 新的偷菜时间(时间戳)
|
||||
|
||||
Returns:
|
||||
bool: 操作是否成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
'UPDATE "userSteal" SET stealCount=?, stealTime=? WHERE uid=? AND soilIndex=? AND stealerUid=?;',
|
||||
(stealCount, stealTime, uid, soilIndex, stealerUid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("更新偷菜记录失败", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def deleteStealRecord(cls, uid: str, soilIndex: int, stealerUid: str) -> bool:
|
||||
"""删除指定偷菜记录
|
||||
|
||||
Args:
|
||||
uid (str): 被偷用户Uid
|
||||
soilIndex (int): 被偷地块索引
|
||||
stealerUid (str): 偷菜用户Uid
|
||||
|
||||
Returns:
|
||||
bool: 删除是否成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
'DELETE FROM "userSteal" WHERE uid=? AND soilIndex=? AND stealerUid=?;',
|
||||
(uid, soilIndex, stealerUid)
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("删除偷菜记录失败", e=e)
|
||||
return False
|
||||
Reference in New Issue
Block a user