Skip to content

Commit

Permalink
Create mock data for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
devo1929 committed Nov 8, 2022
1 parent 4df0f17 commit 0501562
Show file tree
Hide file tree
Showing 21 changed files with 768 additions and 10 deletions.
72 changes: 72 additions & 0 deletions DXMainClient/DXMainClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,78 @@
</ItemGroup>
<ItemGroup>
<Content Include="clienticon.ico" />
<Content Include="MockData\qm_find_match_fatal_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_gamefinished_request.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_gamefinished_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_game_spawned_request.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_game_spawned_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_im_ready_request.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_im_ready_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_please_wait_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_reached_request.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_reached_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_request.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_find_match_spawn_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_ladders_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_ladder_maps_ra2_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_ladder_stats_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_login_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="MockData\qm_user_accounts_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_fatal_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_game_spawned_request.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_game_spawned_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_gamefinished_request.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_gamefinished_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_im_ready_request.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_im_ready_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_please_wait_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_reached_request.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_reached_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_request.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_find_match_spawn_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_ladder_maps_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_ladder_stats_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_ladders_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_login_response.json" />
<None Remove="Domain\Multiplayer\CnCNet\QuickMatch\MockData\qm_user_accounts_response.json" />
<None Remove="MockData\qm_ladder_maps_yr_response.json" />
<Content Include="MockData\qm_ladder_maps_yr_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="JWT" Version="9.0.3" />
Expand Down
21 changes: 11 additions & 10 deletions DXMainClient/Domain/Multiplayer/CnCNet/QuickMatch/QmApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch;

public class QmApiService : IDisposable
{
private static bool useMockService = true;
private HttpClient _httpClient;
private readonly QmSettings qmSettings;
private string _token;

private static QmApiService instance;

private QmApiService()
protected QmApiService()
{
qmSettings = QmSettingsService.GetInstance().GetSettings();
}

public static QmApiService GetInstance() => instance ??= new QmApiService();
public static QmApiService GetInstance() => instance ??= useMockService ? new QmMockApiService() : new QmApiService();

public void SetToken(string token)
{
Expand All @@ -35,7 +36,7 @@ public void SetToken(string token)
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_token}");
}

