Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New manager by Lemird v1 #151

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions Content.Client/Humanoid/MarkingPicker.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using Content.Corvax.Interfaces.Shared;
using Robust.Client.Player; // A-13 Sponsor service
using Content.Shared.Andromeda.AndromedaSponsorService; // A-13 Sponsor service
using Content.Shared.Humanoid;
using Content.Shared.Humanoid.Markings;
using Content.Shared.Humanoid.Prototypes;
Expand All @@ -19,8 +20,8 @@ public sealed partial class MarkingPicker : Control
{
[Dependency] private readonly MarkingManager _markingManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private ISharedSponsorsManager? _sponsorsManager; // Corvax-Sponsors

[Dependency] private readonly GetSponsorAllowedMarkingsMethod _getSponsorAllowedMarkingsMethod = default!; // A-13 Sponsor service
[Dependency] private readonly IPlayerManager _playerManager = default!; // A-13 Sponsor service
public Action<MarkingSet>? OnMarkingAdded;
public Action<MarkingSet>? OnMarkingRemoved;
public Action<MarkingSet>? OnMarkingColorChange;
Expand Down Expand Up @@ -227,10 +228,45 @@ public void Populate(string filter)

var item = CMarkingsUnused.AddItem($"{GetMarkingName(marking)}", marking.Sprites[0].Frame0());
item.Metadata = marking;
// Corvax-Sponsors-Start
if (marking.SponsorOnly && _sponsorsManager != null)
item.Disabled = !_sponsorsManager.GetClientPrototypes().Contains(marking.ID);
// Corvax-Sponsors-End

// A-13 Sponsor service start
var player = _playerManager.LocalSession;
var validatedFile = _getSponsorAllowedMarkingsMethod.FileIsValid();

if (player != null)
{
bool isSponsor = _getSponsorAllowedMarkingsMethod.IsSponsor(player.UserId);
if (validatedFile == true)
{
if (isSponsor == true)
{
bool allowedMarkings = _getSponsorAllowedMarkingsMethod.GetSponsorAllowedMarkings(player.UserId);

if (marking.SponsorOnly)
{
item.Disabled = !allowedMarkings;
Logger.Info($"{player} is bad player, allowedMarking = {allowedMarkings}");
}
}
else
{
if (marking.SponsorOnly)
{
item.Disabled = true;
Logger.Error($"Blocking all sponsor item, its doesn't sponsor");
}
}
}
else
{
if (marking.SponsorOnly)
{
item.Disabled = true;
Logger.Error($"Blocking all sponsor item, file valid");
}
}
}
// A-13 Sponsor service end
}

