1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

added 'none' and extra m16 variants to weapon list

moved killstreak/deathstreak messages into configuration file
cleaned up configuration manager
fixed misc startup issue and threading
added more importing stuff
network id is a ulong now
ip str is now ip
added time played (per server)
This commit is contained in:
RaidMax
2018-02-10 22:33:42 -06:00
parent d1cdb93cc3
commit b8a161161d
33 changed files with 418 additions and 213 deletions

View File

@ -854,7 +854,7 @@ namespace SharedLibrary.Commands
{
StringBuilder message = new StringBuilder();
var names = new List<string>(E.Target.AliasLink.Children.Select(a => a.Name));
var IPs = new List<string>(E.Target.AliasLink.Children.Select(a => a.IPAddress).Distinct());
var IPs = new List<string>(E.Target.AliasLink.Children.Select(a => a.IPAddress.ConvertIPtoString()).Distinct());
await E.Target.Tell($"[^3{E.Target}^7]");

View File

@ -19,6 +19,8 @@ namespace SharedLibrary.Database
{
context = new DatabaseContext();
context.Configuration.AutoDetectChangesEnabled = false;
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.ProxyCreationEnabled = false;
int count = 0;
foreach (var entityToInsert in clients)
@ -96,6 +98,8 @@ namespace SharedLibrary.Database
{
context = new DatabaseContext();
context.Configuration.AutoDetectChangesEnabled = false;
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.ProxyCreationEnabled = false;
int count = 0;
foreach (var entityToInsert in penalties)
@ -161,5 +165,59 @@ namespace SharedLibrary.Database
return context;
}
public static void ImportSQLite<T>(IList<T> SQLiteData) where T : class
{
DatabaseContext context = null;
try
{
context = new DatabaseContext();
context.Configuration.AutoDetectChangesEnabled = false;
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.ProxyCreationEnabled = false;
int count = 0;
foreach (var entityToInsert in SQLiteData)
{
++count;
context = AddSQLite(context, entityToInsert, count, 100, true);
}
context.SaveChanges();
}
finally
{
if (context != null)
context.Dispose();
}
}
private static DatabaseContext AddSQLite<T>(DatabaseContext context, T entity, int count, int commitCount, bool recreateContext) where T : class
{
context.Set<T>().Add(entity);
if (count % commitCount == 0)
{
try
{
context.SaveChanges();
}
catch (Exception e)
{
var a = 1;
}
if (recreateContext)
{
context.Dispose();
context = new DatabaseContext();
context.Configuration.AutoDetectChangesEnabled = false;
}
}
return context;
}
}
}

View File

@ -17,7 +17,7 @@ namespace SharedLibrary.Database
{
Active = true,
DateAdded = DateTime.UtcNow,
IPAddress = "0.0.0.0",
IPAddress = 0,
Name = "IW4MAdmin",
Link = aliasLink
};
@ -30,7 +30,7 @@ namespace SharedLibrary.Database
LastConnection = DateTime.UtcNow,
Level = Objects.Player.Permission.Console,
Masked = true,
NetworkId = "0000000000000000",
NetworkId = 0,
AliasLink = aliasLink,
CurrentAlias = currentAlias
});

View File

@ -14,10 +14,12 @@ namespace SharedLibrary.Database.Models
public virtual EFAliasLink Link { get; set; }
// [Index("IX_IPandName", 0, IsUnique = true)]
//[MaxLength(24)]
[Required]
public string Name { get; set; }
// [Index("IX_IPandName", 1, IsUnique = true)]
// [MaxLength(24)]
public string IPAddress { get; set; }
[Required]
public int IPAddress { get; set; }
[Required]
public DateTime DateAdded { get; set; }
}

View File

