Skip to content

Commit d0ddf6b

Browse files
committed
Spawn service
1 parent 71c0230 commit d0ddf6b

File tree

7 files changed

+115
-75
lines changed

7 files changed

+115
-75
lines changed

DXMainClient/DXGUI/GameClass.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
using DTAClient.Domain.Multiplayer;
1414
using DTAClient.Domain.Multiplayer.CnCNet;
1515
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Services;
16-
using DTAClient.Domain.Multiplayer.CnCNet.Services;
1716
using DTAClient.DXGUI.Multiplayer;
1817
using DTAClient.DXGUI.Multiplayer.CnCNet;
1918
using DTAClient.DXGUI.Multiplayer.GameLobby;
2019
using DTAClient.DXGUI.Multiplayer.QuickMatch;
2120
using DTAClient.Online;
21+
using DTAClient.Services;
2222
using DTAConfig;
2323
using DTAConfig.Settings;
2424
using Microsoft.Extensions.DependencyInjection;
@@ -202,6 +202,7 @@ private IServiceProvider BuildServiceProvider(WindowManager windowManager)
202202
.AddSingleton<PrivateMessageHandler>()
203203
.AddSingleton<MapLoader>()
204204
.AddSingleton<ApiService>()
205+
.AddSingleton<SpawnService>()
205206
.AddSingleton<QmService>()
206207
.AddSingleton<QmSettingsService>()
207208
.AddSingleton<QmUserSettingsService>();

DXMainClient/DXMainClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
<HintPath>..\References\DiscordRPC.dll</HintPath>
137137
</Reference>
138138
</ItemGroup>
139+
<ItemGroup>
140+
<Folder Include="Domain\Multiplayer\CnCNet\Services" />
141+
</ItemGroup>
139142

140143
<Import Project="$(MSBuildThisFileDirectory)..\build\WinForms.props" />
141144
<Import Project="$(MSBuildThisFileDirectory)..\build\AfterPublish.targets" />

DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/Services/MockApiService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Requests;
66
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Responses;
77
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Utilities;
8-
using DTAClient.Domain.Multiplayer.CnCNet.Services;
8+
using DTAClient.Services;
99
using Newtonsoft.Json;
1010

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

DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/Services/QmService.cs

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Requests;
1515
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Responses;
1616
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Utilities;
17-
using DTAClient.Domain.Multiplayer.CnCNet.Services;
1817
using DTAClient.Online;
18+
using DTAClient.Services;
1919
using JWT;
2020
using JWT.Algorithms;
2121
using JWT.Exceptions;
@@ -30,6 +30,7 @@ public class QmService : IDisposable
3030

3131
private readonly QmUserSettingsService qmUserSettingsService;
3232
private readonly ApiService apiService;
33+
private readonly SpawnService spawnService;
3334
private readonly QmSettingsService qmSettingsService;
3435

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

