This commit is contained in:
2026-05-06 01:32:09 +05:30
parent 184d4fbc04
commit c2144630fd
10 changed files with 1675 additions and 1605 deletions
+68 -10
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
@@ -23,6 +24,10 @@ public static class Cupid
public static bool isInitialized => settings != null;
/// <summary>True once <see cref="Init"/> has built a matchmaker base URI and stored a bearer token (required for <c>/settings</c>).</summary>
public static bool IsEndpointReady =>
!string.IsNullOrEmpty(cupidBaseUri) && !string.IsNullOrEmpty(bearerToken);
public static int RoomPort = -1;
@@ -74,16 +79,7 @@ public static class Cupid
return;
}
if (body.Trim() == "403 Unauthorized")
{
Logger.Log("Cupid settings: wrong or missing password query (server rejected /settings).");
settings = null;
return;
}
settings = JsonUtility.FromJson<CupidSettings>(body);
if (settings == null) { throw new NullReferenceException(); }
Logger.Log("Cupid init success");
ApplySettingsJsonBody(body, logSuccess: true);
}
catch (Exception e)
{
@@ -94,6 +90,60 @@ public static class Cupid
}
}
/// <summary>
/// Re-fetches GET <c>/settings</c> (e.g. each time Main Menu is shown). Safe to call from a coroutine; no-op if not <see cref="IsEndpointReady"/>.
/// </summary>
public static IEnumerator CoRefreshSettings()
{
if (!IsEndpointReady)
yield break;
string settingsUrl = CupidURI + "/settings?password=" + UnityWebRequest.EscapeURL(settingsPassword ?? "");
using (UnityWebRequest www = UnityWebRequest.Get(settingsUrl))
{
www.SetRequestHeader("Authorization", "Bearer " + bearerToken);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Logger.Log("Cupid settings refresh failed: " + www.error);
Logger.Log(www.downloadHandler != null ? www.downloadHandler.text : "");
yield break;
}
string body = www.downloadHandler != null ? www.downloadHandler.text : "";
ApplySettingsJsonBody(body, logSuccess: true);
}
}
static void ApplySettingsJsonBody(string body, bool logSuccess)
{
settings = null;
if (string.IsNullOrWhiteSpace(body))
return;
if (body.Trim() == "403 Unauthorized")
{
Logger.Log("Cupid settings: wrong or missing password query (server rejected /settings).");
return;
}
try
{
var parsed = JsonUtility.FromJson<CupidSettings>(body);
if (parsed == null)
throw new NullReferenceException(nameof(CupidSettings));
settings = parsed;
if (logSuccess)
Logger.Log("Cupid settings updated");
}
catch (Exception e)
{
Logger.Log("Cupid settings JSON error: " + e.Message + " body=" + body);
settings = null;
}
}
/// <summary>
/// Plain host/IP + HTTP port, or an absolute URL (https://host with no port uses 443).
/// Sets <paramref name="hostOnly"/> for game client transport; <paramref name="baseUri"/> has no trailing slash.
@@ -212,6 +262,14 @@ public class CupidSettings
public int minimum_players;
public int maximum_players;
public int waiting_time;
public int port_range_min;
public int port_range_max;
/// <summary>RC stake per player for the default match (<c>/settings</c>), in coin units (same as matchmaker <c>entry_fee</c>).</summary>
public int entry_fee;
/// <summary>Optional side-bet or table minimum from matchmaker settings; same units as <see cref="entry_fee"/>.</summary>
public int bet_fee;
/// <summary>Winner net RC prize from settings, coin units (same as room <c>rc_prize</c>).</summary>
public int rc_prize;
}
[System.Serializable]