226 lines
6.4 KiB
C#
226 lines
6.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using GameServer.Client;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
public class serverSelector : MonoBehaviour
|
|
{
|
|
private const string DefaultServerUrl = "http://47.112.187.172:8080";
|
|
private const float RefreshIntervalSeconds = 5f;
|
|
|
|
[Header("button")]
|
|
[SerializeField] private Button serverSelectorButton;
|
|
|
|
[Header("text")]
|
|
[SerializeField] private Text currentServerName;
|
|
[SerializeField] private Image currentServerStatus;
|
|
|
|
[Header("Server Status Sprites")]
|
|
[SerializeField] private Sprite serverOkaySprite;
|
|
[SerializeField] private Sprite serverBusySprite;
|
|
[SerializeField] private Sprite serverErrorSprite;
|
|
[SerializeField] private Sprite serverSupportingSprite;
|
|
[SerializeField] private Sprite userOfflineSprite;
|
|
|
|
[Header("objs")]
|
|
[SerializeField] private GameObject serverSelectorPanel;
|
|
[SerializeField] private Button shutSSPanel;
|
|
|
|
[Header("network")]
|
|
[SerializeField] private string fallbackServerUrl = DefaultServerUrl;
|
|
[SerializeField] private float requestTimeoutSeconds = 5f;
|
|
|
|
private Coroutine _pollCoroutine;
|
|
|
|
[Serializable]
|
|
private class ServerStatusResponse
|
|
{
|
|
public bool success;
|
|
public string server_state;
|
|
public bool maintenance_enabled;
|
|
public string maintenance_message;
|
|
public float requests_per_second;
|
|
public float busy_qps_threshold;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (serverSelectorButton != null)
|
|
{
|
|
serverSelectorButton.onClick.RemoveListener(OpenServerSelectorPanel);
|
|
serverSelectorButton.onClick.AddListener(OpenServerSelectorPanel);
|
|
}
|
|
|
|
if (shutSSPanel != null)
|
|
{
|
|
shutSSPanel.onClick.RemoveListener(CloseServerSelectorPanel);
|
|
shutSSPanel.onClick.AddListener(CloseServerSelectorPanel);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (serverSelectorPanel != null)
|
|
{
|
|
serverSelectorPanel.SetActive(false);
|
|
}
|
|
|
|
if (OnlineModeSettings.IsLocalOnlyMode)
|
|
{
|
|
ApplySprite(userOfflineSprite != null ? userOfflineSprite : serverErrorSprite);
|
|
return;
|
|
}
|
|
|
|
ApplySprite(userOfflineSprite != null ? userOfflineSprite : serverErrorSprite);
|
|
_pollCoroutine = StartCoroutine(StatusPollLoop());
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (_pollCoroutine == null && Application.isPlaying && !OnlineModeSettings.IsLocalOnlyMode)
|
|
{
|
|
_pollCoroutine = StartCoroutine(StatusPollLoop());
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (_pollCoroutine != null)
|
|
{
|
|
StopCoroutine(_pollCoroutine);
|
|
_pollCoroutine = null;
|
|
}
|
|
}
|
|
|
|
private IEnumerator StatusPollLoop()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return RefreshServerStatus();
|
|
yield return new WaitForSecondsRealtime(RefreshIntervalSeconds);
|
|
}
|
|
}
|
|
|
|
private IEnumerator RefreshServerStatus()
|
|
{
|
|
if (OnlineModeSettings.IsLocalOnlyMode)
|
|
{
|
|
ApplySprite(userOfflineSprite != null ? userOfflineSprite : serverErrorSprite);
|
|
yield break;
|
|
}
|
|
|
|
if (Application.internetReachability == NetworkReachability.NotReachable)
|
|
{
|
|
ApplySprite(userOfflineSprite);
|
|
yield break;
|
|
}
|
|
|
|
string statusUrl = BuildServerStatusUrl();
|
|
ApplySprite(serverSupportingSprite);
|
|
using (UnityWebRequest request = UnityWebRequest.Get(statusUrl))
|
|
{
|
|
request.timeout = Mathf.Max(1, Mathf.RoundToInt(requestTimeoutSeconds));
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
ApplySprite(serverErrorSprite);
|
|
yield break;
|
|
}
|
|
|
|
string responseText = request.downloadHandler != null ? request.downloadHandler.text : string.Empty;
|
|
if (string.IsNullOrWhiteSpace(responseText))
|
|
{
|
|
ApplySprite(serverErrorSprite);
|
|
yield break;
|
|
}
|
|
|
|
ServerStatusResponse response;
|
|
try
|
|
{
|
|
response = JsonUtility.FromJson<ServerStatusResponse>(responseText);
|
|
}
|
|
catch
|
|
{
|
|
ApplySprite(serverErrorSprite);
|
|
yield break;
|
|
}
|
|
|
|
if (response == null || !response.success)
|
|
{
|
|
ApplySprite(serverErrorSprite);
|
|
yield break;
|
|
}
|
|
|
|
switch ((response.server_state ?? string.Empty).Trim().ToUpperInvariant())
|
|
{
|
|
case "MAINTENANCE":
|
|
ApplySprite(serverSupportingSprite);
|
|
break;
|
|
case "BUSY":
|
|
ApplySprite(serverBusySprite);
|
|
break;
|
|
case "OK":
|
|
ApplySprite(serverOkaySprite);
|
|
break;
|
|
default:
|
|
ApplySprite(serverErrorSprite);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OpenServerSelectorPanel()
|
|
{
|
|
if (serverSelectorPanel != null)
|
|
{
|
|
serverSelectorPanel.SetActive(true);
|
|
}
|
|
|
|
if (!OnlineModeSettings.IsLocalOnlyMode)
|
|
{
|
|
StartCoroutine(RefreshServerStatus());
|
|
}
|
|
else
|
|
{
|
|
ApplySprite(userOfflineSprite != null ? userOfflineSprite : serverErrorSprite);
|
|
}
|
|
}
|
|
|
|
private void CloseServerSelectorPanel()
|
|
{
|
|
if (serverSelectorPanel != null)
|
|
{
|
|
serverSelectorPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private string ResolveServerBaseUrl()
|
|
{
|
|
NetworkManager manager = NetworkManager.Instance;
|
|
if (manager != null && !string.IsNullOrWhiteSpace(manager.ServerUrl))
|
|
{
|
|
return manager.ServerUrl.TrimEnd('/');
|
|
}
|
|
|
|
return string.IsNullOrWhiteSpace(fallbackServerUrl) ? DefaultServerUrl : fallbackServerUrl.TrimEnd('/');
|
|
}
|
|
|
|
private string BuildServerStatusUrl()
|
|
{
|
|
return $"{ResolveServerBaseUrl()}/api/server-status";
|
|
}
|
|
|
|
private void ApplySprite(Sprite sprite)
|
|
{
|
|
if (currentServerStatus == null || sprite == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
currentServerStatus.sprite = sprite;
|
|
}
|
|
}
|