Skip to content

Commit

Permalink
more group commands
Browse files Browse the repository at this point in the history
- /kick, /promote
- group stuff should work with names with spaces i think maybe
  • Loading branch information
Sichii committed Jan 16, 2025
1 parent 024c877 commit be76733
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 5 deletions.
30 changes: 28 additions & 2 deletions Chaos/Collections/Group.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region
using Chaos.DarkAges.Definitions;
using Chaos.Extensions.Common;
using Chaos.Messaging.Abstractions;
using Chaos.Models.World;
using Chaos.Models.World.Abstractions;
Expand Down Expand Up @@ -245,6 +246,27 @@ public void Leave(Aisling aisling)
}
}

public void Promote(Aisling aisling)
{
using var @lock = Sync.EnterScope();

if (aisling.Equals(Leader))
{
aisling.SendActiveMessage("You dumbly point to yourself");

return;
}

Members.Remove(aisling);
Members.Insert(0, aisling);

foreach (var member in Members)
{
member.SendActiveMessage($"{aisling.Name} has been promoted to group leader");
member.Client.SendSelfProfile();
}
}

private bool Remove(Aisling aisling)
{
using var @lock = Sync.EnterScope();
Expand All @@ -267,10 +289,14 @@ public override string ToString()
var groupString = "Group members";

foreach (var user in Members)
{
var name = user.Name.ReplaceI(" ", "_");

if (user.Equals(Leader))
groupString += '\n' + $"* {user.Name}";
groupString += '\n' + $"* {name}";
else
groupString += '\n' + $" {user.Name}";
groupString += '\n' + $" {name}";
}

groupString += '\n' + $"Total {Count}";

Expand Down
4 changes: 4 additions & 0 deletions Chaos/Messaging/GroupInviteCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#region
using Chaos.Collections.Common;
using Chaos.Extensions.Common;
using Chaos.Messaging.Abstractions;
using Chaos.Models.World;
using Chaos.Networking.Abstractions;
using Chaos.Services.Other.Abstractions;
#endregion

namespace Chaos.Messaging;

Expand All @@ -19,6 +21,8 @@ public ValueTask ExecuteAsync(Aisling source, ArgumentCollection args)
if (!args.TryGetNext<string>(out var targetName))
return default;

targetName = targetName.ReplaceI("_", " ");

var targetClient = ClientRegistry.FirstOrDefault(c => c.Aisling.Name.EqualsI(targetName));

if ((targetClient == null) || (source.IsAdmin != targetClient.Aisling.IsAdmin))
Expand Down
39 changes: 39 additions & 0 deletions Chaos/Messaging/KickCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region
using Chaos.Collections.Common;
using Chaos.Extensions.Common;
using Chaos.Messaging.Abstractions;
using Chaos.Models.World;
using Chaos.Networking.Abstractions;
using Chaos.Services.Other.Abstractions;
#endregion

namespace Chaos.Messaging;

[Command("kick", false, "<targetName>")]
public class KickCommand(IGroupService groupService, IClientRegistry<IChaosWorldClient> clientRegistry) : ICommand<Aisling>
{
private readonly IClientRegistry<IChaosWorldClient> ClientRegistry = clientRegistry;
private readonly IGroupService GroupService = groupService;

/// <inheritdoc />
public ValueTask ExecuteAsync(Aisling source, ArgumentCollection args)
{
if (!args.TryGetNext<string>(out var targetName))
return default;

targetName = targetName.ReplaceI("_", " ");

var targetClient = ClientRegistry.FirstOrDefault(c => c.Aisling.Name.EqualsI(targetName));

if ((targetClient == null) || (source.IsAdmin != targetClient.Aisling.IsAdmin))
{
source.SendOrangeBarMessage($"{targetName} can not be found");

return default;
}

GroupService.Kick(source, targetClient.Aisling);

return default;
}
}
39 changes: 39 additions & 0 deletions Chaos/Messaging/PromoteLeaderCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region
using Chaos.Collections.Common;
using Chaos.Extensions.Common;
using Chaos.Messaging.Abstractions;
using Chaos.Models.World;
using Chaos.Networking.Abstractions;
using Chaos.Services.Other.Abstractions;
#endregion

namespace Chaos.Messaging;

[Command("promote", false, "<targetName>")]
public class PromoteLeaderCommand(IGroupService groupService, IClientRegistry<IChaosWorldClient> clientRegistry) : ICommand<Aisling>
{
private readonly IClientRegistry<IChaosWorldClient> ClientRegistry = clientRegistry;
private readonly IGroupService GroupService = groupService;

/// <inheritdoc />
public ValueTask ExecuteAsync(Aisling source, ArgumentCollection args)
{
if (!args.TryGetNext<string>(out var targetName))
return default;

targetName = targetName.ReplaceI("_", " ");

var targetClient = ClientRegistry.FirstOrDefault(c => c.Aisling.Name.EqualsI(targetName));

if ((targetClient == null) || (source.IsAdmin != targetClient.Aisling.IsAdmin))
{
source.SendOrangeBarMessage($"{targetName} can not be found");

return default;
}

GroupService.Promote(source, targetClient.Aisling);

return default;
}
}
4 changes: 4 additions & 0 deletions Chaos/Messaging/RequestGroupInviteCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#region
using Chaos.Collections.Common;
using Chaos.Extensions.Common;
using Chaos.Messaging.Abstractions;
using Chaos.Models.World;
using Chaos.Networking.Abstractions;
using Chaos.Services.Other.Abstractions;
#endregion

namespace Chaos.Messaging;

Expand All @@ -19,6 +21,8 @@ public ValueTask ExecuteAsync(Aisling source, ArgumentCollection args)
if (!args.TryGetNext<string>(out var targetName))
return default;

targetName = targetName.ReplaceI("_", " ");

var targetClient = ClientRegistry.FirstOrDefault(c => c.Aisling.Name.EqualsI(targetName));

if ((targetClient == null) || (source.IsAdmin != targetClient.Aisling.IsAdmin))
Expand Down
4 changes: 2 additions & 2 deletions Chaos/Scripting/ReactorTileScripts/LeaderboardScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LeaderboardScript : ReactorTileScriptBase
/// <inheritdoc />
public LeaderboardScript(ReactorTile subject, IStorage<LeaderboardObj> storage)
: base(subject)
=> Leaderboard = storage.GetInstance("test_leaderboard");
=> Leaderboard = storage;

/// <inheritdoc />
public override void OnClicked(Aisling source)
Expand All @@ -42,6 +42,6 @@ public override void OnClicked(Aisling source)

public sealed class LeaderboardObj
{
public Dictionary<string, int> Entries { get; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, int> Entries { get; set; } = new(StringComparer.OrdinalIgnoreCase);
}
}
4 changes: 4 additions & 0 deletions Chaos/Services/Other/Abstractions/IGroupService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#region
using Chaos.Models.World;
#endregion

namespace Chaos.Services.Other.Abstractions;

Expand All @@ -14,5 +16,7 @@ enum RequestType
void AcceptRequestToJoin(Aisling sender, Aisling receiver);
RequestType? DetermineRequestType(Aisling sender, Aisling receiver);
void Invite(Aisling sender, Aisling receiver);
void Kick(Aisling sender, Aisling receiver);
void Promote(Aisling sender, Aisling receiver);
void RequestToJoin(Aisling sender, Aisling receiver);
}
58 changes: 58 additions & 0 deletions Chaos/Services/Other/GroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,64 @@ public void Invite(Aisling sender, Aisling receiver)
}
}

