SanAndreasUnity/Assets/Scripts/Networking/MasterServerClient.cs

123 lines
3.5 KiB
C#
Raw Normal View History

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