Files
OldThink/Content.Server/GameTicking/Presets/PresetTraitor.cs
Alex Evgrashin e5df8dbee3 Moving PDA to ECS (#4538)
* Moved pen slot to separate component

* Moved it all to more generic item slot class

* Add sounds

* Item slots now supports many slots

* Some clean-up

* Refactored slots a bit

* Moving ID card out

* Moving pda to system

* Moving PDA owner to ECS

* Moved PDA flashlight to separate component

* Toggle lights work through events

* Fixing UI

* Moving uplink to separate component

* Continue moving uplink to separate component

* More cleaning

* Removing pda shared

* Nuked shared pda component

* Fixed flashlight

* Pen slot now showed in UI

* Light toggle now shows correctly in UI

* Small refactoring of item slots

* Added contained entity

* Fixed tests

* Finished with PDA

* Moving PDA uplink to separate window

* Adding-removing uplink should show new button

* Working on a better debug

* Debug command to add uplink

* Uplink send state to UI

* Almost working UI

* Uplink correcty updates when you buy-sell items

* Ups

* Moved localization to separate file

* Minor fixes

* Removed item slots methods events

* Removed PDA owner name

* Removed one uplink event

* Deleted all uplink events

* Removed flashlight events

* Update Content.Shared/Traitor/Uplink/UplinkVisuals.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Containers/ItemSlot/ItemSlotsSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Containers/ItemSlot/ItemSlotsSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/GameTicking/Presets/PresetTraitorDeathMatch.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Item slots system review

* Flashlight review

* PDA to XAML

* Move UplinkMenu to seperate class, fix WeightedColors methods

* Move UI to XAML

* Moved events to entity id

* Address review

* Removed uplink extensions

* Minor fix

* Moved item slots to shared

* My bad Robust...

* Fixed pda sound

* Fixed pda tests

* Fixed pda test again

Co-authored-by: Alexander Evgrashin <evgrashin.adl@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Visne <vincefvanwijk@gmail.com>
2021-10-03 15:05:52 +11:00

233 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Server.Objectives.Interfaces;
using Content.Server.PDA;
using Content.Server.PDA.Managers;
using Content.Server.Players;
using Content.Server.Traitor;
using Content.Server.Traitor.Uplink;
using Content.Server.Traitor.Uplink.Components;
using Content.Server.Traitor.Uplink.Systems;
using Content.Shared.CCVar;
using Content.Shared.Dataset;
using Content.Shared.Inventory;
using Content.Shared.Traitor.Uplink;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Server.GameTicking.Presets
{
[GamePreset("traitor")]
public class PresetTraitor : GamePreset
{
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] protected readonly IEntityManager EntityManager = default!;
[Dependency] private readonly IUplinkManager _uplinkManager = default!;
public override string ModeTitle => Loc.GetString("traitor-title");
private int MinPlayers { get; set; }
private int PlayersPerTraitor { get; set; }
private int MaxTraitors { get; set; }
private int CodewordCount { get; set; }
private int StartingBalance { get; set; }
private float MaxDifficulty { get; set; }
private int MaxPicks { get; set; }
private readonly List<TraitorRole> _traitors = new ();
public override bool Start(IReadOnlyList<IPlayerSession> readyPlayers, bool force = false)
{
MinPlayers = _cfg.GetCVar(CCVars.TraitorMinPlayers);
PlayersPerTraitor = _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
MaxTraitors = _cfg.GetCVar(CCVars.TraitorMaxTraitors);
CodewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount);
StartingBalance = _cfg.GetCVar(CCVars.TraitorStartingBalance);
MaxDifficulty = _cfg.GetCVar(CCVars.TraitorMaxDifficulty);
MaxPicks = _cfg.GetCVar(CCVars.TraitorMaxPicks);
if (!force && readyPlayers.Count < MinPlayers)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("traitor-not-enough-ready-players", ("readyPlayersCount", readyPlayers.Count), ("minimumPlayers", MinPlayers)));
return false;
}
if (readyPlayers.Count == 0)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("traitor-no-one-ready"));
return false;
}
var list = new List<IPlayerSession>(readyPlayers);
var prefList = new List<IPlayerSession>();
foreach (var player in list)
{
if (!ReadyProfiles.ContainsKey(player.UserId))
{
continue;
}
var profile = ReadyProfiles[player.UserId];
if (profile.AntagPreferences.Contains("Traitor"))
{
prefList.Add(player);
}
}
var numTraitors = MathHelper.Clamp(readyPlayers.Count / PlayersPerTraitor,
1, MaxTraitors);
for (var i = 0; i < numTraitors; i++)
{
IPlayerSession traitor;
if(prefList.Count < numTraitors)
{
if (list.Count == 0)
{
Logger.InfoS("preset", "Insufficient ready players to fill up with traitors, stopping the selection.");
break;
}
traitor = _random.PickAndTake(list);
Logger.InfoS("preset", "Insufficient preferred traitors, picking at random.");
}
else
{
traitor = _random.PickAndTake(prefList);
list.Remove(traitor);
Logger.InfoS("preset", "Selected a preferred traitor.");
}
var mind = traitor.Data.ContentData()?.Mind;
if (mind == null)
{
Logger.ErrorS("preset", "Failed getting mind for picked traitor.");
continue;
}
// creadth: we need to create uplink for the antag.
// PDA should be in place already, so we just need to
// initiate uplink account.
DebugTools.AssertNotNull(mind.OwnedEntity);
var uplinkAccount = new UplinkAccount(mind.OwnedEntity!.Uid, StartingBalance);
_uplinkManager.AddNewAccount(uplinkAccount);
if (!EntityManager.EntitySysManager.GetEntitySystem<UplinkSystem>()
.AddUplink(mind.OwnedEntity, uplinkAccount))
continue;
var traitorRole = new TraitorRole(mind);
mind.AddRole(traitorRole);
_traitors.Add(traitorRole);
}
var adjectives = _prototypeManager.Index<DatasetPrototype>("adjectives").Values;
var verbs = _prototypeManager.Index<DatasetPrototype>("verbs").Values;
var codewordPool = adjectives.Concat(verbs).ToList();
var finalCodewordCount = Math.Min(CodewordCount, codewordPool.Count);
var codewords = new string[finalCodewordCount];
for (var i = 0; i < finalCodewordCount; i++)
{
codewords[i] = _random.PickAndTake(codewordPool);
}
foreach (var traitor in _traitors)
{
traitor.GreetTraitor(codewords);
}
EntitySystem.Get<GameTicker>().AddGameRule<RuleTraitor>();
return true;
}
public override void OnGameStarted()
{
var objectivesMgr = IoCManager.Resolve<IObjectivesManager>();
foreach (var traitor in _traitors)
{
//give traitors their objectives
var difficulty = 0f;
for (var pick = 0; pick < MaxPicks && MaxDifficulty > difficulty; pick++)
{
var objective = objectivesMgr.GetRandomObjective(traitor.Mind);
if (objective == null) continue;
if (traitor.Mind.TryAddObjective(objective))
difficulty += objective.Difficulty;
}
}
}
public override string GetRoundEndDescription()
{
var result = Loc.GetString(
"traitor-round-end-result",
("traitorCount", _traitors.Count)
);
foreach (var traitor in _traitors)
{
if (traitor.Mind.TryGetSession(out var session))
{
result += "\n" + Loc.GetString("traitor-user-was-a-traitor", ("user", session.Name));
}
var objectives = traitor.Mind.AllObjectives.ToArray();
if (objectives.Length == 0)
{
result += ".\n";
continue;
}
result += Loc.GetString("traitor-objective-list-start");
foreach (var objectiveGroup in objectives.GroupBy(o => o.Prototype.Issuer))
{
result += $"\n[color=#87cefa]{objectiveGroup.Key}[/color]";
foreach (var objective in objectiveGroup)
{
foreach (var condition in objective.Conditions)
{
var progress = condition.Progress;
if (progress > 0.99f)
{
result += "\n- " + Loc.GetString(
"traitor-objective-condition-success",
("condition", condition.Title),
("markupColor", "green")
);
}
else
{
result += "\n- " + Loc.GetString(
"traitor-objective-condition-fail",
("condition", condition.Title),
("progress", (int) (progress * 100)),
("markupColor", "red")
);
}
}
}
}
}
return result;
}
}
}