new API and rematch WIP

This commit is contained in:
2026-05-04 17:48:09 +05:30
parent fb0d386ba1
commit 82287d74fb
17 changed files with 5534 additions and 1258 deletions
+44 -1
View File
@@ -142,13 +142,30 @@ public static class Cupid
}
/// <summary>Matchmaker GET / body: <c>"0"</c> while waiting; otherwise a JSON room object.</summary>
public static CupidRoom? ParseRoom(string data)
public static CupidRoom? ParseRoom(string data) => ParseRoom(data, 0, null);
/// <inheritdoc cref="ParseRoom(string)"/>
/// <param name="currentUserId">Used to pick <c>for_red</c> vs <c>for_blue</c> when both list the same players.</param>
/// <param name="teamDisambiguation">Optional hint (e.g. <see cref="GameManager.MyTeam"/>) when the roster is duplicated on both sides.</param>
public static CupidRoom? ParseRoom(string data, int currentUserId, Team? teamDisambiguation)
{
if (string.IsNullOrWhiteSpace(data))
return null;
string t = data.Trim();
if (t == "0")
return null;
if (t.IndexOf("\"for_red\"", StringComparison.Ordinal) >= 0)
{
MatchmadeResponse env = JsonUtility.FromJson<MatchmadeResponse>(t);
if (env == null || !env.ok || !MatchmadeResponse.IsMatchReady(env))
return null;
MatchmadeTeamPayload branch = MatchmadeResponse.SelectTeamPayload(env, currentUserId, teamDisambiguation);
if (branch == null || branch.Port <= 0)
return null;
return ToCupidRoom(branch);
}
try
{
CupidRoom room = JsonUtility.FromJson<CupidRoom>(t);
@@ -161,6 +178,32 @@ public static class Cupid
return null;
}
}
static CupidRoom ToCupidRoom(MatchmadeTeamPayload p)
{
var room = new CupidRoom
{
GameName = p.GameName ?? "",
Port = p.Port,
InitTime = p.InitTime,
match_id = p.match_id,
your_team = p.your_team ?? ""
};
if (p.Players != null)
{
room.Players = new CupidQueueEntry[p.Players.Length];
for (int i = 0; i < p.Players.Length; i++)
{
room.Players[i] = new CupidQueueEntry
{
Name = p.Players[i].Name,
LastSeen = p.Players[i].LastSeen,
UserId = p.Players[i].UserId
};
}
}
return room;
}
}
[System.Serializable]
+23 -13
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
@@ -102,7 +101,12 @@ public class CupidLobby : MonoBehaviour
continue;
}
CupidRoom? room = Cupid.ParseRoom(text);
Team? teamHint = LoginManager.MatchYourTeam;
if (!teamHint.HasValue && GameManager.instance != null)
teamHint = GameManager.MyTeam;
int userId = LoginManager.CurrentUser != null ? LoginManager.CurrentUser.id : 0;
CupidRoom? room = Cupid.ParseRoom(text, userId, teamHint);
if (room == null)
{
if (text != null && text.Trim() != "0")
@@ -113,29 +117,35 @@ public class CupidLobby : MonoBehaviour
CupidRoom _room = (CupidRoom)room;
Logger.Log("Got into a room");
Logger.Log(text);
MatchmadeResponse matchmadeResponse = JsonUtility.FromJson<MatchmadeResponse>(text);
if(matchmadeResponse.Players.Length < 2){
Debug.Log("Waiting for the other player to show up");
MatchmadeResponse env = JsonUtility.FromJson<MatchmadeResponse>(text);
MatchmadeTeamPayload branch = MatchmadeResponse.SelectTeamPayload(env, userId, teamHint);
if (branch == null || !MatchmadeResponse.TryResolveMyAndOpponent(env, branch, userId, out MatchmadePlayer myPlayer, out MatchmadePlayer opponentPlayer))
{
Debug.Log("Waiting for the other player to show up (could not resolve roster)");
yield return new WaitForSeconds(1);
continue;
}
Team myTeam = matchmadeResponse.your_team == "red" ? Team.Red : Team.Blue;
MatchmadePlayer myPlayer = matchmadeResponse.Players.First(p => p.UserId == LoginManager.CurrentUser.id);
MatchmadePlayer opponentPlayer = matchmadeResponse.Players.First(p => p.UserId != LoginManager.CurrentUser.id);
string yt = (branch.your_team ?? "").Trim().ToLowerInvariant();
Team myTeam = yt == "blue" ? Team.Blue : Team.Red;
if(myTeam==Team.Red){
if (myTeam == Team.Red)
{
GameManager.RedPlayer = myPlayer;
GameManager.BluePlayer = opponentPlayer;
}else{
}
else
{
GameManager.BluePlayer = myPlayer;
GameManager.RedPlayer = opponentPlayer;
}
GameManager.MyTeam = myTeam;
// LevelLoadManager.instance.SetupMatchMade(myTeam, myPlayer.Name, opponentPlayer.Name, $"L10 {myPlayer.l10_wins}-{myPlayer.l10_losses}", $"L10 {opponentPlayer.l10_wins}-{opponentPlayer.l10_losses}");
LevelLoadManager.instance.SetupMatchMade(myTeam, myPlayer, opponentPlayer);
GameManager.ConfigureDedicatedMatchReporting(matchmadeResponse.match_id);
int rcPrizeCoins = env.rc_prize > 0 ? env.rc_prize : branch.rc_prize;
GameManager.MatchRcPrizeCoins = rcPrizeCoins;
LevelLoadManager.instance.SetupMatchMade(myTeam, myPlayer, opponentPlayer, CoinHelper.FormatString(rcPrizeCoins) + " RC");
GameManager.ConfigureDedicatedMatchReporting(branch.match_id);
Logger.Log("Setting cupid to load game scene");
Cupid.RoomPort = _room.Port;
LoginManager.SetMatchYourTeam(_room.your_team);