using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using BackEnd;
using BackEnd.Tcp;
using LitJson;
public class CombatManager : MonoBehaviour
{
[SerializeField]
private CharacterPage characterPage;
[SerializeField]
private Canvas canvas;
[SerializeField]
private GameObject combatBackground;
[SerializeField]
private Text text_Matching;
private string currentGameRoomToken;
private void Start()
{
Backend.Match.OnJoinMatchMakingServer = OnJoinMatchMakingServer;
Backend.Match.OnMatchMakingRoomCreate = OnMatchMakingRoomCreate;
Backend.Match.OnMatchMakingResponse = OnMatchMakingResponse;
Backend.Match.OnMatchMakingRoomDestory = OnMatchMakingRoomDestory;
Backend.Match.OnSessionJoinInServer = OnSessionJoinInServer;
Backend.Match.OnMatchInGameStart = OnMatchInGameStart;
//게임 시작 시 미리 매치메이킹 서버에 접속해 두고 계속 접속
//매칭 서버는 계속 접속한 상태로 놔둘 예정.
JoinMatchMakingServer();
}
private void Update()
{
Backend.Match.Poll();
}
//결투 입창 버튼에 이벤트로 연결, 클릭 시 접속 시도.
public void EnterCombat()
{
canvas.sortingOrder = 10;
text_Matching.gameObject.SetActive(true);
combatBackground.SetActive(true);
GlobalManager.Instance.PlayBgm(SOUND.Combat);
BackendReturnObject gameRoomBro = Backend.Match.IsGameRoomActivate();
if (!gameRoomBro.IsSuccess())
{
if(gameRoomBro.GetStatusCode().Equals("404") && gameRoomBro.GetErrorCode().Contains("NotFound"))
{
Backend.Match.CreateMatchRoom();
return;
}
GlobalManager.Instance.CreateErrorPopup(gameRoomBro, Application.Quit);
return;
}
if (gameRoomBro.GetStatusCode().Equals("200"))
{
gameRoomBro.GetStatusCode();
Debug.Log(gameRoomBro.GetReturnValuetoJSON().ToJson());
//재접속 시도 기능 추가 필요, 우선 정보가 출력되는 지 확인만 함.
}
}
public void OnExitCombat()
{
Backend.Match.CancelMatchMaking();
}
private void OnMatchInGameStart()
{
//캐릭터 생성 및 게임 시작 준비.
print("게임 스타트");
}
private void OnSessionJoinInServer(JoinChannelEventArgs args)
{
if (args.ErrInfo.Category != ErrorCode.Success)
{
//에러 출력지점.
GlobalManager.Instance.CreatePopup($"인게임 서버 접속 콜백 -> [{args.ErrInfo.Category}/{args.ErrInfo.SocketErrorCode}] : {args.ErrInfo.Reason}", Application.Quit);
return;
}
Backend.Match.JoinGameRoom(currentGameRoomToken);
}
private void JoinInGameServer(MatchInGameRoomInfo roomInfo)
{
ErrorInfo errorInfo;
if (!Backend.Match.JoinGameServer(roomInfo.m_inGameServerEndPoint.m_address,
roomInfo.m_inGameServerEndPoint.m_port, true, out errorInfo))
{
GlobalManager.Instance.CreatePopup($"인게임 서버 : False -> [{errorInfo.Category}/{errorInfo.SocketErrorCode}] : {errorInfo.Reason}", Application.Quit);
return;
}
if (errorInfo.Category != ErrorCode.Success)
{
GlobalManager.Instance.CreatePopup($"인게임 서버 : True -> [{errorInfo.Category}/{errorInfo.SocketErrorCode}] : {errorInfo.Reason}", Application.Quit);
return;
}
currentGameRoomToken = roomInfo.m_inGameRoomToken;
}
private void OnMatchMakingRoomDestory(MatchMakingInteractionEventArgs args)
{
if (args.ErrInfo != ErrorCode.Success)
{
GlobalManager.Instance.CreatePopup($"매치 대기방 삭제 실패 -> [{args.ErrInfo}] : {args.Reason}", Application.Quit);
return;
}
canvas.sortingOrder = 30;
characterPage.gameObject.SetActive(true);
combatBackground.gameObject.SetActive(false);
}
private void OnMatchMakingResponse(MatchMakingResponseEventArgs args)
{
if (args.ErrInfo == ErrorCode.Success)
{
text_Matching.gameObject.SetActive(false);
JoinInGameServer(args.RoomInfo);
}
else if (args.ErrInfo == ErrorCode.Match_InProgress)
{
text_Matching.gameObject.SetActive(true);
}
else if (args.ErrInfo == ErrorCode.Match_MatchMakingCanceled)
{
Backend.Match.LeaveMatchRoom();
}
else
{
GlobalManager.Instance.CreatePopup($"매치 매칭 실패 -> [{args.ErrInfo}] : {args.Reason}", Application.Quit);
return;
}
}
private void OnMatchMakingRoomCreate(MatchMakingInteractionEventArgs args)
{
if (args.ErrInfo != ErrorCode.Success)
{
GlobalManager.Instance.CreatePopup($"매치 대기방 생성 실패 -> [{args.ErrInfo}] : {args.Reason}", Application.Quit);
return;
}
Backend.Match.RequestMatchMaking(MatchType.Random, MatchModeType.OneOnOne, "2022-06-07T02:27:14.774Z");
}
private void JoinMatchMakingServer()
{
ErrorInfo errorInfo;
if (!Backend.Match.JoinMatchMakingServer(out errorInfo))
{
GlobalManager.Instance.CreatePopup($"매치 서버 : False -> [{errorInfo.Category}/{errorInfo.SocketErrorCode}] : {errorInfo.Reason}", Application.Quit);
return;
}
if (errorInfo.Category != ErrorCode.Success)
{
GlobalManager.Instance.CreatePopup($"매치 서버 : True -> [{errorInfo.Category}/{errorInfo.SocketErrorCode}] : {errorInfo.Reason}", Application.Quit);
return;
}
}
private void OnJoinMatchMakingServer(JoinChannelEventArgs args)
{
if (args.ErrInfo.Category != ErrorCode.Success)
{
GlobalManager.Instance.CreatePopup($"매치 서버 접속 콜백 -> [{args.ErrInfo.Category}/{args.ErrInfo.SocketErrorCode}] : {args.ErrInfo.Reason}", Application.Quit);
return;
}
}
}
코트 첨부 하였습니다.
혹시 잘못 된 점 있으면 조언 부탁드립니다.