Skip to content

Commit def7bdc

Browse files
committed
All user to override settings for quick match via INI
1 parent d2dfbcb commit def7bdc

File tree

8 files changed

+160
-72
lines changed

8 files changed

+160
-72
lines changed

ClientCore/ClientConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ public string GetThemePath(string themeName)
243243

244244
public string MPMapsIniPath => clientDefinitionsIni.GetStringValue(SETTINGS, "MPMapsPath", "INI/MPMaps.ini");
245245

246+
public string QuickMatchPath => clientDefinitionsIni.GetStringValue(SETTINGS, "QuickMatchPath", "INI/QuickMatch.ini");
247+
246248
public string KeyboardINI => clientDefinitionsIni.GetStringValue(SETTINGS, "KeyboardINI", "Keyboard.ini");
247249

248250
public int MinimumIngameWidth => clientDefinitionsIni.GetIntValue(SETTINGS, "MinimumIngameWidth", 640);

DXMainClient/DXMainClient.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@
435435
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmLoginEventArgs.cs" />
436436
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmMapSelectedEventArgs.cs" />
437437
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmSettings.cs" />
438+
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmUserSettings.cs" />
438439
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmData.cs" />
439440
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmLadder.cs" />
440441
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QmLadderMap.cs" />
@@ -450,6 +451,7 @@
450451
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QuickMatchApiService.cs" />
451452
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QuickMatchService.cs" />
452453
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QuickMatchSettingsService.cs" />
454+
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QuickMatchUserSettingsService.cs" />
453455
<Compile Include="Domain\Multiplayer\CnCNet\QuickMatch\QuickMatchStatusMessage.cs" />
454456
<Compile Include="Domain\Multiplayer\CoopHouseInfo.cs" />
455457
<Compile Include="Domain\Multiplayer\CoopMapInfo.cs" />

DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/QmSettings.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
{
33
public class QmSettings
44
{
5-
public string Email { get; set; }
5+
public const string DefaultBaseUrl = "https://ladder.cncnet.org";
6+
public const string DefaultLoginUrl = "/api/v1/auth/login";
7+
public const string DefaultRefreshUrl = "/api/v1/auth/refresh";
8+
public const string DefaultServerStatusUrl = "/api/v1/ping";
9+
public const string DefaultGetUserAccountsUrl = "/api/v1/user/account";
10+
public const string DefaultGetLaddersUrl = "/api/v1/ladder";
11+
public const string DefaultGetLadderMapsUrl = "/api/v1/qm/ladder/{0}/maps";
612

7-
public string Ladder { get; set; }
8-
public QmAuthData AuthData { get; set; }
13+
public string BaseUrl { get; set; } = DefaultBaseUrl;
14+
public string LoginUrl { get; set; } = DefaultLoginUrl;
15+
public string RefreshUrl { get; set; } = DefaultRefreshUrl;
16+
public string ServerStatusUrl { get; set; } = DefaultServerStatusUrl;
17+
public string GetUserAccountsUrl { get; set; } = DefaultGetUserAccountsUrl;
18+
public string GetLaddersUrl { get; set; } = DefaultGetLaddersUrl;
19+
public string GetLadderMapsUrl { get; set; } = DefaultGetLadderMapsUrl;
920
}
1021
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch
2+
{
3+
public class QmUserSettings
4+
{
5+
public string Email { get; set; }
6+
public string Ladder { get; set; }
7+
public QmAuthData AuthData { get; set; }
8+
}
9+
}

DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/QuickMatchApiService.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch
99
{
1010
public class QuickMatchApiService
1111
{
12-
private const string BaseUrl = "https://ladder.cncnet.org";
13-
private const string RoutePrefix = "/api/v1";
14-
private static readonly string LoginUrl = $"{RoutePrefix}/auth/login";
15-
private static readonly string RefreshUrl = $"{RoutePrefix}/auth/refresh";
16-
private static readonly string ServerStatusUrl = $"{RoutePrefix}/ping";
17-
private static readonly string GetUserAccountsUrl = $"{RoutePrefix}/user/account";
18-
private static readonly string GetLaddersUrl = $"{RoutePrefix}/ladder";
19-
private static readonly string GetLadderMapsUrlFormat = RoutePrefix + "/qm/ladder/{0}/maps";
20-
12+
private readonly QmSettings qmSettings;
2113
private string _token;
2214

15+
public QuickMatchApiService()
16+
{
17+
var settingsService = new QuickMatchSettingsService();
18+
qmSettings = settingsService.LoadSettings();
19+
}
20+
2321
public void SetToken(string token)
2422
{
2523
_token = token;
@@ -28,7 +26,7 @@ public void SetToken(string token)
2826
public async Task<IEnumerable<QmLadderMap>> FetchLadderMapsForAbbrAsync(string ladderAbbreviation)
2927
{
3028
var httpClient = CreateAuthenticatedClient();
31-
string url = string.Format(GetLadderMapsUrlFormat, ladderAbbreviation);
29+
string url = string.Format(qmSettings.GetLadderMapsUrl, ladderAbbreviation);
3230
var response = await httpClient.GetAsync(url);
3331
if (!response.IsSuccessStatusCode)
3432
throw new ClientException($"Error fetching ladder maps: {response.ReasonPhrase}");
@@ -39,7 +37,7 @@ public async Task<IEnumerable<QmLadderMap>> FetchLadderMapsForAbbrAsync(string l
3937
public async Task<IEnumerable<QmUserAccount>> FetchUserAccountsAsync()
4038
{
4139
var httpClient = CreateAuthenticatedClient();
42-
var response = await httpClient.GetAsync(GetUserAccountsUrl);
40+
var response = await httpClient.GetAsync(qmSettings.GetUserAccountsUrl);
4341
if (!response.IsSuccessStatusCode)
4442
throw new ClientException($"Error fetching user accounts: {response.ReasonPhrase}");
4543

@@ -49,7 +47,7 @@ public async Task<IEnumerable<QmUserAccount>> FetchUserAccountsAsync()
4947
public async Task<IEnumerable<QmLadder>> FetchLaddersAsync()
5048
{
5149
var httpClient = CreateAuthenticatedClient();
52-
var response = await httpClient.GetAsync(GetLaddersUrl);
50+
var response = await httpClient.GetAsync(qmSettings.GetLaddersUrl);
5351
if (!response.IsSuccessStatusCode)
5452
throw new ClientException($"Error fetching ladders: {response.ReasonPhrase}");
5553

@@ -64,7 +62,7 @@ public async Task<QmAuthData> LoginAsync(string email, string password)
6462
Email = email,
6563
Password = password
6664
});
67-
var response = await httpClient.PostAsync(LoginUrl, postBodyContent);
65+
var response = await httpClient.PostAsync(qmSettings.LoginUrl, postBodyContent);
6866
if (!response.IsSuccessStatusCode)
6967
throw new ClientException($"Error logging in: {response.ReasonPhrase}");
7068

@@ -76,7 +74,7 @@ public async Task<QmAuthData> LoginAsync(string email, string password)
7674
public async Task<QmAuthData> RefreshAsync()
7775
{
7876
var httpClient = CreateAuthenticatedClient();
79-
var response = await httpClient.GetAsync(RefreshUrl);
77+
var response = await httpClient.GetAsync(qmSettings.RefreshUrl);
8078
if (!response.IsSuccessStatusCode)
8179
throw new ClientException($"Error refreshing token: {response.ReasonPhrase}");
8280

@@ -88,15 +86,15 @@ public async Task<QmAuthData> RefreshAsync()
8886
public bool IsServerAvailable()
8987
{
9088
var httpClient = CreateAuthenticatedClient();
91-
var response = httpClient.GetAsync(ServerStatusUrl).Result;
89+
var response = httpClient.GetAsync(qmSettings.ServerStatusUrl).Result;
9290
return response.IsSuccessStatusCode;
9391
}
9492

95-
private static HttpClient CreateHttpClient()
93+
private HttpClient CreateHttpClient()
9694
{
9795
var httpClient = new HttpClient
9896
{
99-
BaseAddress = new Uri(BaseUrl)
97+
BaseAddress = new Uri(qmSettings.BaseUrl)
10098
};
10199

102100
return httpClient;

DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/QuickMatchService.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch
1414
{
1515
public class QuickMatchService
1616
{
17-
private readonly QuickMatchSettingsService settingsService;
17+
private readonly QuickMatchUserSettingsService userSettingsService;
1818
private readonly QuickMatchApiService apiService;
19-
private readonly QmSettings qmSettings;
19+
private readonly QmUserSettings qmUserSettings;
2020
private readonly QmData qmData;
2121

2222
private static QuickMatchService Instance;
@@ -27,9 +27,9 @@ public class QuickMatchService
2727

2828
private QuickMatchService()
2929
{
30-
settingsService = new QuickMatchSettingsService();
30+
userSettingsService = new QuickMatchUserSettingsService();
3131
apiService = new QuickMatchApiService();
32-
qmSettings = settingsService.LoadSettings();
32+
qmUserSettings = userSettingsService.LoadSettings();
3333
qmData = new QmData();
3434
}
3535

@@ -67,8 +67,8 @@ public async Task LoginAsync(string email, string password)
6767
/// </summary>
6868
public void Logout()
6969
{
70-
settingsService.ClearAuthData();
71-
settingsService.SaveSettings();
70+
userSettingsService.ClearAuthData();
71+
userSettingsService.SaveSettings();
7272
LoginEvent?.Invoke(this, new QmLoginEventArgs(QmLoginEventStatusEnum.Logout));
7373
}
7474

@@ -100,20 +100,20 @@ public async Task RefreshAsync()
100100

101101
public QmLadder GetLadderForId(int ladderId) => qmData.Ladders.FirstOrDefault(l => l.Id == ladderId);
102102

103-
public string GetCachedEmail() => qmSettings.Email;
103+
public string GetCachedEmail() => qmUserSettings.Email;
104104

105-
public string GetCachedLadder() => qmSettings.Ladder;
105+
public string GetCachedLadder() => qmUserSettings.Ladder;
106106

107107
public bool IsServerAvailable() => apiService.IsServerAvailable();
108108

109109
public bool IsLoggedIn()
110110
{
111-
if (qmSettings.AuthData == null)
111+
if (qmUserSettings.AuthData == null)
112112
return false;
113113

114114
try
115115
{
116-
DecodeToken(qmSettings.AuthData.Token);
116+
DecodeToken(qmUserSettings.AuthData.Token);
117117
}
118118
catch (TokenExpiredException)
119119
{
@@ -126,15 +126,15 @@ public bool IsLoggedIn()
126126
return false;
127127
}
128128

129-
apiService.SetToken(qmSettings.AuthData.Token);
129+
apiService.SetToken(qmUserSettings.AuthData.Token);
130130

131131
return true;
132132
}
133133

134134
public void SetLadder(string ladder)
135135
{
136-
qmSettings.Ladder = ladder;
137-
settingsService.SaveSettings();
136+
qmUserSettings.Ladder = ladder;
137+
userSettingsService.SaveSettings();
138138

139139
FetchLadderMapsForAbbrAsync(ladder);
140140
}
@@ -165,14 +165,14 @@ private async Task FinishLogin(QmAuthData authData, bool refresh, string email =
165165
{
166166
if (authData == null)
167167
{
168-
settingsService.ClearAuthData();
168+
userSettingsService.ClearAuthData();
169169
LoginEvent?.Invoke(this, new QmLoginEventArgs(refresh ? QmLoginEventStatusEnum.FailedRefresh : QmLoginEventStatusEnum.Unauthorized));
170170
return;
171171
}
172172

173-
qmSettings.AuthData = authData;
174-
qmSettings.Email = email ?? qmSettings.Email;
175-
settingsService.SaveSettings();
173+
qmUserSettings.AuthData = authData;
174+
qmUserSettings.Email = email ?? qmUserSettings.Email;
175+
userSettingsService.SaveSettings();
176176
await FetchDataAsync();
177177
}
178178

DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/QuickMatchSettingsService.cs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using ClientCore;
4-
using Newtonsoft.Json;
53
using Rampastring.Tools;
64

75
namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch
86
{
97
public class QuickMatchSettingsService
108
{
11-
private static readonly string SettingsFile = $"{ProgramConstants.ClientUserFilesPath}QuickMatchSettings.ini";
9+
private static readonly string SettingsFile = ClientConfiguration.Instance.QuickMatchPath;
1210

1311
private const string BasicSectionKey = "Basic";
14-
private const string AuthDataKey = "AuthData";
15-
private const string EmailKey = "Email";
16-
private const string LadderKey = "Ladder";
12+
13+
private const string BaseUrlKey = "BaseUrl";
14+
private const string LoginUrlKey = "LoginUrl";
15+
private const string RefreshUrlKey = "RefreshUrl";
16+
private const string ServerStatusUrlKey = "ServerStatusUrl";
17+
private const string GetUserAccountsUrlKey = "GetUserAccountsUrl";
18+
private const string GetLaddersUrlKey = "GetLaddersUrl";
19+
private const string GetLadderMapsUrlKey = "GetLadderMapsUrl";
20+
1721

1822
private QmSettings qmSettings;
1923

@@ -24,49 +28,36 @@ public QmSettings LoadSettings()
2428

2529
qmSettings = new QmSettings();
2630
if (!File.Exists(SettingsFile))
27-
return qmSettings;
31+
SaveSettings(); // init the settings file
2832

2933
var iniFile = new IniFile(SettingsFile);
3034
var basicSection = iniFile.GetSection(BasicSectionKey);
3135
if (basicSection == null)
3236
return qmSettings;
3337

34-
qmSettings.AuthData = GetAuthData(basicSection);
35-
qmSettings.Email = basicSection.GetStringValue(EmailKey, null);
36-
qmSettings.Ladder = basicSection.GetStringValue(LadderKey, null);
38+
qmSettings.BaseUrl = basicSection.GetStringValue(BaseUrlKey, QmSettings.DefaultBaseUrl);
39+
qmSettings.LoginUrl = basicSection.GetStringValue(LoginUrlKey, QmSettings.DefaultLoginUrl);
40+
qmSettings.RefreshUrl = basicSection.GetStringValue(RefreshUrlKey, QmSettings.DefaultRefreshUrl);
41+
qmSettings.ServerStatusUrl = basicSection.GetStringValue(ServerStatusUrlKey, QmSettings.DefaultServerStatusUrl);
42+
qmSettings.GetUserAccountsUrl = basicSection.GetStringValue(GetUserAccountsUrlKey, QmSettings.DefaultGetUserAccountsUrl);
43+
qmSettings.GetLaddersUrl = basicSection.GetStringValue(GetLaddersUrlKey, QmSettings.DefaultGetLaddersUrl);
44+
qmSettings.GetLadderMapsUrl = basicSection.GetStringValue(GetLadderMapsUrlKey, QmSettings.DefaultGetLadderMapsUrl);
3745

3846
return qmSettings;
3947
}
4048

41-
private static QmAuthData GetAuthData(IniSection section)
42-
{
43-
if (!section.KeyExists(AuthDataKey))
44-
return null;
45-
46-
string authDataValue = section.GetStringValue(AuthDataKey, null);
47-
if (string.IsNullOrEmpty(authDataValue))
48-
return null;
49-
50-
try
51-
{
52-
return JsonConvert.DeserializeObject<QmAuthData>(authDataValue);
53-
}
54-
catch (Exception e)
55-
{
56-
Logger.Log(e.StackTrace);
57-
return null;
58-
}
59-
}
60-
61-
public void ClearAuthData() => qmSettings.AuthData = null;
6249

6350
public void SaveSettings()
6451
{
6552
var iniFile = new IniFile();
6653
var basicSection = new IniSection(BasicSectionKey);
67-
basicSection.AddKey(EmailKey, qmSettings.Email ?? string.Empty);
68-
basicSection.AddKey(LadderKey, qmSettings.Ladder ?? string.Empty);
69-
basicSection.AddKey(AuthDataKey, JsonConvert.SerializeObject(qmSettings.AuthData) ?? string.Empty);
54+
basicSection.AddKey(BaseUrlKey, qmSettings.BaseUrl);
55+
basicSection.AddKey(LoginUrlKey, qmSettings.LoginUrl);
56+
basicSection.AddKey(RefreshUrlKey, qmSettings.RefreshUrl);
57+
basicSection.AddKey(ServerStatusUrlKey, qmSettings.ServerStatusUrl);
58+
basicSection.AddKey(GetUserAccountsUrlKey, qmSettings.GetUserAccountsUrl);
59+
basicSection.AddKey(GetLaddersUrlKey, qmSettings.GetLaddersUrl);
60+
basicSection.AddKey(GetLadderMapsUrlKey, qmSettings.GetLadderMapsUrl);
7061

7162
iniFile.AddSection(basicSection);
7263
iniFile.WriteIniFile(SettingsFile);

0 commit comments

Comments
 (0)