/// <inheritdoc />
public void Kick(Aisling sender, Aisling receiver)
{
var group = sender.Group;

if (group is null)
{
sender.SendActiveMessage("You are not in a group");

return;
}

if (group != receiver.Group)
{
sender.SendActiveMessage("You are not in the same group");

return;
}

if (!group.Leader.Equals(sender))
{
sender.SendActiveMessage("You are not the group leader");

return;
}

group.Kick(receiver);
}

/// <inheritdoc />
public void Promote(Aisling sender, Aisling receiver)
{
var group = sender.Group;

if (group is null)
{
sender.SendActiveMessage("You are not in a group");

return;
}

if (group != receiver.Group)
{
sender.SendActiveMessage("You are not in the same group");

return;
}

if (!group.Leader.Equals(sender))
{
sender.SendActiveMessage("You are not the group leader");

return;
}

group.Promote(receiver);
}

public void RequestToJoin(Aisling sender, Aisling receiver)
{
using var @lock = Sync.EnterScope();
Expand Down
2 changes: 2 additions & 0 deletions Chaos/Services/Servers/WorldServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@ public ValueTask OnGroupInvite(IChaosWorldClient client, in Packet packet)

ValueTask InnerOnGroupInvite(IChaosWorldClient localClient, GroupInviteArgs localArgs)
{
localArgs.TargetName = localArgs.TargetName.ReplaceI("_", " ");

var target = Aislings.FirstOrDefault(user => user.Name.EqualsI(localArgs.TargetName));

if (target == null)
Expand Down
7 changes: 7 additions & 0 deletions Data/LocalStorage/LeaderboardObj.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"default": {
"entries": {
"mink": 4
}
}
}
7 changes: 7 additions & 0 deletions Data/LocalStorage/LeaderboardObj.json.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"default": {
"entries": {
"mink": 3
}
}
}
13 changes: 12 additions & 1 deletion Tools/SeqConfigurator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Reflection;
#region
using System.Reflection;
using Chaos.Extensions.Common;
using Chaos.NLog.Logging.Definitions;
using NLog;
using Seq.Api;
using Seq.Api.Model.Dashboarding;
using Seq.Api.Model.Signals;
using SeqConfigurator.Builders;
#endregion

using var seq = new SeqConnection("http://localhost:5341", "AdminGuestSeqToken");
await seq.EnsureConnectedAsync(TimeSpan.FromSeconds(30));
Expand Down Expand Up @@ -114,6 +116,7 @@ await DashboardBuilder.Create(seq)
.WithCharts(
BuildMapCountChart,
BuildAislingCountChart,
BuildUniqueIpAddressesChart,
BuildDeltasChart,
BuildAverageDeltasChart,
BuildUpperDeltasChart)
Expand Down Expand Up @@ -161,6 +164,14 @@ static void BuildAislingCountChart(ChartBuilder chartBuilder)
.WithWhere("Has(AislingName)")
.WithDisplayStyle(MeasurementDisplayType.Line, false, true));

static void BuildUniqueIpAddressesChart(ChartBuilder chartBuilder)
=> chartBuilder.WithTitle("Unique Ip Addresses")
.WithDimensions(12, 2)
.WithQuery(
queryBuilder => queryBuilder.WithSelect(("UniqueIpAddresses", "count(distinct(Aisling.IpAddress))"))
.WithWhere("Has(Aisling)")
.WithDisplayStyle(MeasurementDisplayType.Line, false, true));

static void BuildMapCountChart(ChartBuilder chartBuilder)
=> chartBuilder.WithTitle("MapCount")
.WithSignalExpression("DeltaMonitor")
Expand Down

0 comments on commit be76733

Please sign in to comment.