CMarkingPoints.Visible = _currentMarkings.PointsLeft(_selectedMarkingCategory) != -1;
Expand Down
37 changes: 31 additions & 6 deletions Content.Client/Humanoid/SingleMarkingPicker.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using Content.Corvax.Interfaces.Shared;
using Robust.Client.Player; // A-13 Sponsor service
using Content.Shared.Andromeda.AndromedaSponsorService; // A-13 Sponsor service
using Content.Shared.Humanoid.Markings;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
Expand All @@ -12,7 +13,8 @@ namespace Content.Client.Humanoid;
public sealed partial class SingleMarkingPicker : BoxContainer
{
[Dependency] private readonly MarkingManager _markingManager = default!;
private ISharedSponsorsManager? _sponsorsManager; // Corvax-Sponsors
[Dependency] private readonly GetSponsorAllowedMarkingsMethod _getSponsorAllowedMarkingsMethod = default!; // A-13 Sponsor service
[Dependency] private readonly IPlayerManager _playerManager = default!; // A-13 Sponsor service

/// <summary>
/// What happens if a marking is selected.
Expand Down Expand Up @@ -193,10 +195,33 @@ public void PopulateList(string filter)
{
var item = MarkingList.AddItem(Loc.GetString($"marking-{id}"), marking.Sprites[0].Frame0());
item.Metadata = marking.ID;
// Corvax-Sponsors-Start
if (marking.SponsorOnly && _sponsorsManager != null)
item.Disabled = !_sponsorsManager.GetClientPrototypes().Contains(marking.ID);
// Corvax-Sponsors-End

// A-13 Sponsor service start
var player = _playerManager.LocalSession;
var validatedFile = _getSponsorAllowedMarkingsMethod.FileIsValid();

if (validatedFile == true)
{
if (player != null)
{
bool allowedMarkings = _getSponsorAllowedMarkingsMethod.GetSponsorAllowedMarkings(player.UserId);

if (marking.SponsorOnly)
{
item.Disabled = !allowedMarkings;
Logger.Info($"{player} is bad player, allowedMarking = {allowedMarkings}");
}
}
}
else
{
if (marking.SponsorOnly)
{
item.Disabled = true;
Logger.Error($"Blocking all sponsor item, file valid");
}
}
// A-13 Sponsor service end

if (_markings[Slot].MarkingId == id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Content.Server.Andromeda.AndromedaSponsorService;

public sealed class AndromedaSponsorManager
{
private readonly HashSet<Guid> _sponsors = new HashSet<Guid>();
private readonly string _sponsorsFilePath = "Resources/Prototypes/Andromeda/sponsors.txt";

public void LoadSponsors()
{
if (!File.Exists(_sponsorsFilePath))
{
File.WriteAllText(_sponsorsFilePath, string.Empty);
}

_sponsors.Clear();

foreach (var line in File.ReadLines(_sponsorsFilePath))
{
string[] parts = line.Split(';');

if (parts.Length >= 2 && Guid.TryParse(parts[0], out var guid))
{
_sponsors.Add(guid);
}
}
}

public void SaveSponsors(Guid userId, bool? allowedAntag = null, string? color = null, bool? allowedMarkings = null)
{
var lines = File.ReadAllLines(_sponsorsFilePath).ToList();
var index = lines.FindIndex(line => line.StartsWith(userId.ToString()));

if (index != -1)
{
lines[index] = $"{userId};{allowedAntag ?? false};{color ?? ""};{allowedMarkings ?? false}";
}
else
{
lines.Add($"{userId};{allowedAntag ?? false};{color ?? ""};{allowedMarkings ?? false}");
}

File.WriteAllLines(_sponsorsFilePath, lines);
}

public bool IsSponsor(Guid userId)
{
return _sponsors.Contains(userId);
}

public List<Guid> GetActiveSponsors()
{
return new List<Guid>(_sponsors);
}

public void AddSponsor(Guid userId, bool allowedAntag, string color, bool allowedMarkings)
{
_sponsors.Add(userId);

SaveSponsors(userId, allowedAntag, color, allowedMarkings);
}

public void RemoveSponsor(Guid userId)
{
_sponsors.Remove(userId);

var lines = File.ReadAllLines(_sponsorsFilePath).ToList();
var index = lines.FindIndex(line => line.StartsWith(userId.ToString()));

if (index != -1)
{
lines.RemoveAt(index);
File.WriteAllLines(_sponsorsFilePath, lines);
}
}

public bool GetSponsorAllowedAntag(Guid userId)
{
string[] lines = File.ReadAllLines(_sponsorsFilePath);

foreach (string line in lines)
{
string[] parts = line.Split(';');

if (Guid.Parse(parts[0]) == userId)
{
bool allowedAntag;
if (bool.TryParse(parts[1], out allowedAntag))
{
return allowedAntag;
}
}
}

return false;
}

public Color? GetSponsorOocColor(Guid userId)
{
string[] lines = File.ReadAllLines(_sponsorsFilePath);

foreach (string line in lines)
{
string[] parts = line.Split(';');

if (Guid.Parse(parts[0]) == userId)
{
if (string.IsNullOrWhiteSpace(parts[2]))
{
return null;
}

Color color;
if (Color.TryParse(parts[2], out color))
{
return color;
}
}
}

return null;
}

public bool IsValidColor(string color)
{
return Regex.IsMatch(color, @"^#[0-9A-Fa-f]{6}$");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Content.Server.Andromeda.AndromedaSponsorService;

namespace Content.Server.Andromeda.Commands.SponsorManagerCommand;

[AdminCommand(AdminFlags.Host)]
public sealed class AddSponsorCommand : IConsoleCommand
{
[Dependency] private readonly AndromedaSponsorManager _sponsorManager = default!;

public string Command => "addsponsor";
public string Description => "Adds a sponsor by their user ID.";
public string Help => $"Usage: {Command} <user ID> [allowedAntag] [OOC color] [allowedMarkings]";

public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length == 0)
{
shell.WriteLine(Help);
return;
}

if (!Guid.TryParse(args[0], out var userId))
{
shell.WriteLine($"Invalid user ID: {args[0]}");
return;
}

bool allowedAntag = false;
if (args.Length > 1)
{
if (!bool.TryParse(args[1], out allowedAntag))
{
shell.WriteLine($"Invalid allowedAntag value: {args[1]}");
return;
}
}

string color = "";
if (args.Length > 2)
{
color = args[2];

if (!_sponsorManager.IsValidColor(args[2]))
{
shell.WriteLine($"Invalid color: {args[2]}");
return;
}
}

bool allowedMarkings = false;
if (args.Length > 3)
{
if (!bool.TryParse(args[3], out allowedMarkings))
{
shell.WriteLine($"Invalid allowedMarkings value: {args[3]}");
return;
}
}

if (_sponsorManager.IsSponsor(userId))
{
_sponsorManager.SaveSponsors(userId, allowedAntag, color, allowedMarkings);
shell.WriteLine($"Sponsor data for user {userId} updated successfully.");
}
else
{
_sponsorManager.AddSponsor(userId, allowedAntag, color, allowedMarkings);
shell.WriteLine($"User {userId} added as a sponsor.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Robust.Shared.Console;
using Content.Server.Andromeda.AndromedaSponsorService;
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Shared.Network;
using Robust.Server.Player;

namespace Content.Server.Andromeda.Commands.SponsorManagerCommand;

[AdminCommand(AdminFlags.Admin)]
public sealed class ListActiveSponsorsCommand : IConsoleCommand
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly AndromedaSponsorManager _sponsorManager = default!;

public string Command => "listsponsors";
public string Description => "Lists all active sponsors.";
public string Help => $"Usage: {Command}";

public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var activeSponsors = new List<Guid>();

foreach (var session in _playerManager.Sessions)
{
if (_sponsorManager.IsSponsor(session.UserId))
{
activeSponsors.Add(session.UserId);
}
}

shell.WriteLine("Active sponsors:");
foreach (var sponsor in activeSponsors)
{
var session = _playerManager.GetSessionById(new NetUserId(sponsor));
shell.WriteLine($"- {session?.Name}");
}
}
}
Loading
Loading