Skip to content

Commit

Permalink
Handle explicit party search list requests
Browse files Browse the repository at this point in the history
Closes #76
  • Loading branch information
AlexMacocian committed Aug 12, 2024
1 parent c6427a0 commit 45fc018
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 53 deletions.
20 changes: 15 additions & 5 deletions GuildWarsPartySearch.Bot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ static int send_websocket(const std::string& payload) {
return 0;
}

static int send_ping() {
connect_websocket();
LogInfo("Sending ping");
last_websocket_message = time_get_ms();
ws->sendPing();
return 0;
}

static int send_party_advertisements() {
std::vector<PartySearchAdvertisement> ads;
for (const auto& ad : party_search_advertisements) {
Expand Down Expand Up @@ -436,8 +444,6 @@ static easywsclient::WebSocket::pointer connect_websocket() {

static int main_bot(void* param)
{
load_configuration();

CallbackEntry_Init(&EventType_WorldMapLeave_entry, on_map_left, NULL);
RegisterEvent(EventType_WorldMapLeave, &EventType_WorldMapLeave_entry);

Expand All @@ -455,7 +461,9 @@ static int main_bot(void* param)

CallbackEntry_Init(&EventType_PartySearchType_entry, update_party_search_advertisement, NULL);
RegisterEvent(EventType_PartySearchType, &EventType_PartySearchType_entry);


load_configuration();

wait_until_ingame();

while (running) {
Expand All @@ -466,8 +474,10 @@ static int main_bot(void* param)
party_advertisements_pending = false;
}
if (ws) {
if (time_get_ms() - last_websocket_message > 30000)
send_websocket("ping");
if (time_get_ms() - last_websocket_message > 30000) {
send_ping();

}
ws->dispatch(on_websocket_message);
ws->poll();
}
Expand Down
17 changes: 10 additions & 7 deletions GuildWarsPartySearch/Endpoints/LiveFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
using GuildWarsPartySearch.Server.Services.PartySearch;
using System.Core.Extensions;
using System.Extensions;
using System.Text;

namespace GuildWarsPartySearch.Server.Endpoints;

public sealed class LiveFeed : WebSocketRouteBase<TextContent, PartySearchList>
public sealed class LiveFeed : WebSocketRouteBase<LiveFeedRequest, PartySearchList>
{
private readonly static byte[] PongAnswer = Encoding.UTF8.GetBytes("pong");

private readonly IPartySearchService partySearchService;
private readonly ILiveFeedService liveFeedService;
private readonly ILogger<LiveFeed> logger;
Expand All @@ -25,12 +22,18 @@ public LiveFeed(
this.logger = logger.ThrowIfNull();
}

public override async Task ExecuteAsync(TextContent? content, CancellationToken cancellationToken)
public override async Task ExecuteAsync(LiveFeedRequest? message, CancellationToken cancellationToken)
{
if (content?.Text?.Equals("ping", StringComparison.OrdinalIgnoreCase) is true)
if (message?.GetFullList is not true)
{
await this.WebSocket!.SendAsync(PongAnswer, System.Net.WebSockets.WebSocketMessageType.Text, true, cancellationToken);
return;
}

var searches = await this.partySearchService.GetAllPartySearches(cancellationToken);
await this.SendMessage(new PartySearchList
{
Searches = searches
}, cancellationToken);
}

public override async Task SocketAccepted(CancellationToken cancellationToken)
Expand Down
83 changes: 45 additions & 38 deletions GuildWarsPartySearch/Endpoints/PostPartySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public override async Task ExecuteAsync(PostPartySearchRequest? message, Cancell

await this.botStatusService.RecordBotUpdateActivity(userAgent, this.Context.RequestAborted);
var result = await this.partySearchService.PostPartySearch(message, cancellationToken);
var response = result.Switch<PostPartySearchResponse>(
onSuccess: _ =>
var response = await result.Switch<Task<PostPartySearchResponse>>(
onSuccess: async _ =>
{
this.liveFeedService.PushUpdate(new PartySearch
await this.liveFeedService.PushUpdate(new PartySearch
{
Map = message?.Map,
District = message?.District ?? 0,
Expand All @@ -90,7 +90,14 @@ public override async Task ExecuteAsync(PostPartySearchRequest? message, Cancell
return e;
}).ToList(),
}, cancellationToken);
return Success;
var response = Success.Result;
if (message?.GetFullList is true)
{
response.PartySearches = await this.partySearchService.GetAllPartySearches(cancellationToken);
}
return response;
},
onFailure: failure => failure switch
{
Expand Down Expand Up @@ -120,105 +127,105 @@ public override async Task ExecuteAsync(PostPartySearchRequest? message, Cancell
}
}

private static PostPartySearchResponse Success => new()
private static Task<PostPartySearchResponse> Success => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Posted entries"
};
});

private static PostPartySearchResponse InvalidPayload => new()
private static Task<PostPartySearchResponse> InvalidPayload => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid payload"
};
});

private static PostPartySearchResponse InvalidMap => new()
private static Task<PostPartySearchResponse> InvalidMap => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid map"
};
});

