Created
July 7, 2016 10:42
-
-
Save arun02139/79088ea7248e0025a014670096aba8ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Assertions; | |
using UnityEngine.UI; | |
using UnityEngine.Networking; | |
using UnityEngine.Networking.Match; | |
using UnityEngine.Networking.Types; | |
using System.Collections; | |
// TODO: seperate the logic in here into a custom NetworkManager class and | |
// the pure UI (basically, just a cancel button and maybe debug text ui) | |
public class OpenGamesUI : MonoBehaviour | |
{ | |
string randomString; | |
float _hostSpan; | |
float _searchSpan; // randomize between 3 and 6 | |
Text _titleText; | |
Text _contentText; | |
NetworkManager _netManager; // in this example, LobbyManager : NetworkLobbyManager : NetworkManager | |
ListMatchResponse _openMatches; | |
bool _hosting; | |
bool _searching; | |
float _lastHostAttemptEnd; | |
public delegate void BackButtonDelegate(); | |
public BackButtonDelegate backDelegate; | |
void Awake() | |
{ | |
randomString = Util.GetUniqueString (8); | |
NetworkManager[] netManagers = FindObjectsOfType<NetworkManager> (); | |
Assert.IsTrue (netManagers != null); | |
Assert.IsTrue (netManagers.Length == 1); | |
_netManager = netManagers [0]; | |
_contentText = gameObject.FindRecursive("Content").GetComponentInChildren<Text> (); | |
_titleText = gameObject.FindRecursive("Title").GetComponentInChildren<Text> (); | |
_lastHostAttemptEnd = Time.unscaledTime; | |
} | |
// Use this for initialization | |
IEnumerator Start () | |
{ | |
string isMatchMakerNull = _netManager.matchMaker == null ? "null" : "not null"; | |
Debug.Log (string.Format ("OpenGamesUI.Start: netManager({0}).matchMaker is {1}", _netManager.name, isMatchMakerNull)); | |
_hosting = false; | |
if (_netManager.matchMaker == null) { | |
_netManager.StartMatchMaker (); | |
} | |
float startTime = Time.time; | |
while (_netManager.matchMaker == null) | |
{ | |
yield return new WaitForSeconds (.2f); | |
_contentText.text = string.Format ("OpenGamesUI.Start: Starting match maker [{0}]", (Time.time - startTime).ToString("#.#")); | |
} | |
// this function continuously switches between looking for a match to join one | |
// and, if not, trying to host one for a short period | |
_titleText.text = "Searching"; | |
_searching = true; | |
_searchSpan = UnityEngine.Random.Range (3f, 6f); // HARDCODE | |
_hostSpan = 6f; // HARDCODE | |
StartCoroutine(AutoMatch()); | |
} | |
IEnumerator AutoMatch() | |
{ | |
// loops forever | |
while (_searching) | |
{ | |
// search for existing match for 6 seconds | |
while (!_hosting) | |
{ | |
// request fresh match list | |
Assert.IsNotNull(_netManager); | |
Assert.IsNotNull(_netManager.matchMaker); | |
_netManager.matchMaker.ListMatches (0, 10, "", OnMatchList); | |
// after searching for a while, switch to host mode | |
if (Time.unscaledTime - _lastHostAttemptEnd > _searchSpan) | |
{ | |
_hosting = true; | |
// or else, wait 2 seconds before requesing the fresh matche list | |
} | |
else | |
{ | |
yield return new WaitForSeconds (2f); // HARDCODE | |
} | |
} | |
// after hosting for a while, switch back to search mode | |
while (_hosting) | |
{ | |
_titleText.text = "Hosting"; | |
StartHosting (); | |
yield return new WaitForSeconds (_hostSpan); | |
Assert.IsNotNull(_netManager.matchMaker); | |
StopHosting (); | |
Assert.IsNotNull(_netManager.matchMaker); | |
_lastHostAttemptEnd = Time.time; | |
_searchSpan = UnityEngine.Random.Range (3f, 6f); // HARDCODE | |
_hosting = false; | |
_titleText.text = "Searching"; | |
} | |
} | |
} | |
void JoinMatch(NetworkID networkId, NetworkManager netManager) | |
{ | |
// TODO: allow cancelling at this point as well (see how callbacks are implemented above) | |
Debug.Log (string.Format("OpenGamesUI.JoinMatch: networkID = {0}", networkId)); | |
// netManager.matchMaker.JoinMatch(networkID, "", netManager.OnMatchJoined); | |
backDelegate = () => { | |
_netManager.StopClient (); | |
_netManager.StopMatchMaker(); | |
_netManager = null; | |
// TODO: navigate back to 'battle' panel | |
// SceneManager. | |
}; | |
} | |
void StartHosting() | |
{ | |
_netManager.matchMaker.CreateMatch(randomString, (uint)2, true, "", _netManager.OnMatchCreate); | |
Debug.Log (string.Format("OpenGamesUI.StartHosting: created match, matchName = {0}", randomString)); | |
backDelegate = () => { | |
_netManager.StopHost (); | |
_netManager.StopMatchMaker(); | |
_netManager = null; | |
// TODO: navigate back to 'battle' panel | |
// SceneManager. | |
}; | |
} | |
void StopHosting() | |
{ | |
_netManager.StopHost (); | |
} | |
public void OnMatchList(ListMatchResponse response) | |
{ | |
// refresh list of open matches | |
_openMatches = response; | |
// show them for degugging | |
_contentText.text = _openMatches.PrettyPrint (); | |
// try to jump into a match if one is available! | |
if (_openMatches.matches.Count > 0) | |
{ | |
// stop main auto-matching loop | |
_searching = false; | |
// grab open match's net id | |
var netId = _openMatches.matches [0].networkId; | |
JoinMatch (netId, _netManager); | |
} | |
} | |
public void OnDestroy() | |
{ | |
// cleanup | |
if (_netManager != null) | |
{ | |
Debug.Log (string.Format ("OpenGamesUI.OnDestroy: stopping match maker {0}", "")); | |
_netManager.StopMatchMaker (); | |
} | |
} | |
public void CancelButton() | |
{ | |
backDelegate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment