Commit 53068b6a by Ford

修改了WorldChat支持高并发,加了协程。

parent 889709d3
......@@ -60,7 +60,7 @@ type WorldClient struct {
//DpConversations *models.DpConversations
WorldConversations *models.WorldConversation
Socket *websocket.Conn
Send chan *WorldEchoRespones
Send chan *WorldEchoResponse
WorldName string
AuthId string
UserSource string
......@@ -989,7 +989,7 @@ func (c *WorldClient) WorldRead() {
// ExtractRating 从响应中提取整体评分
func ExtractRating(response WorldEchoRespones) (int, error) {
func ExtractRating(response WorldEchoResponse) (int, error) {
endStr, ok := response.WObj["EndStr"].(string)
if !ok || endStr == "" {
return 0, nil
......@@ -1009,7 +1009,7 @@ func ExtractRating(response WorldEchoRespones) (int, error) {
return 0, fmt.Errorf("未找到评分信息")
}
func ExtractRating1(response WorldEchoRespones) string {
func ExtractRating1(response WorldEchoResponse) string {
endStr, ok := response.WObj["EndStr"].(string)
if !ok {
......@@ -1165,3 +1165,160 @@ func (c *WorldClient) WorldWrite() {
}
/* ============================================================================================= */
// ParseEndStrAndReformat 重新结构化 WorldEchoRespones,以包含结论消息的详细拆分
func ParseEndStrAndReformat1(response *WorldEchoResponse) {
if response == nil || response.WObj == nil {
return
}
endStr, ok := response.WObj["EndStr"].(string)
if !ok || endStr == "" {
return
}
// 解析 "EndStr" 中的详细字段
title := strings.Split(endStr, "@")[0] // 提取 '@' 前的标题
overallScore := extractBetween(endStr, "【整体评分】:", "\n") // 提取整体评分
objectiveEvaluation := extractBetween(endStr, "【客观评价】:", "**言行举止**") // 提取客观评价
// 将 "EndStr" 结构化为 JSON 对象
endStrObj := map[string]interface{}{
"title": title,
"OverallScore": strings.TrimSpace(overallScore),
"ObjectiveEvaluation": strings.TrimSpace(objectiveEvaluation),
}
response.WObj["EndStr"] = endStrObj // 将格式化后的结论字符串对象重新赋值给响应
// 可选处理其他字段如 '地点' 和 '表情'
if address, exists := response.WObj["地点"]; exists {
response.WObj["address"] = address
delete(response.WObj, "地点")
}
if emotion, exists := response.WObj["表情"]; exists {
response.WObj["emotion"] = emotion
delete(response.WObj, "表情")
}
}
// ParseEndStrAndReformat 将 WorldSoulReplyMsg 中的 WObj 修改后返回一个新的 WorldSoulReplyMsg 结构体
func ParseEndStrAndReformat2(response *WorldSoulReplyMsg) *WorldSoulReplyMsg {
if response == nil || response.WObj == nil {
return nil // 如果输入是nil,直接返回nil
}
newResponse := *response // 创建一个新的响应副本以避免修改原始数据
endStr, ok := newResponse.WObj["EndStr"].(string)
if !ok || endStr == "" {
return &newResponse // 如果没有 EndStr 或者为空,返回原始数据的副本
}
// 解析 "EndStr" 中的详细字段
title := strings.Split(endStr, "@")[0] // 提取 '@' 前的标题
overallScore := extractBetween(endStr, "【整体评分】:", "\n") // 提取整体评分
objectiveEvaluation := extractToEnd(endStr, "【客观评价】:") // 提取客观评价
// 将 "EndStr" 结构化为 JSON 对象
endStrObj := map[string]interface{}{
"title": title,
"OverallScore": strings.TrimSpace(overallScore),
"ObjectiveEvaluation": strings.TrimSpace(objectiveEvaluation),
}
newResponse.WObj["EndStr"] = endStrObj // 将格式化后的结论字符串对象重新赋值给响应
// 可选处理其他字段如 '地点' 和 '表情'
if address, exists := newResponse.WObj["地点"]; exists {
newResponse.WObj["address"] = address
delete(newResponse.WObj, "地点")
}
if emotion, exists := newResponse.WObj["表情"]; exists {
newResponse.WObj["emotion"] = emotion
delete(newResponse.WObj, "表情")
}
return &newResponse // 返回修改后的新响应体
}
func ParseEndStrAndReformat3(response *WorldSoulReplyMsg) *WorldSoulReplyMsg {
if response == nil || response.WObj == nil {
return nil // If the input is nil, return nil
}
newResponse := *response // Create a copy of the response to modify
// Determine the message type based on the presence of specific keywords or structures
if len(newResponse.WObj) == 0 && strings.Contains(newResponse.ISLIU, "【任务提示】") {
newResponse.MessageStatusType = "开场白"
} else if endStr, ok := newResponse.WObj["EndStr"].(string); ok && endStr != "" {
newResponse.MessageStatusType = "结局"
} else {
newResponse.MessageStatusType = "会话中消息"
}
// Further reformatting based on the presence of "EndStr"
if endStr, ok := newResponse.WObj["EndStr"].(string); ok {
if endStr != "" {
// Parse details from "EndStr"
title := strings.Split(endStr, "@")[0]
overallScore := extractBetween(endStr, "【整体评分】:", "\n")
objectiveEvaluation := extractToEnd(endStr, "【客观评价】:")
// Structurize "EndStr" into a JSON object
endStrObj := map[string]interface{}{
"title": title,
"overallScore": strings.TrimSpace(overallScore),
"objectiveEvaluation": strings.TrimSpace(objectiveEvaluation),
}
newResponse.WObj["EndStr"] = endStrObj // Assign the reformatted conclusion string object back to the response
}
}
// Handle additional fields such as '地点' and '表情'
if address, exists := newResponse.WObj["地点"]; exists {
newResponse.WObj["address"] = address
delete(newResponse.WObj, "地点")
}
if emotion, exists := newResponse.WObj["表情"]; exists {
newResponse.WObj["emotion"] = emotion
delete(newResponse.WObj, "表情")
}
return &newResponse // Return the modified response
}
// 使用例
func ExampleSend() {
IsLiu_Id := "d018fd63c598f4a13e31f7f8faf0ac15"
originalMsg := WorldSoulReplyMsg{
Code: 1,
WObj: map[string]interface{}{"EndStr": "确定下单@\n## 【整体评分】: 90\n\n## 【客观评价】:\n\n这位业务经理在与客户沟通的过程中展现出了良好的专业素养和沟通技巧。他能清晰地介绍产品,并针对客户提出的问题和疑虑进行耐心解答,并根据客户的需求进行灵活调整,最终促成了交易。\n\n**言行举止**: 业务经理在与客户沟通的过程中保持着礼貌和专业的态度,没有出现任何不当言行。\n\n**优势**: 业务经理对产品知识掌握熟练,能够根据客户的需求进行灵活的方案调整,并展现出一定的谈判技巧,最终促成了交易。\n\n**不足与建议**: 在价格方面,业务经理可以尝试更早地了解客户的心理价位,以便制定更有针对性的报价策略。此外,在介绍产品时,可以更加突出云电脑相比传统电脑的优势,例如节省维护成本、方便升级等。 \n",
"地点": "老板办公室",
"表情": "满意"},
ISLIU: "Some text",
WorldName: "Example World",
IsLIUId: &IsLiu_Id,
}
// 转换消息
transformedMsg := ParseEndStrAndReformat(&originalMsg)
// 序列化消息为JSON
jsonMsg, err := json.Marshal(transformedMsg)
if err != nil {
log.Printf("JSON编码失败: %v", err)
return
}
// 发送消息
//err = c.Socket.WriteMessage(websocket.TextMessage, jsonMsg)
//if err != nil {
// log.Println("发送WebSocket消息失败: ", err)
// WorldDestroyWs(c) // 关闭WebSocket连接
//}
fmt.Println("jsonMsg ==> ", string(jsonMsg))
}
......@@ -14,12 +14,14 @@ import (
)
//创建世界API接口时候返回的响应体
type WorldEchoRespones struct {
type WorldEchoResponse struct {
Code int `from:"code" json:"code"`
WObj map[string]interface{} `json:"WObj"`
ISLIU string `from:"ISLIU" json:"ISLIU"`
MuseValue int `json:"muse"`
WorldName string `json:"worldName" `
IsLIUId *string `json:"IsLIUId,omitempty"` // 使用指针类型,并添加 omitempty 标签,可选字段
MessageStatusType string `json:"messageStatusType,omitempty"` // Optional field to indicate message type
}
//世界推演结局
......@@ -29,7 +31,7 @@ type OutcomeContent struct {
}
// WorldClient
func (client *WorldClient) WorldEchoAPI(msg *WorldSendMsg) (*WorldEchoRespones, error) {
func (client *WorldClient) WorldEchoAPI(msg *WorldSendMsg) (*WorldEchoResponse, error) {
/*
创建一个请求参数对象
*/
......@@ -70,7 +72,7 @@ func (client *WorldClient) WorldEchoAPI(msg *WorldSendMsg) (*WorldEchoRespones,
return nil, err
}
// 解析JSON
var response WorldEchoRespones
var response WorldEchoResponse
err = json.Unmarshal(body, &response)
// 将JSON打印为字符串
......@@ -135,7 +137,7 @@ func (client *WorldClient) WorldEchoAPI(msg *WorldSendMsg) (*WorldEchoRespones,
}
func AddWorldEchoEndStrRecord(worldId, userId int64, response *WorldEchoRespones) {
func AddWorldEchoEndStrRecord(worldId, userId int64, response *WorldEchoResponse) {
// 检查是否存在结束标记 "EndStr"
if endStr, ok := response.WObj["EndStr"]; ok {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment