91 lines
2.9 KiB
C#
91 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using Mirror.SimpleWeb;
|
|
using UnityEngine;
|
|
|
|
public class ServerKickstarter : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
#if UNITY_SERVER
|
|
|
|
Debug.Log("Starting server with arguments: " + JsonUtility.ToJson(System.Environment.GetCommandLineArgs()));
|
|
string[] args = System.Environment.GetCommandLineArgs();
|
|
Dictionary<string, string> arguments = new Dictionary<string, string>();
|
|
|
|
for (int i = 0; i < args.Length - 1; i++)
|
|
{
|
|
if (args[i].StartsWith("-"))
|
|
{
|
|
arguments[args[i].Substring(1)] = args[i + 1];
|
|
}
|
|
}
|
|
|
|
if (!arguments.ContainsKey("port"))
|
|
{
|
|
arguments["port"] = "5000";
|
|
arguments["address"] = "2h6hptZhts119MhaY39SyxSCYp966Y9EkKubEReb7wbT";
|
|
arguments["isDev"] = "true";
|
|
}
|
|
|
|
try
|
|
{
|
|
NetworkManager.singleton.GetComponent<SimpleWebTransport>().port = (ushort)int.Parse(arguments["port"]);
|
|
GameData.port = int.Parse(arguments["port"]);
|
|
GameData.isDev = arguments["isDev"] == "true";
|
|
if(GameData.isDev){
|
|
Debug.Log("Starting server in dev mode");
|
|
}
|
|
StartCoroutine(CoroutineUpdateGameData(arguments["address"]));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError("Error starting server, please check the arguments: " + e.Message);
|
|
#if UNITY_EDITOR
|
|
Debug.Log("Happened in Unity Editor, So its fine. Using dummy data");
|
|
GameData.DummyData();
|
|
NetworkManager.singleton.StartServer();
|
|
|
|
#endif
|
|
}
|
|
#endif
|
|
}
|
|
|
|
IEnumerator CoroutineUpdateGameData(string address)
|
|
{
|
|
GameData.address = address;
|
|
string validator_url = GameData.isDev ? Constants.VALIDATOR_URL_DEV : Constants.VALIDATOR_URL_PROD;
|
|
string url = validator_url + "fetchBet?address=" + address;
|
|
WWW www = new WWW(url);
|
|
yield return www;
|
|
Debug.Log(url);
|
|
Debug.Log("bet: " + www.text);
|
|
try
|
|
{
|
|
Bet bet = JsonUtility.FromJson<Bet>(www.text);
|
|
GameData.betData = bet;
|
|
GameData.id = bet.id;
|
|
GameData.ownerId = bet.owner;
|
|
GameData.joinerId = bet.joiner;
|
|
GameData.user_id = bet.owner_id;
|
|
GameData.pubkey = bet.owner;
|
|
GameData.wager = (int)bet.wager;
|
|
|
|
Debug.Log("GameData: " + GameData.id + " " + GameData.ownerId + " " + GameData.joinerId + " " + GameData.user_id + " " + GameData.pubkey + " " + GameData.wager);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
#if UNITY_EDITOR
|
|
throw e;
|
|
#endif
|
|
Debug.LogError("Error updating game data: " + e.Message + ", using dummy data");
|
|
GameData.DummyData();
|
|
}
|
|
NetworkManager.singleton.StartServer();
|
|
}
|
|
|
|
|
|
} |