@ -13,8 +13,7 @@ namespace SharedLibrary.Database.Models
[Key]
public int ClientId { get; set; }
[Index(IsUnique = true)]
public string NetworkId { get; set; }
public long NetworkId { get; set; }
[Required]
public int Connections { get; set; }
[Required]
@ -44,12 +43,15 @@ namespace SharedLibrary.Database.Models
set { }
}
[NotMapped]
public virtual string IPAddress
public virtual int IPAddress
{
get { return CurrentAlias.IPAddress; }
set { }
}
[NotMapped]
public string IPAddressString => new System.Net.IPAddress(BitConverter.GetBytes(IPAddress)).ToString();
public virtual ICollection<EFPenalty> ReceivedPenalties { get; set; }
public virtual ICollection<EFPenalty> AdministeredPenalties { get; set; }

View File

@ -131,7 +131,7 @@ namespace SharedLibrary
if (removeTime.Contains("ScriptKill"))
{
return new Event(GType.Script, String.Join(";", line), SV.Players.FirstOrDefault(p => p != null && p.NetworkId == line[1]), SV.Players.FirstOrDefault(p => p != null && p.NetworkId == line[2]), SV);
return new Event(GType.Script, String.Join(";", line), SV.Players.FirstOrDefault(p => p != null && p.NetworkId == line[1].ConvertLong()), SV.Players.FirstOrDefault(p => p != null && p.NetworkId == line[2].ConvertLong()), SV);
}
if (removeTime.Contains("ExitLevel"))

View File

@ -53,5 +53,10 @@ namespace SharedLibrary.Helpers
RunAverage = RunAverage + ((DateTime.Now - StartTime).TotalMilliseconds - RunAverage - UpdateFrequency) / TimesRun;
StartTime = DateTime.Now;
}
public void Abort()
{
RequestedTask = null;
}
}
}

View File

@ -6,65 +6,32 @@ namespace SharedLibrary.Helpers
{
public class ConfigurationManager
{
ConcurrentDictionary<string, Dictionary<string, object>> ConfigurationSet;
ConcurrentDictionary<string, object> ConfigSet;
Type PluginType;
ConcurrentDictionary<string, dynamic> ConfigSet;
Server ServerInstance;
public ConfigurationManager(Type PluginType)
{
ConfigurationSet = new ConcurrentDictionary<string, Dictionary<string, object>>();
this.PluginType = PluginType;
}
public ConfigurationManager(Server S)
{
try
{
ConfigSet = Interfaces.Serialize<ConcurrentDictionary<string, object>>.Read($"config/Plugins_{S}.cfg");
ConfigSet = Interfaces.Serialize<ConcurrentDictionary<string, dynamic>>.Read($"config/plugins_{S.ToString()}.cfg");
}
catch (Exception)
{
S.Logger.WriteInfo("ConfigurationManager could not deserialize configuration file, so initializing default config set");
ConfigSet = new ConcurrentDictionary<string, object>();
ConfigSet = new ConcurrentDictionary<string, dynamic>();
}
ServerInstance = S;
SaveChanges();
}
private void SaveChanges()
{
Interfaces.Serialize<ConcurrentDictionary<string, object>>.Write($"config/Plugins_{ServerInstance}.cfg", ConfigSet);
Interfaces.Serialize<ConcurrentDictionary<string, dynamic>>.Write($"config/plugins_{ServerInstance.ToString()}.cfg", ConfigSet);
}
public void AddConfiguration(Server S)
{
/* if (ConfigurationSet.ContainsKey(S.ToString()))
{
S.Logger.WriteWarning($"not adding server configuration for {S} as it already exists");
return;
}*/
try
{
var Config = Interfaces.Serialize<Dictionary<string, object>>.Read($"config/{PluginType.ToString()}_{S.ToString()}.cfg");
ConfigurationSet.TryAdd(S.ToString(), Config);
}
catch (Exceptions.SerializeException)
{
ConfigurationSet.TryAdd(S.ToString(), new Dictionary<string, object>());
}
}
public void AddProperty(Server S, KeyValuePair<string, object> Property)
{
ConfigurationSet[S.ToString()].Add(Property.Key, Property.Value);
Interfaces.Serialize<Dictionary<string, object>>.Write($"config/{PluginType.ToString()}_{S.ToString()}.cfg", ConfigurationSet[S.ToString()]);
}
public void AddProperty(KeyValuePair<string, object> prop)
public void AddProperty(KeyValuePair<string, dynamic> prop)
{
if (!ConfigSet.ContainsKey(prop.Key))
ConfigSet.TryAdd(prop.Key, prop.Value);
@ -72,13 +39,7 @@ namespace SharedLibrary.Helpers
SaveChanges();
}
public void UpdateProperty(Server S, KeyValuePair<string, object> Property)
{
ConfigurationSet[S.ToString()][Property.Key] = Property.Value;
Interfaces.Serialize<Dictionary<string, object>>.Write($"config/{PluginType.ToString()}_{S.ToString()}.cfg", ConfigurationSet[S.ToString()]);
}
public void UpdateProperty(KeyValuePair<string, object> prop)
public void UpdateProperty(KeyValuePair<string, dynamic> prop)
{
if (ConfigSet.ContainsKey(prop.Key))
ConfigSet[prop.Key] = prop.Value;
@ -86,21 +47,16 @@ namespace SharedLibrary.Helpers
SaveChanges();
}
public IDictionary<string, object> GetConfiguration(Server S)
{
return ConfigurationSet[S.ToString()];
}
public object GetProperty(string prop)
public T GetProperty<T>(string prop)
{
try
{
return ConfigSet[prop];
return ConfigSet[prop].ToObject<T>();
}
catch (Exception)
{
return null;
return default(T);
}
}
}

View File

@ -14,7 +14,7 @@ namespace SharedLibrary.Interfaces
Task<T> Delete(T entity);
Task<T> Update(T entity);
Task<T> Get(int entityID);
Task<T> GetUnique(string entityProperty);
Task<T> GetUnique(long entityProperty);
Task<IList<T>> Find(Func<T, bool> expression);
}
}

