forked from space-syndicate/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from Lemirda/upstream-may
New manager by Lemird v1
- Loading branch information
Showing
14 changed files
with
478 additions
and
37 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
131 changes: 131 additions & 0 deletions
131
Content.Server/Andromeda/AndromedaSponsorService/AndromedaSponsorManager.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,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}$"); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
Content.Server/Andromeda/Commands/SponsorManagerCommand/AddSponsor.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,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."); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Content.Server/Andromeda/Commands/SponsorManagerCommand/ListActiveSponsor.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,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}"); | ||
} | ||
} | ||
} |
Oops, something went wrong.