2021-03-22 12:09:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Data.Models;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Client.Game;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using Stats.Client.Abstractions;
|
2021-07-10 22:37:51 -04:00
|
|
|
|
using Stats.Client.Game;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
namespace Stats.Client;
|
|
|
|
|
|
|
|
|
|
public class HitInfoBuilder : IHitInfoBuilder
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private readonly IWeaponNameParser _weaponNameParser;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private const int MaximumDamage = 1000;
|
|
|
|
|
|
|
|
|
|
public HitInfoBuilder(ILogger<HitInfoBuilder> logger, IWeaponNameParser weaponNameParser)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_weaponNameParser = weaponNameParser;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
2021-06-03 11:51:03 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
public HitInfo Build(string[] log, ParserRegex parserRegex, int entityId, bool isSelf, bool isVictim,
|
|
|
|
|
Reference.Game gameName)
|
|
|
|
|
{
|
|
|
|
|
var eventType = log[(uint)ParserRegex.GroupType.EventType].First();
|
|
|
|
|
HitType hitType;
|
2021-06-03 11:51:03 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (isVictim)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (isSelf)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
hitType = HitType.Suicide;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
hitType = eventType == 'D' ? HitType.WasDamaged : HitType.WasKilled;
|
2021-07-12 15:57:44 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-07-12 15:57:44 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hitType = eventType == 'D' ? HitType.Damage : HitType.Kill;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var damage = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
damage = Math.Min(MaximumDamage,
|
|
|
|
|
log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.Damage]
|
|
|
|
|
? int.Parse(log[parserRegex.GroupMapping[ParserRegex.GroupType.Damage]])
|
|
|
|
|
: 0);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hitInfo = new HitInfo()
|
|
|
|
|
{
|
|
|
|
|
EntityId = entityId,
|
|
|
|
|
IsVictim = isVictim,
|
|
|
|
|
HitType = hitType,
|
|
|
|
|
Damage = damage,
|
|
|
|
|
Location = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.HitLocation]
|
|
|
|
|
? log[parserRegex.GroupMapping[ParserRegex.GroupType.HitLocation]]
|
|
|
|
|
: "Unknown",
|
|
|
|
|
Weapon = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.Weapon]
|
|
|
|
|
? _weaponNameParser.Parse(log[parserRegex.GroupMapping[ParserRegex.GroupType.Weapon]], gameName)
|
|
|
|
|
: new WeaponInfo { Name = "Unknown" },
|
|
|
|
|
MeansOfDeath = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.MeansOfDeath]
|
|
|
|
|
? log[parserRegex.GroupMapping[ParserRegex.GroupType.MeansOfDeath]]
|
|
|
|
|
: "Unknown",
|
|
|
|
|
Game = gameName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return hitInfo;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|