-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed commits: [ec3080ad7] # This is a combination of 2 commits. Add parallel starting Revert port
- Loading branch information
1 parent
5fe7b3d
commit 5b9f49d
Showing
14 changed files
with
327 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
addCommandHandler("sampltest", function() | ||
outputChatBox("ResourceA is running!") | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
addCommandHandler("sampltest", function() | ||
outputChatBox("ResourceB is running!") | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.Extensions.Logging; | ||
using SlipeServer.Server; | ||
using SlipeServer.Server.Elements; | ||
using SlipeServer.Server.Resources; | ||
using SlipeServer.Server.Resources.Providers; | ||
using SlipeServer.Server.Services; | ||
|
||
namespace SlipeServer.Example; | ||
|
||
public class ResourcesLogic | ||
{ | ||
private readonly ChatBox chatBox; | ||
private readonly ResourceService resourceService; | ||
private readonly ILogger logger; | ||
|
||
public ResourcesLogic(MtaServer mtaServer, ChatBox chatBox, IResourceProvider resourceProvider, ResourceService resourceService, ILogger logger) | ||
{ | ||
this.chatBox = chatBox; | ||
this.resourceService = resourceService; | ||
this.logger = logger; | ||
this.resourceService.StartResource("ResourceA"); | ||
this.resourceService.StartResource("ResourceB"); | ||
this.resourceService.AllStarted += HandleAllStarted; | ||
mtaServer.PlayerJoined += HandlePlayerJoin; | ||
} | ||
|
||
private async void HandlePlayerJoin(Player player) | ||
{ | ||
try | ||
{ | ||
await this.resourceService.StartResourcesForPlayer(player); | ||
} | ||
catch (Exception ex) | ||
{ | ||
this.logger.LogError(ex, "Failed to start resources for {playerName}", player.Name); | ||
} | ||
} | ||
|
||
private void HandleAllStarted(Player player) | ||
{ | ||
Console.WriteLine("All resources started for: {0}", player.Name); | ||
this.chatBox.Output($"All resources started for: {player.Name}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using SlipeServer.Server.Resources.Interpreters; | ||
using SlipeServer.Server.Resources.Providers; | ||
using SlipeServer.Server.Resources; | ||
using System.Collections.Generic; | ||
|
||
namespace SlipeServer.Server.TestTools; | ||
|
||
public class TestResourceProvider : IResourceProvider | ||
{ | ||
private readonly Dictionary<string, Resource> resources = []; | ||
private readonly object netIdLock = new(); | ||
private ushort netId = 0; | ||
|
||
public void AddResource(Resource resource) | ||
{ | ||
this.resources[resource.Name] = resource; | ||
} | ||
|
||
public Resource GetResource(string name) | ||
{ | ||
return this.resources[name]; | ||
} | ||
|
||
public IEnumerable<Resource> GetResources() | ||
{ | ||
return this.resources.Values; | ||
} | ||
|
||
public void Refresh() { } | ||
|
||
private IEnumerable<Resource> IndexResourceDirectory(string directory) => []; | ||
|
||
public IEnumerable<string> GetFilesForResource(string name) => []; | ||
|
||
public byte[] GetFileContent(string resource, string file) => []; | ||
|
||
public ushort ReserveNetId() | ||
{ | ||
lock (this.netIdLock) | ||
return this.netId++; | ||
} | ||
|
||
public void AddResourceInterpreter(IResourceInterpreter resourceInterpreter) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
SlipeServer.Server.Tests/Integration/Services/ResourceServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using SlipeServer.Packets.Definitions.Resources; | ||
using SlipeServer.Packets.Enums; | ||
using SlipeServer.Server.Clients; | ||
using SlipeServer.Server.PacketHandling.Handlers; | ||
using SlipeServer.Server.Resources; | ||
using SlipeServer.Server.Resources.Providers; | ||
using SlipeServer.Server.TestTools; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace SlipeServer.Server.Tests.Integration.Services; | ||
|
||
public class ClientPlayerResourceStartedPacketHandler : IPacketHandler<ResourceStartPacket> | ||
{ | ||
public PacketId PacketId => PacketId.PACKET_ID_RESOURCE_START; | ||
|
||
public ClientPlayerResourceStartedPacketHandler() | ||
{ | ||
} | ||
|
||
public void HandlePacket(IClient client, ResourceStartPacket packet) | ||
{ | ||
client.Player.TriggerResourceStarted(packet.NetId); | ||
} | ||
} | ||
|
||
public class ResourceServiceTests | ||
{ | ||
class ResourceA : Resource | ||
{ | ||
public ResourceA(MtaServer server) : base(server, server.RootElement, "ResourceA") { } | ||
} | ||
|
||
class ResourceB : Resource | ||
{ | ||
public ResourceB(MtaServer server) : base(server, server.RootElement, "ResourceB") { } | ||
} | ||
|
||
[InlineData(true)] | ||
[InlineData(false)] | ||
[Theory] | ||
public async Task ResourceServiceShouldWork(bool parallel) | ||
{ | ||
var server = new TestingServer((Configuration?)null, builder => | ||
{ | ||
builder.ConfigureServices(services => | ||
{ | ||
services.RemoveAll<IResourceProvider>(); | ||
services.AddSingleton<TestResourceProvider>(); | ||
services.AddSingleton<IResourceProvider>(x => x.GetRequiredService<TestResourceProvider>()); | ||
}); | ||
}); | ||
|
||
server.RegisterPacketHandler<ClientPlayerResourceStartedPacketHandler, ResourceStartPacket>(); | ||
|
||
var resourceProvider = server.GetRequiredService<TestResourceProvider>(); | ||
var resourceA = new ResourceA(server); | ||
server.AddAdditionalResource(resourceA, []); | ||
resourceProvider.AddResource(resourceA); | ||
var resourceB = new ResourceB(server); | ||
server.AddAdditionalResource(resourceB, []); | ||
resourceProvider.AddResource(resourceB); | ||
|
||
var resourceService = server.CreateInstance<ResourceService>(); | ||
using var monitor = resourceService.Monitor(); | ||
|
||
resourceService.StartResource("ResourceA"); | ||
resourceService.StartResource("ResourceB"); | ||
|
||
var player = server.AddFakePlayer(); | ||
await resourceService.StartResourcesForPlayer(player, parallel); | ||
|
||
using var _ = new AssertionScope(); | ||
monitor.OccurredEvents.Select(x => x.EventName).Should().BeEquivalentTo(["Started", "Started", "AllStarted"]); | ||
server.VerifyResourceStartedPacketSent(player, resourceA); | ||
server.VerifyResourceStartedPacketSent(player, resourceB); | ||
} | ||
} |
Oops, something went wrong.