2025-03-20 18:06:38 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
|
import logging
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
from io import StringIO
|
|
|
|
|
|
from typing import Dict, List, Tuple
|
2025-03-20 00:45:05 +08:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
from ..config import g_pJsonManager, g_sResourcePath
|
|
|
|
|
|
from ..database import g_pSqlManager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CFarmManager:
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-03-19 18:00:50 +08:00
|
|
|
|
async def drawFarmByUid(cls, uid: str) -> bytes:
|
|
|
|
|
|
"""绘制用户农场
|
2025-03-19 01:16:08 +08:00
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户UID
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
bytes: 返回绘制结果
|
|
|
|
|
|
"""
|
|
|
|
|
|
soilNumber = await g_pSqlManager.getUserLevelByUid(uid)
|
|
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
img = BuildImage(background = g_sResourcePath / "background/background.jpg")
|
2025-03-19 01:16:08 +08:00
|
|
|
|
|
|
|
|
|
|
soilSize = g_pJsonManager.m_pSoil['size'] # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
#TODO 缺少判断用户土地资源状况
|
2025-03-20 18:06:38 +08:00
|
|
|
|
soil = BuildImage(background = g_sResourcePath / "soil/普通土地.png")
|
2025-03-19 01:16:08 +08:00
|
|
|
|
await soil.resize(0, soilSize[0], soilSize[1])
|
|
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
grass = BuildImage(background = g_sResourcePath / "soil/草土地.png")
|
2025-03-19 01:16:08 +08:00
|
|
|
|
await grass.resize(0, soilSize[0], soilSize[1])
|
|
|
|
|
|
|
|
|
|
|
|
soilPos = g_pJsonManager.m_pSoil['soil'] # type: ignore
|
2025-03-19 18:00:50 +08:00
|
|
|
|
soilUnlock = g_pJsonManager.m_pLevel['soil'] # type: ignore
|
2025-03-19 01:16:08 +08:00
|
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
|
x = 0
|
|
|
|
|
|
y = 0
|
2025-03-20 18:06:38 +08:00
|
|
|
|
isFirstExpansion = True #首次添加扩建图片
|
|
|
|
|
|
isFirstRipe = True
|
|
|
|
|
|
plant = None
|
2025-03-19 18:00:50 +08:00
|
|
|
|
for index, level in enumerate(soilUnlock):
|
|
|
|
|
|
x = soilPos[str(index + 1)]['x']
|
|
|
|
|
|
y = soilPos[str(index + 1)]['y']
|
|
|
|
|
|
|
2025-03-20 00:45:05 +08:00
|
|
|
|
#如果土地已经到达对应等级
|
2025-03-19 18:00:50 +08:00
|
|
|
|
if soilNumber >= int(level):
|
|
|
|
|
|
await img.paste(soil, (x, y))
|
|
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
isPlant, plant, isRipe= await cls.drawSoilPlant(uid, f"soil{str(index + 1)}")
|
|
|
|
|
|
|
|
|
|
|
|
if isPlant:
|
|
|
|
|
|
await img.paste(plant, (x + soilSize[0] // 2 - plant.width // 2,
|
|
|
|
|
|
y + soilSize[1] // 2 - plant.height // 2))
|
|
|
|
|
|
|
|
|
|
|
|
#首次添加可收获图片
|
|
|
|
|
|
if isRipe and isFirstRipe:
|
|
|
|
|
|
ripe = BuildImage(background = g_sResourcePath / "background/ripe.png")
|
|
|
|
|
|
|
|
|
|
|
|
await img.paste(ripe, (x + soilSize[0] // 2 - ripe.width // 2,
|
|
|
|
|
|
y - ripe.height // 2))
|
|
|
|
|
|
|
|
|
|
|
|
isFirstRipe = False
|
2025-03-19 01:16:08 +08:00
|
|
|
|
else:
|
2025-03-19 18:00:50 +08:00
|
|
|
|
await img.paste(grass, (x, y))
|
|
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
if isFirstExpansion:
|
|
|
|
|
|
isFirstExpansion = False
|
2025-03-20 00:45:05 +08:00
|
|
|
|
|
|
|
|
|
|
#首次添加扩建图片
|
2025-03-20 18:06:38 +08:00
|
|
|
|
expansion = BuildImage(background = g_sResourcePath / "background/expansion.png")
|
2025-03-20 00:45:05 +08:00
|
|
|
|
await expansion.resize(0, 69, 69)
|
2025-03-20 18:06:38 +08:00
|
|
|
|
await img.paste(expansion, (x + soilSize[0] // 2 - expansion.width // 2,
|
|
|
|
|
|
y + soilSize[1] // 2 - expansion.height))
|
2025-03-20 00:45:05 +08:00
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
await img.resize(0.6)
|
2025-03-19 18:00:50 +08:00
|
|
|
|
return img.pic2bytes()
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-03-20 18:06:38 +08:00
|
|
|
|
async def drawSoilPlant(cls, uid: str, soilid: str) -> tuple[bool, BuildImage, bool]:
|
|
|
|
|
|
"""绘制植物资源
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
|
soilid (str): 土地id
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
tuple[bool, BuildImage]: [绘制是否成功,资源图片, 是否成熟]
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
plant = None
|
|
|
|
|
|
soilStatus, soilInfo = await g_pSqlManager.getUserSoilStatusBySoilID(uid, soilid)
|
|
|
|
|
|
|
|
|
|
|
|
if soilStatus == True:
|
|
|
|
|
|
return False, None, False
|
|
|
|
|
|
else:
|
|
|
|
|
|
soilInfo = soilInfo.split(',')
|
|
|
|
|
|
plantInfo = g_pJsonManager.m_pPlant['plant'][soilInfo[0]] # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
if int(soilInfo[3]) == 4:
|
|
|
|
|
|
plant = BuildImage(background = g_sResourcePath / f"plant/basic/9.png")
|
|
|
|
|
|
|
|
|
|
|
|
return True, plant, False
|
|
|
|
|
|
|
|
|
|
|
|
currentTime = datetime.now()
|
|
|
|
|
|
matureTime = datetime.fromtimestamp(int(soilInfo[2]))
|
|
|
|
|
|
|
|
|
|
|
|
if currentTime >= matureTime:
|
|
|
|
|
|
phase = int(plantInfo['phase'])
|
|
|
|
|
|
plant = BuildImage(background = g_sResourcePath / f"plant/{soilInfo[0]}/{phase - 1}.png")
|
|
|
|
|
|
|
|
|
|
|
|
return True, plant, True
|
|
|
|
|
|
else:
|
|
|
|
|
|
plantedTime = datetime.fromtimestamp(int(soilInfo[1]))
|
|
|
|
|
|
|
|
|
|
|
|
elapsedTime = currentTime - plantedTime
|
|
|
|
|
|
elapsedMinutes = elapsedTime.total_seconds() / 60
|
|
|
|
|
|
|
|
|
|
|
|
currentStage = int(elapsedMinutes / (plantInfo['time'] / (plantInfo['phase'] - 1)))
|
|
|
|
|
|
|
|
|
|
|
|
#TODO 缺少判断部分种子是否是通用0阶段图片
|
|
|
|
|
|
if currentStage <= 0:
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
return True, plant, False
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def getUserSeedByUid(cls, uid: str) -> bytes:
|
2025-03-20 00:45:05 +08:00
|
|
|
|
"""获取用户种子仓库
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
bytes: 返回图片
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
|
data_list = []
|
|
|
|
|
|
column_name = [
|
|
|
|
|
|
"-",
|
|
|
|
|
|
"种子名称",
|
2025-03-20 00:45:05 +08:00
|
|
|
|
"数量",
|
2025-03-19 18:00:50 +08:00
|
|
|
|
"收获经验",
|
|
|
|
|
|
"收获数量",
|
|
|
|
|
|
"成熟时间(分钟)",
|
|
|
|
|
|
"收获次数",
|
|
|
|
|
|
"再次成熟时间(分钟)",
|
|
|
|
|
|
"是否可以上架交易行"
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
seed = await g_pSqlManager.getUserSeedByUid(uid)
|
2025-03-19 18:00:50 +08:00
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
if seed == None:
|
2025-03-19 18:00:50 +08:00
|
|
|
|
result = await ImageTemplate.table_page(
|
|
|
|
|
|
"种子仓库",
|
2025-03-20 18:06:38 +08:00
|
|
|
|
"播种示例:@小真寻 播种 大白菜 [数量]",
|
2025-03-19 18:00:50 +08:00
|
|
|
|
column_name,
|
|
|
|
|
|
data_list,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return result.pic2bytes()
|
|
|
|
|
|
|
|
|
|
|
|
sell = ""
|
2025-03-20 18:06:38 +08:00
|
|
|
|
for item in seed.split(','):
|
2025-03-19 18:00:50 +08:00
|
|
|
|
if '|' in item:
|
2025-03-20 18:06:38 +08:00
|
|
|
|
seedName, count = item.split('|', 1) # 分割一次,避免多竖线问题
|
2025-03-19 18:00:50 +08:00
|
|
|
|
try:
|
2025-03-20 18:06:38 +08:00
|
|
|
|
plantInfo = g_pJsonManager.m_pPlant['plant'][seedName] # type: ignore
|
2025-03-19 18:00:50 +08:00
|
|
|
|
|
|
|
|
|
|
icon = ""
|
2025-03-20 18:06:38 +08:00
|
|
|
|
icon_path = g_sResourcePath / f"plant/{seedName}/icon.png"
|
2025-03-19 18:00:50 +08:00
|
|
|
|
if icon_path.exists():
|
|
|
|
|
|
icon = (icon_path, 33, 33)
|
|
|
|
|
|
|
|
|
|
|
|
if plantInfo['again'] == True:
|
|
|
|
|
|
sell = "可以"
|
|
|
|
|
|
else:
|
|
|
|
|
|
sell = "不可以"
|
|
|
|
|
|
|
|
|
|
|
|
data_list.append(
|
|
|
|
|
|
[
|
|
|
|
|
|
icon,
|
2025-03-20 18:06:38 +08:00
|
|
|
|
seedName,
|
2025-03-19 18:00:50 +08:00
|
|
|
|
count,
|
|
|
|
|
|
plantInfo['experience'],
|
|
|
|
|
|
plantInfo['harvest'],
|
|
|
|
|
|
plantInfo['time'],
|
|
|
|
|
|
plantInfo['crop'],
|
|
|
|
|
|
plantInfo['again'],
|
|
|
|
|
|
sell
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
result = await ImageTemplate.table_page(
|
2025-03-20 18:06:38 +08:00
|
|
|
|
"种子仓库",
|
|
|
|
|
|
"播种示例:@小真寻 播种 大白菜 [数量]",
|
2025-03-19 18:00:50 +08:00
|
|
|
|
column_name,
|
|
|
|
|
|
data_list,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return result.pic2bytes()
|
2025-03-19 01:16:08 +08:00
|
|
|
|
|
2025-03-20 00:45:05 +08:00
|
|
|
|
@classmethod
|
2025-03-20 18:06:38 +08:00
|
|
|
|
async def sowing(cls, uid: str, name: str, num: int = -1) -> str:
|
2025-03-20 00:45:05 +08:00
|
|
|
|
"""播种
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
|
name (str): 播种种子名称
|
|
|
|
|
|
num (int, optional): 播种数量
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
str:
|
|
|
|
|
|
"""
|
2025-03-20 18:06:38 +08:00
|
|
|
|
plant = await g_pSqlManager.getUserSeedByUid(uid)
|
2025-03-20 00:45:05 +08:00
|
|
|
|
|
|
|
|
|
|
if plant == None:
|
|
|
|
|
|
return "你的种子仓库是空的,快去买点吧!"
|
|
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
plantDict = {}
|
|
|
|
|
|
for item in plant.split(','):
|
|
|
|
|
|
if '|' in item:
|
|
|
|
|
|
seed_name, count = item.split('|', 1)
|
|
|
|
|
|
plantDict[seed_name] = int(count)
|
|
|
|
|
|
|
|
|
|
|
|
isAll = False
|
|
|
|
|
|
if num == -1:
|
|
|
|
|
|
isAll = True
|
|
|
|
|
|
|
|
|
|
|
|
soilNumber = await g_pSqlManager.getUserSoilByUid(uid)
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(1, soilNumber + 1):
|
|
|
|
|
|
if plantDict[name] > 0:
|
|
|
|
|
|
if isAll or num > 0:
|
|
|
|
|
|
soilName = f"soil{i}"
|
|
|
|
|
|
success, message = await g_pSqlManager.getUserSoilStatusBySoilID(uid, soilName)
|
|
|
|
|
|
if success:
|
|
|
|
|
|
# 更新种子数量
|
|
|
|
|
|
num -= 1
|
|
|
|
|
|
plantDict[name] -= 1
|
|
|
|
|
|
if plantDict[name] == 0:
|
|
|
|
|
|
del plantDict[name]
|
|
|
|
|
|
|
|
|
|
|
|
# 更新数据库
|
|
|
|
|
|
await g_pSqlManager.updateUserSoilStatusByPlantName(uid, soilName, name)
|
|
|
|
|
|
|
|
|
|
|
|
await g_pSqlManager.updateUserSeedByUid(
|
|
|
|
|
|
uid,
|
|
|
|
|
|
','.join([f"{k}|{v}" for k, v in plantDict.items()])
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if num > 0:
|
|
|
|
|
|
return f"播种数量超出解锁土地数量,已将可播种土地成功播种{name}!仓库还剩下{plantDict[name]}个种子"
|
|
|
|
|
|
else:
|
|
|
|
|
|
return f"播种{name}成功!仓库还剩下{plantDict[name]}个种子"
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def harvest(cls, uid: str) -> str:
|
|
|
|
|
|
"""收获作物
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
str: 返回
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
soilNumber = await g_pSqlManager.getUserLevelByUid(uid)
|
|
|
|
|
|
soilUnlock = g_pJsonManager.m_pLevel['soil'] # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
plant = {}
|
|
|
|
|
|
|
|
|
|
|
|
soilNames = [f"soil{i + 1}" for i, level in enumerate(soilUnlock) if soilNumber >= level]
|
|
|
|
|
|
soilStatuses = await asyncio.gather(*[
|
|
|
|
|
|
g_pSqlManager.getUserSoilStatusBySoilID(uid, name)
|
|
|
|
|
|
for name in soilNames
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
plant: Dict[str, int] = {}
|
|
|
|
|
|
harvest_records: List[str] = []
|
|
|
|
|
|
|
|
|
|
|
|
for (soil_name, (status, info)) in zip(soilNames, soilStatuses):
|
|
|
|
|
|
if not status:
|
|
|
|
|
|
soil_info = info.split(',')
|
|
|
|
|
|
plant_id = soil_info[0]
|
|
|
|
|
|
plant_info = g_pJsonManager.m_pPlant['plant'][plant_id] # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
current_time = datetime.now()
|
|
|
|
|
|
mature_time = datetime.fromtimestamp(int(soil_info[2]))
|
|
|
|
|
|
|
|
|
|
|
|
if current_time >= mature_time:
|
|
|
|
|
|
plant[plant_id] = plant.get(plant_id, 0) + plant_info['harvest']
|
|
|
|
|
|
harvest_records.append(f"收获作物:{plant_id},数量为:{plant_info['harvest']}")
|
|
|
|
|
|
|
|
|
|
|
|
# 批量更新数据库操作
|
|
|
|
|
|
await g_pSqlManager.updateUserSoilStatusByPlantName(uid, soil_name, "", 4)
|
|
|
|
|
|
|
|
|
|
|
|
if not plant:
|
|
|
|
|
|
return "可收获作物为0哦~ 不要试图拔苗助长"
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 批量更新用户作物数据
|
|
|
|
|
|
await g_pSqlManager.updateUserPlantByUid(
|
|
|
|
|
|
uid,
|
|
|
|
|
|
','.join([f"{k}|{v}" for k, v in plant.items()])
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return "\n".join(harvest_records)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def getUserPlantByUid(cls, uid: str) -> bytes:
|
|
|
|
|
|
"""获取用户作物仓库
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户Uid
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
bytes: 返回图片
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
data_list = []
|
|
|
|
|
|
column_name = [
|
|
|
|
|
|
"-",
|
|
|
|
|
|
"作物名称",
|
|
|
|
|
|
"数量",
|
|
|
|
|
|
"单价",
|
|
|
|
|
|
"总价",
|
|
|
|
|
|
"是否可以上架交易行"
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
plant = await g_pSqlManager.getUserPlantByUid(uid)
|
|
|
|
|
|
|
|
|
|
|
|
if plant == None:
|
|
|
|
|
|
result = await ImageTemplate.table_page(
|
|
|
|
|
|
"作物仓库",
|
|
|
|
|
|
"播种示例:@小真寻 出售作物 大白菜 [数量]",
|
|
|
|
|
|
column_name,
|
|
|
|
|
|
data_list,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return result.pic2bytes()
|
|
|
|
|
|
|
|
|
|
|
|
sell = ""
|
2025-03-20 00:45:05 +08:00
|
|
|
|
for item in plant.split(','):
|
|
|
|
|
|
if '|' in item:
|
|
|
|
|
|
plantName, count = item.split('|', 1) # 分割一次,避免多竖线问题
|
2025-03-20 18:06:38 +08:00
|
|
|
|
try:
|
|
|
|
|
|
plantInfo = g_pJsonManager.m_pPlant['plant'][plantName] # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
icon = ""
|
|
|
|
|
|
icon_path = g_sResourcePath / f"plant/{plantName}/icon.png"
|
|
|
|
|
|
if icon_path.exists():
|
|
|
|
|
|
icon = (icon_path, 33, 33)
|
2025-03-20 00:45:05 +08:00
|
|
|
|
|
2025-03-20 18:06:38 +08:00
|
|
|
|
if plantInfo['again'] == True:
|
|
|
|
|
|
sell = "可以"
|
|
|
|
|
|
else:
|
|
|
|
|
|
sell = "不可以"
|
|
|
|
|
|
|
|
|
|
|
|
data_list.append(
|
|
|
|
|
|
[
|
|
|
|
|
|
icon,
|
|
|
|
|
|
plantName,
|
|
|
|
|
|
count,
|
|
|
|
|
|
plantInfo['price'],
|
|
|
|
|
|
count * int(plantInfo['price']),
|
|
|
|
|
|
sell
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
result = await ImageTemplate.table_page(
|
|
|
|
|
|
"作物仓库",
|
|
|
|
|
|
"播种示例:@小真寻 出售作物 大白菜 [数量]",
|
|
|
|
|
|
column_name,
|
|
|
|
|
|
data_list,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return result.pic2bytes()
|
2025-03-20 00:45:05 +08:00
|
|
|
|
|
2025-03-19 18:00:50 +08:00
|
|
|
|
g_pFarmManager = CFarmManager()
|