mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 15:20:48 -05:00
More EF Changes.
Added DB importer
This commit is contained in:
@ -65,7 +65,7 @@ namespace SharedLibrary.Commands
|
||||
if (E.Origin.Level <= E.Target.Level)
|
||||
await E.Origin.Tell($"You do not have the required privileges to warn {E.Target.Name}");
|
||||
else
|
||||
await E.Target.Warn(E.Data.RemoveWords(1), E.Origin);
|
||||
await E.Target.Warn(E.Data, E.Origin);
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,7 +113,7 @@ namespace SharedLibrary.Commands
|
||||
if (E.Origin.Level > E.Target.Level)
|
||||
{
|
||||
await E.Owner.ExecuteEvent(new Event(Event.GType.Kick, E.Data, E.Origin, E.Target, E.Owner));
|
||||
await E.Target.Kick(E.Data.RemoveWords(1), E.Origin);
|
||||
await E.Target.Kick(E.Data, E.Origin);
|
||||
await E.Origin.Tell($"^5{E.Target} ^7has been kicked");
|
||||
}
|
||||
else
|
||||
@ -165,11 +165,8 @@ namespace SharedLibrary.Commands
|
||||
|
||||
public override async Task ExecuteAsync(Event E)
|
||||
{
|
||||
String Message = Utilities.RemoveWords(E.Data, 1);
|
||||
var length = Message.ParseTimespan();
|
||||
|
||||
if (length.TotalHours != 1)
|
||||
Message = Utilities.RemoveWords(Message, 1);
|
||||
String Message = Utilities.RemoveWords(E.Data, 1).Trim();
|
||||
var length = E.Data.Split(' ')[0].ParseTimespan();
|
||||
|
||||
if (E.Origin.Level > E.Target.Level)
|
||||
{
|
||||
@ -402,7 +399,7 @@ namespace SharedLibrary.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
Player.Permission newPerm = Utilities.MatchPermission(Utilities.RemoveWords(E.Data, 1));
|
||||
Player.Permission newPerm = Utilities.MatchPermission(E.Data);
|
||||
|
||||
if (newPerm == Player.Permission.Owner && E.Origin.Level != Player.Permission.Console)
|
||||
newPerm = Player.Permission.Banned;
|
||||
@ -566,7 +563,8 @@ namespace SharedLibrary.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
var db_aliases = await E.Owner.Manager.GetAliasService().Find(a => a.Name.Contains(E.Data));
|
||||
var db_aliases = await E.Owner.Manager.GetAliasService()
|
||||
.Find(a => a.Name.ToLower().Contains(E.Data.ToLower()));
|
||||
|
||||
if (db_aliases.Count == 0)
|
||||
{
|
||||
@ -575,9 +573,7 @@ namespace SharedLibrary.Commands
|
||||
}
|
||||
|
||||
foreach (var P in db_aliases)
|
||||
{
|
||||
await E.Origin.Tell($"^4{P.Name} ^7now goes by ^5{P.Link.Children.OrderByDescending(a => a.DateAdded).First().Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -630,7 +626,6 @@ namespace SharedLibrary.Commands
|
||||
|
||||
public override async Task ExecuteAsync(Event E)
|
||||
{
|
||||
E.Data = Utilities.RemoveWords(E.Data, 1);
|
||||
await E.Target.Tell("^1" + E.Origin.Name + " ^3[PM]^7 - " + E.Data);
|
||||
await E.Origin.Tell(String.Format("To ^3{0} ^7-> {1}", E.Target.Name, E.Data));
|
||||
}
|
||||
@ -680,13 +675,12 @@ namespace SharedLibrary.Commands
|
||||
if (E.Target.Level == Player.Permission.Flagged)
|
||||
{
|
||||
E.Target.Level = Player.Permission.User;
|
||||
//E.Owner.Manager.GetClientPenalties().RemovePenalty(new Penalty(Penalty.PenaltyType.Flag, "", E.Target.NetworkId, "", DateTime.Now, "", DateTime.Now));
|
||||
await E.Owner.Manager.GetClientService().Update(E.Target);
|
||||
await E.Origin.Tell("You have ^5unflagged ^7" + E.Target.Name);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
E.Data = Utilities.RemoveWords(E.Data, 1);
|
||||
E.Target.Level = Player.Permission.Flagged;
|
||||
|
||||
Penalty newPenalty = new Penalty()
|
||||
@ -697,7 +691,8 @@ namespace SharedLibrary.Commands
|
||||
Offense = E.Data,
|
||||
Punisher = E.Origin,
|
||||
Active = true,
|
||||
When = DateTime.UtcNow
|
||||
When = DateTime.UtcNow,
|
||||
Link = E.Target.AliasLink
|
||||
};
|
||||
|
||||
await E.Owner.Manager.GetPenaltyService().Create(newPenalty);
|
||||
@ -705,7 +700,6 @@ namespace SharedLibrary.Commands
|
||||
await E.Origin.Tell("You have ^5flagged ^7" + E.Target.Name);
|
||||
}
|
||||
|
||||
await E.Owner.Manager.GetClientService().Update(E.Target);
|
||||
}
|
||||
}
|
||||
|
||||
@ -792,7 +786,7 @@ namespace SharedLibrary.Commands
|
||||
public class CMask : Command
|
||||
{
|
||||
public CMask() :
|
||||
base("mask", "hide your online presence from online admin list", "hide", Player.Permission.Administrator, false)
|
||||
base("mask", "hide your presence as an administrator", "hide", Player.Permission.Moderator, false)
|
||||
{ }
|
||||
|
||||
public override async Task ExecuteAsync(Event E)
|
||||
@ -837,7 +831,7 @@ namespace SharedLibrary.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
await E.Origin.Tell(String.Format("^1{0} ^7was banned by ^5{1} ^7for: {2} {3}", E.Target.Name, penalty.Punisher.Name, penalty.Offense, penalty.Type == Penalty.PenaltyType.TempBan ? $"({(penalty.Expires - DateTime.Now).TimeSpanText()} remaining)" : ""));
|
||||
await E.Origin.Tell(String.Format("^1{0} ^7was banned by ^5{1} ^7for: {2} {3}", E.Target.Name, penalty.Punisher.Name, penalty.Offense, penalty.Type == Penalty.PenaltyType.TempBan ? $"({(penalty.Expires - DateTime.UtcNow).TimeSpanText()} remaining)" : ""));
|
||||
}
|
||||
}
|
||||
|
||||
@ -858,7 +852,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.IP));
|
||||
var IPs = new List<string>(E.Target.AliasLink.Children.Select(a => a.IPAddress).Distinct());
|
||||
|
||||
await E.Target.Tell($"[^3{E.Target}^7]");
|
||||
|
||||
|
@ -6,34 +6,37 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharedLibrary.Database.Models;
|
||||
using System.Data.SqlServerCe;
|
||||
using System.Data.Entity.ModelConfiguration.Conventions;
|
||||
|
||||
namespace SharedLibrary.Database
|
||||
{
|
||||
public class IW4MAdminDatabaseContext : DbContext
|
||||
public class DatabaseContext : DbContext
|
||||
{
|
||||
public DbSet<EFClient> Clients { get; set; }
|
||||
public DbSet<EFAlias> Aliases { get; set; }
|
||||
public DbSet<EFAliasLink> AliasLinks { get; set; }
|
||||
public DbSet<EFPenalty> Penalties { get; set; }
|
||||
|
||||
public IW4MAdminDatabaseContext() : base("DefaultConnection")
|
||||
public DatabaseContext() : base("DefaultConnection")
|
||||
{
|
||||
System.Data.Entity.Database.SetInitializer(new Initializer());
|
||||
Configuration.LazyLoadingEnabled = false;
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<EFPenalty>()
|
||||
.HasRequired(p => p.Punisher)
|
||||
.WithMany(c => c.AdministeredPenalties)
|
||||
.HasForeignKey(c => c.PunisherId)
|
||||
.WillCascadeOnDelete(false);
|
||||
.HasRequired(p => p.Offender)
|
||||
.WithMany(c => c.ReceivedPenalties)
|
||||
.HasForeignKey(c => c.OffenderId)
|
||||
.WillCascadeOnDelete(false);
|
||||
|
||||
|
||||
modelBuilder.Entity<EFPenalty>()
|
||||
.HasRequired(p => p.Offender)
|
||||
.WithMany(c => c.ReceivedPenalties)
|
||||
.HasForeignKey(c => c.OffenderId)
|
||||
.WillCascadeOnDelete(false);
|
||||
.HasRequired(p => p.Punisher)
|
||||
.WithMany(c => c.AdministeredPenalties)
|
||||
.HasForeignKey(c => c.PunisherId)
|
||||
.WillCascadeOnDelete(false);
|
||||
|
||||
modelBuilder.Entity<EFAliasLink>()
|
||||
.HasMany(e => e.Children)
|
||||
@ -41,7 +44,17 @@ namespace SharedLibrary.Database
|
||||
.HasForeignKey(a => a.LinkId)
|
||||
.WillCascadeOnDelete(true);
|
||||
|
||||
// todo custom load DBSets from plugins
|
||||
/* modelBuilder.Entity<EFAlias>()
|
||||
.HasIndex(a => new { a.IP, a.Name })
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<EFAlias>()
|
||||
.HasIndex(p => new { p.Name, p.IPAddress }).IsUnique();*/
|
||||
|
||||
|
||||
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
|
||||
|
||||
// todo: custom load DBSets from plugins
|
||||
// https://aleemkhan.wordpress.com/2013/02/28/dynamically-adding-dbset-properties-in-dbcontext-for-entity-framework-code-first/
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
165
SharedLibrary/Database/Importer.cs
Normal file
165
SharedLibrary/Database/Importer.cs
Normal file
@ -0,0 +1,165 @@
|
||||
using SharedLibrary.Database.Models;
|
||||
using SharedLibrary.Objects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database
|
||||
{
|
||||
//https://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework
|
||||
public static class Importer
|
||||
{
|
||||
public static void ImportClients(IList<Player> clients)
|
||||
{
|
||||
DatabaseContext context = null;
|
||||
|
||||
try
|
||||
{
|
||||
context = new DatabaseContext();
|
||||
context.Configuration.AutoDetectChangesEnabled = false;
|
||||
|
||||
int count = 0;
|
||||
foreach (var entityToInsert in clients)
|
||||
{
|
||||
++count;
|
||||
|
||||
var link = new EFAliasLink() { Active = true };
|
||||
|
||||
var alias = new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = entityToInsert.LastConnection,
|
||||
IPAddress = entityToInsert.IPAddress,
|
||||
Link = link,
|
||||
Name = entityToInsert.Name,
|
||||
};
|
||||
|
||||
var client = new EFClient()
|
||||
{
|
||||
Active = true,
|
||||
AliasLink = link,
|
||||
Connections = entityToInsert.Connections,
|
||||
CurrentAlias = alias,
|
||||
FirstConnection = entityToInsert.LastConnection,
|
||||
Level = entityToInsert.Level,
|
||||
LastConnection = entityToInsert.LastConnection,
|
||||
TotalConnectionTime = entityToInsert.TotalConnectionTime,
|
||||
Masked = entityToInsert.Masked,
|
||||
NetworkId = entityToInsert.NetworkId
|
||||
};
|
||||
|
||||
context = AddClient(context, client, count, 100, true);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (context != null)
|
||||
context.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static DatabaseContext AddClient(DatabaseContext context, EFClient client, int count, int commitCount, bool recreateContext)
|
||||
{
|
||||
context.Clients.Add(client);
|
||||
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;
|
||||
}
|
||||
|
||||
public static void ImportPenalties(IList<Penalty> penalties)
|
||||
{
|
||||
DatabaseContext context = null;
|
||||
|
||||
try
|
||||
{
|
||||
context = new DatabaseContext();
|
||||
context.Configuration.AutoDetectChangesEnabled = false;
|
||||
|
||||
int count = 0;
|
||||
foreach (var entityToInsert in penalties)
|
||||
{
|
||||
++count;
|
||||
var punisher = entityToInsert.Offender.NetworkId == entityToInsert.Punisher.NetworkId ?
|
||||
context.Clients.SingleOrDefault(c => c.ClientId == 1) :
|
||||
context.Clients.SingleOrDefault(c => c.NetworkId == entityToInsert.Punisher.NetworkId);
|
||||
if (punisher == null)
|
||||
continue;
|
||||
var offender = context.Clients.Include("AliasLink").SingleOrDefault(c => c.NetworkId == entityToInsert.Offender.NetworkId);
|
||||
|
||||
if (offender == null)
|
||||
continue;
|
||||
|
||||
|
||||
var penalty = new EFPenalty()
|
||||
{
|
||||
Active = true,
|
||||
Expires = entityToInsert.Expires.Year == 9999 ? DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MinValue.ToString()) : entityToInsert.Expires,
|
||||
Offender = offender,
|
||||
Punisher = punisher,
|
||||
Offense = entityToInsert.Offense,
|
||||
Type = entityToInsert.Type,
|
||||
When = entityToInsert.When == DateTime.MinValue ? DateTime.UtcNow : entityToInsert.When,
|
||||
Link = offender.AliasLink
|
||||
};
|
||||
|
||||
context = AddPenalty(context, penalty, count, 100, true);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (context != null)
|
||||
context.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static DatabaseContext AddPenalty(DatabaseContext context, EFPenalty penalty, int count, int commitCount, bool recreateContext)
|
||||
{
|
||||
context.Penalties.Add(penalty);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,22 +7,32 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database
|
||||
{
|
||||
public class Initializer : DropCreateDatabaseIfModelChanges<IW4MAdminDatabaseContext>
|
||||
public class Initializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
|
||||
{
|
||||
protected override void Seed(IW4MAdminDatabaseContext context)
|
||||
protected override void Seed(DatabaseContext context)
|
||||
{
|
||||
var aliasLink = new Models.EFAliasLink();
|
||||
|
||||
var currentAlias = new Models.EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = "0.0.0.0",
|
||||
Name = "IW4MAdmin",
|
||||
Link = aliasLink
|
||||
};
|
||||
|
||||
context.Clients.Add(new Models.EFClient()
|
||||
{
|
||||
Active = false,
|
||||
Connections = 0,
|
||||
AliasLink = new Models.EFAliasLink(),
|
||||
FirstConnection = DateTime.UtcNow,
|
||||
IPAddress = "127.0.0.1",
|
||||
LastConnection = DateTime.UtcNow,
|
||||
Level = Objects.Player.Permission.Console,
|
||||
Name = "IW4MAdmin",
|
||||
Masked = true,
|
||||
NetworkId = "0000000000000000",
|
||||
AliasLink = aliasLink,
|
||||
CurrentAlias = currentAlias
|
||||
});
|
||||
|
||||
base.Seed(context);
|
||||
|
@ -1,10 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
@ -16,10 +12,12 @@ namespace SharedLibrary.Database.Models
|
||||
public int LinkId { get; set; }
|
||||
[ForeignKey("LinkId")]
|
||||
public virtual EFAliasLink Link { get; set; }
|
||||
[Required]
|
||||
// [Index("IX_IPandName", 0, IsUnique = true)]
|
||||
//[MaxLength(24)]
|
||||
public string Name { get; set; }
|
||||
[Required]
|
||||
public string IP { get; set; }
|
||||
// [Index("IX_IPandName", 1, IsUnique = true)]
|
||||
// [MaxLength(24)]
|
||||
public string IPAddress { get; set; }
|
||||
[Required]
|
||||
public DateTime DateAdded { get; set; }
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharedLibrary.Database.Models
|
||||
{
|
||||
@ -8,10 +10,12 @@ namespace SharedLibrary.Database.Models
|
||||
[Key]
|
||||
public int AliasLinkId { get; set; }
|
||||
public virtual ICollection<EFAlias> Children { get; set; }
|
||||
public virtual ICollection<EFPenalty> ReceivedPenalties { get; set; }
|
||||
|
||||
public EFAliasLink()
|
||||
{
|
||||
Children = new List<EFAlias>();
|
||||
ReceivedPenalties = new List<EFPenalty>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,17 +15,11 @@ namespace SharedLibrary.Database.Models
|
||||
[Index(IsUnique = true)]
|
||||
public string NetworkId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
[Required]
|
||||
public Objects.Player.Permission Level { get; set; }
|
||||
[Required]
|
||||
public int Connections { get; set; }
|
||||
[Required]
|
||||
public int TotalConnectionTime { get; set; }
|
||||
[Required]
|
||||
public string IPAddress { get; set; }
|
||||
[Required]
|
||||
public DateTime FirstConnection { get; set; }
|
||||
[Required]
|
||||
public DateTime LastConnection { get; set; }
|
||||
@ -34,6 +28,27 @@ namespace SharedLibrary.Database.Models
|
||||
public int AliasLinkId { get; set; }
|
||||
[ForeignKey("AliasLinkId")]
|
||||
public virtual EFAliasLink AliasLink { get; set; }
|
||||
[Required]
|
||||
public Objects.Player.Permission Level { get; set; }
|
||||
|
||||
[Required]
|
||||
public int CurrentAliasId { get; set; }
|
||||
[ForeignKey("CurrentAliasId")]
|
||||
public virtual EFAlias CurrentAlias { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
public virtual string Name
|
||||
{
|
||||
get { return CurrentAlias.Name; }
|
||||
set { }
|
||||
}
|
||||
[NotMapped]
|
||||
public virtual string IPAddress
|
||||
{
|
||||
get { return CurrentAlias.IPAddress; }
|
||||
set { }
|
||||
}
|
||||
|
||||
public virtual ICollection<EFPenalty> ReceivedPenalties { get; set; }
|
||||
public virtual ICollection<EFPenalty> AdministeredPenalties { get; set; }
|
||||
|
||||
|
@ -5,7 +5,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibrary.Objects
|
||||
{
|
||||
{
|
||||
public class Player : Database.Models.EFClient
|
||||
{
|
||||
public enum Permission
|
||||
@ -25,8 +25,9 @@ namespace SharedLibrary.Objects
|
||||
public Player()
|
||||
{
|
||||
ConnectionTime = DateTime.UtcNow;
|
||||
ClientNumber = -1;
|
||||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Name}::{NetworkId}";
|
||||
@ -63,7 +64,7 @@ namespace SharedLibrary.Objects
|
||||
}
|
||||
|
||||
[NotMapped]
|
||||
public int ClientNumber { get; set; }
|
||||
public int ClientNumber { get; set; }
|
||||
[NotMapped]
|
||||
public int Ping { get; set; }
|
||||
[NotMapped]
|
||||
@ -72,5 +73,18 @@ namespace SharedLibrary.Objects
|
||||
public DateTime ConnectionTime { get; set; }
|
||||
[NotMapped]
|
||||
public Server CurrentServer { get; set; }
|
||||
|
||||
private string _ipaddress;
|
||||
public override string IPAddress
|
||||
{
|
||||
get { return _ipaddress; }
|
||||
set { _ipaddress = value; }
|
||||
}
|
||||
private string _name;
|
||||
public override string Name
|
||||
{
|
||||
get { return _name; }
|
||||
set { _name = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,22 @@ namespace SharedLibrary.Services
|
||||
{
|
||||
public async Task<EFAlias> Create(EFAlias entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
throw new Exception();
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity.Link = await context.AliasLinks.FirstAsync(a => a.AliasLinkId == entity.Link.AliasLinkId);
|
||||
var addedEntity = context.Aliases.Add(entity);
|
||||
var alias = new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = entity.IPAddress,
|
||||
Name = entity.Name
|
||||
};
|
||||
|
||||
entity.Link = await context.AliasLinks
|
||||
.FirstAsync(a => a.AliasLinkId == entity.Link.AliasLinkId);
|
||||
context.Aliases.Add(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return addedEntity;
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,11 +42,11 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFAlias> Delete(EFAlias entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Aliases.Single(e => e.AliasId == entity.AliasId);
|
||||
entity.Active = false;
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
var alias = context.Aliases
|
||||
.Single(e => e.AliasId == entity.AliasId);
|
||||
alias.Active = false;
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
@ -44,14 +54,18 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFAlias>> Find(Func<EFAlias, bool> expression)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await Task.Run(() => context.Aliases.Where(expression).ToList());
|
||||
using (var context = new DatabaseContext())
|
||||
return await Task.Run(() => context.Aliases
|
||||
.AsNoTracking()
|
||||
.Include(a => a.Link.Children)
|
||||
.Where(expression).ToList());
|
||||
}
|
||||
|
||||
public async Task<EFAlias> Get(int entityID)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Aliases
|
||||
.AsNoTracking()
|
||||
.SingleOrDefaultAsync(e => e.AliasId == entityID);
|
||||
}
|
||||
|
||||
@ -62,7 +76,8 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFAlias> Update(EFAlias entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
throw new Exception();
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Aliases.Attach(entity);
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
@ -73,7 +88,7 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFAliasLink> CreateLink(EFAliasLink link)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
context.AliasLinks.Add(link);
|
||||
await context.SaveChangesAsync();
|
||||
|
@ -11,46 +11,61 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace SharedLibrary.Services
|
||||
{
|
||||
|
||||
public class ClientService : Interfaces.IEntityService<EFClient>
|
||||
{
|
||||
private Dictionary<int, IW4MAdminDatabaseContext> _context;
|
||||
|
||||
public ClientService()
|
||||
{
|
||||
_context = new Dictionary<int, IW4MAdminDatabaseContext>();
|
||||
}
|
||||
public async Task<EFClient> Create(EFClient entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
|
||||
bool hasExistingAlias = false;
|
||||
// get all aliases by IP
|
||||
var alias = await context.Aliases.FirstOrDefaultAsync(a => a.IP == entity.IPAddress);
|
||||
EFAliasLink link = alias?.Link;
|
||||
var aliases = await context.Aliases
|
||||
.Include(a => a.Link)
|
||||
.Where(a => a.IPAddress == entity.IPAddress)
|
||||
.ToListAsync();
|
||||
|
||||
// see if they have a matching IP + Name but new NetworkId
|
||||
var existingAlias = aliases.SingleOrDefault(a => a.Name == entity.Name);
|
||||
// if existing alias matches link them
|
||||
EFAliasLink aliasLink = existingAlias?.Link;
|
||||
// if no exact matches find the first IP that matches
|
||||
aliasLink = aliasLink ?? aliases.FirstOrDefault()?.Link;
|
||||
// if no exact or IP matches, create new link
|
||||
aliasLink = aliasLink ?? new EFAliasLink()
|
||||
{
|
||||
Active = true,
|
||||
};
|
||||
|
||||
// this has to be set here because we can't evalute it properly later
|
||||
hasExistingAlias = existingAlias != null;
|
||||
|
||||
// if no existing alias create new alias
|
||||
existingAlias = existingAlias ?? new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = entity.IPAddress,
|
||||
Link = aliasLink,
|
||||
Name = entity.Name,
|
||||
};
|
||||
|
||||
var client = new EFClient()
|
||||
{
|
||||
Active = true,
|
||||
Name = entity.Name,
|
||||
// set the level to the level of the existing client if they have the same IP + Name but new NetworkId
|
||||
Level = hasExistingAlias ?
|
||||
context.Clients.First(c => c.CurrentAliasId == existingAlias.AliasId).Level :
|
||||
Objects.Player.Permission.User,
|
||||
FirstConnection = DateTime.UtcNow,
|
||||
Connections = 1,
|
||||
IPAddress = entity.IPAddress,
|
||||
LastConnection = DateTime.UtcNow,
|
||||
Level = Objects.Player.Permission.User,
|
||||
Masked = false,
|
||||
NetworkId = entity.NetworkId,
|
||||
AliasLink = link ?? new EFAliasLink() { Active = true }
|
||||
AliasLink = aliasLink,
|
||||
CurrentAlias = existingAlias
|
||||
};
|
||||
|
||||
client.AliasLink.Children.Add(new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IP = entity.IPAddress,
|
||||
Link = client.AliasLink,
|
||||
Name = entity.Name
|
||||
});
|
||||
|
||||
context.Clients.Add(client);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
@ -60,11 +75,11 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFClient> Delete(EFClient entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Clients.Single(e => e.ClientId == entity.ClientId);
|
||||
var client = context.Clients
|
||||
.Single(e => e.ClientId == entity.ClientId);
|
||||
entity.Active = false;
|
||||
entity.Level = Objects.Player.Permission.User;
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
@ -73,25 +88,31 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFClient>> Find(Func<EFClient, bool> e)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await Task.Run(() => context.Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(c => c.AliasLink.Children)
|
||||
.Where(e).ToList());
|
||||
}
|
||||
|
||||
public async Task<EFClient> Get(int entityID)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await new IW4MAdminDatabaseContext().Clients
|
||||
using (var context = new DatabaseContext())
|
||||
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)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
return await context.Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(c => c.AliasLink.Children)
|
||||
.SingleOrDefaultAsync(c => c.NetworkId == entityAttribute);
|
||||
}
|
||||
@ -99,42 +120,97 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFClient> Update(EFClient entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Clients.Attach(entity);
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
// grab the context version of the entity
|
||||
var client = context.Clients
|
||||
.Include(c => c.AliasLink)
|
||||
.Include(c=> c.CurrentAlias)
|
||||
.Single(e => e.ClientId == entity.ClientId);
|
||||
|
||||
// if their level has been changed
|
||||
if (entity.Level != client.Level)
|
||||
{
|
||||
// get all clients that use the same aliasId
|
||||
var matchingClients = await context.Clients
|
||||
.Where(c => c.CurrentAliasId == client.CurrentAliasId)
|
||||
.ToListAsync();
|
||||
|
||||
// update all related clients level
|
||||
matchingClients.ForEach(c => c.Level = (client.Level == Objects.Player.Permission.Banned) ?
|
||||
client.Level : entity.Level);
|
||||
}
|
||||
|
||||
// their alias has been updated and not yet saved
|
||||
if (entity.CurrentAlias.AliasId == 0)
|
||||
{
|
||||
client.CurrentAlias = new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = entity.CurrentAlias.IPAddress,
|
||||
Name = entity.CurrentAlias.Name,
|
||||
Link = client.AliasLink
|
||||
};
|
||||
}
|
||||
|
||||
// set remaining non-navigation properties that may have been updated
|
||||
client.Level = entity.Level;
|
||||
client.LastConnection = entity.LastConnection;
|
||||
client.Connections = entity.Connections;
|
||||
client.FirstConnection = entity.FirstConnection;
|
||||
client.Masked = entity.Masked;
|
||||
client.TotalConnectionTime = entity.TotalConnectionTime;
|
||||
|
||||
// update in database
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
|
||||
// this is set so future updates don't trigger a new alias add
|
||||
if (entity.CurrentAlias.AliasId == 0)
|
||||
entity.CurrentAlias.AliasId = client.CurrentAlias.AliasId;
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
#region ServiceSpecific
|
||||
#region ServiceSpecific
|
||||
public async Task<IList<EFClient>> GetOwners()
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await context.Clients.Where(c => c.Level == Objects.Player.Permission.Owner).ToListAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Clients
|
||||
.Where(c => c.Level == Objects.Player.Permission.Owner)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IList<EFClient>> GetPrivilegedClients()
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await new IW4MAdminDatabaseContext().Clients
|
||||
.Where(c => c.Level >= Objects.Player.Permission.Trusted)
|
||||
.ToListAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await new DatabaseContext().Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Where(c => c.Level >= Objects.Player.Permission.Trusted)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task<IList<EFClient>> GetRecentClients(int offset, int count)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await context.Clients.OrderByDescending(p => p.ClientId).Skip(offset).Take(count).ToListAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(p => p.AliasLink)
|
||||
.OrderByDescending(p => p.ClientId)
|
||||
.Skip(offset)
|
||||
.Take(count)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IList<EFClient>> PruneInactivePrivilegedClients(int inactiveDays)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
var inactive = await context.Clients.Where(c => c.Level > Objects.Player.Permission.Flagged)
|
||||
.AsNoTracking()
|
||||
.Where(c => (DateTime.UtcNow - c.LastConnection).TotalDays >= inactiveDays)
|
||||
.ToListAsync();
|
||||
inactive.ForEach(c => c.Level = Objects.Player.Permission.User);
|
||||
@ -145,14 +221,15 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<int> GetTotalClientsAsync()
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await context.Clients.CountAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Clients
|
||||
.CountAsync();
|
||||
}
|
||||
|
||||
public Task<EFClient> CreateProxy()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,29 @@ namespace SharedLibrary.Services
|
||||
{
|
||||
public async Task<EFPenalty> Create(EFPenalty entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity.Offender = context.Clients.First(e => e.ClientId == entity.Offender.ClientId);
|
||||
entity.Punisher = context.Clients.First(e => e.ClientId == entity.Punisher.ClientId);
|
||||
entity.Link = context.AliasLinks.First(l => l.AliasLinkId == entity.Link.AliasLinkId);
|
||||
if (entity.Expires == DateTime.MinValue)
|
||||
|
||||
// make bans propogate to all aliases
|
||||
if (entity.Type == Objects.Penalty.PenaltyType.Ban)
|
||||
{
|
||||
entity.Expires = DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString());
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == entity.Link.AliasLinkId)
|
||||
.ForEachAsync(c => c.Level = Objects.Player.Permission.Banned);
|
||||
}
|
||||
|
||||
// make flags propogate to all aliases
|
||||
else if (entity.Type == Objects.Penalty.PenaltyType.Flag)
|
||||
{
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == entity.Link.AliasLinkId)
|
||||
.ForEachAsync(c => c.Level = Objects.Player.Permission.Flagged);
|
||||
}
|
||||
|
||||
context.Penalties.Add(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
@ -40,7 +56,7 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
return await Task.Run(() => context.Penalties
|
||||
.Include(p => p.Offender)
|
||||
@ -68,11 +84,12 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFPenalty>> GetRecentPenalties(int count, int offset)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Penalties
|
||||
.Include(p => p.Offender)
|
||||
.Include(p => p.Punisher)
|
||||
.Where(p => p.Active)
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Offender.CurrentAlias)
|
||||
.Include(p => p.Punisher.CurrentAlias)
|
||||
.Where(p => p.Active)
|
||||
.OrderByDescending(p => p.When)
|
||||
.Skip(offset)
|
||||
.Take(count)
|
||||
@ -81,18 +98,19 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFPenalty>> GetClientPenaltiesAsync(int clientId)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Penalties
|
||||
.Where(p => p.OffenderId == clientId)
|
||||
.Where(p => p.Active)
|
||||
.Include(p => p.Offender)
|
||||
.Include(p => p.Punisher)
|
||||
.ToListAsync();
|
||||
.AsNoTracking()
|
||||
.Where(p => p.OffenderId == clientId)
|
||||
.Where(p => p.Active)
|
||||
.Include(p => p.Offender.CurrentAlias)
|
||||
.Include(p => p.Punisher.CurrentAlias)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task RemoveActivePenalties(int aliasLinkId)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var penalties = await context.Penalties
|
||||
@ -104,10 +122,11 @@ namespace SharedLibrary.Services
|
||||
penalties.ForEach(async p =>
|
||||
{
|
||||
p.Active = false;
|
||||
var clients = await context.Clients.Where(cl => cl.AliasLinkId == p.LinkId).ToListAsync();
|
||||
foreach (var c in clients)
|
||||
if (c.Level == Objects.Player.Permission.Banned)
|
||||
c.Level = Objects.Player.Permission.User;
|
||||
// reset the player levels
|
||||
if (p.Type == Objects.Penalty.PenaltyType.Ban)
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == p.LinkId)
|
||||
.ForEachAsync(c => c.Level = Objects.Player.Permission.User);
|
||||
});
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
@ -94,8 +94,9 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Database\Importer.cs" />
|
||||
<Compile Include="Database\Initializer.cs" />
|
||||
<Compile Include="Database\IW4MAdminDatabaseContext.cs" />
|
||||
<Compile Include="Database\DatabaseContext.cs" />
|
||||
<Compile Include="Database\Models\EFAlias.cs" />
|
||||
<Compile Include="Database\Models\EFAliasLink.cs" />
|
||||
<Compile Include="Database\Models\EFClient.cs" />
|
||||
|
@ -90,7 +90,7 @@ namespace SharedLibrary
|
||||
{
|
||||
if (str == null)
|
||||
return "";
|
||||
return Regex.Replace(str, @"\^([0-9]|\:)", "");
|
||||
return Regex.Replace(str, @"(\^+((?![a-z]|[A-Z]).){0,1})+", "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -207,7 +207,7 @@ namespace SharedLibrary
|
||||
|
||||
public static String GetTimePassed(DateTime start, bool includeAgo)
|
||||
{
|
||||
TimeSpan Elapsed = DateTime.Now - start;
|
||||
TimeSpan Elapsed = DateTime.UtcNow - start;
|
||||
string ago = includeAgo ? " ago" : "";
|
||||
|
||||
if (Elapsed.TotalSeconds < 30 && includeAgo)
|
||||
@ -305,19 +305,20 @@ namespace SharedLibrary
|
||||
return client == null ? null : new Player()
|
||||
{
|
||||
Active = client.Active,
|
||||
AliasLink =client.AliasLink,
|
||||
AliasLink = client.AliasLink,
|
||||
AliasLinkId = client.AliasLinkId,
|
||||
ClientId = client.ClientId,
|
||||
ClientNumber = 0,
|
||||
FirstConnection = client.FirstConnection,
|
||||
Connections = client.Connections,
|
||||
IPAddress = client.IPAddress,
|
||||
NetworkId = client.NetworkId,
|
||||
Name = client.Name,
|
||||
Level = client.Level,
|
||||
TotalConnectionTime = client.TotalConnectionTime,
|
||||
Masked = client.Masked,
|
||||
LastConnection = DateTime.UtcNow
|
||||
Name = client.CurrentAlias.Name,
|
||||
IPAddress = client.CurrentAlias.IPAddress,
|
||||
Level = client.Level,
|
||||
LastConnection = DateTime.UtcNow,
|
||||
CurrentAlias = client.CurrentAlias
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user