✨ 更新数据库逻辑

This commit is contained in:
2025-04-27 10:52:28 +08:00
parent 4cd0302426
commit 719494e4e5
19 changed files with 407 additions and 199 deletions
+29 -35
View File
@@ -213,9 +213,12 @@ class CFarmManager:
currentStage = int(elapsedHour / (plantInfo['time'] / (plantInfo['phase'] - 1)))
#TODO 缺少判断部分种子是否是通用0阶段图片
if currentStage <= 0:
plant = BuildImage(background = g_sResourcePath / f"plant/basic/0.png")
if plantInfo['general'] == False:
plant = BuildImage(background = g_sResourcePath / f"plant/{soilInfo[0]}/0.png")
else:
plant = BuildImage(background = g_sResourcePath / f"plant/basic/0.png")
await plant.resize(0, 35, 58)
else:
plant = BuildImage(background = g_sResourcePath / f"plant/{soilInfo[0]}/{currentStage}.png")
@@ -395,7 +398,7 @@ class CFarmManager:
harvestRecords.append(f"收获作物:{plantId},数量为:{number},经验为:{plantInfo['experience']}")
#更新数据库操作
await g_pSqlManager.addUserPlantByPlant(uid, plantId, number)
await g_pSqlManager.addUserPlantByUid(uid, plantId, number)
await g_pSqlManager.updateUserSoilStatusByPlantName(uid, soil_name, "", 4)
if experience > 0:
@@ -455,7 +458,6 @@ class CFarmManager:
Returns:
bytes: 返回图片
"""
data_list = []
column_name = [
"-",
@@ -468,48 +470,40 @@ class CFarmManager:
plant = await g_pSqlManager.getUserPlantByUid(uid)
if plant == None:
if plant is None:
result = await ImageTemplate.table_page(
"作物仓库",
"播种示例:@小真寻 出售作物 大白菜 [数量]",
column_name,
data_list,
)
return result.pic2bytes()
sell = ""
for item in plant.split(','):
if '|' in item:
plantName, count = item.split('|', 1) #分割一次,避免多竖线问题
try:
plantInfo = g_pJsonManager.m_pPlant['plant'][plantName]
for name, count in plant.items(): # 使用 .items() 来遍历字典
plantInfo = g_pJsonManager.m_pPlant['plant'][name]
icon = ""
icon_path = g_sResourcePath / f"plant/{name}/icon.png"
if icon_path.exists():
icon = (icon_path, 33, 33)
icon = ""
icon_path = g_sResourcePath / f"plant/{plantName}/icon.png"
if icon_path.exists():
icon = (icon_path, 33, 33)
if plantInfo['again'] == True:
sell = "可以"
else:
sell = "不可以"
if plantInfo['again'] == True:
sell = "可以"
else:
sell = "不可以"
number = int(count) * plantInfo['price']
number = int(count) * plantInfo['price']
data_list.append(
[
icon,
plantName,
count,
plantInfo['price'],
number,
sell
]
)
except Exception as e:
continue
data_list.append(
[
icon,
name,
count,
plantInfo['price'],
number,
sell
]
)
result = await ImageTemplate.table_page(
"作物仓库",
@@ -611,7 +605,7 @@ class CFarmManager:
logger.info(f"{randomNumber}")
if randomNumber > 0:
await g_pSqlManager.addUserPlantByPlant(uid, plantId, randomNumber)
await g_pSqlManager.addUserPlantByUid(uid, plantId, randomNumber)
harvestRecords.append(f"成功偷到作物:{plantId},数量为:{randomNumber}")
stealingStatus.append(f"{uid}-{randomNumber}")
+30 -69
View File
@@ -1,4 +1,5 @@
import math
from re import I
from zhenxun.services.log import logger
from zhenxun.utils._build_image import BuildImage
@@ -22,8 +23,9 @@ class CShopManager:
column_name = [
"-",
"种子名称",
"种子单价"
"解锁等级",
"种子单价",
"果实单价",
"收获经验",
"收获数量",
"成熟时间(小时)",
@@ -53,6 +55,7 @@ class CShopManager:
[
icon,
key,
plant['buy'],
plant['level'],
plant['price'],
plant['experience'],
@@ -105,7 +108,7 @@ class CShopManager:
return "你的等级不够哦,努努力吧"
point = await g_pSqlManager.getUserPointByUid(uid)
total = int(plantInfo['price']) * num
total = int(plantInfo['buy']) * num
logger.debug(f"用户:{uid}购买{name},数量为{num}。用户农场币为{point},购买需要{total}")
@@ -114,7 +117,7 @@ class CShopManager:
else:
await g_pSqlManager.updateUserPointByUid(uid, point - total)
if await g_pSqlManager.addUserSeedByPlant(uid, name, num) == False:
if await g_pSqlManager.addUserSeedByUid(uid, name, num) == False:
return "购买失败,执行数据库错误!"
return f"成功购买{name},花费{total}农场币, 剩余{point - total}农场币"
@@ -129,81 +132,39 @@ class CShopManager:
Returns:
str:
"""
plant = await g_pSqlManager.getUserPlantByUid(uid)
if plant == None:
plantDict = await g_pSqlManager.getUserPlantByUid(uid)
if not plantDict:
return "你仓库没有可以出售的作物"
point = 0
totalSold = 0
remainingItems = []
totalPoint = 0
isAll = False
if num == -1:
isAll = True
items = plant.split(',')
if len(name) <= 0:
#出售全部
for item in items:
if '|' in item:
plant_name, count_str = item.split('|', 1)
try:
count = int(count_str)
plant_info = g_pJsonManager.m_pPlant['plant'][plant_name]
point += plant_info['price'] * count
except Exception:
continue
await g_pSqlManager.updateUserPlantByUid(uid, "") # 清空仓库
if not name:
for plantName, count in plantDict.items():
plantInfo = g_pJsonManager.m_pPlant['plant'][plantName]
totalPoint += plantInfo['price'] * count
await g_pSqlManager.deleteUserPlantByName(uid, plantName)
else:
for item in items:
if '|' in item:
plantName, countStr = item.split('|', 1)
try:
count = int(countStr)
if plantName == name:
currentCount = plantDict.get(name)
if currentCount is None:
return f"出售作物{name}出错:你没有这种作物"
if isAll:
sellAmount = count
else:
sellAmount = min(num, count)
if num == -1:
sellCount = currentCount
else:
if num > currentCount:
return f"出售作物{name}出错:数量不足"
sellCount = num
totalSold += sellAmount
remaining = count - sellAmount
if remaining > 0:
remainingItems.append(f"{plantName}|{remaining}")
if isAll == False:
num -= sellAmount
break
except (ValueError, TypeError):
continue
if num > 0 and isAll == False:
return f"出售作物{name}出错:数量不足"
#计算收益
try:
plantInfo = g_pJsonManager.m_pPlant['plant'][name]
totalPoint = plantInfo['price'] * totalSold
except KeyError:
return f"出售作物{name}出错:作物不存在"
totalPoint = plantInfo['price'] * sellCount
await g_pSqlManager.addUserPlantByUid(uid, name, -sellCount)
#更新剩余作物
remainingPlant = ','.join(remainingItems) if remainingItems else ""
await g_pSqlManager.updateUserPlantByUid(uid, remainingPlant)
point = await g_pSqlManager.getUserPointByUid(uid)
await g_pSqlManager.updateUserPointByUid(uid, point + totalPoint)
#更新农场币
p = await g_pSqlManager.getUserPointByUid(uid)
await g_pSqlManager.updateUserPointByUid(uid, p + totalPoint)
if name:
return f"成功出售{name},获得农场币:{totalPoint}"
if not name:
return f"成功出售所有作物,获得农场币:{totalPoint},当前农场币:{point + totalPoint}"
else:
return f"成功出售所有作物,获得农场币:{totalPoint}"
return f"成功出售{name},获得农场币:{totalPoint},当前农场币:{point + totalPoint}"
g_pShopManager = CShopManager()