Skip to content

Commit d6cb192

Browse files
committed
Revert change to EphemeralRandom & Nuget Update
1 parent b9270ab commit d6cb192

File tree

5 files changed

+43
-133
lines changed

5 files changed

+43
-133
lines changed

Zolian.GameServer/Zolian.GameServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</PropertyGroup>
4242

4343
<ItemGroup>
44-
<PackageReference Include="Chaos-Networking" Version="2.6.1" />
44+
<PackageReference Include="Chaos-Networking" Version="2.6.3" />
4545
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.0-preview2.25289.6" />
4646
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
4747
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0">
Lines changed: 13 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,33 @@
11
using System.Numerics;
2+
using Chaos.Common.Abstractions;
3+
using Chaos.Extensions.Common;
24

35
namespace Darkages.Common;
46

5-
/// <summary>
6-
/// Generates short-lived random numeric IDs and keeps a small history
7-
/// to greatly reduce the chance of immediate repeats.
8-
///
9-
/// Supports ONLY integral types:
10-
/// sbyte, byte, short, ushort, int, uint, long, ulong.
11-
/// </summary>
12-
/// <typeparam name="T">The numeric type to generate.</typeparam>
13-
public sealed class EphemeralRandomIdGenerator<T> where T : INumber<T>
7+
public sealed class EphemeralRandomIdGenerator<T> : IIdGenerator<T> where T : INumber<T>
148
{
15-
private const int HistorySize = ushort.MaxValue;
16-
private readonly T[] _history = new T[HistorySize];
17-
private int _index;
18-
private static readonly Func<T> _nextRandom = CreateGenerator();
19-
private static readonly EqualityComparer<T> _cmp = EqualityComparer<T>.Default;
9+
private const int HISTORY_SIZE = byte.MaxValue;
10+
private readonly T[] IdHistory = new T[HISTORY_SIZE];
11+
private int Index;
2012

21-
/// <summary>
22-
/// Shared singleton instance
23-
/// </summary>
24-
public static EphemeralRandomIdGenerator<T> Shared { get; } = new();
25-
26-
/// <summary>
27-
/// Returns the next random ID
28-
/// </summary>
13+
/// <inheritdoc />
2914
public T NextId
3015
{
3116
get
3217
{
18+
var index = Interlocked.Increment(ref Index) % HISTORY_SIZE;
3319
T id;
3420

35-
// Avoid immediate repeats within the last HistorySize values.
3621
do
3722
{
38-
id = _nextRandom();
39-
} while (IsDuplicate(id));
40-
41-
var slot = Interlocked.Increment(ref _index) % HistorySize;
42-
_history[slot] = id;
23+
id = Random.Shared.Next<T>();
24+
IdHistory[index] = id;
25+
} while (IdHistory.Count(num => num == id) > 1);
4326

4427
return id;
4528
}
4629
}
4730

48-
private bool IsDuplicate(T value)
49-
{
50-
for (var i = 0; i < HistorySize; i++)
51-
{
52-
if (_cmp.Equals(_history[i], value))
53-
return true;
54-
}
55-
56-
return false;
57-
}
58-
59-
/// <summary>
60-
/// Build a per-type random generator once, then reuse.
61-
/// </summary>
62-
private static Func<T> CreateGenerator()
63-
{
64-
var t = typeof(T);
65-
66-
return t switch
67-
{
68-
// signed
69-
var _ when t == typeof(sbyte) => () =>
70-
{
71-
var value = (sbyte)Random.Shared.Next(sbyte.MinValue, sbyte.MaxValue + 1);
72-
return (T)(object)value;
73-
}
74-
,
75-
76-
var _ when t == typeof(short) => () =>
77-
{
78-
var value = (short)Random.Shared.Next(short.MinValue, short.MaxValue + 1);
79-
return (T)(object)value;
80-
}
81-
,
82-
83-
var _ when t == typeof(int) => () =>
84-
{
85-
var value = Random.Shared.Next(int.MinValue, int.MaxValue); // max is exclusive
86-
return (T)(object)value;
87-
}
88-
,
89-
90-
var _ when t == typeof(long) => () =>
91-
{
92-
var value = Random.Shared.NextInt64(long.MinValue, long.MaxValue);
93-
return (T)(object)value;
94-
}
95-
,
96-
97-
// unsigned
98-
var _ when t == typeof(byte) => () =>
99-
{
100-
var value = (byte)Random.Shared.Next(0, byte.MaxValue + 1);
101-
return (T)(object)value;
102-
}
103-
,
104-
105-
var _ when t == typeof(ushort) => () =>
106-
{
107-
var value = (ushort)Random.Shared.Next(0, ushort.MaxValue + 1);
108-
return (T)(object)value;
109-
}
110-
,
111-
112-
var _ when t == typeof(uint) => () =>
113-
{
114-
var value = (uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1);
115-
return (T)(object)value;
116-
}
117-
,
118-
119-
var _ when t == typeof(ulong) => () =>
120-
{
121-
// Build a full 64-bit value from two 32-bit chunks.
122-
var hi = (ulong)(uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1);
123-
var lo = (ulong)(uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1);
124-
var value = (hi << 32) | lo;
125-
return (T)(object)value;
126-
}
127-
,
128-
129-
_ => throw new NotSupportedException(
130-
$"EphemeralRandomIdGenerator<{t.Name}> supports only integral types " +
131-
"(sbyte, byte, short, ushort, int, uint, long, ulong).")
132-
};
133-
}
31+
/// <inheritdoc />
32+
public static IIdGenerator<T> Shared { get; } = new EphemeralRandomIdGenerator<T>();
13433
}

