-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHathoraMatchmaking.cs
174 lines (146 loc) · 6.67 KB
/
HathoraMatchmaking.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Created by [email protected]
using System;
using System.Threading.Tasks;
using Fusion;
using Fusion.Photon.Realtime;
using Hathora.Cloud.Sdk.Model;
using Hathora.Core.Scripts.Runtime.Common.Extensions;
using Newtonsoft.Json;
using TMPro;
using TPSBR;
using UnityEngine;
using UnityEngine.UI;
using HathoraRegion = Hathora.Cloud.Sdk.Model.Region;
using Assert = UnityEngine.Assertions.Assert;
using Debug = UnityEngine.Debug;
namespace HathoraPhoton
{
/// <summary>Early logic originally ported over from Photon's Networking.cs</summary>
public class HathoraMatchmaking : Matchmaking
{
#region Serialized
[Header("Pre-Create")]
[SerializeField]
private TextMeshProUGUI hathoraCreateStatusTxt;
[SerializeField]
private Button createLobbyBtn;
[Header("Post-Create")]
[SerializeField]
private TextMeshProUGUI hathoraCreateDoneStatusTxt;
[SerializeField]
private GameObject createSettingsModalPnl;
#endregion // Serialized
private static HathoraRegion HATHORA_FALLBACK_REGION => HathoraRegion.WashingtonDC;
private static HathoraPhotonClientMgr clientMgr => HathoraPhotonClientMgr.Singleton;
#region Base Overrides
protected override void OnAwake()
{
base.OnAwake();
Debug.Log("[HathoraMatchmaking] OnAwake");
}
/// <summary>Host only</summary>
/// <param name="_request"></param>
public override async void CreateSession(SessionRequest _request)
{
if (_request.GameMode != GameMode.Host)
{
// Continue as normal ->
base.CreateSession(_request);
return;
}
if (_request.GameMode == GameMode.Host)
{
// ##############################################################
// "Host" Acting as a Hathora Client:
//
// 1. Ensure authed; already via HathoraPhotonClientMgr.Awake()
// 2. Create Lobby (a Room with server browsing capabilities)
// 3. Wait for Lobby to be created
// 4. Hide the modal create settings window and show success status
//
// You should now be able to see the lobby within the list
// ##############################################################
await createHathoraSessionAsync(_request);
}
}
#endregion // Base Overrides
/// <summary>High-level, async Task wrapper for CreateSession</summary>
/// <param name="_request"></param>
private async Task createHathoraSessionAsync(SessionRequest _request)
{
// Photon bypasses the Button interactable check
if (!createLobbyBtn.interactable)
return;
string logPrefix = $"[HathoraMatchmaking.{nameof(createHathoraSessionAsync)}]";
Debug.Log($"{logPrefix} (as Photon 'Host')");
// Ensure authed; already via HathoraPhotonClientMgr.Awake()
if (!clientMgr.HathoraClientSession.IsAuthed)
{
Debug.Log($"{logPrefix} !IsAuthed");
return;
}
toggleCreateUi(_isCreating: true);
// Create Lobby (a Room with server browsing capabilities)
Lobby lobby = await photonHostCreateHathoraLobbyAsync(_request);
Assert.IsNotNull(lobby?.RoomId, $"{logPrefix} Expected Lobby?.RoomId");
// Wait for Lobby to be created
ConnectionInfoV2 connectionInfo = await clientMgr.GetActiveConnectionInfo(lobby.RoomId);
// IPAddress ip = await HathoraUtils.ConvertHostToIpAddress(connectionInfo.ExposedPort.Host);
// Hide the modal create settings window and show success status
onCreateLobbySuccessUI(lobby);
}
private void toggleCreateUi(bool _isCreating)
{
hathoraCreateStatusTxt.gameObject.SetActive(_isCreating);
createLobbyBtn.gameObject.SetActive(!_isCreating);
}
/// <summary>
/// Hide the modal create settings window and show success status
/// </summary>
private void onCreateLobbySuccessUI(Lobby _lobby)
{
Debug.Log($"[HathoraMatchmaking] onCreateLobbySuccessUI");
string friendlyHathoraRegion = _lobby.Region.ToString().SplitPascalCase();
hathoraCreateDoneStatusTxt.text = $"Created Lobby: {_lobby.RoomId} ({friendlyHathoraRegion})";
hathoraCreateDoneStatusTxt.gameObject.SetActive(true);
createSettingsModalPnl.SetActive(false);
toggleCreateUi(_isCreating: false);
}
/// <summary>
/// Creates a Hathora Lobby (extended Room) with the given SessionRequest as initConfig.
/// Photon Region is converted to the closest Hathora Region.
/// Awaits Lobby creation, then gets the connect info.
/// This could take a few moments (~5s).
/// </summary>
/// <param name="_request"></param>
/// <returns></returns>
private async Task<Lobby> photonHostCreateHathoraLobbyAsync(SessionRequest _request)
{
_request.UserID = Context.PlayerData.UserID;
_request.CustomLobby = GetLobbyName();
// Get the selected Photon Region -> Map to closest Hathora Region
// "Any" region falls back to WashingtonDC
string photonRegionStr = base.GetCurrentRegion(); // From top-left dropdown in `Menu` scene
HathoraRegion hathoraRegion = HathoraRegionMap.GetHathoraRegionEnumFromPhoton(
photonRegionStr); // alt: getHathoraRegionFromPhoton()
string initConfigJsonStr = JsonConvert.SerializeObject(_request);
Lobby lobby = await clientMgr.CreateLobbyAsync(
hathoraRegion,
CreateLobbyRequest.VisibilityEnum.Public,
initConfigJsonStr);
Assert.IsNotNull(lobby?.RoomId, "!lobby.RoomId");
return lobby;
}
// private static Region getHathoraRegionFromPhoton()
// {
// string photonRegionStr = PhotonAppSettings.Instance.AppSettings.FixedRegion;
// bool hasPhotonRegionStr = !string.IsNullOrEmpty(photonRegionStr);
//
// Region hathoraRegion = hasPhotonRegionStr
// ? (Region)HathoraRegionMap.GetHathoraRegionFromPhoton(photonRegionStr)
// : HATHORA_FALLBACK_REGION;
//
// return hathoraRegion;
// }
}
}