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
+153 -6
View File
@@ -1,8 +1,10 @@
using UnityEngine;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GameOverCanvas : MonoBehaviour
{
@@ -28,7 +30,7 @@ public class GameOverCanvas : MonoBehaviour
public Sprite redIcon, blueIcon;
public RectTransform p1_winner, p2_winner;
[Header("Rematch")]
[Header("Game Over Data")]
public Button btnRematch;
public Button btnLeave;
public GameObject rematchWarningLoser;
@@ -39,6 +41,17 @@ public class GameOverCanvas : MonoBehaviour
public float returnToMenuCountdown = 15f;
[Header("Rematch panel")]
public CanvasGroup rematchPanel;
public RectTransform rematchPopup;
public TMP_Text txtMyRCBalance, txtOpponentRCBalance;
public TMP_Text txtSelectedBet;
public Slider betSlider;
public Button btnConfirmBet;
public Button btnCancelBet;
public static GameOverCanvas instance;
void Awake()
@@ -64,11 +77,38 @@ public class GameOverCanvas : MonoBehaviour
btnRematch.onClick.AddListener(OnBtnRematchClicked);
btnLeave.onClick.AddListener(OnBtnLeaveGameClicked);
betSlider.onValueChanged.AddListener(OnBetSliderValueChanged);
}
void OnBetSliderValueChanged(float value){
txtSelectedBet.text = CoinHelper.FormatString((int)value);
}
void OnBtnRematchClicked(){
// LevelLoadManager.LoadLevel("Game");
NetPlayer.localPlayer.RequestRematch();
SetRematchPendingState(true);
}
public void OnRematchRequested(){
if(winningTeam == myTeam){
//Only i can initiate the rematch, so here we go
ShowRematchPanel();
}
}
void SetRematchPendingState(bool val){
btnRematch.interactable = false;
btnLeave.interactable = !val;
if(val){
StopCoroutine(countdownCoroutine);
}else{
countdownCoroutine = StartCoroutine(CoroutineCountdown());
}
}
void OnBtnLeaveGameClicked(){
LevelLoadManager.LoadLevel("MainMenu");
}
@@ -115,10 +155,17 @@ public class GameOverCanvas : MonoBehaviour
canvasGroup.alpha = 0;
canvasGroup.blocksRaycasts = false;
canvasGroup.interactable = false;
rematchPanel.alpha=0;
rematchPanel.blocksRaycasts = false;
rematchPanel.interactable = false;
}
Team winningTeam;
Team myTeam;
public void Setup(Team winningTeam, Team myTeam, int redScore, int blueScore){
this.winningTeam = winningTeam;
this.myTeam = myTeam;
p1_winner.gameObject.SetActive(false);
p2_winner.gameObject.SetActive(false);
if(myTeam == Team.Red){
@@ -184,10 +231,12 @@ public class GameOverCanvas : MonoBehaviour
rcPrize = 8.2f;
}
btnRematch.interactable = winningTeam == myTeam;
rematchWarningLoser.SetActive(winningTeam != myTeam);
btnRematch.interactable = winningTeam != myTeam;
rematchWarningLoser.SetActive(winningTeam == myTeam);
StartCoroutine(CoroutineShow());
StartCoroutine(CoroutineFetchAndUpdateBalances());
}
IEnumerator CoroutineShow(){
@@ -232,9 +281,9 @@ public class GameOverCanvas : MonoBehaviour
StartCoroutine(CoroutineCountdown());
countdownCoroutine = StartCoroutine(CoroutineCountdown());
}
Coroutine countdownCoroutine;
IEnumerator CoroutineSetTextNumber(float number, TMP_Text text, float speed = 100f, string formatting = "N0", string prefix = "", string suffix = ""){
float t=0;
@@ -260,4 +309,102 @@ public class GameOverCanvas : MonoBehaviour
LevelLoadManager.LoadLevel("MainMenu");
}
public void ShowRematchPanel(){
SetRematchPendingState(true);
StartCoroutine(CoroutineShowRematchPanel());
betSlider.maxValue = (LevelLoadManager.myPlayer.rc < LevelLoadManager.opponentPlayer.rc) ? LevelLoadManager.myPlayer.rc : LevelLoadManager.opponentPlayer.rc;
}
IEnumerator CoroutineShowRematchPanel(){
rematchPopup.localScale = new Vector3(0, 0, 0);
rematchPanel.blocksRaycasts = true;
rematchPanel.interactable = true;
rematchPanel.DOFade(1, 0.3f).SetEase(Ease.InOutBack);
yield return new WaitForSeconds(0.1f);
rematchPopup.DOScale(1, 0.2f).SetEase(Ease.OutBack);
yield return new WaitForSeconds(0.2f);
}
public void OnRematchCancelled(){
SetRematchPendingState(false);
StartCoroutine(CoroutineHideRematchPanel());
}
IEnumerator CoroutineHideRematchPanel(){
rematchPopup.DOScale(0, 0.2f).SetEase(Ease.InBack);
yield return new WaitForSeconds(0.2f);
rematchPanel.DOFade(0, 0.3f).SetEase(Ease.InOutBack);
yield return new WaitForSeconds(0.3f);
}
IEnumerator CoroutineFetchAndUpdateBalances()
{
var my = LevelLoadManager.myPlayer;
var opp = LevelLoadManager.opponentPlayer;
string baseUrl = (GameManager.DedicatedInternalApiBase ?? "").TrimEnd('/');
if (string.IsNullOrEmpty(baseUrl) || my == null || opp == null)
{
ApplyRcBalancesToUi(null);
yield break;
}
if (my.UserId > 0)
yield return FetchPlayerRcAndApply(baseUrl, my);
if (opp.UserId > 0 && opp.UserId != my.UserId)
yield return FetchPlayerRcAndApply(baseUrl, opp);
ApplyRcBalancesToUi(null);
}
void ApplyRcBalancesToUi(string loadingPlaceholder)
{
var my = LevelLoadManager.myPlayer;
var opp = LevelLoadManager.opponentPlayer;
string myText = loadingPlaceholder ?? (my != null ? CoinHelper.FormatString((int)my.rc) : "0.0 RC");
string oppText = loadingPlaceholder ?? (opp != null ? CoinHelper.FormatString((int)opp.rc) : "0.0 RC");
if (txtMyRCBalance != null)
txtMyRCBalance.text = myText;
if (txtOpponentRCBalance != null)
txtOpponentRCBalance.text = oppText;
}
IEnumerator FetchPlayerRcAndApply(string baseUrl, MatchmadePlayer player)
{
string url = $"{baseUrl}/players/{player.UserId}/rc";
using (var req = UnityWebRequest.Get(url))
{
yield return req.SendWebRequest();
if (req.responseCode != 200)
{
Logger.Log($"Player RC fetch HTTP {(int)req.responseCode}: {req.error} {url}");
yield break;
}
string text = req.downloadHandler != null ? req.downloadHandler.text : "";
if (string.IsNullOrEmpty(text))
yield break;
var resp = JsonUtility.FromJson<PlayerRcApiResponse>(text);
if (resp != null && resp.ok)
player.rc = resp.rc;
else if (resp != null && !string.IsNullOrEmpty(resp.error))
Logger.Log($"Player RC fetch: {resp.error} ({url})");
}
}
[Serializable]
class PlayerRcApiResponse
{
public bool ok;
public int player_id;
public float rc;
public string error;
}
}