Skip to content

Commit

Permalink
update formatting to match lang standards
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-robertson committed Feb 20, 2023
1 parent b912e0a commit 8be291f
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 311 deletions.
281 changes: 191 additions & 90 deletions src/Commands/ConsoleCmdAmnesia.cs

Large diffs are not rendered by default.

237 changes: 163 additions & 74 deletions src/Data/Config.cs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/Data/Values.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Linq;

namespace Amnesia.Data {
internal class Values {
namespace Amnesia.Data
{
internal class Values
{

// cvars
public const string CVarLongTermMemoryLevel = "amnesiaLongTermMemoryLevel";
Expand Down
22 changes: 15 additions & 7 deletions src/Handlers/EntityKilled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@
using Amnesia.Utilities;
using System;

namespace Amnesia.Handlers {
internal class EntityKilled {
namespace Amnesia.Handlers
{
internal class EntityKilled
{
private static readonly ModLog<EntityKilled> log = new ModLog<EntityKilled>();

internal static void Handle(Entity killedEntity, Entity killerEntity) {
try {
internal static void Handle(Entity killedEntity, Entity killerEntity)
{
try
{
if (killerEntity == null || killerEntity.entityType != EntityType.Player) { return; }
if (!Config.PositiveOutlookTimeOnKill.TryGetValue(killedEntity.GetDebugName(), out var entry)) {
if (!Config.PositiveOutlookTimeOnKill.TryGetValue(killedEntity.GetDebugName(), out var entry))
{
return;
}

var minutes = entry.value / 60f;
MessagingSystem.Broadcast($"[007fff]{killerEntity.GetDebugName()} just took down a {entry.caption}!");
MessagingSystem.Broadcast($"[007fff]Relief washes over each survivor as a newfound confidence takes hold: [00ff80]all online players receive Double XP for {(minutes > 1 ? minutes + " Minutes!" : entry.value + " Seconds!")}");
foreach (var player in GameManager.Instance.World.Players.list) {
foreach (var player in GameManager.Instance.World.Players.list)
{
_ = PlayerHelper.AddPositiveOutlookTime(player, entry.value);
}
} catch (Exception e) {
}
catch (Exception e)
{
log.Error("HandleEntityKilled", e);
}
}
Expand Down
41 changes: 28 additions & 13 deletions src/Handlers/GameMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,60 @@
using Amnesia.Utilities;
using System;

namespace Amnesia.Handlers {
internal class GameMessage {
namespace Amnesia.Handlers
{
internal class GameMessage
{
private static readonly ModLog<GameMessage> log = new ModLog<GameMessage>();

public static bool Handle(ClientInfo clientInfo, EnumGameMessages messageType, string message, string mainName, bool localizeMain, string secondaryName, bool localizeSecondary) {
public static bool Handle(ClientInfo clientInfo, EnumGameMessages messageType, string message, string mainName, bool localizeMain, string secondaryName, bool localizeSecondary)
{
if (!Config.Loaded) { return true; } // do not interrupt other mods from processing event
try {
if (EnumGameMessages.EntityWasKilled != messageType) {
try
{
if (EnumGameMessages.EntityWasKilled != messageType)
{
return true; // only focus on entity killed messages
}

if (!GameManager.Instance.World.Players.dict.TryGetValue(clientInfo.entityId, out var player)) {
if (!GameManager.Instance.World.Players.dict.TryGetValue(clientInfo.entityId, out var player))
{
return true; // player not present; skip
}

if (player.Buffs.HasBuff(Values.BuffBloodmoonLifeProtection) || player.Buffs.HasBuff(Values.BuffPostBloodmoonLifeProtection)) {
if (player.Buffs.HasBuff(Values.BuffBloodmoonLifeProtection) || player.Buffs.HasBuff(Values.BuffPostBloodmoonLifeProtection))
{
log.Trace($"{clientInfo.InternalId.CombinedString} ({player.GetDebugName()}) died but had bloodmoon memory protection.");
return true; // player had protection
} else {
}
else
{
log.Trace($"{clientInfo.InternalId.CombinedString} ({player.GetDebugName()}) died and did not have bloodmoon memory protection.");
}

if (Config.ProtectMemoryDuringPvp && mainName != secondaryName) {
if (Config.ProtectMemoryDuringPvp && mainName != secondaryName)
{
var killerClient = ConnectionManager.Instance.Clients.GetForNameOrId(secondaryName);
if (killerClient != null) {
if (killerClient != null)
{
log.Trace($"{clientInfo.InternalId.CombinedString} ({player.GetDebugName()}) was killed by {secondaryName} but this server has pvp deaths set to not harm memory.");
return true; // being killed in pvp doesn't count against player
}
}

if (player.Progression.Level < Config.LongTermMemoryLevel) {
if (player.Progression.Level < Config.LongTermMemoryLevel)
{
log.Trace($"{clientInfo.InternalId.CombinedString} ({player.GetDebugName()}) died but had not yet reached the configured LongTermMemoryLevel of {Config.LongTermMemoryLevel}");
return true;
}

if (!ModApi.Obituary.ContainsKey(clientInfo.entityId)) {
if (!ModApi.Obituary.ContainsKey(clientInfo.entityId))
{
ModApi.Obituary.Add(clientInfo.entityId, true);
}
} catch (Exception e) {
}
catch (Exception e)
{
log.Error("Failed to handle GameMessage event.", e);
}
return true; // do not interrupt other mods from processing event
Expand Down
45 changes: 31 additions & 14 deletions src/Handlers/GameUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,63 @@
using Amnesia.Utilities;
using System;

namespace Amnesia.Handlers {
internal class GameUpdate {
namespace Amnesia.Handlers
{
internal class GameUpdate
{
private static readonly ModLog<GameUpdate> log = new ModLog<GameUpdate>();
private static readonly uint _ceiling = 100;
private static uint _counter = 0;
private static bool isBloodmoon = false;

internal static void Handle() {
internal static void Handle()
{
if (!Config.Loaded || !Config.ProtectMemoryDuringBloodmoon) { return; }
try {
try
{
_counter++;
if (_counter > _ceiling) {
if (_counter > _ceiling)
{
_counter = 0;
HandleBloodMoon();
}
} catch (Exception e) {
}
catch (Exception e)
{
log.Error("Failed to handle GameUpdate event.", e);
}
}

private static void HandleBloodMoon() {
private static void HandleBloodMoon()
{
// TODO: this should probably be injected into triggers when bloodmoon starts and ends, honestly
try {
if (isBloodmoon == GameManager.Instance.World.aiDirector.BloodMoonComponent.BloodMoonActive) {
try
{
if (isBloodmoon == GameManager.Instance.World.aiDirector.BloodMoonComponent.BloodMoonActive)
{
return;
}
isBloodmoon = !isBloodmoon;

var players = GameManager.Instance.World.Players.list;
if (isBloodmoon) {
for (var i = 0; i < players.Count; i++) {
if (isBloodmoon)
{
for (var i = 0; i < players.Count; i++)
{
_ = players[i].Buffs.AddBuff(Values.BuffBloodmoonLifeProtection);
}
} else {
for (var i = 0; i < players.Count; i++) {
}
else
{
for (var i = 0; i < players.Count; i++)
{
_ = players[i].Buffs.AddBuff(Values.BuffPostBloodmoonLifeProtection);
players[i].Buffs.RemoveBuff(Values.BuffBloodmoonLifeProtection);
}
}
} catch (Exception e) {
}
catch (Exception e)
{
log.Error("Failed to handle bloodmoon.", e);
}
}
Expand Down
51 changes: 35 additions & 16 deletions src/Handlers/PlayerSpawnedInWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using Amnesia.Utilities;
using System;

namespace Amnesia.Handlers {
internal class PlayerSpawnedInWorld {
namespace Amnesia.Handlers
{
internal class PlayerSpawnedInWorld
{
private static readonly ModLog<PlayerSpawnedInWorld> log = new ModLog<PlayerSpawnedInWorld>();

/// <summary>
Expand All @@ -13,13 +15,17 @@ internal class PlayerSpawnedInWorld {
/// <param name="respawnType">The type of respawn.</param>
/// <param name="pos">The position this player is respawning to.</param>
/// <remarks>This mod supports being dropped into an existing game, thanks to how we handle this process.</remarks>
public static void Handle(ClientInfo clientInfo, RespawnType respawnType, Vector3i pos) {
public static void Handle(ClientInfo clientInfo, RespawnType respawnType, Vector3i pos)
{
if (!Config.Loaded) { return; }
try {
if (clientInfo == null || !GameManager.Instance.World.Players.dict.TryGetValue(clientInfo.entityId, out var player) || !player.IsAlive()) {
try
{
if (clientInfo == null || !GameManager.Instance.World.Players.dict.TryGetValue(clientInfo.entityId, out var player) || !player.IsAlive())
{
return; // exit early if player cannot be found in active world or is dead
}
switch (respawnType) {
switch (respawnType)
{
case RespawnType.EnterMultiplayer: // first-time login for new player
_ = PlayerHelper.AddPositiveOutlookTime(player, Config.PositiveOutlookTimeOnFirstJoin);
RefundHardenedMemory(clientInfo, player);
Expand All @@ -36,7 +42,9 @@ public static void Handle(ClientInfo clientInfo, RespawnType respawnType, Vector
HandleStandardRespawnSteps(player);
break;
}
} catch (Exception e) {
}
catch (Exception e)
{
log.Error("Failed to handle PlayerSpawnedInWorld event.", e);
}
}
Expand All @@ -46,27 +54,36 @@ public static void Handle(ClientInfo clientInfo, RespawnType respawnType, Vector
/// </summary>
/// <param name="player">Player to process steps for.</param>
/// <remarks>This method also handles cleanup when player was already dead on Enter/Join (happens if player logged out while dead).</remarks>
private static void HandleStandardRespawnSteps(EntityPlayer player) {
private static void HandleStandardRespawnSteps(EntityPlayer player)
{

// Ensure joining/respawning players have their constants updated
if (player.GetCVar(Values.CVarLongTermMemoryLevel) != Config.LongTermMemoryLevel) {
if (player.GetCVar(Values.CVarLongTermMemoryLevel) != Config.LongTermMemoryLevel)
{
player.SetCVar(Values.CVarLongTermMemoryLevel, Config.LongTermMemoryLevel);
}

// Remove Positive Outlook if admin disabled it since player's last login
if (Config.PositiveOutlookTimeOnMemoryLoss == 0 && player.Buffs.HasBuff(Values.BuffPositiveOutlook)) {
if (Config.PositiveOutlookTimeOnMemoryLoss == 0 && player.Buffs.HasBuff(Values.BuffPositiveOutlook))
{
player.Buffs.RemoveBuff(Values.BuffPositiveOutlook);
}

// Apply/Remove memory protection based on configuration
if (Config.ProtectMemoryDuringBloodmoon) {
if (Config.ProtectMemoryDuringBloodmoon)
{
// add or remove protection based on whether BM is active
if (GameManager.Instance.World.aiDirector.BloodMoonComponent.BloodMoonActive) {
if (GameManager.Instance.World.aiDirector.BloodMoonComponent.BloodMoonActive)
{
_ = player.Buffs.AddBuff(Values.BuffBloodmoonLifeProtection);
} else {
}
else
{
player.Buffs.RemoveBuff(Values.BuffBloodmoonLifeProtection);
}
} else {
}
else
{
// remove/clean up since protection is inactive
player.Buffs.RemoveBuff(Values.BuffBloodmoonLifeProtection);
player.Buffs.RemoveBuff(Values.BuffPostBloodmoonLifeProtection);
Expand All @@ -77,8 +94,10 @@ private static void HandleStandardRespawnSteps(EntityPlayer player) {
/// Temporary method to automatically refund any players with the Hardened Memory buff from version 1.0.0.
/// </summary>
/// <param name="player">EntityPlayer to check buffs for and refund if hardened.</param>
private static void RefundHardenedMemory(ClientInfo clientInfo, EntityPlayer player) {
if (player.Buffs.HasBuff(Values.BuffHardenedMemory)) {
private static void RefundHardenedMemory(ClientInfo clientInfo, EntityPlayer player)
{
if (player.Buffs.HasBuff(Values.BuffHardenedMemory))
{
PlayerHelper.GiveItem(clientInfo, player, Values.NameMemoryBoosters);
player.Buffs.RemoveBuff(Values.BuffHardenedMemory);
}
Expand Down
37 changes: 25 additions & 12 deletions src/Handlers/SavePlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,38 @@
using System.Collections;
using UnityEngine;

namespace Amnesia.Handlers {
internal class SavePlayerData {
namespace Amnesia.Handlers
{
internal class SavePlayerData
{
private static readonly ModLog<SavePlayerData> log = new ModLog<SavePlayerData>();

public static void Handle(ClientInfo clientInfo, PlayerDataFile playerDataFile) {
public static void Handle(ClientInfo clientInfo, PlayerDataFile playerDataFile)
{
if (!Config.Loaded) { return; }
try {
if (!ModApi.Obituary.ContainsKey(clientInfo.entityId)) {
try
{
if (!ModApi.Obituary.ContainsKey(clientInfo.entityId))
{
return;
}
_ = ModApi.Obituary.Remove(clientInfo.entityId);

if (clientInfo == null || !GameManager.Instance.World.Players.dict.TryGetValue(clientInfo.entityId, out var player)) {
if (clientInfo == null || !GameManager.Instance.World.Players.dict.TryGetValue(clientInfo.entityId, out var player))
{
log.Warn("EntityWasKilled event sent from a non-player client... may want to investigate");
return; // exit early, do not interrupt other mods from processing event
}

if (!player.Buffs.HasBuff(Values.BuffFragileMemory)) {
if (!player.Buffs.HasBuff(Values.BuffFragileMemory))
{
_ = player.Buffs.AddBuff(Values.BuffFragileMemory);
log.Info($"{clientInfo.InternalId.CombinedString} ({player.GetDebugName()}) died and will not be reset, but now has a Fragile Memory.");
return; // let player know it's time for memory boosters
}

if ((Config.ForgetActiveQuests || Config.ForgetInactiveQuests) && QuestHelper.ResetQuests(player)) {
if ((Config.ForgetActiveQuests || Config.ForgetInactiveQuests) && QuestHelper.ResetQuests(player))
{
// TODO: actually just redesign quest resets to issue remote admin call for client to run locally (an amazing feature!)
// =================================================

Expand All @@ -48,18 +56,22 @@ public static void Handle(ClientInfo clientInfo, PlayerDataFile playerDataFile)
// Reset Player
log.Info($"{clientInfo.InternalId.CombinedString} ({player.GetDebugName()}) died and has suffered memory loss.");
PlayerHelper.ResetPlayer(player);
} catch (Exception e) {
}
catch (Exception e)
{
log.Error("Failed to handle OnSavePlayerData", e);
}
}

protected static IEnumerator SaveLater(float _delayInSec, ClientInfo clientInfo, EntityPlayer player) {
protected static IEnumerator SaveLater(float _delayInSec, ClientInfo clientInfo, EntityPlayer player)
{
yield return new WaitForSecondsRealtime(_delayInSec);
WritePlayerData(clientInfo, player);
yield break;
}

private static void WritePlayerData(ClientInfo clientInfo, EntityPlayer player, bool saveMap = false) {
private static void WritePlayerData(ClientInfo clientInfo, EntityPlayer player, bool saveMap = false)
{

var pdf = new PlayerDataFile();
pdf.FromPlayer(player);
Expand All @@ -69,7 +81,8 @@ private static void WritePlayerData(ClientInfo clientInfo, EntityPlayer player,
//clientInfo.latestPlayerData = pdf;
//clientInfo.latestPlayerData.Save(GameIO.GetPlayerDataDir(), clientInfo.InternalId.CombinedString);

if (saveMap && player.ChunkObserver.mapDatabase != null) {
if (saveMap && player.ChunkObserver.mapDatabase != null)
{
_ = ThreadManager.AddSingleTask(
new ThreadManager.TaskFunctionDelegate(player.ChunkObserver.mapDatabase.SaveAsync),
new MapChunkDatabase.DirectoryPlayerId(GameIO.GetPlayerDataDir(),
Expand Down
Loading

0 comments on commit 8be291f

Please sign in to comment.