Petition-to-the-Gods-V3/Assets/export_canvas/script/SurveyManager.cs
chiyu.lin 4ede3ccfa6 fix Q15
資料移到GlobalData
串接星星與回覆,調整星星顯示與觸發
2025-11-19 17:40:43 +08:00

338 lines
9.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class SurveyManager : MonoBehaviour
{
[Header("UI 元件")]
public StarRating question1Stars;
public StarRating question2Stars;
public StarRating question3Stars;
public InputField commentInputField;
public TMP_InputField commentInputFieldTMP;
public Button submitButton;
public GameObject warningText;
[Header("計時器")]
public Text timerText;
public TMP_Text timerTextTMP;
public float countdownTime = 30f;
private float currentTime;
private bool isGameRunning = false;
public System.Action<int, int, int, string> OnSurveyConfirmed;
void Start()
{
if (warningText != null)
{
warningText.SetActive(false);
}
if (submitButton != null)
{
submitButton.onClick.AddListener(OnSubmitButtonClicked);
}
currentTime = countdownTime;
StartGame();
}
void Update()
{
if (isGameRunning)
{
if (currentTime > 0)
{
currentTime -= Time.deltaTime;
int seconds = Mathf.CeilToInt(currentTime);
string timeText = "時間倒數 " + seconds + " 秒";
if (timerText != null)
{
timerText.text = timeText;
timerText.color = Color.red;
}
if (timerTextTMP != null)
{
timerTextTMP.text = timeText;
timerTextTMP.color = Color.red;
}
}
else
{
currentTime = 0;
string timeUpText = "時間到!";
if (timerText != null)
{
timerText.text = timeUpText;
timerText.color = Color.red;
}
if (timerTextTMP != null)
{
timerTextTMP.text = timeUpText;
timerTextTMP.color = Color.red;
}
isGameRunning = false;
//強制送出
OnSurveySubmit(true);
}
}
}
public void StartGame()
{
Debug.Log("遊戲開始!");
isGameRunning = true;
currentTime = countdownTime;
string startText = "時間倒數 " + Mathf.CeilToInt(currentTime) + " 秒";
if (timerText != null)
{
timerText.color = Color.red;
timerText.text = startText;
}
if (timerTextTMP != null)
{
timerTextTMP.color = Color.red;
timerTextTMP.text = startText;
}
if (question1Stars != null) question1Stars.ResetRating();
if (question2Stars != null) question2Stars.ResetRating();
if (question3Stars != null) question3Stars.ResetRating();
if (commentInputField != null)
{
commentInputField.text = "";
}
if (commentInputFieldTMP != null)
{
commentInputFieldTMP.text = "";
}
}
void OnSubmitButtonClicked()
{
Debug.Log(">>> 點擊送出按鈕");
OnSurveySubmit();
}
private void OnSurveySubmit(bool isForce = false)
{
// 取得三題的評分
int rating1 = 0;
int rating2 = 0;
int rating3 = 0;
if (question1Stars != null) rating1 = question1Stars.GetRating();
if (question2Stars != null) rating2 = question2Stars.GetRating();
if (question3Stars != null) rating3 = question3Stars.GetRating();
// 檢查是否至少填寫了一個星星評分
if (rating1 == 0 && rating2 == 0 && rating3 == 0 && isForce == false)
{
ShowWarning();
return;
}
// 取得自行填寫的文字
string userComment = "";
if (commentInputField != null && !string.IsNullOrEmpty(commentInputField.text))
{
userComment = commentInputField.text;
}
else if (commentInputFieldTMP != null && !string.IsNullOrEmpty(commentInputFieldTMP.text))
{
userComment = commentInputFieldTMP.text;
}
// 送出資料
SubmitSurvey(rating1, rating2, rating3, userComment);
OnSurveyConfirmed?.Invoke(rating1, rating2, rating3, userComment);
}
void ShowWarning()
{
if (warningText != null)
{
warningText.SetActive(true);
Invoke("HideWarning", 2f);
}
Debug.Log("⚠️ 警告:請至少填寫一個問題的評分!");
}
void HideWarning()
{
if (warningText != null)
{
warningText.SetActive(false);
}
}
void SubmitSurvey(int rating1, int rating2, int rating3, string comment)
{
isGameRunning = false;
// ====== 清楚的資料統整顯示 ======
Debug.Log("╔══════════════════════════════════════╗");
Debug.Log("║ 問卷結果統整 ║");
Debug.Log("╚══════════════════════════════════════╝");
Debug.Log("");
// 問題 1
Debug.Log("【問題 1】覺得這個廟宇的神明有成功幫到你嗎");
if (rating1 > 0)
{
Debug.Log(" ⭐ 評分:" + rating1 + " 顆星");
Debug.Log(" " + GetStarDisplay(rating1));
}
else
{
Debug.Log(" ⚪ 未填寫");
}
Debug.Log("");
// 問題 2
Debug.Log("【問題 2】下次還會回訪嗎");
if (rating2 > 0)
{
Debug.Log(" ⭐ 評分:" + rating2 + " 顆星");
Debug.Log(" " + GetStarDisplay(rating2));
}
else
{
Debug.Log(" ⚪ 未填寫");
}
Debug.Log("");
// 問題 3
Debug.Log("【問題 3】有成功成為他的信眾嗎");
if (rating3 > 0)
{
Debug.Log(" ⭐ 評分:" + rating3 + " 顆星");
Debug.Log(" " + GetStarDisplay(rating3));
}
else
{
Debug.Log(" ⚪ 未填寫");
}
Debug.Log("");
// 自行填寫
Debug.Log("【自行填寫】");
if (!string.IsNullOrEmpty(comment))
{
Debug.Log(" 💬 內容:「" + comment + "」");
}
else
{
Debug.Log(" ⚪ 未填寫");
}
Debug.Log("");
// 統計資料
Debug.Log("─────────────────────────────────────");
Debug.Log("【統計資料】");
// 計算填寫問題數量
int answeredQuestions = 0;
if (rating1 > 0) answeredQuestions++;
if (rating2 > 0) answeredQuestions++;
if (rating3 > 0) answeredQuestions++;
Debug.Log(" 📊 填寫問題數:" + answeredQuestions + " / 3");
// 計算總星數和平均
int totalStars = rating1 + rating2 + rating3;
Debug.Log(" ⭐ 總星數:" + totalStars + " 顆星");
if (answeredQuestions > 0)
{
float averageRating = (float)totalStars / answeredQuestions;
Debug.Log(" 📈 平均評分:" + averageRating.ToString("F2") + " 顆星");
// 滿意度評價
string satisfaction = GetSatisfactionLevel(averageRating);
Debug.Log(" 😊 滿意度:" + satisfaction);
}
// 剩餘時間
int remainingTime = Mathf.CeilToInt(currentTime);
Debug.Log(" ⏱️ 剩餘時間:" + remainingTime + " 秒");
// 完成時間
int usedTime = Mathf.CeilToInt(countdownTime - currentTime);
Debug.Log(" ⏰ 完成時間:" + usedTime + " 秒");
Debug.Log("─────────────────────────────────────");
Debug.Log("✅ 問卷送出成功!");
Debug.Log("══════════════════════════════════════");
Debug.Log("");
// 更新計時器顯示
string successText = "問卷送出成功!";
if (timerText != null)
{
timerText.text = successText;
timerText.color = Color.green;
}
if (timerTextTMP != null)
{
timerTextTMP.text = successText;
timerTextTMP.color = Color.green;
}
}
// 顯示星星圖示
string GetStarDisplay(int rating)
{
string stars = "";
for (int i = 0; i < 5; i++)
{
if (i < rating)
{
stars += "★";
}
else
{
stars += "☆";
}
}
return stars;
}
// 取得滿意度評價
string GetSatisfactionLevel(float averageRating)
{
if (averageRating >= 4.5f)
{
return "非常滿意 🌟🌟🌟";
}
else if (averageRating >= 3.5f)
{
return "滿意 😊";
}
else if (averageRating >= 2.5f)
{
return "普通 😐";
}
else if (averageRating >= 1.5f)
{
return "不太滿意 😕";
}
else
{
return "非常不滿意 😞";
}
}
}