@@ -589,75 +589,6 @@ class SMYMongoDBAPI:
|
||||
#=====================在线礼包系统======================
|
||||
|
||||
|
||||
#=====================作物数据系统======================
|
||||
def get_crop_data_config(self) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
获取作物数据配置
|
||||
|
||||
Returns:
|
||||
Dict: 作物数据配置
|
||||
"""
|
||||
try:
|
||||
collection = self.get_collection("gameconfig")
|
||||
|
||||
# 使用已知的文档ID查找
|
||||
object_id = ObjectId("687cfb3d8e77ba00a7414bac")
|
||||
result = collection.find_one({"_id": object_id})
|
||||
|
||||
if result:
|
||||
# 移除MongoDB的_id字段和updated_at字段
|
||||
if "_id" in result:
|
||||
del result["_id"]
|
||||
if "updated_at" in result:
|
||||
del result["updated_at"]
|
||||
|
||||
self.logger.info("成功获取作物数据配置")
|
||||
return result
|
||||
else:
|
||||
self.logger.warning("未找到作物数据配置")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"获取作物数据配置失败: {e}")
|
||||
return None
|
||||
|
||||
def update_crop_data_config(self, config_data: Dict[str, Any]) -> bool:
|
||||
"""
|
||||
更新作物数据配置
|
||||
|
||||
Args:
|
||||
config_data: 配置数据
|
||||
|
||||
Returns:
|
||||
bool: 是否成功
|
||||
"""
|
||||
try:
|
||||
collection = self.get_collection("gameconfig")
|
||||
|
||||
# 使用已知的文档ID更新
|
||||
object_id = ObjectId("687cfb3d8e77ba00a7414bac")
|
||||
|
||||
# 添加更新时间
|
||||
update_data = {
|
||||
"updated_at": datetime.now(),
|
||||
**config_data
|
||||
}
|
||||
|
||||
result = collection.replace_one({"_id": object_id}, update_data)
|
||||
|
||||
if result.acknowledged and result.matched_count > 0:
|
||||
self.logger.info("成功更新作物数据配置")
|
||||
return True
|
||||
else:
|
||||
self.logger.error("更新作物数据配置失败")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"更新作物数据配置异常: {e}")
|
||||
return False
|
||||
#=====================作物数据系统======================
|
||||
|
||||
|
||||
#=====================道具配置系统======================
|
||||
def get_item_config(self) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
|
||||
@@ -433,7 +433,7 @@ class TCPGameServer(TCPServer):
|
||||
|
||||
#加载作物配置数据(优化版本)
|
||||
def _load_crop_data(self):
|
||||
"""加载作物配置数据(带缓存优化,优先从MongoDB加载)"""
|
||||
"""加载作物配置数据(带缓存优化)"""
|
||||
current_time = time.time()
|
||||
|
||||
# 检查缓存是否有效
|
||||
@@ -442,26 +442,10 @@ class TCPGameServer(TCPServer):
|
||||
return self.crop_data_cache
|
||||
|
||||
# 缓存过期或不存在,重新加载
|
||||
# 优先尝试从MongoDB加载
|
||||
if self.use_mongodb and self.mongo_api:
|
||||
try:
|
||||
crop_data = self.mongo_api.get_crop_data_config()
|
||||
if crop_data:
|
||||
self.crop_data_cache = crop_data
|
||||
self.crop_data_cache_time = current_time
|
||||
self.log('INFO', "成功从MongoDB加载作物数据", 'SERVER')
|
||||
return self.crop_data_cache
|
||||
else:
|
||||
self.log('WARNING', "MongoDB中未找到作物数据,尝试从JSON文件加载", 'SERVER')
|
||||
except Exception as e:
|
||||
self.log('ERROR', f"从MongoDB加载作物数据失败: {str(e)},尝试从JSON文件加载", 'SERVER')
|
||||
|
||||
# MongoDB加载失败或不可用,从JSON文件加载
|
||||
try:
|
||||
with open("config/crop_data.json", 'r', encoding='utf-8') as file:
|
||||
self.crop_data_cache = json.load(file)
|
||||
self.crop_data_cache_time = current_time
|
||||
self.log('INFO', "成功从JSON文件加载作物数据", 'SERVER')
|
||||
return self.crop_data_cache
|
||||
except Exception as e:
|
||||
self.log('ERROR', f"无法加载作物数据: {str(e)}", 'SERVER')
|
||||
@@ -7949,7 +7933,6 @@ class TCPGameServer(TCPServer):
|
||||
|
||||
|
||||
# ================================账户设置处理方法================================
|
||||
#处理修改账号信息请求
|
||||
def _handle_modify_account_info_request(self, client_id, message):
|
||||
"""处理修改账号信息请求"""
|
||||
# 检查用户是否已登录
|
||||
@@ -7978,6 +7961,9 @@ class TCPGameServer(TCPServer):
|
||||
if not new_farm_name:
|
||||
return self._send_modify_account_error(client_id, "农场名称不能为空")
|
||||
|
||||
if len(new_password) < 6:
|
||||
return self._send_modify_account_error(client_id, "密码长度至少6个字符")
|
||||
|
||||
if len(new_player_name) > 20:
|
||||
return self._send_modify_account_error(client_id, "玩家昵称不能超过20个字符")
|
||||
|
||||
@@ -8020,7 +8006,6 @@ class TCPGameServer(TCPServer):
|
||||
self.log('ERROR', f"修改账号信息时出错: {str(e)}", 'ACCOUNT')
|
||||
return self._send_modify_account_error(client_id, "修改账号信息失败,请稍后重试")
|
||||
|
||||
#处理删除账号请求
|
||||
def _handle_delete_account_request(self, client_id, message):
|
||||
"""处理删除账号请求"""
|
||||
# 检查用户是否已登录
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -8,55 +8,8 @@
|
||||
"user_password": "密码",
|
||||
"last_login_time": "2025年07月20日17时19分16秒",
|
||||
"total_login_time": "0时0分0秒",
|
||||
"farm_lots": [
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":true,"is_planted":false,"max_grow_time":3,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0},
|
||||
{"crop_type":"","grow_time":0,"is_dead":false,"is_diged":false,"is_planted":false,"max_grow_time":5,"已浇水":false,"已施肥":false,"土地等级":0}
|
||||
],
|
||||
"last_water_reset_date": "2025-06-05",
|
||||
"farm_lots": [],
|
||||
"player_bag": [],
|
||||
"作物仓库": [],
|
||||
"宠物背包": [],
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
{}
|
||||
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"experience": 455,
|
||||
"level": 36,
|
||||
"money": 200797690,
|
||||
"money": 200797815,
|
||||
"farm_name": "柚大青の小农场",
|
||||
"player_name": "柚大青",
|
||||
"user_name": "2143323382",
|
||||
"user_password": "tyh@19900420",
|
||||
"last_login_time": "2025年07月21日07时50分09秒",
|
||||
"total_login_time": "6时52分47秒",
|
||||
"last_login_time": "2025年07月20日22时15分13秒",
|
||||
"total_login_time": "6时47分9秒",
|
||||
"farm_lots": [
|
||||
{
|
||||
"crop_type": "",
|
||||
@@ -132,27 +132,35 @@
|
||||
},
|
||||
{
|
||||
"crop_type": "杂交树1",
|
||||
"grow_time": 21600,
|
||||
"grow_time": 21042,
|
||||
"is_dead": false,
|
||||
"is_diged": true,
|
||||
"is_planted": true,
|
||||
"max_grow_time": 21600,
|
||||
"已浇水": false,
|
||||
"已施肥": false,
|
||||
"已施肥": true,
|
||||
"土地等级": 3,
|
||||
"施肥时间": 1753019866.8176558,
|
||||
"施肥类型": "农家肥",
|
||||
"施肥倍数": 2.0,
|
||||
"施肥持续时间": 1800,
|
||||
"浇水时间": 1753019871.817958
|
||||
},
|
||||
{
|
||||
"crop_type": "杂交树2",
|
||||
"grow_time": 25254,
|
||||
"grow_time": 21636,
|
||||
"is_dead": false,
|
||||
"is_diged": true,
|
||||
"is_planted": true,
|
||||
"max_grow_time": 25200,
|
||||
"已浇水": false,
|
||||
"已施肥": false,
|
||||
"已施肥": true,
|
||||
"土地等级": 3,
|
||||
"浇水时间": 1753019874.5774846
|
||||
"浇水时间": 1753019874.5774846,
|
||||
"施肥时间": 1753019862.2419734,
|
||||
"施肥类型": "农家肥",
|
||||
"施肥倍数": 2.0,
|
||||
"施肥持续时间": 1800
|
||||
},
|
||||
{
|
||||
"crop_type": "",
|
||||
@@ -576,7 +584,7 @@
|
||||
{
|
||||
"name": "胡萝卜",
|
||||
"quality": "普通",
|
||||
"count": 2
|
||||
"count": 1
|
||||
},
|
||||
{
|
||||
"name": "番茄",
|
||||
@@ -672,15 +680,11 @@
|
||||
"name": "杂交树2",
|
||||
"quality": "传奇",
|
||||
"count": 1
|
||||
},
|
||||
{
|
||||
"name": "大豆",
|
||||
"quality": "普通",
|
||||
"count": 1
|
||||
}
|
||||
],
|
||||
"last_water_reset_date": "2025-06-05",
|
||||
"注册时间": "2025年05月21日15时00分00秒",
|
||||
"个人简介": "其实我是一个梨子,真的,不骗你,hhh",
|
||||
"个人简介": "其实我是一个梨子,真的,不骗你",
|
||||
"作物仓库": [
|
||||
{
|
||||
"name": "番茄",
|
||||
@@ -991,24 +995,24 @@
|
||||
"2025年07月13日07时26分04秒": "金币302 经验63 土豆x5 小麦x3"
|
||||
},
|
||||
"在线礼包": {
|
||||
"当前日期": "2025-07-21",
|
||||
"今日在线时长": 0.0,
|
||||
"当前日期": "2025-07-20",
|
||||
"今日在线时长": 999999.2718074322,
|
||||
"已领取礼包": [],
|
||||
"登录时间": 1753055025.953216
|
||||
"登录时间": 1753003043.7163484
|
||||
},
|
||||
"点赞系统": {
|
||||
"今日剩余点赞次数": 10,
|
||||
"点赞上次刷新时间": "2025-07-21"
|
||||
"点赞上次刷新时间": "2025-07-20"
|
||||
},
|
||||
"新手礼包": {
|
||||
"已领取": true,
|
||||
"领取时间": "2025-07-20 20:21:04"
|
||||
},
|
||||
"体力系统": {
|
||||
"当前体力值": 25,
|
||||
"当前体力值": 24,
|
||||
"最大体力值": 25,
|
||||
"上次刷新时间": "2025-07-21",
|
||||
"上次恢复时间": 1753055025.9496412
|
||||
"上次刷新时间": "2025-07-20",
|
||||
"上次恢复时间": 1753018615.8492067
|
||||
},
|
||||
"道具背包": [
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,118 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
作物数据MongoDB迁移脚本
|
||||
作者: AI Assistant
|
||||
功能: 将crop_data.json中的数据迁移到MongoDB
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from SMYMongoDBAPI import SMYMongoDBAPI
|
||||
|
||||
def migrate_crop_data():
|
||||
"""迁移作物数据到MongoDB"""
|
||||
print("=== 作物数据MongoDB迁移脚本 ===")
|
||||
|
||||
# 1. 连接MongoDB
|
||||
print("\n1. 连接MongoDB...")
|
||||
try:
|
||||
api = SMYMongoDBAPI("mengyafarm") # 使用正式数据库
|
||||
if not api.is_connected():
|
||||
print("❌ MongoDB连接失败")
|
||||
return False
|
||||
print("✅ MongoDB连接成功")
|
||||
except Exception as e:
|
||||
print(f"❌ MongoDB连接异常: {e}")
|
||||
return False
|
||||
|
||||
# 2. 从JSON文件加载作物数据
|
||||
print("\n2. 从JSON文件加载作物数据...")
|
||||
try:
|
||||
with open("config/crop_data.json", 'r', encoding='utf-8') as file:
|
||||
crop_data = json.load(file)
|
||||
print(f"✅ JSON数据加载成功,包含 {len(crop_data)} 种作物")
|
||||
except Exception as e:
|
||||
print(f"❌ 加载JSON文件失败: {e}")
|
||||
return False
|
||||
|
||||
# 3. 检查MongoDB中是否已有数据
|
||||
print("\n3. 检查MongoDB中的现有数据...")
|
||||
try:
|
||||
existing_data = api.get_crop_data_config()
|
||||
if existing_data:
|
||||
print(f"⚠️ MongoDB中已存在作物数据,包含 {len(existing_data)} 种作物")
|
||||
choice = input("是否要覆盖现有数据?(y/N): ").strip().lower()
|
||||
if choice not in ['y', 'yes']:
|
||||
print("取消迁移")
|
||||
return False
|
||||
else:
|
||||
print("✅ MongoDB中暂无作物数据,可以进行迁移")
|
||||
except Exception as e:
|
||||
print(f"❌ 检查MongoDB数据时异常: {e}")
|
||||
return False
|
||||
|
||||
# 4. 迁移数据到MongoDB
|
||||
print("\n4. 迁移数据到MongoDB...")
|
||||
try:
|
||||
success = api.update_crop_data_config(crop_data)
|
||||
if success:
|
||||
print("✅ 作物数据迁移成功")
|
||||
else:
|
||||
print("❌ 作物数据迁移失败")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ 迁移数据时异常: {e}")
|
||||
return False
|
||||
|
||||
# 5. 验证迁移结果
|
||||
print("\n5. 验证迁移结果...")
|
||||
try:
|
||||
migrated_data = api.get_crop_data_config()
|
||||
if migrated_data and len(migrated_data) == len(crop_data):
|
||||
print(f"✅ 迁移验证成功,MongoDB中包含 {len(migrated_data)} 种作物")
|
||||
|
||||
# 检查几个关键作物
|
||||
test_crops = ["小麦", "胡萝卜", "苹果", "松露"]
|
||||
print("\n验证关键作物数据:")
|
||||
for crop_name in test_crops:
|
||||
if crop_name in crop_data and crop_name in migrated_data:
|
||||
original = crop_data[crop_name]
|
||||
migrated = migrated_data[crop_name]
|
||||
if original == migrated:
|
||||
print(f"✅ {crop_name}: 数据一致")
|
||||
else:
|
||||
print(f"⚠️ {crop_name}: 数据不一致")
|
||||
else:
|
||||
print(f"❌ {crop_name}: 数据缺失")
|
||||
else:
|
||||
print("❌ 迁移验证失败")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ 验证迁移结果时异常: {e}")
|
||||
return False
|
||||
|
||||
print("\n=== 迁移完成 ===")
|
||||
print("\n📋 迁移摘要:")
|
||||
print(f" • 源文件: config/crop_data.json")
|
||||
print(f" • 目标数据库: mengyafarm")
|
||||
print(f" • 目标集合: gameconfig")
|
||||
print(f" • 文档ID: 687cfb3d8e77ba00a7414bac")
|
||||
print(f" • 迁移作物数量: {len(crop_data)}")
|
||||
print("\n✅ 作物数据已成功迁移到MongoDB!")
|
||||
print("\n💡 提示: 服务器现在会优先从MongoDB加载作物数据,如果MongoDB不可用会自动回退到JSON文件。")
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
try:
|
||||
migrate_crop_data()
|
||||
except KeyboardInterrupt:
|
||||
print("\n迁移被用户中断")
|
||||
except Exception as e:
|
||||
print(f"迁移过程中发生异常: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,142 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
作物数据MongoDB迁移测试脚本
|
||||
作者: AI Assistant
|
||||
功能: 测试作物数据从JSON到MongoDB的迁移功能
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from SMYMongoDBAPI import SMYMongoDBAPI
|
||||
|
||||
def load_crop_data_from_json():
|
||||
"""从JSON文件加载作物数据"""
|
||||
try:
|
||||
with open("config/crop_data.json", 'r', encoding='utf-8') as file:
|
||||
return json.load(file)
|
||||
except Exception as e:
|
||||
print(f"❌ 加载JSON文件失败: {e}")
|
||||
return None
|
||||
|
||||
def test_crop_data_migration():
|
||||
"""测试作物数据迁移"""
|
||||
print("=== 作物数据MongoDB迁移测试 ===")
|
||||
|
||||
# 1. 连接MongoDB
|
||||
print("\n1. 连接MongoDB...")
|
||||
try:
|
||||
api = SMYMongoDBAPI("test")
|
||||
if not api.is_connected():
|
||||
print("❌ MongoDB连接失败")
|
||||
return False
|
||||
print("✅ MongoDB连接成功")
|
||||
except Exception as e:
|
||||
print(f"❌ MongoDB连接异常: {e}")
|
||||
return False
|
||||
|
||||
# 2. 从JSON文件加载作物数据
|
||||
print("\n2. 从JSON文件加载作物数据...")
|
||||
json_data = load_crop_data_from_json()
|
||||
if not json_data:
|
||||
print("❌ JSON数据加载失败")
|
||||
return False
|
||||
print(f"✅ JSON数据加载成功,包含 {len(json_data)} 种作物")
|
||||
|
||||
# 3. 测试从MongoDB获取作物数据
|
||||
print("\n3. 从MongoDB获取作物数据...")
|
||||
try:
|
||||
mongo_data = api.get_crop_data_config()
|
||||
if mongo_data:
|
||||
print(f"✅ MongoDB数据获取成功,包含 {len(mongo_data)} 种作物")
|
||||
|
||||
# 4. 比较数据一致性
|
||||
print("\n4. 比较数据一致性...")
|
||||
if len(json_data) == len(mongo_data):
|
||||
print("✅ 作物数量一致")
|
||||
else:
|
||||
print(f"⚠️ 作物数量不一致: JSON({len(json_data)}) vs MongoDB({len(mongo_data)})")
|
||||
|
||||
# 检查几个关键作物
|
||||
test_crops = ["小麦", "胡萝卜", "苹果", "松露"]
|
||||
for crop_name in test_crops:
|
||||
if crop_name in json_data and crop_name in mongo_data:
|
||||
json_crop = json_data[crop_name]
|
||||
mongo_crop = mongo_data[crop_name]
|
||||
if json_crop == mongo_crop:
|
||||
print(f"✅ {crop_name} 数据一致")
|
||||
else:
|
||||
print(f"⚠️ {crop_name} 数据不一致")
|
||||
print(f" JSON: {json_crop.get('花费', 'N/A')}元, {json_crop.get('生长时间', 'N/A')}秒")
|
||||
print(f" MongoDB: {mongo_crop.get('花费', 'N/A')}元, {mongo_crop.get('生长时间', 'N/A')}秒")
|
||||
else:
|
||||
print(f"❌ {crop_name} 在某个数据源中缺失")
|
||||
else:
|
||||
print("❌ MongoDB中未找到作物数据")
|
||||
|
||||
# 5. 如果MongoDB中没有数据,尝试更新
|
||||
print("\n5. 尝试更新MongoDB中的作物数据...")
|
||||
try:
|
||||
success = api.update_crop_data_config(json_data)
|
||||
if success:
|
||||
print("✅ 作物数据更新到MongoDB成功")
|
||||
|
||||
# 再次验证
|
||||
print("\n6. 验证更新后的数据...")
|
||||
updated_data = api.get_crop_data_config()
|
||||
if updated_data and len(updated_data) == len(json_data):
|
||||
print("✅ 数据更新验证成功")
|
||||
else:
|
||||
print("❌ 数据更新验证失败")
|
||||
else:
|
||||
print("❌ 作物数据更新到MongoDB失败")
|
||||
except Exception as e:
|
||||
print(f"❌ 更新MongoDB数据时异常: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 从MongoDB获取数据时异常: {e}")
|
||||
return False
|
||||
|
||||
# 7. 测试服务器加载逻辑
|
||||
print("\n7. 测试服务器加载逻辑...")
|
||||
try:
|
||||
# 模拟服务器的加载逻辑
|
||||
from TCPGameServer import TCPGameServer
|
||||
|
||||
# 创建服务器实例(不启动网络服务)
|
||||
server = TCPGameServer()
|
||||
|
||||
# 测试加载作物数据
|
||||
crop_data = server._load_crop_data()
|
||||
if crop_data and len(crop_data) > 0:
|
||||
print(f"✅ 服务器成功加载作物数据,包含 {len(crop_data)} 种作物")
|
||||
|
||||
# 测试几个关键作物
|
||||
test_crops = ["小麦", "胡萝卜"]
|
||||
for crop_name in test_crops:
|
||||
if crop_name in crop_data:
|
||||
crop = crop_data[crop_name]
|
||||
print(f"✅ {crop_name}: {crop.get('花费', 'N/A')}元, {crop.get('生长时间', 'N/A')}秒, {crop.get('品质', 'N/A')}")
|
||||
else:
|
||||
print(f"❌ 服务器数据中缺少 {crop_name}")
|
||||
else:
|
||||
print("❌ 服务器加载作物数据失败")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 测试服务器加载逻辑时异常: {e}")
|
||||
|
||||
print("\n=== 测试完成 ===")
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
try:
|
||||
test_crop_data_migration()
|
||||
except KeyboardInterrupt:
|
||||
print("\n测试被用户中断")
|
||||
except Exception as e:
|
||||
print(f"测试过程中发生异常: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user