mirror of
https://github.com/Shu-Ying/main_plugin_farm.git
synced 2026-10-30 22:30:01 +08:00
Merge branch 'Beta' of https://github.com/Shu-Ying/zhenxun_plugin_farm into Beta
This commit is contained in:
@@ -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
|
||||
@@ -1,4 +1,5 @@
|
||||
import math
|
||||
import re
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
@@ -384,7 +385,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 +405,31 @@ class CUserSoilDB(CSqlManager):
|
||||
async with cls._transaction():
|
||||
prev = soilInfo or {}
|
||||
await cls._deleteUserSoil(uid, soilIndex)
|
||||
# 根据此植物收获次数对其星级进行升级
|
||||
# 为避免对已带星级前缀的作物重复添加前缀,先提取基础名称
|
||||
base_name = plantName
|
||||
m = re.match(r"^(?:一星|二星|三星|四星|五星)", plantName)
|
||||
if m:
|
||||
base_name = plantName[len(m.group(0)) :]
|
||||
|
||||
# 只有当存在一星升级配置时才尝试按收获次数升级
|
||||
if await g_pDBService.plant.existsPlant("一星" + base_name):
|
||||
harvestCounts = (
|
||||
await g_pDBService.userPlantCount.getUserPlantCountByPlantName(
|
||||
uid, base_name
|
||||
)
|
||||
)
|
||||
# 根据阈值设定最高星级(避免重复拼接)
|
||||
prefix = ""
|
||||
if harvestCounts >= 1200:
|
||||
prefix = "三星"
|
||||
elif harvestCounts >= 700:
|
||||
prefix = "二星"
|
||||
elif harvestCounts >= 350:
|
||||
prefix = "一星"
|
||||
|
||||
plantName = prefix + base_name if prefix else base_name
|
||||
|
||||
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()
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import math
|
||||
import random
|
||||
import re
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.models.user_console import UserConsole
|
||||
@@ -630,6 +631,21 @@ class CFarmManager:
|
||||
uid, soilInfo["plantName"], number
|
||||
)
|
||||
|
||||
# 统计收获次数
|
||||
try:
|
||||
# 处理星级前缀(只在前两字为 一星/二星/三星/四星/五星 时去掉前缀)
|
||||
plant_name = soilInfo["plantName"]
|
||||
m = re.match(r"^(?:一星|二星|三星|四星|五星)", plant_name)
|
||||
if m:
|
||||
plant_name = plant_name[len(m.group(0)) :]
|
||||
|
||||
await g_pDBService.userPlantCount.addUserPlantCountByUid(
|
||||
uid, plant_name
|
||||
)
|
||||
except Exception:
|
||||
# 忽略统计异常,避免影响主流程
|
||||
pass
|
||||
|
||||
# 如果到达收获次数上限
|
||||
if soilInfo["harvestCount"] + 1 >= plantInfo["crop"]:
|
||||
await g_pDBService.userSoil.updateUserSoil(
|
||||
|
||||
Reference in New Issue
Block a user