Skip to content

Commit

Permalink
feat: adding demo for mirror cloud services (#2026)
Browse files Browse the repository at this point in the history
* adding mirror list services

* fixing code smells

* removing runtime example folders

* removing matchmaking code till feature is ready

* fixing scene path

* updating readme to say where example is
  • Loading branch information
James-Frowen committed Jun 23, 2020
1 parent 5b05c46 commit f1fdc95
Show file tree
Hide file tree
Showing 94 changed files with 7,668 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/Mirror/Cloud.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions Assets/Mirror/Cloud/ApiConnector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Mirror.CloudServices.ListServerService;
using UnityEngine;

namespace Mirror.CloudServices
{
/// <summary>
/// Used to requests and responses from the mirror api
/// </summary>
public interface IApiConnector
{
ListServer ListServer { get; }
}

/// <summary>
/// Used to requests and responses from the mirror api
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Network/CloudServices/ApiConnector")]
[HelpURL("https://mirror-networking.com/docs/CloudServices/ApiConnector.html")]
public class ApiConnector : MonoBehaviour, IApiConnector, ICoroutineRunner
{
#region Inspector
[Header("Settings")]

[Tooltip("Base URL of api, including https")]
[SerializeField] string ApiAddress = "";

[Tooltip("Api key required to access api")]
[SerializeField] string ApiKey = "";

[Header("Events")]

[Tooltip("Triggered when server list updates")]
[SerializeField] ServerListEvent _onServerListUpdated = new ServerListEvent();
#endregion

IRequestCreator requestCreator;

public ListServer ListServer { get; private set; }

void Awake()
{
requestCreator = new RequestCreator(ApiAddress, ApiKey, this);

InitListServer();
}

void InitListServer()
{
IListServerServerApi serverApi = new ListServerServerApi(this, requestCreator);
IListServerClientApi clientApi = new ListServerClientApi(this, requestCreator, _onServerListUpdated);
ListServer = new ListServer(serverApi, clientApi);
}

public void OnDestroy()
{
ListServer.ServerApi.Shutdown();
ListServer.ClientApi.Shutdown();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/ApiConnector.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Mirror/Cloud/Core.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Assets/Mirror/Cloud/Core/BaseApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Mirror.CloudServices
{
public interface IBaseApi
{
/// <summary>
/// Cleans up any data created by the instance
/// <para>For Example: removing server from list</para>
/// </summary>
void Shutdown();
}

public abstract class BaseApi
{
protected readonly ICoroutineRunner runner;
protected readonly IRequestCreator requestCreator;

protected BaseApi(ICoroutineRunner runner, IRequestCreator requestCreator)
{
this.runner = runner ?? throw new ArgumentNullException(nameof(runner));
this.requestCreator = requestCreator ?? throw new ArgumentNullException(nameof(requestCreator));
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/BaseApi.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/Mirror/Cloud/Core/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Mirror.CloudServices.ListServerService;
using UnityEngine.Events;

namespace Mirror.CloudServices
{
[Serializable]
public class ServerListEvent : UnityEvent<ServerCollectionJson> { }

[Serializable]
public class MatchFoundEvent : UnityEvent<ServerJson> { }
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/Events.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/Mirror/Cloud/Core/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using UnityEngine.Networking;

namespace Mirror.CloudServices
{
public static class Extensions
{
public static bool IsOk(this UnityWebRequest webRequest)
{
return 200 <= webRequest.responseCode && webRequest.responseCode <= 299;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/Extensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/Mirror/Cloud/Core/ICoroutineRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using UnityEngine;

namespace Mirror.CloudServices
{
public interface ICoroutineRunner : IUnityEqualCheck
{
Coroutine StartCoroutine(IEnumerator routine);
void StopCoroutine(IEnumerator routine);
void StopCoroutine(Coroutine routine);
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/ICoroutineRunner.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions Assets/Mirror/Cloud/Core/IRequestCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections;
using UnityEngine.Networking;

namespace Mirror.CloudServices
{
public delegate void RequestSuccess(string responseBody);

public delegate void RequestFail(string responseBody);

/// <summary>
/// Objects that can be sent to the Api must have this interface
/// </summary>
public interface ICanBeJson { }

/// <summary>
/// Methods to create and send UnityWebRequest
/// </summary>
public interface IRequestCreator
{
UnityWebRequest Delete(string page);
UnityWebRequest Get(string page);
UnityWebRequest Patch<T>(string page, T json) where T : struct, ICanBeJson;
UnityWebRequest Post<T>(string page, T json) where T : struct, ICanBeJson;

/// <summary>
/// Sends Request to api and invokes callback when finished
/// <para>Starts Coroutine of SendRequestEnumerator</para>
/// </summary>
/// <param name="request"></param>
/// <param name="onSuccess"></param>
/// <param name="onFail"></param>
void SendRequest(UnityWebRequest request, RequestSuccess onSuccess = null, RequestFail onFail = null);
/// <summary>
/// Sends Request to api and invokes callback when finished
/// </summary>
/// <param name="request"></param>
/// <param name="onSuccess"></param>
/// <param name="onFail"></param>
/// <returns></returns>
IEnumerator SendRequestEnumerator(UnityWebRequest request, RequestSuccess onSuccess = null, RequestFail onFail = null);
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/IRequestCreator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Assets/Mirror/Cloud/Core/IUnityEqualCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;

namespace Mirror.CloudServices
{
/// <summary>
/// Adds Extension to check if unity object is null.
/// <para>Use these methods to stop MissingReferenceException</para>
/// </summary>
public interface IUnityEqualCheck
{

}

public static class UnityEqualCheckExtension
{
public static bool IsNull(this IUnityEqualCheck obj)
{
return (obj as Object) == null;
}

public static bool IsNotNull(this IUnityEqualCheck obj)
{
return (obj as Object) != null;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/IUnityEqualCheck.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Mirror/Cloud/Core/JsonStructs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Mirror.CloudServices
{
[Serializable]
public struct CreatedIdJson : ICanBeJson
{
public string id;
}

[Serializable]
public struct ErrorJson : ICanBeJson
{
public string code;
public string message;

public int HtmlCode => int.Parse(code);
}

[Serializable]
public struct EmptyJson : ICanBeJson
{
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Cloud/Core/JsonStructs.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1fdc95

Please sign in to comment.