public async Task<IEnumerable<QmLadderMap>> LoadLadderMapsForAbbrAsync(string ladderAbbreviation)
public virtual async Task<IEnumerable<QmLadderMap>> LoadLadderMapsForAbbrAsync(string ladderAbbreviation)
{
HttpClient httpClient = GetHttpClient();
string url = string.Format(qmSettings.GetLadderMapsUrlFormat, ladderAbbreviation);
Expand All @@ -46,7 +47,7 @@ public async Task<IEnumerable<QmLadderMap>> LoadLadderMapsForAbbrAsync(string la
return JsonConvert.DeserializeObject<IEnumerable<QmLadderMap>>(await response.Content.ReadAsStringAsync());
}

public async Task<QmLadderStats> LoadLadderStatsForAbbrAsync(string ladderAbbreviation)
public virtual async Task<QmLadderStats> LoadLadderStatsForAbbrAsync(string ladderAbbreviation)
{
HttpClient httpClient = GetHttpClient();
string url = string.Format(qmSettings.GetLadderStatsUrlFormat, ladderAbbreviation);
Expand All @@ -57,7 +58,7 @@ public async Task<QmLadderStats> LoadLadderStatsForAbbrAsync(string ladderAbbrev
return JsonConvert.DeserializeObject<QmLadderStats>(await response.Content.ReadAsStringAsync());
}

public async Task<IEnumerable<QmUserAccount>> LoadUserAccountsAsync()
public virtual async Task<IEnumerable<QmUserAccount>> LoadUserAccountsAsync()
{
HttpClient httpClient = GetHttpClient();
HttpResponseMessage response = await httpClient.GetAsync(qmSettings.GetUserAccountsUrl);
Expand All @@ -67,7 +68,7 @@ public async Task<IEnumerable<QmUserAccount>> LoadUserAccountsAsync()
return JsonConvert.DeserializeObject<IEnumerable<QmUserAccount>>(await response.Content.ReadAsStringAsync());
}

public async Task<IEnumerable<QmLadder>> LoadLaddersAsync()
public virtual async Task<IEnumerable<QmLadder>> LoadLaddersAsync()
{
HttpClient httpClient = GetHttpClient();
HttpResponseMessage response = await httpClient.GetAsync(qmSettings.GetLaddersUrl);
Expand All @@ -77,7 +78,7 @@ public async Task<IEnumerable<QmLadder>> LoadLaddersAsync()
return JsonConvert.DeserializeObject<IEnumerable<QmLadder>>(await response.Content.ReadAsStringAsync());
}

public async Task<QmAuthData> LoginAsync(string email, string password)
public virtual async Task<QmAuthData> LoginAsync(string email, string password)
{
HttpClient httpClient = GetHttpClient();
var postBodyContent = new StringContent(JsonConvert.SerializeObject(new QMLoginRequest() { Email = email, Password = password }), Encoding.Default, "application/json");
Expand All @@ -86,7 +87,7 @@ public async Task<QmAuthData> LoginAsync(string email, string password)
return await HandleLoginResponse(response, QmStrings.LoggingInUnknownErrorFormat);
}

public async Task<QmAuthData> RefreshAsync()
public virtual async Task<QmAuthData> RefreshAsync()
{
HttpClient httpClient = GetHttpClient();
HttpResponseMessage response = await httpClient.GetAsync(qmSettings.RefreshUrl);
Expand Down Expand Up @@ -127,7 +128,7 @@ private async Task<QmAuthData> HandleFailedLoginResponse(HttpResponseMessage res
throw new ClientRequestException(message, response.StatusCode);
}

public bool IsServerAvailable()
public virtual bool IsServerAvailable()
{
HttpClient httpClient = GetHttpClient();
HttpResponseMessage response = httpClient.GetAsync(qmSettings.ServerStatusUrl).Result;
Expand All @@ -137,7 +138,7 @@ public bool IsServerAvailable()
private HttpClient GetHttpClient() =>
_httpClient ??= new HttpClient { BaseAddress = new Uri(qmSettings.BaseUrl), Timeout = TimeSpan.FromSeconds(10) };

public async Task<QmRequestResponse> QuickMatchRequestAsync(string ladder, string playerName, QmRequest qmRequest)
public virtual async Task<QmRequestResponse> QuickMatchRequestAsync(string ladder, string playerName, QmRequest qmRequest)
{
HttpClient httpClient = GetHttpClient();
string url = string.Format(qmSettings.QuickMatchUrlFormat, ladder, playerName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ClientCore.Exceptions;
using DTAClient.Domain.Multiplayer.CnCNet.QuickMatch.Models;
using Newtonsoft.Json;
using Rampastring.Tools;

namespace DTAClient.Domain.Multiplayer.CnCNet.QuickMatch;

public class QmMockApiService : QmApiService
{
public override async Task<IEnumerable<QmLadderMap>> LoadLadderMapsForAbbrAsync(string ladderAbbreviation) => LoadMockData<IEnumerable<QmLadderMap>>($"qm_ladder_maps_{ladderAbbreviation}_response.json");

public override async Task<QmLadderStats> LoadLadderStatsForAbbrAsync(string ladderAbbreviation) => LoadMockData<QmLadderStats>("qm_ladder_stats_response.json");

public override async Task<IEnumerable<QmUserAccount>> LoadUserAccountsAsync() => LoadMockData<IEnumerable<QmUserAccount>>("qm_user_accounts_response.json");

public override async Task<IEnumerable<QmLadder>> LoadLaddersAsync() => LoadMockData<IEnumerable<QmLadder>>("qm_ladders_response.json");

public override async Task<QmAuthData> LoginAsync(string email, string password) => LoadMockData<QmAuthData>("qm_login_response.json");

public override async Task<QmAuthData> RefreshAsync() => LoadMockData<QmAuthData>("qm_login_response.json");

public override async Task<QmRequestResponse> QuickMatchRequestAsync(string ladder, string playerName, QmRequest qmRequest)
{
const string responseType = QmResponseTypes.Wait;
return responseType switch
{
QmResponseTypes.Error => LoadMockData<QmRequestResponse>("qm_find_match_spawn_response.json"),
QmResponseTypes.Spawn => LoadMockData<QmRequestResponse>("qm_find_match_spawn_response.json"),
QmResponseTypes.Wait => LoadMockData<QmRequestResponse>("qm_find_match_please_wait_response.json"),
_ => throw new NotImplementedException("unknown mock response type")
};
}

public override bool IsServerAvailable() => true;

private T LoadMockData<T>(string mockDataFileName)
{
string content = File.ReadAllText($"MockData/{mockDataFileName}");

return JsonConvert.DeserializeObject<T>(content);
}
}
5 changes: 5 additions & 0 deletions DXMainClient/MockData/qm_find_match_fatal_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "fatal",
"message": "fatal message",
"description": "fatal description"
}
14 changes: 14 additions & 0 deletions DXMainClient/MockData/qm_find_match_game_spawned_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"peers": [
{
"address": "127.0.0.1",
"id": "1234567890",
"port": 51143,
"rtt": 59
}
],
"seed": 123,
"status": "GameSpawned",
"type": "update",
"version": "2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "update qm match: GameSpawned"
}
6 changes: 6 additions & 0 deletions DXMainClient/MockData/qm_find_match_gamefinished_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"seed": 123,
"status": "GameFinished",
"type": "update",
"version": "1.74"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "update qm match: GameFinished"
}
6 changes: 6 additions & 0 deletions DXMainClient/MockData/qm_find_match_im_ready_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"seed": 123,
"status": "Ready",
"type": "update",
"version": "2.0"
}
3 changes: 3 additions & 0 deletions DXMainClient/MockData/qm_find_match_im_ready_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "update qm match: Ready"
}
5 changes: 5 additions & 0 deletions DXMainClient/MockData/qm_find_match_please_wait_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "please wait",
"checkback": 10,
"no_sooner_than": 5
}
6 changes: 6 additions & 0 deletions DXMainClient/MockData/qm_find_match_reached_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"seed": 123,
"status": "Reached",
"type": "update",
"version": "2.0"
}
3 changes: 3 additions & 0 deletions DXMainClient/MockData/qm_find_match_reached_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "update qm match: Reached"
}
52 changes: 52 additions & 0 deletions DXMainClient/MockData/qm_find_match_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"ai_dat": false,
"ddraw": "38f9496b915547ea816a1cd716b65c0d",
"exe_hash": "f265a5c6ce05488884563a5de6e323d7",
"ip_address": "127.0.0.1",
"ip_port": 51143,
"ipv6_address": "",
"ipv6_port": 0,
"lan_ip": "127.0.0.1",
"lan_port": 51143,
"map_bitfield": 2147483647,
"map_sides": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"platform": "win32",
"session": "",
"side": 0,
"type": "match me up",
"version": "2.0"
}
50 changes: 50 additions & 0 deletions DXMainClient/MockData/qm_find_match_spawn_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type": "spawn",
"gameID": 456,
"spawn": {
"SpawnLocations": {
"Multi2": -1,
"Multi1": -1
},
"Settings": {
"UIMapName": "Test Map",
"MapHash": "35f8f92ebbb94b62918326da5837fedd",
"Seed": 123,
"GameID": 456,
"WOLGameID": 456,
"Host": "No",
"IsSpectator": "No",
"Name": "TestUserMe",
"Port": 51143,
"Side": 0,
"Color": 1,
"GameSpeed": "0",
"Credits": "10000",
"UnitCount": "0",
"Superweapons": "Yes",
"Tournament": "1",
"ShortGame": "Yes",
"Bases": "Yes",
"MCVRedeploy": "Yes",
"MultipleFactory": "Yes",
"Crates": "No",
"GameMode": "Battle",
"FrameSendRate": "4",
"DisableSWvsYuri": "Yes"
},
"Other1": {
"Name": "TestUser2",
"Side": 0,
"Color": 0,
"Ip": "127.0.0.1",
"Port": 51143,
"IPv6": "",
"PortV6": 0,
"LanIP": "",
"LanPort": 51143
}
},
"client": {
"show_map_preview": 1
}
}
Loading

0 comments on commit 0501562

Please sign in to comment.