WXMC/proj/unity/Assets/Scripts/Wish/PuzzleQuestionBoard.cs
2024-12-04 16:18:46 +08:00

100 lines
3.1 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PuzzleQuestionBoard : MonoBehaviour
{
public Button QuestionButton;
public Button[] RecQuestionButtons;
public TMP_InputField InputQuestions;
public Button questionBoardReturnButton;
public bool isDebugging = false;
[HideInInspector]
public PuzzleCardExplainItem cardExplainItem;
// Start is called before the first frame update
void Start()
{
foreach (var recQuestionButton in RecQuestionButtons)
{
recQuestionButton.onClick.AddListener(delegate
{
InputQuestions.text = recQuestionButton.GetComponentInChildren<TMP_Text>().text;
});
}
QuestionButton.onClick.AddListener(SendQuestion);
questionBoardReturnButton.onClick.AddListener(delegate
{
cardExplainItem.canvasGroup.gameObject.SetActive(true);
gameObject.SetActive(false);
});
}
void SendQuestion()
{
if (isDebugging)
{
StartCoroutine(TestCoroutine());
}
else
{
StartCoroutine(SendQuestionRequest());
}
}
ChatGPTRequestStruct GenerateMessages(WishCard card)
{
var s = new ChatGPTRequestStruct();
string q1 = string.Format("Answer the input question with regard to the following Tarot card:\n {0}", card.CardFrontText.text);
string q2 = InputQuestions.text;
s.messages = new ChatGPTMessageStruct[2]
{
new ChatGPTMessageStruct("system", q1),
new ChatGPTMessageStruct("user", q2)
};
return s;
}
WenXinRequestStruct GenerateWenXinMessages()
{
var s = new WenXinRequestStruct();
s.system = string.Format("Answer the input question with regard to the following Tarot card:\n {0}", cardExplainItem.m_explainText.text);
string q2 = InputQuestions.text;
s.messages = new List<WenXinRequestMessage>
{
WenXinRequestMessage.UserMessage(q2),
};
return s;
}
IEnumerator TestCoroutine()
{
string answer = "Test answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\nTest answer!\n";
yield return new WaitForSeconds(2.0f);
cardExplainItem.m_explainText.SetText(answer);
gameObject.SetActive(false);
cardExplainItem.canvasGroup.gameObject.SetActive(true);
}
IEnumerator SendQuestionRequest()
{
string response;
var wenXinParam = GenerateWenXinMessages();
WenXinRequest rs = new WenXinRequest();
yield return rs.Post(this, wenXinParam);
response = rs.Response;
cardExplainItem.m_explainText.SetText(response);
gameObject.SetActive(false);
cardExplainItem.canvasGroup.gameObject.SetActive(true);
}
}