161 lines
5.1 KiB
C#
161 lines
5.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class UIManager_Playing : MonoBehaviour
|
|
{
|
|
public AudioSource audioSource;
|
|
public Text scoreLabel;
|
|
|
|
private bool finished = false;
|
|
private bool started = false;
|
|
private MusicPlayer musicPlayer;
|
|
private TouchManager_Playing touchManager;
|
|
|
|
void Start()
|
|
{
|
|
musicPlayer = FindAnyObjectByType<MusicPlayer>();
|
|
touchManager = FindAnyObjectByType<TouchManager_Playing>();
|
|
if (scoreLabel != null) scoreLabel.text = "載入中…";
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (musicPlayer == null || finished) return;
|
|
|
|
// 曲包讀取失敗:回選歌畫面
|
|
if (musicPlayer.Error != null)
|
|
{
|
|
finished = true;
|
|
Toast.Show(musicPlayer.Error);
|
|
GoToChooseScene();
|
|
return;
|
|
}
|
|
if (!musicPlayer.Ready) return;
|
|
if (!started)
|
|
{
|
|
started = true;
|
|
if (scoreLabel != null) scoreLabel.text = "0";
|
|
}
|
|
|
|
// 只觸發一次,避免每幀重複上傳成績
|
|
if (musicPlayer.Finished)
|
|
{
|
|
finished = true;
|
|
StartCoroutine(SendScoreAndGoToChooseScene());
|
|
}
|
|
}
|
|
|
|
IEnumerator SendScoreAndGoToChooseScene()
|
|
{
|
|
int uid = PlayerPrefs.GetInt("UID", 0);
|
|
SongInfo song = musicPlayer.Song;
|
|
int level = musicPlayer.Level;
|
|
PlayResult result = touchManager != null ? touchManager.Result : new PlayResult();
|
|
PlayResult.Last = result;
|
|
Debug.Log($"[Play] song={song.id} level={level} notes={musicPlayer.TotalNotes} {result}");
|
|
|
|
string status;
|
|
if (uid == 0)
|
|
{
|
|
status = "未登入,成績不會上傳";
|
|
}
|
|
else
|
|
{
|
|
bool ok = false;
|
|
yield return UploadScore(uid, song, level, result.score, success => ok = success);
|
|
status = ok ? "成績已上傳" : "成績上傳失敗";
|
|
}
|
|
|
|
Font font = scoreLabel != null ? scoreLabel.font : null;
|
|
if (scoreLabel != null) scoreLabel.gameObject.SetActive(false);
|
|
ResultOverlay.Show(result, musicPlayer.TotalNotes, font, status);
|
|
|
|
yield return WaitForTap();
|
|
GoToChooseScene();
|
|
}
|
|
|
|
// 等待一次新的點擊(忽略結算出現前就按著的手指)
|
|
IEnumerator WaitForTap()
|
|
{
|
|
UnityEngine.InputSystem.EnhancedTouch.EnhancedTouchSupport.Enable();
|
|
yield return new WaitForSeconds(0.5f);
|
|
while (true)
|
|
{
|
|
foreach (var t in UnityEngine.InputSystem.EnhancedTouch.Touch.activeTouches)
|
|
if (t.phase == UnityEngine.InputSystem.TouchPhase.Began) yield break;
|
|
if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame) yield break;
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
IEnumerator UploadScore(int uid, SongInfo song, int level, int score, System.Action<bool> onDone)
|
|
{
|
|
// 曲包不在伺服器上註冊過時,伺服器會用這些資訊建立歌曲資料
|
|
string jsonData = JsonUtility.ToJson(new ScoreData
|
|
{
|
|
request = "user_play",
|
|
uid = uid,
|
|
token = PlayerPrefs.GetString("Token", ""),
|
|
password = PlayerPrefs.GetString("Password", ""), // 舊版相容
|
|
song = song.id,
|
|
title = song.name,
|
|
artist = song.artist,
|
|
bpm = song.bpm,
|
|
duration_sec = audioSource != null && audioSource.clip != null ? Mathf.RoundToInt(audioSource.clip.length) : 0,
|
|
level = level,
|
|
level_num = song.levelNum[level],
|
|
score = score
|
|
});
|
|
|
|
using (UnityWebRequest www = new UnityWebRequest(ServerConfig.GameServerUrl, "POST"))
|
|
{
|
|
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
|
|
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError("Error sending score: " + www.error);
|
|
onDone(false);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Score sent successfully: " + www.downloadHandler.text);
|
|
onDone(www.downloadHandler.text.Contains("\"success\""));
|
|
}
|
|
}
|
|
}
|
|
|
|
void GoToChooseScene()
|
|
{
|
|
if (SceneManager.GetActiveScene().name != "ChooseScene")
|
|
{
|
|
SceneManager.LoadScene("ChooseScene");
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class ScoreData
|
|
{
|
|
public string request;
|
|
public int uid;
|
|
public string token;
|
|
public string password;
|
|
public string song;
|
|
public string title;
|
|
public string artist;
|
|
public double bpm;
|
|
public int duration_sec;
|
|
public int level;
|
|
public int level_num;
|
|
public int score;
|
|
}
|
|
}
|