43-
public QmService(QmSettingsService qmSettingsService, QmUserSettingsService qmUserSettingsService, ApiService apiService)
44+
public QmService(
45+
QmSettingsService qmSettingsService,
46+
QmUserSettingsService qmUserSettingsService,
47+
ApiService apiService,
48+
SpawnService spawnService
49+
)
4450
{
4551
this.qmUserSettingsService = qmUserSettingsService;
4652
this.apiService = apiService;
53+
this.spawnService = spawnService;
4754
this.qmSettingsService = qmSettingsService;
4855

4956
qmUserSettings = this.qmUserSettingsService.GetSettings();
@@ -227,11 +234,12 @@ public void RequestMatchAsync() =>
227234
/// <summary>
228235
/// This is called when the user clicks the "I'm Ready" button in the match found dialog.
229236
/// </summary>
237+
/// <param name="spawn">Spawn settings from the API.</param>
230238
public void AcceptMatchAsync(QmSpawn spawn)
231239
{
232240
ExecuteLoadingRequest(new QmReadyRequestMatchEvent(), async () =>
233241
{
234-
WriteSpawnIni(spawn);
242+
spawnService.WriteSpawnInfo(spawn);
235243
retryRequestmatchTimer.Stop();
236244
var readyRequest = new QmReadyRequest(spawn.Settings.Seed);
237245
QmResponse<QmResponseMessage> response = await apiService.QuickMatchRequestAsync(userAccount.Ladder.Abbreviation, userAccount.Username, readyRequest);
@@ -242,6 +250,7 @@ public void AcceptMatchAsync(QmSpawn spawn)
242250
/// <summary>
243251
/// This is called when the user clicks the "Cancel" button in the match found dialog.
244252
/// </summary>
253+
/// <param name="spawn">Spawn settings from the API.</param>
245254
public void RejectMatchAsync(QmSpawn spawn)
246255
{
247256
ExecuteLoadingRequest(new QmNotReadyRequestMatchEvent(), async () =>
@@ -254,71 +263,6 @@ public void RejectMatchAsync(QmSpawn spawn)
254263
CancelRequestMatchAsync();
255264
}
256265

257-
public void WriteSpawnIni(QmSpawn spawn)
258-
{
259-
IniFile spawnIni = CreateSpawnIniFile();
260-
261-
AddSpawnSettingsSection(spawn, spawnIni);
262-
AddSpawnOtherSections(spawn, spawnIni);
263-
AddSpawnLocationsSection(spawn, spawnIni);
264-
AddSpawnTunnelSection(spawn, spawnIni);
265-
266-
spawnIni.WriteIniFile();
267-
}
268-
269-
private static void AddSpawnSettingsSection(QmSpawn spawn, IniFile spawnIni)
270-
{
271-
var settings = new IniSection("Settings");
272-
settings.SetStringValue("Scenario", "spawnmap.ini");
273-
settings.SetStringValue("QuickMatch", "Yes");
274-
275-
foreach (PropertyInfo prop in spawn.Settings.GetType().GetProperties())
276-
settings.SetStringValue(prop.Name, prop.GetValue(spawn.Settings).ToString());
277-
278-
spawnIni.AddSection(settings);
279-
}
280-
281-
private static void AddSpawnOtherSections(QmSpawn spawn, IniFile spawnIni)
282-
{
283-
for (int i = 0; i < spawn.Others.Count; i++)
284-
{
285-
// Headers for OTHER# sections are 1-based index
286-
var otherSection = new IniSection($"Other{i + 1}");
287-
QmSpawnOther other = spawn.Others[i];
288-
289-
foreach (PropertyInfo otherProp in other.GetType().GetProperties())
290-
otherSection.SetStringValue(otherProp.Name, otherProp.GetValue(other).ToString());
291-
292-
spawnIni.AddSection(otherSection);
293-
}
294-
}
295-
296-
private static void AddSpawnLocationsSection(QmSpawn spawn, IniFile spawnIni)
297-
{
298-
var spawnLocationsSection = new IniSection("SpawnLocations");
299-
foreach (KeyValuePair<string, int> spawnLocation in spawn.SpawnLocations)
300-
spawnLocationsSection.SetStringValue(spawnLocation.Key, spawnLocation.Value.ToString());
301-
302-
spawnIni.AddSection(spawnLocationsSection);
303-
}
304-
305-
private static void AddSpawnTunnelSection(QmSpawn spawn, IniFile spawnIni)
306-
{
307-
var tunnel = new IniSection("Tunnel");
308-
tunnel.SetStringValue("Ip", "52.232.96.199");
309-
tunnel.SetIntValue("Port", 50001);
310-
spawnIni.AddSection(tunnel);
311-
}
312-
313-
public IniFile CreateSpawnIniFile()
314-
{
315-
FileInfo spawnerSettingsFile = SafePath.GetFile(ProgramConstants.GamePath, ProgramConstants.SPAWNER_SETTINGS);
316-
317-
spawnerSettingsFile.Delete();
318-
319-
return new IniFile(spawnerSettingsFile.FullName);
320-
}
321-
322266
public void Dispose()
323267
{
324268
apiService.Dispose();

DXMainClient/Domain/Multiplayer/CnCNet/Services/ApiService.cs renamed to DXMainClient/Services/ApiService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
using System.Collections.Generic;
33
using System.Net.Http;
44
using System.Threading.Tasks;
5+
using DTAClient.Domain.Multiplayer.CnCNet;
56
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
67
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Requests;
78
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Responses;
8-
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Services;
99
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Utilities;
1010

11-
namespace DTAClient.Domain.Multiplayer.CnCNet.Services;
11+
namespace DTAClient.Services;
1212

1313
public class ApiService : IDisposable
1414
{
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System.IO;
22
using ClientCore;
3-
using ClientCore.Exceptions;
4-
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
3+
using DTAClient.Domain.Multiplayer.CnCNet;
54
using Rampastring.Tools;
65

7-
namespace DTAClient.Domain.Multiplayer.CnCNet.Services;
6+
namespace DTAClient.Services;
87

98
public class ApiSettingsService
109
{
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Reflection;
4+
using ClientCore;
5+
using DTAClient.Domain.Multiplayer;
6+
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
7+
using Rampastring.Tools;
8+
9+
namespace DTAClient.Services;
10+
11+
public class SpawnService
12+
{
13+
private readonly MapLoader mapLoader;
14+
15+
public SpawnService(
16+
MapLoader mapLoader
17+
)
18+
{
19+
this.mapLoader = mapLoader;
20+
}
21+
22+
public void WriteSpawnInfo(QmSpawn spawn)
23+
{
24+
IniFile spawnIni = GetSpawnIniFile();
25+
26+
AddSpawnSettingsSection(spawn, spawnIni);
27+
AddSpawnOtherSections(spawn, spawnIni);
28+
AddSpawnLocationsSection(spawn, spawnIni);
29+
AddSpawnTunnelSection(spawn, spawnIni);
30+
31+
spawnIni.WriteIniFile();
32+
33+
WriteSpawnMapIni(spawn.Settings.MapHash);
34+
}
35+
36+
public void WriteSpawnMapIni(string mapHash)
37+
{
38+
Map map = mapLoader.GetMapForSHA(mapHash);
39+
IniFile mapIni = map.GetMapIni();
40+
mapIni.WriteIniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, ProgramConstants.SPAWNMAP_INI));
41+
}
42+
43+
private static void AddSpawnSettingsSection(QmSpawn spawn, IniFile spawnIni)
44+
{
45+
var settings = new IniSection("Settings");
46+
settings.SetStringValue("Scenario", "spawnmap.ini");
47+
settings.SetStringValue("QuickMatch", "Yes");
48+
49+
foreach (PropertyInfo prop in spawn.Settings.GetType().GetProperties())
50+
settings.SetStringValue(prop.Name, prop.GetValue(spawn.Settings).ToString());
51+
52+
spawnIni.AddSection(settings);
53+
}
54+
55+
private static void AddSpawnOtherSections(QmSpawn spawn, IniFile spawnIni)
56+
{
57+
for (int i = 0; i < spawn.Others.Count; i++)
58+
{
59+
// Headers for OTHER# sections are 1-based index
60+
var otherSection = new IniSection($"Other{i + 1}");
61+
QmSpawnOther other = spawn.Others[i];
62+
63+
foreach (PropertyInfo otherProp in other.GetType().GetProperties())
64+
otherSection.SetStringValue(otherProp.Name, otherProp.GetValue(other).ToString());
65+
66+
spawnIni.AddSection(otherSection);
67+
}
68+
}
69+
70+
private static void AddSpawnLocationsSection(QmSpawn spawn, IniFile spawnIni)
71+
{
72+
var spawnLocationsSection = new IniSection("SpawnLocations");
73+
foreach (KeyValuePair<string, int> spawnLocation in spawn.SpawnLocations)
74+
spawnLocationsSection.SetStringValue(spawnLocation.Key, spawnLocation.Value.ToString());
75+
76+
spawnIni.AddSection(spawnLocationsSection);
77+
}
78+
79+
private static void AddSpawnTunnelSection(QmSpawn spawn, IniFile spawnIni)
80+
{
81+
var tunnel = new IniSection("Tunnel");
82+
tunnel.SetStringValue("Ip", "52.232.96.199");
83+
tunnel.SetIntValue("Port", 50001);
84+
spawnIni.AddSection(tunnel);
85+
}
86+
87+
private static IniFile GetSpawnIniFile()
88+
{
89+
FileInfo spawnerMapSettingsFile = SafePath.GetFile(ProgramConstants.GamePath, ProgramConstants.SPAWNER_SETTINGS);
90+
spawnerMapSettingsFile.Delete();
91+
return new IniFile(spawnerMapSettingsFile.FullName);
92+
}
93+
}

0 commit comments

Comments
 (0)