mirror of
https://github.com/Shu-Ying/main_plugin_farm.git
synced 2026-10-28 13:10:02 +08:00
@@ -0,0 +1,142 @@
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from .database import CSqlManager
|
||||
|
||||
|
||||
class CUserPlantCountDB(CSqlManager):
|
||||
@classmethod
|
||||
async def initDB(cls):
|
||||
userPlantCount = {
|
||||
"uid": "TEXT NOT NULL", # 用户Uid
|
||||
"plant": "TEXT NOT NULL", # 植物名称
|
||||
"count": "INTEGER NOT NULL DEFAULT 0", # 数量
|
||||
"PRIMARY KEY": "(uid, plant)",
|
||||
}
|
||||
|
||||
await cls.ensureTableSchema("userPlantCount", userPlantCount)
|
||||
|
||||
@classmethod
|
||||
async def addUserPlantCountByUid(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 userPlantCount WHERE uid = ? AND plant = ?",
|
||||
(uid, plant),
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
|
||||
if row:
|
||||
newCount = row[0] + count
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userPlantCount SET count = ? WHERE uid = ? AND plant = ?",
|
||||
(newCount, uid, plant),
|
||||
)
|
||||
else:
|
||||
newCount = count
|
||||
await cls.m_pDB.execute(
|
||||
"INSERT INTO userPlantCount (uid, plant, count) VALUES (?, ?, ?)",
|
||||
(uid, plant, count),
|
||||
)
|
||||
|
||||
if newCount <= 0:
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userPlantCount WHERE uid = ? AND plant = ?",
|
||||
(uid, plant),
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("addUserPlantByUid 失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def getUserPlantCountByUid(cls, uid: str) -> dict:
|
||||
"""根据用户Uid获取该用户的所有植物的收获次数
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
|
||||
Returns:
|
||||
dict: 植物信息
|
||||
"""
|
||||
|
||||
cursor = await cls.m_pDB.execute(
|
||||
"SELECT plant, count FROM userPlantCount WHERE uid=?", (uid,)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return {row["plant"]: row["count"] for row in rows}
|
||||
|
||||
@classmethod
|
||||
async def getUserPlantCountByPlantName(cls, uid: str, plant: str) -> int:
|
||||
"""根据用户Uid和植物名称获取该对应的收获次数
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
plant (str): 植物名称
|
||||
|
||||
Returns:
|
||||
int: 收获次数
|
||||
"""
|
||||
cursor = await cls.m_pDB.execute(
|
||||
"SELECT count FROM userPlantCount WHERE uid = ? AND plant = ?",
|
||||
(uid, plant),
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row["count"] if row else 0
|
||||
|
||||
@classmethod
|
||||
async def updateUserPlantCountByName(cls, uid: str, plant: str, count: int) -> bool:
|
||||
"""根据植物名称更新植物的收获次数
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
plant (str): 植物名称
|
||||
count (int): 收获次数
|
||||
|
||||
Returns:
|
||||
bool: 是否成功
|
||||
"""
|
||||
try:
|
||||
if count <= 0:
|
||||
return await cls.deleteUserPlantCountByName(uid, plant)
|
||||
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"UPDATE userPlantCount SET count = ? WHERE uid = ? AND plant = ?",
|
||||
(count, uid, plant),
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("updateUserPlantByName失败!", e=e)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def deleteUserPlantCountByName(cls, uid: str, plant: str) -> bool:
|
||||
"""根据植物名称从植物仓库中删除植物
|
||||
|
||||
Args:
|
||||
uid (str): 用户uid
|
||||
seed (str): 种子名称
|
||||
|
||||
Returns:
|
||||
bool: 是否成功
|
||||
"""
|
||||
try:
|
||||
async with cls._transaction():
|
||||
await cls.m_pDB.execute(
|
||||
"DELETE FROM userPlantCount WHERE uid = ? AND plant = ?",
|
||||
(uid, plant),
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning("deleteUserPlantCountByName 删除失败!", e=e)
|
||||
return False
|
||||
@@ -384,7 +384,7 @@ class CUserSoilDB(CSqlManager):
|
||||
if soilInfo and soilInfo.get("plantName"):
|
||||
return False
|
||||
|
||||
# 获取植物配置
|
||||
# 获取植物配置""" """
|
||||
plantCfg = await g_pDBService.plant.getPlantByName(plantName)
|
||||
if not plantCfg:
|
||||
logger.error(f"未知植物: {plantName}")
|
||||
@@ -404,6 +404,20 @@ class CUserSoilDB(CSqlManager):
|
||||
async with cls._transaction():
|
||||
prev = soilInfo or {}
|
||||
await cls._deleteUserSoil(uid, soilIndex)
|
||||
# 根据此植物收获次数对其星级进行升级
|
||||
if g_pDBService.plant.existsPlant("一星" + plantName):
|
||||
harvestCounts = (
|
||||
await g_pDBService.userPlantCount.getUserPlantCountByPlantName(
|
||||
uid, plantName
|
||||
)
|
||||
)
|
||||
if harvestCounts >= 350:
|
||||
plantName = "一星" + plantName
|
||||
if harvestCounts >= 700:
|
||||
plantName = "二星" + plantName
|
||||
if harvestCounts >= 1200:
|
||||
plantName = "三星" + plantName
|
||||
|
||||
await cls._insertUserSoil(
|
||||
{
|
||||
"uid": uid,
|
||||
|
||||
@@ -4,6 +4,7 @@ class CDBService:
|
||||
from .database.user import CUserDB
|
||||
from .database.userItem import CUserItemDB
|
||||
from .database.userPlant import CUserPlantDB
|
||||
from .database.userPlantCount import CUserPlantCountDB
|
||||
from .database.userSeed import CUserSeedDB
|
||||
from .database.userSign import CUserSignDB
|
||||
from .database.userSoil import CUserSoilDB
|
||||
@@ -33,6 +34,9 @@ class CDBService:
|
||||
self.userSign = CUserSignDB()
|
||||
await self.userSign.initDB()
|
||||
|
||||
self.userPlantCount = CUserPlantCountDB()
|
||||
await self.userPlantCount.initDB()
|
||||
|
||||
async def cleanup(self):
|
||||
await self.plant.cleanup()
|
||||
|
||||
|
||||
@@ -629,6 +629,22 @@ class CFarmManager:
|
||||
uid, soilInfo["plantName"], number
|
||||
)
|
||||
|
||||
# 统计收获次数
|
||||
try:
|
||||
# 统一处理星级植物名称
|
||||
plant_name = soilInfo["plantName"]
|
||||
|
||||
# 简单判断是否包含"星",如果包含则分割取后半部分
|
||||
if "星" in plant_name:
|
||||
plant_name = plant_name.split("星", 1)[-1]
|
||||
|
||||
await g_pDBService.userPlantCount.addUserPlantCountByUid(
|
||||
uid, plant_name
|
||||
)
|
||||
except Exception:
|
||||
# 忽略统计异常,避免影响主流程
|
||||
pass
|
||||
|
||||
# 如果到达收获次数上限
|
||||
if soilInfo["harvestCount"] + 1 >= plantInfo["crop"]:
|
||||
await g_pDBService.userSoil.updateUserSoil(
|
||||
|
||||
Binary file not shown.
@@ -41,6 +41,7 @@ g_sTranslation = {
|
||||
"success": "✅ 农场开通成功!\n💼 初始资金:{point}农场币 🥳🎉",
|
||||
"repeat": "🎉 您已经开通农场啦~ 😄",
|
||||
"error": "⚠️ 开通失败,请稍后再试 💔",
|
||||
"already": "🚜 你已经开通农场啦~ 快去种田吧 🌱",
|
||||
},
|
||||
"buySeed": {
|
||||
"notSeed": "🌱 请在指令后跟需要购买的种子名称",
|
||||
|
||||
Reference in New Issue
Block a user