-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
67 changed files
with
2,328 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using DotNetty.Common.Utilities; | ||
using NetScape.Abstractions.Model; | ||
using NetScape.Abstractions.Model.Game; | ||
|
||
namespace NetScape.Abstractions | ||
{ | ||
public class Constants | ||
{ | ||
public static readonly int RegionSize = 8; | ||
public static readonly int ArchiveCount = 9; | ||
public static readonly AttributeKey<Player> PlayerAttributeKey = AttributeKey<Player>.ValueOf("Player"); | ||
public static int RegionSize { get; } = 8; | ||
public static AttributeKey<Player> PlayerAttributeKey { get; } = AttributeKey<Player>.ValueOf("Player"); | ||
public static Position HomePosition { get; } = new Position(3333, 3333, 0); | ||
} | ||
} |
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,110 @@ | ||
using Autofac; | ||
using NetScape.Abstractions.FileSystem; | ||
using NetScape.Abstractions.Interfaces.Login; | ||
using NetScape.Abstractions.Model.Login; | ||
using Serilog; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NetScape.Abstractions.Login | ||
{ | ||
public abstract class DefaultLoginProcessor<TRequest, TResponse> : ILoginProcessor<TRequest, TResponse>, | ||
IStartable, | ||
IDisposable | ||
where TRequest : LoginRequest<TResponse> | ||
{ | ||
protected internal readonly ILogger _logger; | ||
private readonly IList<TRequest> _loginRequests = new List<TRequest>(); | ||
|
||
private readonly object _lockObject = new object(); | ||
|
||
private CancellationToken _cancellationToken; | ||
private CancellationTokenSource _cancellationTokenSource; | ||
|
||
public DefaultLoginProcessor(ILogger logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Enqueues the specified request. | ||
/// </summary> | ||
/// <param name="request">The request.</param> | ||
/// <exception cref="InvalidOperationException">Login already exists</exception> | ||
public void Enqueue(TRequest request) | ||
{ | ||
lock (_lockObject) | ||
{ | ||
var loginExists = _loginRequests.Any(t => t.Credentials.Username.Equals(request.Credentials.Username, StringComparison.InvariantCultureIgnoreCase) | ||
&& t.Credentials.Password.Equals(request.Credentials.Password, StringComparison.InvariantCultureIgnoreCase)); | ||
|
||
if (loginExists) | ||
{ | ||
throw new InvalidOperationException("Login already exists"); | ||
} | ||
|
||
_loginRequests.Add(request); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Processes a single by retriving the player from <see cref="IPlayerSerializer"/> | ||
/// </summary> | ||
/// <param name="request">The login request.</param> | ||
/// <returns></returns> | ||
protected abstract Task<TResponse> ProcessAsync(TRequest request); | ||
|
||
/// <summary> | ||
/// Handles the login queue | ||
/// </summary> | ||
private async Task ProcessLoginsAsync() | ||
{ | ||
while (!_cancellationToken.IsCancellationRequested) | ||
{ | ||
while (_loginRequests.Count > 0) | ||
{ | ||
var requests = _loginRequests.ToList(); | ||
var tasks = requests.Select(loginTask => | ||
(request: loginTask, responseTask: ProcessAsync(loginTask))) | ||
.ToList(); | ||
await Task.WhenAll(tasks.Select(t => t.responseTask)); | ||
tasks.ForEach(t => | ||
{ | ||
var responseTask = t.responseTask; | ||
var request = t.request; | ||
if (responseTask.IsCompletedSuccessfully) | ||
{ | ||
_loginRequests.Remove(t.request); | ||
t.request.Result = request.Result; | ||
_ = request.OnResult(responseTask.Result); | ||
_logger.Debug("Processed Login Request: {@LoginRequest}", request.Credentials); | ||
} | ||
}); | ||
} | ||
await Task.Delay(600); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Perform once-off startup processing. | ||
/// </summary> | ||
public void Start() | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
_cancellationToken = _cancellationTokenSource.Token; | ||
Task.Factory.StartNew(ProcessLoginsAsync, _cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
_cancellationTokenSource?.Cancel(); | ||
_cancellationTokenSource?.Dispose(); | ||
} | ||
} | ||
} |
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
File renamed without changes.
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
31 changes: 31 additions & 0 deletions
31
NetScape.Modules.FourSevenFour.Game/FourSevenFourGameModule.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,31 @@ | ||
using Autofac; | ||
using NetScape.Abstractions.Interfaces.Game.Interface; | ||
using NetScape.Abstractions.Interfaces.Game.Player; | ||
using NetScape.Abstractions.Interfaces.Messages; | ||
using NetScape.Modules.FourSevenFour.Game.Interface; | ||
using NetScape.Modules.FourSevenFour.Game.Players; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NetScape.Modules.FourSevenFour.Game | ||
{ | ||
public class FourSevenFourGameModule : Module | ||
{ | ||
protected override void Load(ContainerBuilder builder) | ||
{ | ||
builder.RegisterType<TabManager>().As<ITabManager>(); | ||
builder.RegisterType<PlayerInitializer>().As<IPlayerInitializer>(); | ||
|
||
#region Handlers | ||
builder.RegisterAssemblyTypes(typeof(FourSevenFourGameModule).Assembly) | ||
.AsClosedTypesOf(typeof(IMessageDecoder<>)) | ||
.As<IMessageDecoder>() | ||
.SingleInstance(); | ||
#endregion | ||
base.Load(builder); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
NetScape.Modules.FourSevenFour.Game/Messages/Decoders/WalkingQueueMessageDecoder.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,48 @@ | ||
using NetScape.Abstractions.Model; | ||
using NetScape.Abstractions.Model.Game; | ||
using NetScape.Modules.Messages; | ||
using NetScape.Modules.Messages.Builder; | ||
using NetScape.Modules.Messages.Models; | ||
using System.Linq; | ||
|
||
namespace NetScape.Modules.FourSevenFour.Game.Messages.Decoders | ||
{ | ||
public class WalkingQueueMessageDecoder : MessageDecoderBase<FourSevenFourDecoderMessages.Types.WalkingQueueMessage> | ||
{ | ||
public override int[] Ids { get; } = new int[] { 11, 46, 59 }; | ||
public override FrameType FrameType { get; } = FrameType.VariableByte; | ||
|
||
protected override FourSevenFourDecoderMessages.Types.WalkingQueueMessage Decode(Player player, MessageFrame frame) | ||
{ | ||
var reader = new MessageFrameReader(frame); | ||
var length = frame.Payload.ReadableBytes; | ||
|
||
if (frame.Id == 11) | ||
{ | ||
length -= 14; // strip off anti-cheat data | ||
} | ||
|
||
int steps = (length - 5) / 2; | ||
int[,] path = new int[steps, 2]; | ||
for (int i = 0; i < steps; i++) | ||
{ | ||
path[i, 0] = (int)reader.GetSigned(MessageType.Byte); | ||
path[i, 1] = (int)reader.GetSigned(MessageType.Byte, DataTransformation.Subtract); | ||
} | ||
int x = (int)reader.GetUnsigned(MessageType.Short, DataTransformation.Add); | ||
int y = (int)reader.GetUnsigned(MessageType.Short, DataOrder.Little); | ||
var run = reader.GetUnsigned(MessageType.Byte, DataTransformation.Negate) == 1; | ||
|
||
var positions = new Position[steps + 1]; | ||
positions[0] = new Position(x, y); | ||
for (int i = 0; i < steps; i++) | ||
{ | ||
positions[i + 1] = new Position(path[i, 0] + x, path[i, 1] + y); | ||
} | ||
FourSevenFourDecoderMessages.Types.WalkingQueueMessage walkingQueueMessage = new() { Run = run, }; | ||
walkingQueueMessage.X.Add(positions.Select(t => t.X)); | ||
walkingQueueMessage.Y.Add(positions.Select(t => t.Y)); | ||
return walkingQueueMessage; | ||
} | ||
} | ||
} |
Oops, something went wrong.