mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
huge commit for advanced stats feature.
broke data out into its own library. may be breaking changes with existing plugins
This commit is contained in:
75
Plugins/Stats/Client/WeaponNameParser.cs
Normal file
75
Plugins/Stats/Client/WeaponNameParser.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Stats.Client.Abstractions;
|
||||
using Stats.Client.Game;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using IW4MAdmin.Plugins.Stats.Config;
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
||||
namespace Stats.Client
|
||||
{
|
||||
public class WeaponNameParser : IWeaponNameParser
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly StatsConfiguration _config;
|
||||
|
||||
public WeaponNameParser(ILogger<WeaponNameParser> logger, IConfigurationHandler<StatsConfiguration> config)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config.Configuration();
|
||||
}
|
||||
|
||||
public WeaponInfo Parse(string weaponName, Server.Game gameName)
|
||||
{
|
||||
var configForGame = _config.WeaponNameParserConfigurations
|
||||
?.FirstOrDefault(config => config.Game == gameName);
|
||||
|
||||
if (configForGame == null)
|
||||
{
|
||||
_logger.LogWarning("No weapon parser config available for game {game}", gameName);
|
||||
return new WeaponInfo()
|
||||
{
|
||||
Name = "Unknown"
|
||||
};
|
||||
}
|
||||
|
||||
var splitWeaponName = weaponName.Split(configForGame.Delimiters);
|
||||
|
||||
if (!splitWeaponName.Any())
|
||||
{
|
||||
_logger.LogError("Could not parse weapon name {weapon}", weaponName);
|
||||
|
||||
return new WeaponInfo()
|
||||
{
|
||||
Name = "Unknown"
|
||||
};
|
||||
}
|
||||
|
||||
// remove the _mp suffix
|
||||
var filtered = splitWeaponName.Where(part => part != configForGame.WeaponSuffix);
|
||||
var baseName = splitWeaponName.First();
|
||||
var attachments = new List<string>();
|
||||
|
||||
if (filtered.Count() > 1)
|
||||
{
|
||||
attachments.AddRange(filtered.Skip(1));
|
||||
}
|
||||
|
||||
var weaponInfo = new WeaponInfo()
|
||||
{
|
||||
RawName = weaponName,
|
||||
Name = baseName,
|
||||
Attachments = attachments.Select(attachment => new AttachmentInfo()
|
||||
{
|
||||
Name = attachment
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
// _logger.LogDebug("Parsed weapon info {@info}", weaponInfo);
|
||||
|
||||
return weaponInfo;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user