Skip to content

Instantly share code, notes, and snippets.

@Vagonn
Created August 6, 2020 05:07
Show Gist options
  • Save Vagonn/ea443d0d6aa996d9a4ffd9b5208cc530 to your computer and use it in GitHub Desktop.
Save Vagonn/ea443d0d6aa996d9a4ffd9b5208cc530 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Barebones.MasterServer;
using System.Security.Cryptography;
using System;
using TMPro;
[Serializable]
public class OnAccess : UnityEvent<string, int, string>
{
}
public class ConnectorToGame : MonoBehaviour
{
public static ConnectorToGame singleton;
public string roomRegion = "";
public string roomName = "MyRoom";
public string sceneName;
public OnAccess onAccess = new OnAccess();
private bool gotAccess = false;
public event Action<string, float> UpdateTextAndProgress;
void Awake()
{
DontDestroyOnLoad(gameObject);
if (singleton == null)
singleton = this;
else
Destroy(gameObject);
}
void Start()
{
MyNetworkManager.Disconnected += OnDisconnect;
}
private void OnDisconnect()
{
MyNetworkManager.Disconnected -= OnDisconnect;
Destroy(gameObject);
}
public void OnMasterConnected()
{
Msf.Client.Matchmaker.FindGames((games) =>
{
print("Looking for rooms");
UpdateTextAndProgress?.Invoke("Looking for rooms", 0.1f);
// eger gameList-de room varsa
if (games.Count > 0)
{
// gameList-e baxir
foreach (GameInfoPacket game in games)
{
// eger userin roomName-i siyahidaki gamelerden birinin
// Name-i ile eynidirse, hemin room-a giris etmeye calisir
if (game.Name == roomName)
{
print("Room matched");
//if(LoginData.roomId.Equals(game.Id) && //LoginData.roomPassword.Equals(game.CustomOptions.)
print("gameId = " + game.Id);
print("game.CustomOptions.ToReadableString = " + game.CustomOptions.ToReadableString());
GetRoomAccess(game.Id);
gotAccess = true;
}
}
// eger userin roomName-i siyahidada yoxdursa yeni room yaradir
if (!gotAccess)
{
print("No room matches");
UpdateTextAndProgress?.Invoke("No room matches", 0.25f);
SendRequest();
}
}
// eger hec oyun yoxdursa, yeni room yaradir
else
{
print("No rooms found");
UpdateTextAndProgress?.Invoke("No rooms found", 0.25f);
SendRequest();
}
});
}
public void SendRequest()
{
if (!Msf.Connection.IsConnected)
{
print("Not connected to master server");
UpdateTextAndProgress?.Invoke("Not connected to master server", 0f);
return;
}
// These options will be send to spawner, and then passed
// to spawned process, so that it knows what kind of game server to start.
// You can add anything to this dictionary
var dictionaryOptions = new DictionaryOptions();
var customOptions = new DictionaryOptions();
dictionaryOptions.Add(MsfDictKeys.region, roomRegion);
dictionaryOptions.Add(MsfDictKeys.roomName, roomName);
dictionaryOptions.Add(MsfDictKeys.sceneName, sceneName);
dictionaryOptions.Add(MsfDictKeys.roomPassword, LoginData.roomPassword);
customOptions.Add(Msf.Args.Names.RoomMaxConnections, 20);
//customOptions.Add(Msf.Args.Names.RoomPassword, LoginData.roomPassword);
print("Requesting room");
UpdateTextAndProgress?.Invoke("Requesting room", 0.35f);
Msf.Client.Spawners.RequestSpawn(dictionaryOptions, customOptions,
roomRegion, (controller, error) =>
{
if (controller == null)
{
print("Room request failed: " + error);
UpdateTextAndProgress?.Invoke("Room request failed: " + error, 0f);
return;
}
// If we got here, the request is being handled,
// but we need to wait until it's done
// We'll start a coroutine for that (they are perfect for waiting ^_^)
StartCoroutine(WaitServerToBeFinalized(controller));
});
}
private IEnumerator WaitServerToBeFinalized(SpawnRequestController request)
{
print("Opening room");
UpdateTextAndProgress?.Invoke("Opening room", 0.5f);
// Cache status
var currentStatus = request.Status;
// Keep looping until spawn request is finalized
while (request.Status != SpawnStatus.Finalized)
{
yield return null;
// If status has changed
if (currentStatus != request.Status)
{
print("Status changed to: " + request.Status);
//UpdateText?.Invoke("Status changed to: " + request.Status);
currentStatus = request.Status;
}
}
// If we got here, the spawn request has been finalized
print("Request Finalized");
UpdateTextAndProgress?.Invoke("Request Finalized", 0.75f);
// When spawned process finalizes, it gives master server some,
// information about itself, which includes room id
// We can retrieve this data from master server:
request.GetFinalizationData((data, error) =>
{
if (data == null)
{
print("Failed to get finalization data: " + error);
UpdateTextAndProgress?.Invoke("Failed to get finalization data: " + error, 0f);
return;
}
foreach (KeyValuePair<string, string> kvp in data)
{
print("Key = " + kvp.Key + " Value = " + kvp.Value);
}
if (!data.ContainsKey(MsfDictKeys.roomId))
{
print("Spawned server didn't add a room ID to finalization data");
UpdateTextAndProgress?.Invoke("Spawned server didn't add a room ID to finalization data", 0f);
return;
}
// So we've received the roomId of the game server that we've requested to spawn
var roomId = int.Parse(data[MsfDictKeys.roomId]);
GetRoomAccess(roomId);
});
}
public void GetRoomAccess(int roomId)
{
print("Entering room");
UpdateTextAndProgress?.Invoke("Entering room", .95f);
Msf.Client.Rooms.GetAccess(roomId, (access, error) =>
{
if (access == null)
{
print("Failed to get room access: " + error);
UpdateTextAndProgress?.Invoke("Failed to get room access: " + error, 0f);
return;
}
////Dictionary<string, string> =
//if (LoginData.roomPassword.Equals(access.CustomOptions.ToDictionary()[Msf.Args.Names.RoomPassword]))
//{
//}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment