Skip to content

Commit

Permalink
Spawn service
Browse files Browse the repository at this point in the history
  • Loading branch information
devo1929 committed Nov 28, 2022
1 parent 71c0230 commit d0ddf6b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 75 deletions.
3 changes: 2 additions & 1 deletion DXMainClient/DXGUI/GameClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
using DTAClient.Domain.Multiplayer;
using DTAClient.Domain.Multiplayer.CnCNet;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Services;
using DTAClient.Domain.Multiplayer.CnCNet.Services;
using DTAClient.DXGUI.Multiplayer;
using DTAClient.DXGUI.Multiplayer.CnCNet;
using DTAClient.DXGUI.Multiplayer.GameLobby;
using DTAClient.DXGUI.Multiplayer.QuickMatch;
using DTAClient.Online;
using DTAClient.Services;
using DTAConfig;
using DTAConfig.Settings;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -202,6 +202,7 @@ private IServiceProvider BuildServiceProvider(WindowManager windowManager)
.AddSingleton<PrivateMessageHandler>()
.AddSingleton<MapLoader>()
.AddSingleton<ApiService>()
.AddSingleton<SpawnService>()
.AddSingleton<QmService>()
.AddSingleton<QmSettingsService>()
.AddSingleton<QmUserSettingsService>();
Expand Down
3 changes: 3 additions & 0 deletions DXMainClient/DXMainClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
<HintPath>..\References\DiscordRPC.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Domain\Multiplayer\CnCNet\Services" />
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\build\WinForms.props" />
<Import Project="$(MSBuildThisFileDirectory)..\build\AfterPublish.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Requests;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Responses;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Utilities;
using DTAClient.Domain.Multiplayer.CnCNet.Services;
using DTAClient.Services;
using Newtonsoft.Json;

namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Requests;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Responses;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Utilities;
using DTAClient.Domain.Multiplayer.CnCNet.Services;
using DTAClient.Online;
using DTAClient.Services;
using JWT;
using JWT.Algorithms;
using JWT.Exceptions;
Expand All @@ -30,6 +30,7 @@ public class QmService : IDisposable

private readonly QmUserSettingsService qmUserSettingsService;
private readonly ApiService apiService;
private readonly SpawnService spawnService;
private readonly QmSettingsService qmSettingsService;

private readonly QmUserSettings qmUserSettings;
Expand All @@ -40,10 +41,16 @@ public class QmService : IDisposable
private QmUserAccount userAccount;
private IEnumerable<int> mapSides;

public QmService(QmSettingsService qmSettingsService, QmUserSettingsService qmUserSettingsService, ApiService apiService)
public QmService(
QmSettingsService qmSettingsService,
QmUserSettingsService qmUserSettingsService,
ApiService apiService,
SpawnService spawnService
)
{
this.qmUserSettingsService = qmUserSettingsService;
this.apiService = apiService;
this.spawnService = spawnService;
this.qmSettingsService = qmSettingsService;

qmUserSettings = this.qmUserSettingsService.GetSettings();
Expand Down Expand Up @@ -227,11 +234,12 @@ public void RequestMatchAsync() =>
/// <summary>
/// This is called when the user clicks the "I'm Ready" button in the match found dialog.
/// </summary>
/// <param name="spawn">Spawn settings from the API.</param>
public void AcceptMatchAsync(QmSpawn spawn)
{
ExecuteLoadingRequest(new QmReadyRequestMatchEvent(), async () =>
{
WriteSpawnIni(spawn);
spawnService.WriteSpawnInfo(spawn);
retryRequestmatchTimer.Stop();
var readyRequest = new QmReadyRequest(spawn.Settings.Seed);
QmResponse<QmResponseMessage> response = await apiService.QuickMatchRequestAsync(userAccount.Ladder.Abbreviation, userAccount.Username, readyRequest);
Expand All @@ -242,6 +250,7 @@ public void AcceptMatchAsync(QmSpawn spawn)
/// <summary>
/// This is called when the user clicks the "Cancel" button in the match found dialog.
/// </summary>
/// <param name="spawn">Spawn settings from the API.</param>
public void RejectMatchAsync(QmSpawn spawn)
{
ExecuteLoadingRequest(new QmNotReadyRequestMatchEvent(), async () =>
Expand All @@ -254,71 +263,6 @@ public void RejectMatchAsync(QmSpawn spawn)
CancelRequestMatchAsync();
}

public void WriteSpawnIni(QmSpawn spawn)
{
IniFile spawnIni = CreateSpawnIniFile();

AddSpawnSettingsSection(spawn, spawnIni);
AddSpawnOtherSections(spawn, spawnIni);
AddSpawnLocationsSection(spawn, spawnIni);
AddSpawnTunnelSection(spawn, spawnIni);

spawnIni.WriteIniFile();
}

private static void AddSpawnSettingsSection(QmSpawn spawn, IniFile spawnIni)
{
var settings = new IniSection("Settings");
settings.SetStringValue("Scenario", "spawnmap.ini");
settings.SetStringValue("QuickMatch", "Yes");

foreach (PropertyInfo prop in spawn.Settings.GetType().GetProperties())
settings.SetStringValue(prop.Name, prop.GetValue(spawn.Settings).ToString());

spawnIni.AddSection(settings);
}

private static void AddSpawnOtherSections(QmSpawn spawn, IniFile spawnIni)
{
for (int i = 0; i < spawn.Others.Count; i++)
{
// Headers for OTHER# sections are 1-based index
var otherSection = new IniSection($"Other{i + 1}");
QmSpawnOther other = spawn.Others[i];

foreach (PropertyInfo otherProp in other.GetType().GetProperties())
otherSection.SetStringValue(otherProp.Name, otherProp.GetValue(other).ToString());

spawnIni.AddSection(otherSection);
}
}

private static void AddSpawnLocationsSection(QmSpawn spawn, IniFile spawnIni)
{
var spawnLocationsSection = new IniSection("SpawnLocations");
foreach (KeyValuePair<string, int> spawnLocation in spawn.SpawnLocations)
spawnLocationsSection.SetStringValue(spawnLocation.Key, spawnLocation.Value.ToString());

spawnIni.AddSection(spawnLocationsSection);
}

private static void AddSpawnTunnelSection(QmSpawn spawn, IniFile spawnIni)
{
var tunnel = new IniSection("Tunnel");
tunnel.SetStringValue("Ip", "52.232.96.199");
tunnel.SetIntValue("Port", 50001);
spawnIni.AddSection(tunnel);
}

public IniFile CreateSpawnIniFile()
{
FileInfo spawnerSettingsFile = SafePath.GetFile(ProgramConstants.GamePath, ProgramConstants.SPAWNER_SETTINGS);

spawnerSettingsFile.Delete();

return new IniFile(spawnerSettingsFile.FullName);
}

public void Dispose()
{
apiService.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using DTAClient.Domain.Multiplayer.CnCNet;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Requests;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Responses;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Services;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Utilities;

namespace DTAClient.Domain.Multiplayer.CnCNet.Services;
namespace DTAClient.Services;

public class ApiService : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.IO;
using ClientCore;
using ClientCore.Exceptions;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
using DTAClient.Domain.Multiplayer.CnCNet;
using Rampastring.Tools;

namespace DTAClient.Domain.Multiplayer.CnCNet.Services;
namespace DTAClient.Services;

public class ApiSettingsService
{
Expand Down
93 changes: 93 additions & 0 deletions DXMainClient/Services/SpawnService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using ClientCore;
using DTAClient.Domain.Multiplayer;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
using Rampastring.Tools;

namespace DTAClient.Services;

public class SpawnService
{
private readonly MapLoader mapLoader;

public SpawnService(
MapLoader mapLoader
)
{
this.mapLoader = mapLoader;
}

public void WriteSpawnInfo(QmSpawn spawn)
{
IniFile spawnIni = GetSpawnIniFile();

AddSpawnSettingsSection(spawn, spawnIni);
AddSpawnOtherSections(spawn, spawnIni);
AddSpawnLocationsSection(spawn, spawnIni);
AddSpawnTunnelSection(spawn, spawnIni);

spawnIni.WriteIniFile();

WriteSpawnMapIni(spawn.Settings.MapHash);
}

public void WriteSpawnMapIni(string mapHash)
{
Map map = mapLoader.GetMapForSHA(mapHash);
IniFile mapIni = map.GetMapIni();
mapIni.WriteIniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, ProgramConstants.SPAWNMAP_INI));
}

private static void AddSpawnSettingsSection(QmSpawn spawn, IniFile spawnIni)
{
var settings = new IniSection("Settings");
settings.SetStringValue("Scenario", "spawnmap.ini");
settings.SetStringValue("QuickMatch", "Yes");

foreach (PropertyInfo prop in spawn.Settings.GetType().GetProperties())
settings.SetStringValue(prop.Name, prop.GetValue(spawn.Settings).ToString());

spawnIni.AddSection(settings);
}

private static void AddSpawnOtherSections(QmSpawn spawn, IniFile spawnIni)
{
for (int i = 0; i < spawn.Others.Count; i++)
{
// Headers for OTHER# sections are 1-based index
var otherSection = new IniSection($"Other{i + 1}");
QmSpawnOther other = spawn.Others[i];

foreach (PropertyInfo otherProp in other.GetType().GetProperties())
otherSection.SetStringValue(otherProp.Name, otherProp.GetValue(other).ToString());

spawnIni.AddSection(otherSection);
}
}

private static void AddSpawnLocationsSection(QmSpawn spawn, IniFile spawnIni)
{
var spawnLocationsSection = new IniSection("SpawnLocations");
foreach (KeyValuePair<string, int> spawnLocation in spawn.SpawnLocations)
spawnLocationsSection.SetStringValue(spawnLocation.Key, spawnLocation.Value.ToString());

spawnIni.AddSection(spawnLocationsSection);
}

private static void AddSpawnTunnelSection(QmSpawn spawn, IniFile spawnIni)
{
var tunnel = new IniSection("Tunnel");
tunnel.SetStringValue("Ip", "52.232.96.199");
tunnel.SetIntValue("Port", 50001);
spawnIni.AddSection(tunnel);
}

private static IniFile GetSpawnIniFile()
{
FileInfo spawnerMapSettingsFile = SafePath.GetFile(ProgramConstants.GamePath, ProgramConstants.SPAWNER_SETTINGS);
spawnerMapSettingsFile.Delete();
return new IniFile(spawnerMapSettingsFile.FullName);
}
}

0 comments on commit d0ddf6b

Please sign in to comment.