Zolian.Server.Base/Network/Server/WorldServer.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
1+
using System.Collections.Concurrent;
2+
using System.Collections.Frozen;
3+
using System.Diagnostics;
4+
using System.Net;
5+
using System.Net.Sockets;
6+
using System.Numerics;
7+
using System.Text;
18
using Chaos.Cryptography;
29
using Chaos.Networking.Abstractions;
10+
using Chaos.Networking.Abstractions.Definitions;
311
using Chaos.Networking.Entities.Client;
412
using Chaos.Packets;
513
using Chaos.Packets.Abstractions;
6-
714
using Darkages.CommandSystem;
815
using Darkages.Common;
916
using Darkages.Database;
1017
using Darkages.Enums;
1118
using Darkages.GameScripts.Mundanes.Generic;
19+
using Darkages.GameScripts.Spells;
20+
using Darkages.Managers;
1221
using Darkages.Meta;
1322
using Darkages.Models;
1423
using Darkages.Network.Client;
24+
using Darkages.Network.Client.Abstractions;
1525
using Darkages.Network.Components;
1626
using Darkages.Object;
1727
using Darkages.ScriptingBase;
1828
using Darkages.Sprites;
29+
using Darkages.Sprites.Entity;
1930
using Darkages.Templates;
2031
using Darkages.Types;
32+
using JetBrains.Annotations;
2133
using Microsoft.Extensions.Logging;
2234
using ServiceStack;
23-
24-
using System.Collections.Concurrent;
25-
using System.Collections.Frozen;
26-
using System.Diagnostics;
27-
using System.Net;
28-
using System.Net.Sockets;
29-
using System.Numerics;
30-
using System.Text;
31-
using Chaos.Networking.Abstractions.Definitions;
32-
using Darkages.Managers;
33-
using JetBrains.Annotations;
34-
using Redirect = Chaos.Networking.Entities.Redirect;
35-
using ServerOptions = Chaos.Networking.Options.ServerOptions;
3635
using IWorldClient = Darkages.Network.Client.Abstractions.IWorldClient;
3736
using MapFlags = Darkages.Enums.MapFlags;
38-
using Darkages.Network.Client.Abstractions;
39-
using Darkages.Sprites.Entity;
37+
using Redirect = Chaos.Networking.Entities.Redirect;
38+
using ServerOptions = Chaos.Networking.Options.ServerOptions;
4039

4140
namespace Darkages.Network.Server;
4241

@@ -1396,6 +1395,8 @@ private static void AssailRoutine(IWorldClient lpClient)
13961395

13971396
// Skill animation and execute
13981397
ExecuteAssail(lpClient, skill);
1398+
// Stress Test
1399+
//AnimationOnAssailStressTest(lpClient);
13991400

14001401
// Skill cleanup
14011402
skill.CurrentCooldown = skill.Template.Cooldown;
@@ -1411,6 +1412,16 @@ private static void AssailRoutine(IWorldClient lpClient)
14111412
lpClient.SendServerMessage(ServerMessageType.ActiveMessage, $"{{=bOverburdened!");
14121413
}
14131414

1415+
// Stress test
1416+
//private async static void AnimationOnAssailStressTest(IWorldClient lpClient)
1417+
//{
1418+
// while (true)
1419+
// {
1420+
// lpClient.Aisling.SendAnimationNearby((ushort)Random.Shared.Next(0, 300), null, lpClient.Aisling.Serial);
1421+
// await Task.Delay(50);
1422+
// }
1423+
//}
1424+
14141425
private static void ExecuteAssail(IWorldClient lpClient, Skill lpSkill, bool optExecuteScript = true)
14151426
{
14161427
// On skill "Assail" also use weapon script, if there is one

Zolian.Server.Base/Zolian.Server.Base.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
</PropertyGroup>
4343

4444
<ItemGroup>
45-
<PackageReference Include="Chaos-Networking" Version="2.6.1" />
45+
<PackageReference Include="Chaos-Networking" Version="2.6.3" />
4646
<PackageReference Include="Dapper.StrongName" Version="2.1.66" />
4747
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.0-preview2.25289.6" />
4848
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />

ZolianTest/ZolianTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Chaos-Networking" Version="2.6.1" />
22+
<PackageReference Include="Chaos-Networking" Version="2.6.3" />
2323
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.0-preview2.25289.6" />
2424
<PackageReference Include="Microsoft.DependencyValidation.Analyzers" Version="0.11.0" />
2525
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />

0 commit comments

Comments
 (0)