Better server info handling

This commit is contained in:
Sardelka
2022-08-12 18:10:56 +08:00
parent 0bd1d2c2a7
commit 495e5dc6b0
6 changed files with 69 additions and 45 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
<Costura ExcludeAssemblies="RageCoop.Core"/>
</Weavers>

View File

@ -5,32 +5,10 @@ using System.Collections.Generic;
using System.Drawing;
using System.Net;
using System.Threading;
using RageCoop.Core;
namespace RageCoop.Client.Menus
{
internal class ServerListClass
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("port")]
public string Port { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("players")]
public int Players { get; set; }
[JsonProperty("maxPlayers")]
public int MaxPlayers { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
}
/// <summary>
/// Don't use it!
@ -76,9 +54,9 @@ namespace RageCoop.Client.Menus
private static void GetAllServers()
{
List<ServerListClass> serverList = null;
List<ServerInfo> serverList = null;
var realUrl = Main.Settings.MasterServer;
serverList = JsonConvert.DeserializeObject<List<ServerListClass>>(DownloadString(realUrl));
serverList = JsonConvert.DeserializeObject<List<ServerInfo>>(DownloadString(realUrl));
// Need to be processed in main thread
Main.QueueAction(() =>
@ -94,7 +72,7 @@ namespace RageCoop.Client.Menus
return;
}
CleanUpList();
foreach (ServerListClass server in serverList)
foreach (ServerInfo server in serverList)
{
string address = $"{server.Address}:{server.Port}";
NativeItem tmpItem = new NativeItem($"[{server.Country}] {server.Name}", $"~b~{address}~s~~n~~g~Version {server.Version}.x~s~") { AltTitle = $"[{server.Players}/{server.MaxPlayers}]" };

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace RageCoop.Core
{
internal class ServerInfo
{
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("port")]
public int Port { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("players")]
public int Players { get; set; }
[JsonProperty("maxPlayers")]
public int MaxPlayers { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("website")]
public string Website { get; set; }
[JsonProperty("gameMode")]
public string GameMode { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
}
}

View File

@ -1,3 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
<Costura ExcludeAssemblies="RageCoop.Core" />
</Weavers>

View File

@ -39,7 +39,7 @@ namespace RageCoop.Server
/// </summary>
public API API { get; private set; }
internal readonly BaseScript BaseScript;
internal readonly ServerSettings Settings;
internal readonly Settings Settings;
internal NetServer MainNetServer;
internal ServerEntities Entities;
@ -68,7 +68,7 @@ namespace RageCoop.Server
/// <param name="settings"></param>
/// <param name="logger"></param>
/// <exception cref="ArgumentNullException"></exception>
public Server(ServerSettings settings,Logger logger=null)
public Server(Settings settings,Logger logger=null)
{
Settings = settings;
if (settings==null) { throw new ArgumentNullException("Server settings cannot be null!"); }
@ -142,20 +142,21 @@ namespace RageCoop.Server
}
while (!_stopping)
{
string msg =
"{ " +
"\"address\": \"" + info.Address + "\", " +
"\"port\": \"" + Settings.Port + "\", " +
"\"country\": \"" + info.Country + "\", " +
"\"name\": \"" + Settings.Name + "\", " +
"\"version\": \"" + _compatibleVersion.Replace("_", ".") + "\", " +
"\"players\": \"" + MainNetServer.ConnectionsCount + "\", " +
"\"maxPlayers\": \"" + Settings.MaxPlayers + "\", " +
"\"description\": \"" + Settings.Description + "\", " +
"\"website\": \"" + Settings.Website + "\", " +
"\"gameMode\": \"" + Settings.GameMode + "\", " +
"\"language\": \"" + Settings.Language + "\"" +
" }";
var serverInfo = new ServerInfo
{
Address = info.Address,
Port=Settings.Port,
Country=info.Country,
Name=Settings.Name,
Version=_compatibleVersion.Replace("_", "."),
Players=MainNetServer.ConnectionsCount,
MaxPlayers=Settings.MaxPlayers,
Description=Settings.Description,
Website=Settings.Website,
GameMode=Settings.GameMode,
Language=Settings.Language,
};
string msg = JsonConvert.SerializeObject(serverInfo);
HttpResponseMessage response = null;
try
{

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
using RageCoop.Core;
using Newtonsoft.Json;
namespace RageCoop.Server
{
class Program
@ -20,7 +21,7 @@ namespace RageCoop.Server
try
{
Console.Title = "RAGECOOP";
var setting = Util.Read<ServerSettings>("Settings.xml");
var setting = Util.Read<Settings>("Settings.xml");
#if DEBUG
setting.LogLevel=0;
#endif