ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Net/AccountPanel.cs

247 lines
17 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 System;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame.Net
{
/// <summary>
/// 옵션 팝업의 계정 칸 — 서버 모드에서만 열린다(로비 「옵션」 버튼).
/// · 소셜 계정으로 로그인한 유저 = 「로그아웃」 → 타이틀의 로그인 화면으로 · 「계정 탈퇴」 → 복구할 수 없고 유예 일수(서버 값) 뒤 완전 삭제된다는 안내를 보고
/// 신청한다(로그아웃됨 · 유예 안에 다시 로그인하면 취소할 수 있다).
/// · 게스트 = 로그아웃 대신 「계정 삭제」(확인 문구 「계정 삭제」를 입력해야 한다 → 데이터 삭제 · 로그인 상태 초기화) + 「소셜 계정 연동」.
/// · 연동하려는 소셜 계정이 이미 다른 게임 계정에 연동돼 있으면 「어느 계정으로 할지」 고르는 팝업(계정 요약 · 데이터가 사라질 수 있다는 안내).
/// (PD 2026-11-17)
/// </summary>
public static class AccountPanel
{
/// <summary>유저가 입력해야 하는 확인 문구.</summary>
public const string DeletePhrase = "계정 삭제";
// 서버의 선택 값(연동할 소셜 계정이 이미 다른 계정에 연동돼 있을 때)
public const string KeepCurrent = "keep_current";
public const string UseExisting = "use_existing";
const string AlreadyLinked = "OAUTH_ALREADY_LINKED";
const float Width = 860f;
const float CardH = 430f;
const float CardGap = 16f;
public static bool Available
{
get { return ServerSync.Instance != null; }
}
public static void Open(OutGameRoot root)
{
var sync = ServerSync.Instance;
if (sync == null) return;
var content = root.ModalCustom("옵션", 1180f, new OutGameRoot.ModalButton("닫기", UiKit.Dark, null));
var status = UiKit.Label(content, "계정 정보를 불러오는 중…", 32, UiKit.InkSoft, TextAnchor.UpperLeft, new Vector2(0f, 1f), new Vector2(0f, 1f), new Vector2(8f, 0f), new Vector2(Width - 16f, 220f), false);
status.horizontalOverflow = HorizontalWrapMode.Wrap;
sync.AccountInfo(r =>
{
if (content == null) return; // 받는 사이에 팝업을 닫았다
if (!r.ok)
{
status.text = "계정 정보를 받지 못했습니다 (" + r.error + ")";
return;
}
Fill(root, sync, content, status, r);
});
}
static void Fill(OutGameRoot root, ServerSync sync, RectTransform content, Text status, ServerResult info)
{
bool guest = info.Str("accountType") == ServerClient.MethodGuest;
string provider = info.Str("provider");
string login = guest ? "게스트" : SocialSignIn.Label(provider) + (info.Has("socialName") ? " (" + info.Str("socialName") + ")" : "");
status.text = "닉네임 " + info.Str("nickname") + "\n로그인 계정 " + login + "\n유저 번호 " + info.Str("userId");
var top = new Vector2(0.5f, 1f);
float y = -250f;
if (!guest)
{
UiKit.Label(content, "로그아웃하면 타이틀의 로그인 화면으로 돌아갑니다. 같은 소셜 계정으로 다시 로그인하면 이어서 플레이합니다.", 26, UiKit.InkSoft, TextAnchor.UpperLeft, top, top, new Vector2(0f, y), new Vector2(Width - 16f, 80f), false)
.horizontalOverflow = HorizontalWrapMode.Wrap;
UiKit.Btn("Logout", content, "로그아웃", 36, UiKit.Dark, UiKit.White, top, top, new Vector2(0f, y - 100f), new Vector2(560f, 110f), () =>
root.Modal("로그아웃", "로그아웃할까요?\n타이틀의 로그인 화면으로 돌아갑니다.",
new OutGameRoot.ModalButton("로그아웃", UiKit.Red, () => sync.Logout(why => root.Toast("로그아웃하지 못했습니다 (" + why + ")"))),
new OutGameRoot.ModalButton("취소", UiKit.Dark, null)));
// 소셜 계정 — 계정 삭제 대신 계정 탈퇴(유예 일수 = 서버 값 · 값이 없으면 서버가 탈퇴를 받지 않는다)
int days = (int)info.Num("withdrawGraceDays");
if (days > 0)
{
UiKit.Label(content, WithdrawNotice(days), 26, UiKit.InkSoft, TextAnchor.UpperLeft, top, top, new Vector2(0f, y - 250f), new Vector2(Width - 16f, 120f), false)
.horizontalOverflow = HorizontalWrapMode.Wrap;
UiKit.Btn("Withdraw", content, "계정 탈퇴", 34, UiKit.Red, UiKit.White, top, top, new Vector2(0f, y - 390f), new Vector2(560f, 100f), () => ConfirmWithdraw(root, sync, days));
}
return;
}
// 게스트 — 소셜 계정 연동
UiKit.Label(content, "게스트 계정은 이 기기에서만 이어 할 수 있습니다. 소셜 계정에 연동하면 다른 기기에서도 그 계정으로 로그인해 이어 할 수 있습니다.", 26, UiKit.InkSoft, TextAnchor.UpperLeft, top, top, new Vector2(0f, y), new Vector2(Width - 16f, 80f), false)
.horizontalOverflow = HorizontalWrapMode.Wrap;
y -= 100f;
InputField devId = null;
bool google = SocialSignIn.Available(SocialSignIn.Google), apple = SocialSignIn.Available(SocialSignIn.Apple);
if (SocialSignIn.DevMode && (google || apple))
{
devId = LoginPanel.Field(content, "DevId", "개발용 — 시험용 소셜 계정 번호", top, new Vector2(0f, y), new Vector2(560f, 84f), SocialSignIn.DevIdMax);
devId.text = sync.Client.LastSocialId;
y -= 104f;
}
if (google)
{
UiKit.Btn("LinkGoogle", content, "Google Play 계정 연동", 34, UiKit.Jade, UiKit.White, top, top, new Vector2(0f, y), new Vector2(560f, 100f), () => Link(root, sync, SocialSignIn.Google, devId != null ? devId.text : "", null));
y -= 116f;
}
if (apple)
{
UiKit.Btn("LinkApple", content, "Game Center 계정 연동", 34, UiKit.Jade, UiKit.White, top, top, new Vector2(0f, y), new Vector2(560f, 100f), () => Link(root, sync, SocialSignIn.Apple, devId != null ? devId.text : "", null));
y -= 116f;
}
if (!google && !apple)
{
UiKit.Label(content, "소셜 계정 연동은 준비 중입니다.", 28, UiKit.InkSoft, TextAnchor.MiddleCenter, top, top, new Vector2(0f, y), new Vector2(Width - 16f, 60f), false);
y -= 80f;
}
// 게스트 — 로그아웃 대신 계정 삭제
UiKit.Label(content, "게스트 계정은 로그아웃할 수 없습니다(다시 들어올 방법이 없습니다). 계정을 삭제하면 모든 데이터가 지워지고 되돌릴 수 없습니다.", 26, UiKit.InkSoft, TextAnchor.UpperLeft, top, top, new Vector2(0f, y - 20f), new Vector2(Width - 16f, 80f), false)
.horizontalOverflow = HorizontalWrapMode.Wrap;
UiKit.Btn("Delete", content, "계정 삭제", 34, UiKit.Red, UiKit.White, top, top, new Vector2(0f, y - 120f), new Vector2(560f, 100f), () => ConfirmDelete(root, sync));
}
/// <summary>계정 탈퇴 안내 글 — 복구할 수 없다 · 유예 일수 뒤 완전 삭제 · 그 안에 취소할 수 있다.</summary>
public static string WithdrawNotice(int days)
{
return "계정을 탈퇴하면 복구할 수 없으며, " + days + "일 뒤 계정과 모든 데이터가 완전히 삭제됩니다. " + days + "일 안에 같은 소셜 계정으로 다시 로그인하면 언제든 탈퇴를 취소할 수 있습니다.";
}
/// <summary>계정 탈퇴 확인(소셜 계정) — 안내를 보고 「탈퇴」를 눌러야 신청한다.</summary>
public static void ConfirmWithdraw(OutGameRoot root, ServerSync sync, int days)
{
root.Modal("계정 탈퇴", WithdrawNotice(days) + "\n\n탈퇴를 신청할까요?",
new OutGameRoot.ModalButton("탈퇴", UiKit.Red, () => sync.Withdraw(why => root.Toast("계정 탈퇴를 신청하지 못했습니다 (" + why + ")"))),
new OutGameRoot.ModalButton("취소", UiKit.Dark, null));
}
/// <summary>계정 삭제 확인 — 유저가 「계정 삭제」를 입력해야 삭제 버튼이 통한다.</summary>
public static void ConfirmDelete(OutGameRoot root, ServerSync sync)
{
InputField input = null;
var content = root.ModalCustom("계정 삭제", 760f,
new OutGameRoot.ModalButton("삭제", UiKit.Red, () =>
{
if (input == null || !Typed(input.text))
{
root.Toast("「" + DeletePhrase + "」를 정확히 입력해 주세요");
return;
}
root.CloseModal();
sync.DeleteAccount(why => root.Toast("계정을 삭제하지 못했습니다 (" + why + ")"));
}, true),
new OutGameRoot.ModalButton("취소", UiKit.Dark, null));
var top = new Vector2(0.5f, 1f);
UiKit.Label(content, "계정을 삭제하면 영웅 · 재화 · 진행도 등 모든 데이터가 지워지고 되돌릴 수 없습니다.\n삭제하려면 아래 칸에 「" + DeletePhrase + "」를 입력해 주세요.", 30, UiKit.Ink, TextAnchor.UpperCenter, top, top, new Vector2(0f, -10f), new Vector2(Width - 40f, 220f), false)
.horizontalOverflow = HorizontalWrapMode.Wrap;
input = LoginPanel.Field(content, "Phrase", DeletePhrase, top, new Vector2(0f, -250f), new Vector2(560f, 90f), 20);
}
/// <summary>입력한 글이 확인 문구와 같은가(앞뒤 빈칸은 봐준다).</summary>
public static bool Typed(string text)
{
return (text ?? "").Trim() == DeletePhrase;
}
/// <summary>소셜 계정 연동 — 이미 다른 게임 계정에 연동된 소셜 계정이면 계정 고르기 팝업을 띄운다.</summary>
public static void Link(OutGameRoot root, ServerSync sync, string method, string socialId, string resolve)
{
sync.Link(method, socialId, resolve, r =>
{
if (r.ok)
{
root.Toast(r.Str("result") == "switched" ? "기존 연동 계정으로 들어갑니다" : SocialSignIn.Label(method) + " 계정에 연동했습니다");
if (r.Str("result") != "switched") Open(root);
return;
}
if (r.status == 409 && r.Message == AlreadyLinked)
{
Choose(root, sync, method, socialId, r);
return;
}
root.Toast("연동하지 못했습니다 (" + (string.IsNullOrEmpty(r.Message) ? r.error : r.Message) + ")");
});
}
/// <summary>
/// 계정 고르기 — 고른 소셜 계정이 이미 다른 게임 계정에 연동돼 있다. 어느 계정으로 플레이할지 유저가 고른다.
/// · 지금 계정으로 연동 = 기존에 연동돼 있던 계정은 연동이 해제되고, 그 계정의 데이터는 사라질 수 있다.
/// · 기존 연동 계정으로 = 지금 플레이 중인 게스트 계정의 데이터는 사라질 수 있다.
/// </summary>
static void Choose(OutGameRoot root, ServerSync sync, string method, string socialId, ServerResult conflict)
{
string name = SocialSignIn.Label(method);
var content = root.ModalCustom("계정 선택", 1320f, new OutGameRoot.ModalButton("취소", UiKit.Dark, null));
// 내용 칸 높이 = 1320 − 290 = 1030 — 안내 110 + 카드 2장(CardH) + 사이 CardGap 이 그 안에 들어간다
var top = new Vector2(0.5f, 1f);
UiKit.Label(content, "이 " + name + " 계정은 이미 다른 게임 계정에 연동되어 있습니다.\n어느 계정으로 플레이할지 골라 주세요.", 30, UiKit.Ink, TextAnchor.UpperCenter, top, top, new Vector2(0f, -4f), new Vector2(Width - 20f, 110f), false)
.horizontalOverflow = HorizontalWrapMode.Wrap;
Card(content, -124f, "지금 플레이 중인 계정 (게스트)", conflict.At("current"),
"이 계정을 " + name + " 계정에 연동합니다.\n<color=#FF8A7A>기존에 연동돼 있던 계정은 연동이 해제되며, 그 계정의 데이터는 사라질 수 있습니다.</color>",
"이 계정으로 연동", () => Confirm(root, "기존에 연동돼 있던 계정은 연동이 해제되고 다시 들어갈 수 없습니다.\n지금 계정으로 연동할까요?", () => Link(root, sync, method, socialId, KeepCurrent)));
Card(content, -124f - CardH - CardGap, "기존에 연동된 계정", conflict.At("existing"),
"기존에 연동된 계정으로 로그인합니다.\n<color=#FF8A7A>지금 플레이 중인 게스트 계정의 데이터는 사라질 수 있습니다.</color>",
"기존 계정으로 로그인", () => Confirm(root, "지금 플레이 중인 게스트 계정에는 다시 들어갈 수 없습니다.\n기존에 연동된 계정으로 로그인할까요?", () => Link(root, sync, method, socialId, UseExisting)));
}
static void Confirm(OutGameRoot root, string body, Action yes)
{
root.Modal("확인", body, new OutGameRoot.ModalButton("진행", UiKit.Red, yes), new OutGameRoot.ModalButton("취소", UiKit.Dark, null));
}
/// <summary>계정 요약 카드 — 닉네임 · 가입일 · 마지막 접속 · 진행도(마지막으로 깬 스테이지 · 영웅 수) + 고르는 버튼.</summary>
static void Card(RectTransform content, float y, string title, JToken account, string warn, string button, Action onClick)
{
var top = new Vector2(0.5f, 1f);
var left = new Vector2(0f, 1f);
var box = UiKit.Box("Card", content, top, top, new Vector2(0f, y), new Vector2(Width - 10f, CardH), UiKit.Paper); // 우편함 줄과 같은 바탕(글자 = 밝은 색)
UiKit.Border(box, UiKit.Line, 2f);
var t = box.transform;
UiKit.Label(t, title, 32, UiKit.Ink, TextAnchor.MiddleLeft, left, left, new Vector2(24f, -14f), new Vector2(Width - 60f, 46f));
UiKit.Label(t, Summary(account), 26, UiKit.InkSoft, TextAnchor.UpperLeft, left, left, new Vector2(24f, -62f), new Vector2(Width - 60f, 120f), false).horizontalOverflow = HorizontalWrapMode.Wrap;
var w = UiKit.Label(t, warn, 24, UiKit.Ink, TextAnchor.UpperLeft, left, left, new Vector2(24f, -190f), new Vector2(Width - 60f, 110f), false);
w.horizontalOverflow = HorizontalWrapMode.Wrap;
w.supportRichText = true;
UiKit.Btn("Pick", t, button, 32, UiKit.Jade, UiKit.White, new Vector2(0.5f, 0f), new Vector2(0.5f, 0f), new Vector2(0f, 18f), new Vector2(520f, 96f), onClick);
}
/// <summary>연동 응답 안의 계정 요약 글 — which = current(지금 계정) · existing(기존에 연동된 계정).</summary>
public static string SummaryOf(ServerResult conflict, string which)
{
return Summary(conflict.At(which));
}
/// <summary>계정 요약 글(서버가 준 값만 — 없는 값은 줄을 뺀다).</summary>
internal static string Summary(JToken account)
{
var o = account as JObject;
if (o == null) return "계정 정보를 받지 못했습니다";
string text = "닉네임 " + (string)o["nickname"];
text += "\n가입 " + Day((string)o["createdAt"]) + " 마지막 접속 " + Day((string)o["lastLoginAt"]);
var s = o["summary"] as JObject;
if (s == null) return text + "\n아직 게임을 시작하지 않은 계정";
var last = s["lastStage"];
text += "\n진행 " + (last == null || last.Type == JTokenType.Null ? "깬 스테이지 없음" : "스테이지 " + (string)last + " 까지 (" + (int)(s["clearedStages"] ?? 0) + "곳)");
text += " 영웅 " + (int)(s["heroes"] ?? 0) + "명";
return text;
}
static string Day(string iso)
{
DateTime at;
if (string.IsNullOrEmpty(iso) || !DateTime.TryParse(iso, null, System.Globalization.DateTimeStyles.RoundtripKind, out at)) return "-";
return at.ToLocalTime().ToString("yyyy-MM-dd");
}
}
}