2021-01-12 22:11:10 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Newtonsoft.Json;
|
2021-01-12 21:06:38 +00:00
|
|
|
|
using SanAndreasUnity.Net;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class MasterServerClient : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private string _masterServerUrl;
|
|
|
|
|
private ServerInfo _serverInfo;
|
|
|
|
|
private bool _updating;
|
|
|
|
|
public static MasterServerClient Instance { get; private set; }
|
|
|
|
|
private HttpClient _client;
|
|
|
|
|
|
2021-01-12 22:11:10 +00:00
|
|
|
|
|
|
|
|
|
private void Awake()
|
2021-01-12 21:06:38 +00:00
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
2021-01-12 22:11:10 +00:00
|
|
|
|
}
|
2021-01-12 21:06:38 +00:00
|
|
|
|
|
2021-01-12 22:11:10 +00:00
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2021-01-12 21:06:38 +00:00
|
|
|
|
_client = new HttpClient();
|
|
|
|
|
|
|
|
|
|
Config.Load();
|
|
|
|
|
_masterServerUrl = Config.Get<string>("masterserverurl");
|
|
|
|
|
|
2021-01-12 22:11:10 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
|
|
|
|
Debug.LogError("Url of master server not defined in config");
|
|
|
|
|
|
2021-01-12 21:06:38 +00:00
|
|
|
|
NetManager.Instance.onServerStatusChanged += OnServerStatusChange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void OnServerStatusChange()
|
|
|
|
|
{
|
2021-01-12 22:11:10 +00:00
|
|
|
|
if (!NetStatus.IsServer)
|
|
|
|
|
return;
|
2021-01-12 21:06:38 +00:00
|
|
|
|
|
2021-01-12 22:11:10 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_serverInfo = new ServerInfo
|
2021-01-12 21:06:38 +00:00
|
|
|
|
{
|
2021-01-12 22:11:10 +00:00
|
|
|
|
Name = Config.Get<string>("server_name"),
|
|
|
|
|
Port = NetManager.listenPortNumber,
|
|
|
|
|
NumPlayersOnline = NetManager.numConnections,
|
|
|
|
|
MaxPlayers = NetManager.maxNumPlayers,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await RegisterServer();
|
2021-01-12 21:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RegisterServer()
|
|
|
|
|
{
|
2021-01-12 22:11:10 +00:00
|
|
|
|
await SendRequestToRegister();
|
2021-01-12 21:06:38 +00:00
|
|
|
|
|
|
|
|
|
_updating = true;
|
2021-01-12 22:11:10 +00:00
|
|
|
|
Invoke(nameof(UpdateServer), 10);
|
2021-01-12 21:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateServer()
|
|
|
|
|
{
|
|
|
|
|
while (_updating)
|
|
|
|
|
{
|
2021-01-12 22:11:10 +00:00
|
|
|
|
_serverInfo.NumPlayersOnline = NetManager.numConnections;
|
|
|
|
|
|
|
|
|
|
await SendRequestToRegister();
|
2021-01-12 21:06:38 +00:00
|
|
|
|
|
2021-01-12 22:11:10 +00:00
|
|
|
|
await Task.Delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SendRequestToRegister()
|
|
|
|
|
{
|
|
|
|
|
var response = await _client.PostAsync(_masterServerUrl + "/register", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
string str = await response.Content.ReadAsStringAsync();
|
|
|
|
|
Debug.LogError($"Master server returned error while trying to register: {str}");
|
2021-01-12 21:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UnregisterServer()
|
|
|
|
|
{
|
|
|
|
|
_updating = false;
|
|
|
|
|
|
2021-01-12 22:11:10 +00:00
|
|
|
|
if (null == _serverInfo)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-01-12 21:06:38 +00:00
|
|
|
|
await _client.PostAsync(_masterServerUrl + "/unregister", new StringContent(JsonConvert.SerializeObject(_serverInfo), Encoding.UTF8, "application/json"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<ServerInfo>> GetAllServers()
|
|
|
|
|
{
|
2021-01-12 22:11:10 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(_masterServerUrl))
|
|
|
|
|
return new List<ServerInfo>();
|
|
|
|
|
|
2021-01-12 21:06:38 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<List<ServerInfo>>(await _client.GetStringAsync(_masterServerUrl));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void OnDestroy()
|
|
|
|
|
{
|
2021-01-12 22:11:10 +00:00
|
|
|
|
await UnregisterServer();
|
2021-01-12 21:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ServerInfo
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public int NumPlayersOnline { get; set; }
|
|
|
|
|
public string IP { get; set; }
|
|
|
|
|
public int Port { get; set; }
|
|
|
|
|
public int MaxPlayers { get; set; }
|
|
|
|
|
}
|