2025-04-09 17:14:37 +08:00
|
|
|
import math
|
|
|
|
|
|
2025-03-19 01:16:08 +08:00
|
|
|
from zhenxun.services.log import logger
|
|
|
|
|
from zhenxun.utils._build_image import BuildImage
|
2025-03-19 18:00:50 +08:00
|
|
|
from zhenxun.utils.image_utils import ImageTemplate
|
2025-03-19 01:16:08 +08:00
|
|
|
|
2025-05-07 17:04:08 +08:00
|
|
|
from ..config import g_sResourcePath
|
2025-04-28 19:27:16 +08:00
|
|
|
from ..dbService import g_pDBService
|
2025-05-07 17:04:08 +08:00
|
|
|
from ..json import g_pJsonManager
|
2025-03-17 18:07:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CShopManager:
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-04-09 17:14:37 +08:00
|
|
|
async def getSeedShopImage(cls, num: int = 1) -> bytes:
|
2025-03-20 18:06:38 +08:00
|
|
|
"""获取商店页面
|
|
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
Returns:
|
2025-03-20 18:06:38 +08:00
|
|
|
bytes: 返回商店图片bytes
|
2025-03-19 18:00:50 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
data_list = []
|
|
|
|
|
column_name = [
|
|
|
|
|
"-",
|
|
|
|
|
"种子名称",
|
2025-04-27 17:00:58 +08:00
|
|
|
"种子单价",
|
2025-03-19 18:00:50 +08:00
|
|
|
"解锁等级",
|
2025-04-27 10:52:28 +08:00
|
|
|
"果实单价",
|
2025-03-19 18:00:50 +08:00
|
|
|
"收获经验",
|
|
|
|
|
"收获数量",
|
2025-03-21 00:25:20 +08:00
|
|
|
"成熟时间(小时)",
|
2025-03-19 18:00:50 +08:00
|
|
|
"收获次数",
|
2025-03-21 00:25:20 +08:00
|
|
|
"再次成熟时间(小时)",
|
2025-03-19 18:00:50 +08:00
|
|
|
"是否可以上架交易行"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
sell = ""
|
2025-04-09 17:14:37 +08:00
|
|
|
plants = list(g_pJsonManager.m_pPlant['plant'].items())
|
|
|
|
|
start = (num - 1) * 15
|
|
|
|
|
maxItems = min(len(plants) - start, 15)
|
|
|
|
|
items = plants[start:start + maxItems]
|
|
|
|
|
|
|
|
|
|
for key, plant in items:
|
2025-03-19 18:00:50 +08:00
|
|
|
icon = ""
|
|
|
|
|
icon_path = g_sResourcePath / f"plant/{key}/icon.png"
|
|
|
|
|
if icon_path.exists():
|
|
|
|
|
icon = (icon_path, 33, 33)
|
|
|
|
|
|
|
|
|
|
if plant['again'] == True:
|
|
|
|
|
sell = "可以"
|
|
|
|
|
else:
|
|
|
|
|
sell = "不可以"
|
|
|
|
|
|
|
|
|
|
data_list.append(
|
|
|
|
|
[
|
|
|
|
|
icon,
|
|
|
|
|
key,
|
2025-04-27 10:52:28 +08:00
|
|
|
plant['buy'],
|
2025-03-19 18:00:50 +08:00
|
|
|
plant['level'],
|
|
|
|
|
plant['price'],
|
|
|
|
|
plant['experience'],
|
|
|
|
|
plant['harvest'],
|
|
|
|
|
plant['time'],
|
|
|
|
|
plant['crop'],
|
|
|
|
|
plant['again'],
|
|
|
|
|
sell
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-09 17:14:37 +08:00
|
|
|
count = math.ceil(len(g_pJsonManager.m_pPlant['plant']) / 15)
|
|
|
|
|
title = f"种子商店 页数: {num}/{count}"
|
|
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
result = await ImageTemplate.table_page(
|
2025-04-09 17:14:37 +08:00
|
|
|
title,
|
2025-03-19 18:00:50 +08:00
|
|
|
"购买示例:@小真寻 购买种子 大白菜 5",
|
|
|
|
|
column_name,
|
|
|
|
|
data_list,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return result.pic2bytes()
|
2025-03-19 01:16:08 +08:00
|
|
|
|
|
|
|
|
@classmethod
|
2025-03-20 18:06:38 +08:00
|
|
|
async def buySeed(cls, uid: str, name: str, num: int = 1) -> str:
|
|
|
|
|
"""购买种子
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
name (str): 植物名称
|
|
|
|
|
num (int, optional): 购买数量
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str:
|
|
|
|
|
"""
|
|
|
|
|
|
2025-03-19 01:16:08 +08:00
|
|
|
if num <= 0:
|
|
|
|
|
return "请输入购买数量!"
|
|
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
plantInfo = None
|
2025-03-19 01:16:08 +08:00
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
try:
|
2025-04-09 17:14:37 +08:00
|
|
|
plantInfo = g_pJsonManager.m_pPlant['plant'][name]
|
2025-03-19 18:00:50 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
return "购买出错!请检查需购买的种子名称!"
|
|
|
|
|
|
2025-04-28 19:27:16 +08:00
|
|
|
level = await g_pDBService.user.getUserLevelByUid(uid)
|
2025-03-21 18:14:50 +08:00
|
|
|
|
2025-04-07 18:52:27 +08:00
|
|
|
if level[0] < int(plantInfo['level']):
|
2025-03-21 18:14:50 +08:00
|
|
|
return "你的等级不够哦,努努力吧"
|
|
|
|
|
|
2025-04-28 19:27:16 +08:00
|
|
|
point = await g_pDBService.user.getUserPointByUid(uid)
|
2025-04-27 10:52:28 +08:00
|
|
|
total = int(plantInfo['buy']) * num
|
2025-03-19 18:00:50 +08:00
|
|
|
|
|
|
|
|
logger.debug(f"用户:{uid}购买{name},数量为{num}。用户农场币为{point},购买需要{total}")
|
|
|
|
|
|
|
|
|
|
if point < total:
|
|
|
|
|
return "你的农场币不够哦~ 快速速氪金吧!"
|
|
|
|
|
else:
|
2025-04-28 19:27:16 +08:00
|
|
|
await g_pDBService.user.updateUserPointByUid(uid, point - total)
|
2025-03-19 18:00:50 +08:00
|
|
|
|
2025-04-28 19:27:16 +08:00
|
|
|
if await g_pDBService.userSeed.addUserSeedByUid(uid, name, num) == False:
|
2025-04-07 18:52:27 +08:00
|
|
|
return "购买失败,执行数据库错误!"
|
|
|
|
|
|
|
|
|
|
return f"成功购买{name},花费{total}农场币, 剩余{point - total}农场币"
|
2025-03-19 01:16:08 +08:00
|
|
|
|
2025-03-21 18:14:50 +08:00
|
|
|
@classmethod
|
|
|
|
|
async def sellPlantByUid(cls, uid: str, name: str = "", num: int = 1) -> str:
|
|
|
|
|
"""出售作物
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str:
|
|
|
|
|
"""
|
2025-04-27 17:00:58 +08:00
|
|
|
if not isinstance(name, str) or name.strip() == "":
|
|
|
|
|
name = ""
|
|
|
|
|
|
2025-04-28 19:27:16 +08:00
|
|
|
plant = await g_pDBService.userPlant.getUserPlantByUid(uid)
|
2025-04-27 17:00:58 +08:00
|
|
|
if not plant:
|
2025-03-21 18:14:50 +08:00
|
|
|
return "你仓库没有可以出售的作物"
|
|
|
|
|
|
2025-04-27 17:00:58 +08:00
|
|
|
point = 0
|
|
|
|
|
totalSold = 0
|
|
|
|
|
isAll = (num == -1)
|
2025-03-21 18:14:50 +08:00
|
|
|
|
2025-04-27 17:00:58 +08:00
|
|
|
if name == "":
|
|
|
|
|
for plantName, count in plant.items():
|
2025-04-27 10:52:28 +08:00
|
|
|
plantInfo = g_pJsonManager.m_pPlant['plant'][plantName]
|
2025-04-27 17:00:58 +08:00
|
|
|
point += plantInfo['price'] * count
|
2025-04-28 19:27:16 +08:00
|
|
|
await g_pDBService.userPlant.updateUserPlantByName(uid, plantName, 0)
|
2025-04-27 10:52:28 +08:00
|
|
|
else:
|
2025-04-27 17:00:58 +08:00
|
|
|
if name not in plant:
|
|
|
|
|
return f"出售作物{name}出错:仓库中不存在该作物"
|
|
|
|
|
available = plant[name]
|
|
|
|
|
sellAmount = available if isAll else min(available, num)
|
|
|
|
|
if sellAmount <= 0:
|
|
|
|
|
return f"出售作物{name}出错:数量不足"
|
2025-04-28 19:27:16 +08:00
|
|
|
await g_pDBService.userPlant.updateUserPlantByName(uid, name, available - sellAmount)
|
2025-04-27 17:00:58 +08:00
|
|
|
totalSold = sellAmount
|
|
|
|
|
|
|
|
|
|
totalPoint = point if name == "" else totalSold * g_pJsonManager.m_pPlant['plant'][name]['price']
|
2025-04-28 19:27:16 +08:00
|
|
|
currentPoint = await g_pDBService.user.getUserPointByUid(uid)
|
|
|
|
|
await g_pDBService.user.updateUserPointByUid(uid, currentPoint + totalPoint)
|
2025-04-27 17:00:58 +08:00
|
|
|
|
|
|
|
|
if name == "":
|
|
|
|
|
return f"成功出售所有作物,获得农场币:{totalPoint},当前农场币:{currentPoint + totalPoint}"
|
2025-03-21 18:14:50 +08:00
|
|
|
else:
|
2025-04-27 17:00:58 +08:00
|
|
|
return f"成功出售{name},获得农场币:{totalPoint},当前农场币:{currentPoint + totalPoint}"
|
2025-03-19 01:16:08 +08:00
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
g_pShopManager = CShopManager()
|