604 lines
20 KiB
GDScript
604 lines
20 KiB
GDScript
extends Node
|
||
class_name SocialManager
|
||
## 社交管理器
|
||
## 统一管理所有社交功能和系统集成
|
||
|
||
# 社交系统组件
|
||
var friend_system: FriendSystem
|
||
var private_chat_system: PrivateChatSystem
|
||
var relationship_network: RelationshipNetwork
|
||
var community_event_system: CommunityEventSystem
|
||
|
||
# 网络管理器引用
|
||
var network_manager: NetworkManager
|
||
|
||
# 社交状态
|
||
var is_initialized: bool = false
|
||
var current_user_id: String = "player"
|
||
|
||
# 信号
|
||
signal social_systems_ready()
|
||
signal friend_activity(activity_type: String, data: Dictionary)
|
||
signal social_notification(notification_type: String, title: String, message: String, data: Dictionary)
|
||
|
||
func _ready():
|
||
"""初始化社交管理器"""
|
||
print("SocialManager initializing...")
|
||
_initialize_social_systems()
|
||
|
||
## 初始化社交系统
|
||
func _initialize_social_systems() -> void:
|
||
"""初始化所有社交系统组件"""
|
||
# 创建好友系统
|
||
friend_system = FriendSystem.new()
|
||
friend_system.name = "FriendSystem"
|
||
add_child(friend_system)
|
||
|
||
# 创建私聊系统
|
||
private_chat_system = PrivateChatSystem.new()
|
||
private_chat_system.name = "PrivateChatSystem"
|
||
add_child(private_chat_system)
|
||
|
||
# 创建关系网络
|
||
relationship_network = RelationshipNetwork.new()
|
||
relationship_network.name = "RelationshipNetwork"
|
||
add_child(relationship_network)
|
||
|
||
# 创建社区事件系统
|
||
community_event_system = CommunityEventSystem.new()
|
||
community_event_system.name = "CommunityEventSystem"
|
||
add_child(community_event_system)
|
||
|
||
# 设置系统间的引用
|
||
private_chat_system.set_friend_system(friend_system)
|
||
community_event_system.set_relationship_network(relationship_network)
|
||
|
||
# 连接信号
|
||
_connect_system_signals()
|
||
|
||
is_initialized = true
|
||
social_systems_ready.emit()
|
||
|
||
print("SocialManager initialized with all social systems")
|
||
|
||
## 连接系统信号
|
||
func _connect_system_signals() -> void:
|
||
"""连接各个社交系统的信号"""
|
||
# 好友系统信号
|
||
friend_system.friend_request_sent.connect(_on_friend_request_sent)
|
||
# friend_system.friend_request_received.connect(_on_friend_request_received) # 信号暂时未使用
|
||
friend_system.friend_request_accepted.connect(_on_friend_request_accepted)
|
||
friend_system.friend_removed.connect(_on_friend_removed)
|
||
friend_system.friend_online.connect(_on_friend_online)
|
||
friend_system.friend_offline.connect(_on_friend_offline)
|
||
friend_system.relationship_level_changed.connect(_on_relationship_level_changed)
|
||
|
||
# 私聊系统信号
|
||
private_chat_system.conversation_created.connect(_on_conversation_created)
|
||
private_chat_system.message_received.connect(_on_private_message_received)
|
||
private_chat_system.unread_count_changed.connect(_on_unread_count_changed)
|
||
|
||
# 关系网络信号
|
||
relationship_network.relationship_created.connect(_on_relationship_created)
|
||
relationship_network.relationship_updated.connect(_on_relationship_updated)
|
||
relationship_network.influence_score_changed.connect(_on_influence_score_changed)
|
||
|
||
# 社区事件系统信号
|
||
community_event_system.event_created.connect(_on_event_created)
|
||
community_event_system.participant_joined.connect(_on_participant_joined)
|
||
community_event_system.event_started.connect(_on_event_started)
|
||
community_event_system.event_completed.connect(_on_event_completed)
|
||
community_event_system.milestone_achieved.connect(_on_milestone_achieved)
|
||
|
||
## 设置网络管理器
|
||
func set_network_manager(nm: NetworkManager) -> void:
|
||
"""
|
||
设置网络管理器引用
|
||
@param nm: 网络管理器实例
|
||
"""
|
||
network_manager = nm
|
||
if network_manager:
|
||
network_manager.message_received.connect(_on_network_message_received)
|
||
|
||
## 处理网络消息
|
||
func _on_network_message_received(message: Dictionary) -> void:
|
||
"""
|
||
处理来自网络的社交相关消息
|
||
@param message: 网络消息
|
||
"""
|
||
var message_type = message.get("type", "")
|
||
var data = message.get("data", {})
|
||
|
||
match message_type:
|
||
"friend_request":
|
||
_handle_friend_request_message(data)
|
||
"friend_response":
|
||
_handle_friend_response_message(data)
|
||
"private_message":
|
||
_handle_private_message(data)
|
||
"event_invitation":
|
||
_handle_event_invitation(data)
|
||
"social_update":
|
||
_handle_social_update(data)
|
||
|
||
## 发送好友请求
|
||
func send_friend_request(target_id: String, target_name: String) -> bool:
|
||
"""
|
||
发送好友请求
|
||
@param target_id: 目标用户ID
|
||
@param target_name: 目标用户名称
|
||
@return: 是否成功发送
|
||
"""
|
||
if not is_initialized:
|
||
print("Social systems not initialized")
|
||
return false
|
||
|
||
# 本地处理
|
||
var success = friend_system.send_friend_request(target_id, target_name)
|
||
|
||
if success and network_manager:
|
||
# 发送网络消息
|
||
var message = {
|
||
"type": "friend_request",
|
||
"data": {
|
||
"target_id": target_id,
|
||
"requester_name": "Player" # 这里应该从用户数据获取
|
||
}
|
||
}
|
||
network_manager.send_message(message)
|
||
|
||
return success
|
||
|
||
## 接受好友请求
|
||
func accept_friend_request(requester_id: String) -> bool:
|
||
"""
|
||
接受好友请求
|
||
@param requester_id: 请求者ID
|
||
@return: 是否成功接受
|
||
"""
|
||
if not is_initialized:
|
||
return false
|
||
|
||
var success = friend_system.accept_friend_request(requester_id)
|
||
|
||
if success and network_manager:
|
||
# 发送网络消息
|
||
var message = {
|
||
"type": "friend_response",
|
||
"data": {
|
||
"requester_id": requester_id,
|
||
"accepted": true
|
||
}
|
||
}
|
||
network_manager.send_message(message)
|
||
|
||
# 在关系网络中创建好友关系
|
||
relationship_network.create_relationship(current_user_id, requester_id, RelationshipNetwork.RelationshipType.FRIEND)
|
||
|
||
return success
|
||
|
||
## 开始私聊
|
||
func start_private_chat(target_id: String, target_name: String) -> String:
|
||
"""
|
||
开始私聊
|
||
@param target_id: 目标用户ID
|
||
@param target_name: 目标用户名称
|
||
@return: 会话ID
|
||
"""
|
||
if not is_initialized:
|
||
return ""
|
||
|
||
return private_chat_system.start_private_chat(target_id, target_name)
|
||
|
||
## 发送私聊消息
|
||
func send_private_message(conversation_id: String, message: String) -> bool:
|
||
"""
|
||
发送私聊消息
|
||
@param conversation_id: 会话ID
|
||
@param message: 消息内容
|
||
@return: 是否成功发送
|
||
"""
|
||
if not is_initialized:
|
||
return false
|
||
|
||
var success = private_chat_system.send_private_message(conversation_id, message)
|
||
|
||
if success and network_manager:
|
||
# 发送网络消息
|
||
var network_message = {
|
||
"type": "private_message",
|
||
"data": {
|
||
"conversation_id": conversation_id,
|
||
"message": message,
|
||
"sender_id": current_user_id
|
||
}
|
||
}
|
||
network_manager.send_message(network_message)
|
||
|
||
# 记录关系网络互动
|
||
var conversation_info = private_chat_system.get_conversation_messages(conversation_id, 1)
|
||
if conversation_info.size() > 0:
|
||
# 这里需要获取对方ID,简化处理
|
||
relationship_network.record_interaction(current_user_id, "other_user", "private_chat")
|
||
|
||
return success
|
||
|
||
## 创建社区事件
|
||
func create_community_event(title: String, description: String, event_type: CommunityEventSystem.EventType, start_time: float = 0.0) -> String:
|
||
"""
|
||
创建社区事件
|
||
@param title: 事件标题
|
||
@param description: 事件描述
|
||
@param event_type: 事件类型
|
||
@param start_time: 开始时间
|
||
@return: 事件ID
|
||
"""
|
||
if not is_initialized:
|
||
return ""
|
||
|
||
var event_id = community_event_system.create_event(title, description, event_type, current_user_id, start_time)
|
||
|
||
if not event_id.is_empty() and network_manager:
|
||
# 发送网络消息
|
||
var message = {
|
||
"type": "event_created",
|
||
"data": {
|
||
"event_id": event_id,
|
||
"title": title,
|
||
"description": description,
|
||
"event_type": event_type,
|
||
"organizer_id": current_user_id,
|
||
"start_time": start_time
|
||
}
|
||
}
|
||
network_manager.send_message(message)
|
||
|
||
return event_id
|
||
|
||
## 加入社区事件
|
||
func join_community_event(event_id: String) -> bool:
|
||
"""
|
||
加入社区事件
|
||
@param event_id: 事件ID
|
||
@return: 是否成功加入
|
||
"""
|
||
if not is_initialized:
|
||
return false
|
||
|
||
var success = community_event_system.join_event(event_id, current_user_id)
|
||
|
||
if success and network_manager:
|
||
# 发送网络消息
|
||
var message = {
|
||
"type": "event_join",
|
||
"data": {
|
||
"event_id": event_id,
|
||
"participant_id": current_user_id
|
||
}
|
||
}
|
||
network_manager.send_message(message)
|
||
|
||
return success
|
||
|
||
## 获取社交摘要
|
||
func get_social_summary() -> Dictionary:
|
||
"""
|
||
获取社交活动摘要
|
||
@return: 社交摘要数据
|
||
"""
|
||
if not is_initialized:
|
||
return {}
|
||
|
||
var friends_list = friend_system.get_friends_list()
|
||
var friend_requests = friend_system.get_friend_requests()
|
||
var conversations = private_chat_system.get_conversations_list()
|
||
var unread_messages = private_chat_system.get_total_unread_count()
|
||
var user_events = community_event_system.get_user_events(current_user_id)
|
||
var influence_ranking = relationship_network.get_influence_ranking(10)
|
||
|
||
return {
|
||
"friends_count": friends_list.size(),
|
||
"pending_requests": friend_requests.size(),
|
||
"active_conversations": conversations.size(),
|
||
"unread_messages": unread_messages,
|
||
"upcoming_events": user_events.size(),
|
||
"influence_rank": _get_user_influence_rank(influence_ranking),
|
||
"recent_activity": _get_recent_social_activity()
|
||
}
|
||
|
||
## 获取推荐内容
|
||
func get_social_recommendations() -> Dictionary:
|
||
"""
|
||
获取社交推荐内容
|
||
@return: 推荐内容数据
|
||
"""
|
||
if not is_initialized:
|
||
return {}
|
||
|
||
var recommended_events = community_event_system.get_recommended_events(current_user_id, 5)
|
||
var mutual_friends = _get_potential_friends()
|
||
var active_conversations = private_chat_system.get_conversations_list()
|
||
|
||
# 过滤最近活跃的会话
|
||
var recent_conversations = []
|
||
for conv in active_conversations:
|
||
if conv.unread_count > 0 or (Time.get_unix_time_from_system() - conv.last_activity) < 86400: # 24小时内
|
||
recent_conversations.append(conv)
|
||
|
||
return {
|
||
"recommended_events": recommended_events,
|
||
"potential_friends": mutual_friends,
|
||
"active_conversations": recent_conversations.slice(0, 5),
|
||
"trending_topics": _get_trending_topics()
|
||
}
|
||
|
||
## 处理角色上线
|
||
func handle_character_online(character_id: String, character_name: String) -> void:
|
||
"""
|
||
处理角色上线事件
|
||
@param character_id: 角色ID
|
||
@param character_name: 角色名称
|
||
"""
|
||
if not is_initialized:
|
||
return
|
||
|
||
# 检查是否为好友
|
||
if friend_system.is_friend(character_id):
|
||
friend_system.handle_friend_online(character_id)
|
||
|
||
# 发送通知
|
||
social_notification.emit("friend_online", "好友上线", character_name + " 上线了", {"character_id": character_id})
|
||
|
||
## 处理角色下线
|
||
func handle_character_offline(character_id: String, _character_name: String) -> void:
|
||
"""
|
||
处理角色下线事件
|
||
@param character_id: 角色ID
|
||
@param _character_name: 角色名称 (暂未使用)
|
||
"""
|
||
if not is_initialized:
|
||
return
|
||
|
||
# 检查是否为好友
|
||
if friend_system.is_friend(character_id):
|
||
friend_system.handle_friend_offline(character_id)
|
||
|
||
## 记录社交互动
|
||
func record_social_interaction(target_id: String, interaction_type: String, data: Dictionary = {}) -> void:
|
||
"""
|
||
记录社交互动
|
||
@param target_id: 目标角色ID
|
||
@param interaction_type: 互动类型
|
||
@param data: 互动数据
|
||
"""
|
||
if not is_initialized:
|
||
return
|
||
|
||
# 记录到关系网络
|
||
relationship_network.record_interaction(current_user_id, target_id, interaction_type, data)
|
||
|
||
# 如果是好友,记录到好友系统
|
||
if friend_system.is_friend(target_id):
|
||
friend_system.record_interaction(target_id, interaction_type)
|
||
|
||
## 获取用户影响力排名
|
||
func _get_user_influence_rank(ranking: Array[Dictionary]) -> int:
|
||
"""
|
||
获取用户在影响力排行榜中的排名
|
||
@param ranking: 影响力排行数据
|
||
@return: 排名(从1开始,0表示未上榜)
|
||
"""
|
||
for i in range(ranking.size()):
|
||
if ranking[i].character_id == current_user_id:
|
||
return i + 1
|
||
return 0
|
||
|
||
## 获取潜在好友
|
||
func _get_potential_friends() -> Array[Dictionary]:
|
||
"""
|
||
获取潜在好友推荐
|
||
@return: 潜在好友数组
|
||
"""
|
||
var potential_friends = []
|
||
var user_relationships = relationship_network.get_character_relationships(current_user_id)
|
||
|
||
# 基于共同好友推荐
|
||
for relationship in user_relationships:
|
||
if relationship.type == RelationshipNetwork.RelationshipType.FRIEND:
|
||
var mutual_connections = relationship_network.find_mutual_connections(current_user_id, relationship.to_character)
|
||
for connection in mutual_connections:
|
||
if not friend_system.is_friend(connection) and not friend_system.is_blocked(connection):
|
||
potential_friends.append({
|
||
"character_id": connection,
|
||
"reason": "共同好友: " + relationship.to_character,
|
||
"mutual_friends": 1
|
||
})
|
||
|
||
# 去重并限制数量
|
||
var unique_friends = {}
|
||
for friend in potential_friends:
|
||
var id = friend.character_id
|
||
if not unique_friends.has(id):
|
||
unique_friends[id] = friend
|
||
else:
|
||
unique_friends[id].mutual_friends += 1
|
||
|
||
var result = unique_friends.values()
|
||
result.sort_custom(func(a, b): return a.mutual_friends > b.mutual_friends)
|
||
|
||
return result.slice(0, 5)
|
||
|
||
## 获取最近社交活动
|
||
func _get_recent_social_activity() -> Array[Dictionary]:
|
||
"""
|
||
获取最近的社交活动
|
||
@return: 活动数组
|
||
"""
|
||
var activities = []
|
||
|
||
# 获取最近的好友互动
|
||
var friends_list = friend_system.get_friends_list()
|
||
for friend in friends_list:
|
||
if (Time.get_unix_time_from_system() - friend.last_interaction) < 86400: # 24小时内
|
||
activities.append({
|
||
"type": "friend_interaction",
|
||
"description": "与 " + friend.name + " 互动",
|
||
"timestamp": friend.last_interaction
|
||
})
|
||
|
||
# 获取最近的私聊
|
||
var conversations = private_chat_system.get_conversations_list()
|
||
for conv in conversations:
|
||
if (Time.get_unix_time_from_system() - conv.last_activity) < 86400: # 24小时内
|
||
activities.append({
|
||
"type": "private_chat",
|
||
"description": "与 " + conv.participants[0].name + " 私聊",
|
||
"timestamp": conv.last_activity
|
||
})
|
||
|
||
# 按时间排序
|
||
activities.sort_custom(func(a, b): return a.timestamp > b.timestamp)
|
||
|
||
return activities.slice(0, 10)
|
||
|
||
## 获取热门话题
|
||
func _get_trending_topics() -> Array[String]:
|
||
"""
|
||
获取热门话题
|
||
@return: 话题数组
|
||
"""
|
||
# 这里可以基于最近的事件、对话等分析热门话题
|
||
# 简化实现,返回一些示例话题
|
||
return ["AI学习", "技术分享", "游戏开发", "社区建设", "创新项目"]
|
||
|
||
## 处理网络消息 - 好友请求
|
||
func _handle_friend_request_message(data: Dictionary) -> void:
|
||
"""处理好友请求网络消息"""
|
||
var requester_id = data.get("requester_id", "")
|
||
var requester_name = data.get("requester_name", "Unknown")
|
||
|
||
if not requester_id.is_empty():
|
||
# 这里应该显示好友请求通知给用户
|
||
social_notification.emit("friend_request", "好友请求", requester_name + " 想要添加您为好友", data)
|
||
|
||
## 处理网络消息 - 好友响应
|
||
func _handle_friend_response_message(data: Dictionary) -> void:
|
||
"""处理好友响应网络消息"""
|
||
var accepted = data.get("accepted", false)
|
||
var responder_name = data.get("responder_name", "Unknown")
|
||
|
||
if accepted:
|
||
social_notification.emit("friend_accepted", "好友请求已接受", responder_name + " 接受了您的好友请求", data)
|
||
else:
|
||
social_notification.emit("friend_declined", "好友请求被拒绝", responder_name + " 拒绝了您的好友请求", data)
|
||
|
||
## 处理网络消息 - 私聊消息
|
||
func _handle_private_message(data: Dictionary) -> void:
|
||
"""处理私聊消息网络消息"""
|
||
var conversation_id = data.get("conversation_id", "")
|
||
var sender_id = data.get("sender_id", "")
|
||
var message = data.get("message", "")
|
||
|
||
if not conversation_id.is_empty() and not sender_id.is_empty():
|
||
private_chat_system.receive_private_message(conversation_id, sender_id, message)
|
||
|
||
## 处理网络消息 - 事件邀请
|
||
func _handle_event_invitation(data: Dictionary) -> void:
|
||
"""处理事件邀请网络消息"""
|
||
var _event_id = data.get("event_id", "")
|
||
var event_title = data.get("event_title", "")
|
||
var inviter_name = data.get("inviter_name", "Unknown")
|
||
|
||
social_notification.emit("event_invitation", "活动邀请", inviter_name + " 邀请您参加 " + event_title, data)
|
||
|
||
## 处理网络消息 - 社交更新
|
||
func _handle_social_update(data: Dictionary) -> void:
|
||
"""处理社交更新网络消息"""
|
||
var update_type = data.get("update_type", "")
|
||
|
||
match update_type:
|
||
"character_online":
|
||
handle_character_online(data.get("character_id", ""), data.get("character_name", ""))
|
||
"character_offline":
|
||
handle_character_offline(data.get("character_id", ""), data.get("character_name", ""))
|
||
|
||
# 信号处理函数
|
||
func _on_friend_request_sent(friend_id: String, friend_name: String):
|
||
friend_activity.emit("friend_request_sent", {"friend_id": friend_id, "friend_name": friend_name})
|
||
|
||
# func _on_friend_request_received(requester_id: String, requester_name: String):
|
||
# social_notification.emit("friend_request", "新的好友请求", requester_name + " 想要添加您为好友", {"requester_id": requester_id})
|
||
|
||
func _on_friend_request_accepted(friend_id: String, friend_name: String):
|
||
friend_activity.emit("friend_accepted", {"friend_id": friend_id, "friend_name": friend_name})
|
||
social_notification.emit("friend_accepted", "好友请求已接受", "您与 " + friend_name + " 现在是好友了", {"friend_id": friend_id})
|
||
|
||
func _on_friend_removed(friend_id: String, friend_name: String):
|
||
friend_activity.emit("friend_removed", {"friend_id": friend_id, "friend_name": friend_name})
|
||
|
||
func _on_friend_online(friend_id: String, friend_name: String):
|
||
friend_activity.emit("friend_online", {"friend_id": friend_id, "friend_name": friend_name})
|
||
|
||
func _on_friend_offline(friend_id: String, friend_name: String):
|
||
friend_activity.emit("friend_offline", {"friend_id": friend_id, "friend_name": friend_name})
|
||
|
||
func _on_relationship_level_changed(friend_id: String, old_level: int, new_level: int):
|
||
if new_level > old_level:
|
||
var friend_info = friend_system.get_friend_info(friend_id)
|
||
var friend_name = friend_info.get("name", "Unknown")
|
||
social_notification.emit("relationship_improved", "关系提升", "您与 " + friend_name + " 的关系等级提升了", {"friend_id": friend_id, "new_level": new_level})
|
||
|
||
func _on_conversation_created(conversation_id: String, participants: Array[String]):
|
||
friend_activity.emit("conversation_created", {"conversation_id": conversation_id, "participants": participants})
|
||
|
||
func _on_private_message_received(_conversation_id: String, _sender_id: String, _message: String):
|
||
# 这里可以显示消息通知
|
||
pass
|
||
|
||
func _on_unread_count_changed(_conversation_id: String, count: int):
|
||
if count > 0:
|
||
# 更新UI显示未读消息数量
|
||
pass
|
||
|
||
func _on_relationship_created(from_character: String, to_character: String, relationship_type: RelationshipNetwork.RelationshipType):
|
||
friend_activity.emit("relationship_created", {"from": from_character, "to": to_character, "type": relationship_type})
|
||
|
||
func _on_relationship_updated(from_character: String, to_character: String, old_strength: float, new_strength: float):
|
||
friend_activity.emit("relationship_updated", {"from": from_character, "to": to_character, "strength_change": new_strength - old_strength})
|
||
|
||
func _on_influence_score_changed(character_id: String, old_score: float, new_score: float):
|
||
if character_id == current_user_id and new_score > old_score:
|
||
social_notification.emit("influence_increased", "影响力提升", "您的社区影响力增加了", {"score_change": new_score - old_score})
|
||
|
||
func _on_event_created(event_id: String, title: String, organizer_id: String):
|
||
friend_activity.emit("event_created", {"event_id": event_id, "title": title, "organizer_id": organizer_id})
|
||
|
||
func _on_participant_joined(event_id: String, participant_id: String):
|
||
friend_activity.emit("event_joined", {"event_id": event_id, "participant_id": participant_id})
|
||
|
||
func _on_event_started(event_id: String, title: String):
|
||
social_notification.emit("event_started", "活动开始", title + " 活动现在开始了", {"event_id": event_id})
|
||
|
||
func _on_event_completed(event_id: String, title: String, participants: Array[String]):
|
||
if current_user_id in participants:
|
||
social_notification.emit("event_completed", "活动完成", "您参与的 " + title + " 活动已完成", {"event_id": event_id})
|
||
|
||
func _on_milestone_achieved(milestone_type: String, data: Dictionary):
|
||
social_notification.emit("milestone", "里程碑达成", "社区达成了新的里程碑: " + milestone_type, data)
|
||
|
||
## 获取统计信息
|
||
func get_statistics() -> Dictionary:
|
||
"""
|
||
获取社交系统统计信息
|
||
@return: 统计信息字典
|
||
"""
|
||
if not is_initialized:
|
||
return {}
|
||
|
||
return {
|
||
"friend_system": friend_system.get_statistics(),
|
||
"private_chat": private_chat_system.get_statistics(),
|
||
"relationship_network": relationship_network.get_statistics(),
|
||
"community_events": community_event_system.get_statistics()
|
||
}
|