View File

@ -45,7 +45,7 @@ namespace SharedLibrary.Interfaces
{
try
{
string configText = Newtonsoft.Json.JsonConvert.SerializeObject(data);
string configText = Newtonsoft.Json.JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filename, configText);
}

View File

@ -76,8 +76,8 @@ namespace SharedLibrary.Objects
[NotMapped]
public int Score { get; set; }
private string _ipaddress;
public override string IPAddress
private int _ipaddress;
public override int IPAddress
{
get { return _ipaddress; }
set { _ipaddress = value; }
@ -88,5 +88,15 @@ namespace SharedLibrary.Objects
get { return _name; }
set { _name = value; }
}
public override bool Equals(object obj)
{
return ((Player)obj).NetworkId == NetworkId;
}
public override int GetHashCode()
{
return NetworkId.GetHashCode();
}
}
}

View File

@ -39,7 +39,7 @@ namespace SharedLibrary
Reports = new List<Report>();
PlayerHistory = new Queue<Helpers.PlayerHistory>();
ChatHistory = new List<Chat>();
Configuration = new ConfigurationManager(this.GetType());
//Configuration = new ConfigurationManager(this.GetType());
NextMessage = 0;
InitializeTokens();
InitializeAutoMessages();

View File

@ -73,7 +73,7 @@ namespace SharedLibrary.Services
.SingleOrDefaultAsync(e => e.AliasId == entityID);
}
public Task<EFAlias> GetUnique(string entityProperty)
public Task<EFAlias> GetUnique(long entityProperty)
{
throw new NotImplementedException();
}

View File

@ -102,14 +102,18 @@ namespace SharedLibrary.Services
public async Task<EFClient> Get(int entityID)
{
using (var context = new DatabaseContext())
{
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.ProxyCreationEnabled = false;
return await new DatabaseContext().Clients
.AsNoTracking()
.Include(c => c.CurrentAlias)
.Include(c => c.AliasLink.Children)
.SingleOrDefaultAsync(e => e.ClientId == entityID);
}
}
public async Task<EFClient> GetUnique(string entityAttribute)
public async Task<EFClient> GetUnique(long entityAttribute)
{
using (var context = new DatabaseContext())
{
@ -117,7 +121,7 @@ namespace SharedLibrary.Services
.AsNoTracking()
.Include(c => c.CurrentAlias)
.Include(c => c.AliasLink.Children)
.SingleOrDefaultAsync(c => c.NetworkId == entityAttribute);
.SingleOrDefaultAsync(c => c.NetworkId == (long)entityAttribute);
}
}

