189 lines
6.7 KiB
C#
189 lines
6.7 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace GeometryTD.Definition
|
|
{
|
|
public enum CombatParticipantTowerValidationFailureReason
|
|
{
|
|
None = 0,
|
|
TowerMissing = 1,
|
|
MissingMuzzleComponent = 2,
|
|
MissingBearingComponent = 3,
|
|
MissingBaseComponent = 4
|
|
}
|
|
|
|
public sealed class CombatParticipantTowerValidationResult
|
|
{
|
|
public long TowerInstanceId { get; set; }
|
|
|
|
public bool IsValid => FailureReason == CombatParticipantTowerValidationFailureReason.None;
|
|
|
|
public CombatParticipantTowerValidationFailureReason FailureReason { get; set; }
|
|
|
|
public TowerItemData Tower { get; set; }
|
|
}
|
|
|
|
public sealed class CombatParticipantTowerValidationSummary
|
|
{
|
|
public IReadOnlyList<TowerItemData> ValidTowers { get; set; } = System.Array.Empty<TowerItemData>();
|
|
|
|
public IReadOnlyList<CombatParticipantTowerValidationResult> InvalidResults { get; set; } =
|
|
System.Array.Empty<CombatParticipantTowerValidationResult>();
|
|
|
|
public bool HasAnyValidParticipantTower => ValidTowers != null && ValidTowers.Count > 0;
|
|
}
|
|
|
|
public static class CombatParticipantTowerValidationService
|
|
{
|
|
public static CombatParticipantTowerValidationResult ValidateTower(
|
|
BackpackInventoryData inventory,
|
|
long towerInstanceId)
|
|
{
|
|
if (!TryGetTowerById(inventory, towerInstanceId, out TowerItemData tower))
|
|
{
|
|
return new CombatParticipantTowerValidationResult
|
|
{
|
|
TowerInstanceId = towerInstanceId,
|
|
FailureReason = CombatParticipantTowerValidationFailureReason.TowerMissing
|
|
};
|
|
}
|
|
|
|
return ValidateTower(inventory, tower);
|
|
}
|
|
|
|
public static CombatParticipantTowerValidationResult ValidateTower(
|
|
BackpackInventoryData inventory,
|
|
TowerItemData tower)
|
|
{
|
|
if (tower == null || tower.InstanceId <= 0)
|
|
{
|
|
return new CombatParticipantTowerValidationResult
|
|
{
|
|
TowerInstanceId = tower != null ? tower.InstanceId : 0,
|
|
Tower = tower,
|
|
FailureReason = CombatParticipantTowerValidationFailureReason.TowerMissing
|
|
};
|
|
}
|
|
|
|
if (!HasComponent(inventory?.MuzzleComponents, tower.MuzzleComponentInstanceId))
|
|
{
|
|
return new CombatParticipantTowerValidationResult
|
|
{
|
|
TowerInstanceId = tower.InstanceId,
|
|
Tower = tower,
|
|
FailureReason = CombatParticipantTowerValidationFailureReason.MissingMuzzleComponent
|
|
};
|
|
}
|
|
|
|
if (!HasComponent(inventory?.BearingComponents, tower.BearingComponentInstanceId))
|
|
{
|
|
return new CombatParticipantTowerValidationResult
|
|
{
|
|
TowerInstanceId = tower.InstanceId,
|
|
Tower = tower,
|
|
FailureReason = CombatParticipantTowerValidationFailureReason.MissingBearingComponent
|
|
};
|
|
}
|
|
|
|
if (!HasComponent(inventory?.BaseComponents, tower.BaseComponentInstanceId))
|
|
{
|
|
return new CombatParticipantTowerValidationResult
|
|
{
|
|
TowerInstanceId = tower.InstanceId,
|
|
Tower = tower,
|
|
FailureReason = CombatParticipantTowerValidationFailureReason.MissingBaseComponent
|
|
};
|
|
}
|
|
|
|
return new CombatParticipantTowerValidationResult
|
|
{
|
|
TowerInstanceId = tower.InstanceId,
|
|
Tower = tower,
|
|
FailureReason = CombatParticipantTowerValidationFailureReason.None
|
|
};
|
|
}
|
|
|
|
public static CombatParticipantTowerValidationSummary ValidateParticipantTowers(BackpackInventoryData inventory)
|
|
{
|
|
List<TowerItemData> validTowers = new List<TowerItemData>();
|
|
List<CombatParticipantTowerValidationResult> invalidResults =
|
|
new List<CombatParticipantTowerValidationResult>();
|
|
HashSet<long> processedTowerIds = new HashSet<long>();
|
|
|
|
if (inventory?.ParticipantTowerInstanceIds == null || inventory.ParticipantTowerInstanceIds.Count <= 0)
|
|
{
|
|
return new CombatParticipantTowerValidationSummary
|
|
{
|
|
ValidTowers = validTowers,
|
|
InvalidResults = invalidResults
|
|
};
|
|
}
|
|
|
|
for (int i = 0; i < inventory.ParticipantTowerInstanceIds.Count; i++)
|
|
{
|
|
long towerInstanceId = inventory.ParticipantTowerInstanceIds[i];
|
|
if (towerInstanceId <= 0 || !processedTowerIds.Add(towerInstanceId))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
CombatParticipantTowerValidationResult result = ValidateTower(inventory, towerInstanceId);
|
|
if (result.IsValid)
|
|
{
|
|
validTowers.Add(result.Tower);
|
|
}
|
|
else
|
|
{
|
|
invalidResults.Add(result);
|
|
}
|
|
}
|
|
|
|
return new CombatParticipantTowerValidationSummary
|
|
{
|
|
ValidTowers = validTowers,
|
|
InvalidResults = invalidResults
|
|
};
|
|
}
|
|
|
|
private static bool TryGetTowerById(BackpackInventoryData inventory, long towerInstanceId, out TowerItemData tower)
|
|
{
|
|
tower = null;
|
|
if (inventory?.Towers == null || towerInstanceId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < inventory.Towers.Count; i++)
|
|
{
|
|
TowerItemData candidate = inventory.Towers[i];
|
|
if (candidate != null && candidate.InstanceId == towerInstanceId)
|
|
{
|
|
tower = candidate;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static bool HasComponent<TComponent>(IReadOnlyList<TComponent> components, long componentInstanceId)
|
|
where TComponent : TowerCompItemData
|
|
{
|
|
if (components == null || componentInstanceId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < components.Count; i++)
|
|
{
|
|
TComponent component = components[i];
|
|
if (component != null && component.InstanceId == componentInstanceId)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|