IW4M-Admin/Plugins/Stats/Client/HitInfoBuilder.cs

72 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using Data.Models;
using IW4MAdmin.Plugins.Stats.Client.Game;
using Microsoft.Extensions.Logging;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using Stats.Client.Abstractions;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Stats.Client
{
public class HitInfoBuilder : IHitInfoBuilder
{
private readonly IWeaponNameParser _weaponNameParser;
private readonly ILogger _logger;
private const int MaximumDamage = 1000;
2021-06-03 11:51:03 -04:00
public HitInfoBuilder(ILogger<HitInfoBuilder> logger, IWeaponNameParser weaponNameParser)
{
_weaponNameParser = weaponNameParser;
_logger = logger;
}
2021-06-03 11:51:03 -04:00
public HitInfo Build(string[] log, int entityId, bool isSelf, bool isVictim, Server.Game gameName)
{
var eventType = log[(uint) ParserRegex.GroupType.EventType].First();
HitType hitType;
if (isVictim)
{
if (isSelf)
{
hitType = HitType.Suicide;
}
else
{
hitType = eventType == 'D' ? HitType.WasDamaged : HitType.WasKilled;
}
}
else
{
hitType = eventType == 'D' ? HitType.Damage : HitType.Kill;
}
var hitInfo = new HitInfo()
{
EntityId = entityId,
IsVictim = isVictim,
HitType = hitType,
2021-06-03 11:51:03 -04:00
Damage = Math.Min(MaximumDamage,
log.Length > (uint) ParserRegex.GroupType.Damage
? int.Parse(log[(uint) ParserRegex.GroupType.Damage])
: 0),
Location = log.Length > (uint) ParserRegex.GroupType.HitLocation
? log[(uint) ParserRegex.GroupType.HitLocation]
: "Unknown",
Weapon = log.Length == 10 ? _weaponNameParser.Parse(log[8], gameName)
: _weaponNameParser.Parse(log[(uint) ParserRegex.GroupType.Weapon], gameName),
MeansOfDeath = log.Length > (uint) ParserRegex.GroupType.MeansOfDeath
? log[(uint) ParserRegex.GroupType.MeansOfDeath]
: "Unknown",
Game = (Reference.Game) gameName
};
//_logger.LogDebug("Generated new hitInfo {@hitInfo}", hitInfo);
return hitInfo;
}
}
}