View File

@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharedLibrary.Database;
namespace SharedLibrary.Services
{
public class GenericService<T> : Interfaces.IEntityService<T>
{
public async Task<T> Create(T entity)
{
using (var context = new DatabaseContext())
{
var dbSet = context.Set(entity.GetType());
T addedEntity = (T)dbSet.Add(entity);
await context.SaveChangesAsync();
return addedEntity;
}
}
public Task<T> CreateProxy()
{
throw new NotImplementedException();
}
public Task<T> Delete(T entity)
{
throw new NotImplementedException();
}
public Task<IList<T>> Find(Func<T, bool> expression)
{
throw new NotImplementedException();
}
public async Task<T> Get(int entityID)
{
using (var context = new DatabaseContext())
{
var dbSet = context.Set(typeof(T));
return (T)(await dbSet.FindAsync(entityID));
}
}
public async Task<T> Get(params object[] entityKeys)
{
using (var context = new DatabaseContext())
{
var dbSet = context.Set(typeof(T));
return (T)(await dbSet.FindAsync(entityKeys));
}
}
public Task<T> GetUnique(string entityProperty)
{
throw new NotImplementedException();
}
public Task<T> Update(T entity)
{
throw new NotImplementedException();
}
}
}

View File

@ -73,7 +73,7 @@ namespace SharedLibrary.Services
throw new NotImplementedException();
}
public Task<EFPenalty> GetUnique(string entityProperty)
public Task<EFPenalty> GetUnique(long entityProperty)
{
throw new NotImplementedException();
}

View File

@ -139,7 +139,6 @@
<Compile Include="Services\AliasService.cs" />
<Compile Include="Services\ClientService.cs" />
<Compile Include="Services\GenericRepository.cs" />
<Compile Include="Services\GenericService.cs" />
<Compile Include="Services\PenaltyService.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="WebService.cs" />

View File

@ -58,10 +58,10 @@ namespace SharedLibrary
int Ping = -1;
Int32.TryParse(playerInfo[2], out Ping);
String cName = Utilities.StripColors(responseLine.Substring(46, 18)).Trim();
string npID = Regex.Match(responseLine, @"([a-z]|[0-9]){16}", RegexOptions.IgnoreCase).Value;
long npID = Regex.Match(responseLine, @"([a-z]|[0-9]){16}", RegexOptions.IgnoreCase).Value.ConvertLong();
int.TryParse(playerInfo[0], out cID);
var regex = Regex.Match(responseLine, @"\d+\.\d+\.\d+.\d+\:\d{1,5}");
string cIP = regex.Value.Split(':')[0];
int cIP = regex.Value.Split(':')[0].ConvertToIP();
regex = Regex.Match(responseLine, @"[0-9]{1,2}\s+[0-9]+\s+");
int score = Int32.Parse(regex.Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1]);
Player P = new Player() { Name = cName, NetworkId = npID, ClientNumber = cID, IPAddress = cIP, Ping = Ping, Score = score};
@ -197,6 +197,21 @@ namespace SharedLibrary
}
}
public static long ConvertLong(this string str)
{
return Int64.Parse(str, System.Globalization.NumberStyles.HexNumber);
}
public static int ConvertToIP(this string str)
{
return BitConverter.ToInt32(System.Net.IPAddress.Parse(str).GetAddressBytes(), 0);
}
public static string ConvertIPtoString(this int ip)
{
return new System.Net.IPAddress(BitConverter.GetBytes(ip)).ToString();
}
public static String DateTimeSQLite(DateTime datetime)
{
return datetime.ToString("yyyy-MM-dd H:mm:ss");