private static PostPartySearchResponse InvalidEntries => new()
private static Task<PostPartySearchResponse> InvalidEntries => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid entries"
};
});

private static PostPartySearchResponse InvalidSender => new()
private static Task<PostPartySearchResponse> InvalidSender => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid sender"
};
});

private static PostPartySearchResponse InvalidMessage => new()
private static Task<PostPartySearchResponse> InvalidMessage => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid message"
};
});

private static PostPartySearchResponse InvalidDistrictRegion => new()
private static Task<PostPartySearchResponse> InvalidDistrictRegion => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid district region"
};
});

private static PostPartySearchResponse InvalidDistrictLanguage => new()
private static Task<PostPartySearchResponse> InvalidDistrictLanguage => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid district language"
};
});

private static PostPartySearchResponse InvalidDistrictNumber => new()
private static Task<PostPartySearchResponse> InvalidDistrictNumber => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid district number"
};
});

private static PostPartySearchResponse InvalidPartyId => new()
private static Task<PostPartySearchResponse> InvalidPartyId => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid party id"
};
});

private static PostPartySearchResponse InvalidHeroCount => new()
private static Task<PostPartySearchResponse> InvalidHeroCount => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid hero count"
};
});

private static PostPartySearchResponse InvalidHardMode => new()
private static Task<PostPartySearchResponse> InvalidHardMode => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid hard mode"
};
});

private static PostPartySearchResponse InvalidSearchType => new()
private static Task<PostPartySearchResponse> InvalidSearchType => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid search type"
};
});

private static PostPartySearchResponse InvalidPrimary => new()
private static Task<PostPartySearchResponse> InvalidPrimary => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid primary"
};
});

private static PostPartySearchResponse InvalidSecondary => new()
private static Task<PostPartySearchResponse> InvalidSecondary => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid secondary"
};
});

private static PostPartySearchResponse InvalidLevel => new()
private static Task<PostPartySearchResponse> InvalidLevel => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Invalid level"
};
});

private static PostPartySearchResponse UnspecifiedFailure => new()
private static Task<PostPartySearchResponse> UnspecifiedFailure => Task.FromResult(new PostPartySearchResponse
{
Result = 0,
Description = "Unspecified failure"
};
});
}
12 changes: 12 additions & 0 deletions GuildWarsPartySearch/Models/Endpoints/LiveFeedRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GuildWarsPartySearch.Server.Attributes;
using GuildWarsPartySearch.Server.Converters;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Server.Models.Endpoints;

[WebSocketConverter<JsonWebSocketMessageConverter<LiveFeedRequest>, LiveFeedRequest>]
public sealed class LiveFeedRequest
{
[JsonPropertyName("full_list")]
public bool GetFullList { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace GuildWarsPartySearch.Server.Models.Endpoints;
[WebSocketConverter<JsonWebSocketMessageConverter<PostPartySearchRequest>, PostPartySearchRequest>]
public sealed class PostPartySearchRequest
{
[JsonPropertyName("full_list")]
public bool GetFullList { get; set; }

[JsonPropertyName("map_id")]
public Map? Map { get; set; }

Expand Down
10 changes: 7 additions & 3 deletions GuildWarsPartySearch/Models/Endpoints/PostPartySearchResponse.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using GuildWarsPartySearch.Server.Attributes;
using GuildWarsPartySearch.Server.Converters;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace GuildWarsPartySearch.Server.Models.Endpoints;

[WebSocketConverter<JsonWebSocketMessageConverter<PostPartySearchResponse>, PostPartySearchResponse>]
public sealed class PostPartySearchResponse
{
[JsonProperty(nameof(Result))]
[JsonPropertyName(nameof(Result))]
public int Result { get; set; }

[JsonProperty(nameof(Description))]
[JsonPropertyName(nameof(Description))]
public string? Description { get; set; }

[JsonPropertyName(nameof(PartySearches))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public List<PartySearch>? PartySearches { get; set; }
}

0 comments on commit 45fc018

Please sign in to comment.