- One empty scene; App builds the canvas, safe-area layout and a shared 5x6 Grid in code. Pages (Intro, Main, Choose, Play, Account, Settings) replace 5 scenes, 5 prefabs and the per-cell spawner/display components. - Packs: the game reads Rhyculator/package/ (with All files access) or the app folder, like a plugged-in USB stick. Zips stay where they are and are extracted to a cache keyed by name/size/mtime; folders are read directly. Scans run every second on a background thread. Old songs/ imports and inbox zips are migrated automatically. - Download packs by 8-char code from the new pack website, or via rhyculator://pack/<code> links; opened/shared zips go into the package folder. - Play: pause/resume/retry, auto-pause on background, combo and progress bar; judging and scoring unchanged. Choose page fits 9:16 screens; empty state, pack delete. Android back key handled everywhere. - Slimming: removed TextMesh Pro, unused 2D/Timeline/VisualScripting/etc. packages and modules, scene template, unused sprites; menu theme wav (20MB) -> ogg (2.3MB). Build profile replaced by explicit settings in BuildAndroid. - Tests: PackLibrary/PackReader/migration/serializer tests (39 passing). Co-Authored-By: Claude Opus 5.5 <[email protected]>
278 lines
11 KiB
C#
278 lines
11 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
// 帳號:登入 / 註冊 / 信箱驗證 / 個人資訊
|
||
// 登入後手機上只存 token(不存密碼);舊版存的密碼會在進入個人資訊時換成 token 並刪除
|
||
public class AccountPage : Page
|
||
{
|
||
public override bool UsesGrid => false;
|
||
|
||
private RectTransform body;
|
||
private Text error;
|
||
private bool busy;
|
||
|
||
public override void Enter()
|
||
{
|
||
App.Music.Stop();
|
||
SongClock.Stop();
|
||
var back = Ui.Button(Top, "返回", () => App.Go(new MainPage()), 40);
|
||
Ui.Place((RectTransform)back.transform, new Vector2(0, 1), new Vector2(40, -30), new Vector2(200, 100));
|
||
|
||
if (Prefs.LoggedIn) ShowInfo();
|
||
else ShowLogin();
|
||
}
|
||
|
||
// 換一個面板:清掉內容,回傳新的 Column
|
||
RectTransform Panel(string title)
|
||
{
|
||
if (body != null) Object.Destroy(body.parent.parent.gameObject);
|
||
var holder = Ui.Stretch(Ui.Rect(Top, "Panel"), 60, 0, 60, 150);
|
||
body = Ui.Scroll(holder, 28, 10);
|
||
var t = Ui.Label(body, title, 64, Color.white);
|
||
t.fontStyle = FontStyle.Bold;
|
||
Ui.Height(t, 110);
|
||
error = Ui.Label(body, "", 36, Ui.Danger);
|
||
error.transform.SetAsLastSibling();
|
||
return body;
|
||
}
|
||
|
||
InputField Field(string label, string placeholder, InputField.ContentType type = InputField.ContentType.Standard)
|
||
{
|
||
Ui.Label(body, label, 36, Ui.Muted, TextAnchor.MiddleLeft);
|
||
return Ui.Height(Ui.Input(body, placeholder, type), 120);
|
||
}
|
||
|
||
Button Action(string label, System.Action onClick, bool primary = false)
|
||
=> Ui.Height(Ui.Button(body, label, () => { if (!busy) onClick(); }, 46, primary), 120);
|
||
|
||
void ShowError(string msg)
|
||
{
|
||
error.text = msg;
|
||
error.transform.SetAsLastSibling();
|
||
}
|
||
|
||
// ---------- 登入 ----------
|
||
|
||
void ShowLogin()
|
||
{
|
||
Panel("登入帳號");
|
||
var id = Field("ID", "輸入你的 ID");
|
||
var pw = Field("密碼", "輸入你的密碼", InputField.ContentType.Password);
|
||
Action("登入", () => App.StartCoroutine(Login(id.text.Trim(), pw.text)), primary: true);
|
||
Action("註冊帳號", ShowRegister);
|
||
Ui.Label(body, "曲包網站也使用同一組帳號登入", 32, Ui.Muted);
|
||
}
|
||
|
||
IEnumerator Login(string name, string password)
|
||
{
|
||
if (name == "" || password == "") { ShowError("請輸入 ID 與密碼"); yield break; }
|
||
busy = true;
|
||
Api.Response r = null;
|
||
yield return Api.Game(new Dictionary<string, object> { ["request"] = "login", ["name"] = name, ["password"] = password }, x => r = x);
|
||
busy = false;
|
||
if (r.error != null)
|
||
{
|
||
ShowError(r.error == "cant_find_name" || r.error == "password_wrong" ? "ID 或密碼錯誤" : "登入失敗:" + r.error);
|
||
yield break;
|
||
}
|
||
SaveLogin(r.Int("uid"), r.Str("token"), r.Str("email"));
|
||
ShowInfo();
|
||
}
|
||
|
||
static void SaveLogin(int uid, string token, string email)
|
||
{
|
||
PlayerPrefs.SetInt(Prefs.Uid, uid);
|
||
PlayerPrefs.SetString(Prefs.Token, token);
|
||
PlayerPrefs.SetString(Prefs.Email, email);
|
||
PlayerPrefs.DeleteKey(Prefs.LegacyPassword);
|
||
PlayerPrefs.Save();
|
||
}
|
||
|
||
// ---------- 註冊 ----------
|
||
|
||
void ShowRegister()
|
||
{
|
||
Panel("註冊帳號");
|
||
var email = Field("電子信箱", "輸入你的信箱", InputField.ContentType.EmailAddress);
|
||
var id = Field("ID", "自訂一個獨一無二 ID");
|
||
var pw = Field("密碼", "至少 6 位,包含英文與數字", InputField.ContentType.Password);
|
||
var pw2 = Field("確認密碼", "再輸入一次密碼", InputField.ContentType.Password);
|
||
Action("送出", () => App.StartCoroutine(Register(email.text.Trim(), id.text.Trim(), pw.text, pw2.text)), primary: true);
|
||
Action("已有帳號,登入", ShowLogin);
|
||
}
|
||
|
||
IEnumerator Register(string email, string id, string password, string password2)
|
||
{
|
||
string problem =
|
||
!Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$") ? "請輸入有效的電子郵件地址" :
|
||
id.Length < 4 ? "ID 長度不足 4 位" :
|
||
id.Length > 30 ? "ID 最多 30 字" :
|
||
password.Length < 6 ? "密碼長度不足 6 位" :
|
||
!Regex.IsMatch(password, @"^(?=.*[a-zA-Z])(?=.*\d).+$") ? "密碼需包含英文與數字" :
|
||
password != password2 ? "兩次輸入密碼不一致" : null;
|
||
if (problem != null) { ShowError(problem); yield break; }
|
||
|
||
busy = true;
|
||
Api.Response r = null;
|
||
yield return Api.Game(new Dictionary<string, object> { ["request"] = "create_user", ["email"] = email, ["name"] = id, ["password"] = password }, x => r = x);
|
||
busy = false;
|
||
if (r.error != null)
|
||
{
|
||
ShowError(r.error == "email_exists" ? "信箱已被註冊" : r.error == "name_exists" ? "ID 已被使用" : "建立帳號失敗:" + r.error);
|
||
yield break;
|
||
}
|
||
SaveLogin(r.Int("uid"), r.Str("token"), email);
|
||
App.RefreshFooter();
|
||
yield return SendCode(email);
|
||
}
|
||
|
||
// ---------- 信箱驗證 ----------
|
||
|
||
IEnumerator SendCode(string email)
|
||
{
|
||
busy = true;
|
||
Api.Response r = null;
|
||
yield return Api.Verify(new Dictionary<string, object> { ["game"] = "lunchpad", ["email"] = email, ["code"] = "", ["password"] = "" }, x => r = x);
|
||
busy = false;
|
||
if (r.error != null && r.error != "too_many_requests")
|
||
{
|
||
ShowError("驗證信寄送失敗:" + r.error);
|
||
yield break;
|
||
}
|
||
ShowVerify(email);
|
||
if (r.error == "too_many_requests") ShowError("驗證信已寄出,請稍候再重新發送");
|
||
}
|
||
|
||
void ShowVerify(string email)
|
||
{
|
||
Panel("驗證信箱");
|
||
Ui.Label(body, $"驗證信已寄到 {email}\n請檢查收件匣或垃圾郵件,驗證碼 5 分鐘內有效", 36, Ui.Muted);
|
||
var code = Field("驗證碼", "6 位數字", InputField.ContentType.IntegerNumber);
|
||
code.characterLimit = 6;
|
||
Action("驗證", () => App.StartCoroutine(Verify(email, code.text.Trim())), primary: true);
|
||
Action("重新發送", () => App.StartCoroutine(SendCode(email)));
|
||
Action("稍後再驗證", ShowInfo);
|
||
}
|
||
|
||
IEnumerator Verify(string email, string code)
|
||
{
|
||
busy = true;
|
||
Api.Response r = null;
|
||
yield return Api.Verify(new Dictionary<string, object>
|
||
{
|
||
["game"] = "lunchpad", ["email"] = email, ["code"] = code, ["password"] = Credential(),
|
||
}, x => r = x);
|
||
busy = false;
|
||
if (r.error != null)
|
||
{
|
||
ShowError(r.error switch
|
||
{
|
||
"wrong_code" => "驗證碼錯誤",
|
||
"expired" => "驗證碼已過期,請重新發送",
|
||
"not_requested" => "請先發送驗證碼",
|
||
_ => "驗證失敗:" + r.error,
|
||
});
|
||
yield break;
|
||
}
|
||
Toast.Show("信箱驗證完成");
|
||
ShowInfo();
|
||
}
|
||
|
||
// 驗證用憑證:Token 優先,舊版才用密碼
|
||
static string Credential()
|
||
{
|
||
string token = PlayerPrefs.GetString(Prefs.Token, "");
|
||
return token != "" ? token : PlayerPrefs.GetString(Prefs.LegacyPassword, "");
|
||
}
|
||
|
||
// ---------- 個人資訊 ----------
|
||
|
||
void ShowInfo()
|
||
{
|
||
App.RefreshFooter();
|
||
Panel("個人資訊");
|
||
var info = Ui.Label(body, "讀取中…", 40, Ui.TextColor, TextAnchor.UpperLeft);
|
||
info.lineSpacing = 1.3f;
|
||
var verifyButton = Action("驗證信箱", () => App.StartCoroutine(SendCode(PlayerPrefs.GetString(Prefs.Email, ""))));
|
||
verifyButton.gameObject.SetActive(false);
|
||
Action("重新整理", ShowInfo);
|
||
Action("登出", LogOut);
|
||
App.StartCoroutine(LoadInfo(info, verifyButton));
|
||
}
|
||
|
||
IEnumerator LoadInfo(Text info, Button verifyButton)
|
||
{
|
||
string token = PlayerPrefs.GetString(Prefs.Token, "");
|
||
string password = PlayerPrefs.GetString(Prefs.LegacyPassword, "");
|
||
Api.Response r = null;
|
||
yield return Api.Game(new Dictionary<string, object>
|
||
{
|
||
["request"] = "person_info", ["uid"] = Prefs.UserId, ["token"] = token,
|
||
["password"] = token == "" ? password : null,
|
||
}, x => r = x);
|
||
if (info == null) yield break; // 已經換面板
|
||
|
||
if (r.error != null)
|
||
{
|
||
info.text = r.error == "auth_failed" ? "登入已過期,請重新登入" : "載入失敗:" + r.error;
|
||
if (r.error == "auth_failed") ClearLogin();
|
||
yield break;
|
||
}
|
||
|
||
PlayerPrefs.SetString(Prefs.Name, r.Str("name"));
|
||
PlayerPrefs.SetString(Prefs.CreateTime, r.Str("create_time"));
|
||
PlayerPrefs.SetString(Prefs.AccountType, r.Str("account_type"));
|
||
PlayerPrefs.SetString(Prefs.Email, r.Str("email"));
|
||
PlayerPrefs.SetInt(Prefs.EmailVerified, r.Int("email_v"));
|
||
PlayerPrefs.Save();
|
||
App.RefreshFooter();
|
||
|
||
// 舊版只存了密碼:重新登入換取 Token,並刪除手機上的密碼
|
||
if (token == "" && password != "")
|
||
{
|
||
Api.Response login = null;
|
||
yield return Api.Game(new Dictionary<string, object> { ["request"] = "login", ["name"] = r.Str("name"), ["password"] = password }, x => login = x);
|
||
if (login.error == null && login.Str("token") != "")
|
||
SaveLogin(Prefs.UserId, login.Str("token"), r.Str("email"));
|
||
}
|
||
|
||
string type = r.Str("account_type") switch { "R" => "玩家", "A" => "Alpha 測試", "B" => "Beta 測試", "D" => "開發者", var t => t };
|
||
bool verified = r.Int("email_v") == 1;
|
||
info.text =
|
||
$"使用者號碼 {Prefs.UidText()}\n" +
|
||
$"顯示名稱 {r.Str("name")}\n" +
|
||
$"帳號類型 {type}\n" +
|
||
$"建立日期 {r.Str("create_time")}\n" +
|
||
$"電子信箱 {MaskEmail(r.Str("email"))} {(verified ? "<color=#7DD67D>已驗證</color>" : "<color=#FF6060>未驗證</color>")}";
|
||
verifyButton.gameObject.SetActive(!verified);
|
||
}
|
||
|
||
static string MaskEmail(string email)
|
||
{
|
||
int at = email.IndexOf('@');
|
||
return at < 0 ? email : email.Substring(0, Mathf.Min(2, at)) + "*****" + email.Substring(at);
|
||
}
|
||
|
||
void LogOut()
|
||
{
|
||
string token = PlayerPrefs.GetString(Prefs.Token, "");
|
||
if (token != "")
|
||
App.StartCoroutine(Api.Game(new Dictionary<string, object> { ["request"] = "logout", ["token"] = token }, _ => { }));
|
||
ClearLogin();
|
||
ShowLogin();
|
||
}
|
||
|
||
void ClearLogin()
|
||
{
|
||
foreach (var k in new[] { Prefs.Token, Prefs.Uid, Prefs.LegacyPassword, Prefs.Name, Prefs.AccountType, Prefs.CreateTime, Prefs.EmailVerified })
|
||
PlayerPrefs.DeleteKey(k);
|
||
PlayerPrefs.Save();
|
||
App.RefreshFooter();
|
||
}
|
||
|
||
public override void Back() => App.Go(new MainPage());
|
||
}
|