more alias changes :(

fix flag penalty coming from wrong user
This commit is contained in:
RaidMax 2019-01-02 18:32:39 -06:00
parent 7b75c35c9b
commit aaf9eb09b6
7 changed files with 144 additions and 167 deletions

View File

@ -6,12 +6,12 @@
<RuntimeFrameworkVersion>2.1.5</RuntimeFrameworkVersion> <RuntimeFrameworkVersion>2.1.5</RuntimeFrameworkVersion>
<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish> <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
<PackageId>RaidMax.IW4MAdmin.Application</PackageId> <PackageId>RaidMax.IW4MAdmin.Application</PackageId>
<Version>2.2.3.0</Version> <Version>2.2.3.1</Version>
<Authors>RaidMax</Authors> <Authors>RaidMax</Authors>
<Company>Forever None</Company> <Company>Forever None</Company>
<Product>IW4MAdmin</Product> <Product>IW4MAdmin</Product>
<Description>IW4MAdmin is a complete server administration tool for IW4x and most Call of Duty® dedicated server</Description> <Description>IW4MAdmin is a complete server administration tool for IW4x and most Call of Duty® dedicated server</Description>
<Copyright>2018</Copyright> <Copyright>2019</Copyright>
<PackageLicenseUrl>https://github.com/RaidMax/IW4M-Admin/blob/master/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://github.com/RaidMax/IW4M-Admin/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://raidmax.org/IW4MAdmin</PackageProjectUrl> <PackageProjectUrl>https://raidmax.org/IW4MAdmin</PackageProjectUrl>
<RepositoryUrl>https://github.com/RaidMax/IW4M-Admin</RepositoryUrl> <RepositoryUrl>https://github.com/RaidMax/IW4M-Admin</RepositoryUrl>
@ -31,8 +31,8 @@
<PropertyGroup> <PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection> <ServerGarbageCollection>true</ServerGarbageCollection>
<TieredCompilation>true</TieredCompilation> <TieredCompilation>true</TieredCompilation>
<AssemblyVersion>2.2.3.0</AssemblyVersion> <AssemblyVersion>2.2.3.1</AssemblyVersion>
<FileVersion>2.2.3.0</FileVersion> <FileVersion>2.2.3.1</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -843,7 +843,7 @@ namespace IW4MAdmin
Type = Penalty.PenaltyType.Warning, Type = Penalty.PenaltyType.Warning,
Expires = DateTime.UtcNow, Expires = DateTime.UtcNow,
Offender = Target, Offender = Target,
Punisher = Utilities.IW4MAdminClient(this), Punisher = Origin,
Offense = Reason, Offense = Reason,
Link = Target.AliasLink Link = Target.AliasLink
}; };

View File

@ -36,6 +36,13 @@ namespace SharedLibraryCore.Database
this.ChangeTracker.LazyLoadingEnabled = false; this.ChangeTracker.LazyLoadingEnabled = false;
this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
} }
else
{
this.ChangeTracker.AutoDetectChangesEnabled = true;
this.ChangeTracker.LazyLoadingEnabled = true;
this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
}
} }
public DatabaseContext(string connStr, string provider) public DatabaseContext(string connStr, string provider)

View File

@ -465,6 +465,7 @@ namespace SharedLibraryCore.Database.Models
} }
OnConnect(); OnConnect();
await CurrentServer.Manager.GetClientService().Update(this);
CurrentServer.Logger.WriteDebug($"OnConnect finished for {this}"); CurrentServer.Logger.WriteDebug($"OnConnect finished for {this}");

View File

@ -66,7 +66,7 @@ namespace SharedLibraryCore
//Returns list of all current players //Returns list of all current players
public List<EFClient> GetClientsAsList() public List<EFClient> GetClientsAsList()
{ {
return Clients.FindAll(x => x != null); return Clients.FindAll(x => x != null && x.NetworkId != 0);
} }
/// <summary> /// <summary>

View File

@ -50,8 +50,9 @@ namespace SharedLibraryCore.Services
} }
} }
public async Task UpdateAlias(EFClient entity) private async Task UpdateAlias(string name, int? ip, EFClient entity, DatabaseContext context)
{ {
// entity is the tracked db context item
// todo: move this out // todo: move this out
#if DEBUG == false #if DEBUG == false
if (entity.IsBot) if (entity.IsBot)
@ -59,100 +60,93 @@ namespace SharedLibraryCore.Services
return; return;
} }
#endif #endif
using (var context = new DatabaseContext()) // get all aliases by IP address and LinkId
{ var iqAliases = context.Aliases
string name = entity.Name; .Include(a => a.Link)
int? ip = entity.IPAddress; .Where(a => (a.IPAddress == ip) ||
a.LinkId == entity.AliasLinkId);
// indicates if someone appears to have played before
bool hasExistingAlias = false;
// get all aliases by IP address and LinkId
var iqAliases = context.Aliases
.Include(a => a.Link)
//.Where(a => a.Link.Active)
.Where(a => (a.IPAddress == ip) ||
a.LinkId == entity.AliasLinkId);
#if DEBUG == true #if DEBUG == true
var aliasSql = iqAliases.ToSql(); var aliasSql = iqAliases.ToSql();
#endif #endif
var aliases = await iqAliases.ToListAsync(); var aliases = await iqAliases.ToListAsync();
// see if they have a matching IP + Name but new NetworkId // see if they have a matching IP + Name but new NetworkId
var existingAlias = aliases.FirstOrDefault(a => a.Name == name && a.IPAddress == ip); var existingExactAlias = aliases.FirstOrDefault(a => a.Name == name && a.IPAddress == ip);
bool exactAliasMatch = existingAlias != null; bool exactAliasMatch = existingExactAlias != null;
// 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 matches are found, create new link
aliasLink = aliasLink ?? new EFAliasLink();
hasExistingAlias = aliases.Count > 0; // if existing alias matches link them
EFAliasLink aliasLink = existingExactAlias?.Link;
// if no exact matches find the first IP that matches
aliasLink = aliasLink ?? aliases.FirstOrDefault()?.Link;
// if no matches are found, use our current one
aliasLink = aliasLink ?? entity.AliasLink;
// the existing alias matches ip and name, so we can just ignore the temporary one bool hasExistingAlias = aliases.Count > 0;
if (exactAliasMatch)
// this happens when an alias exists but the current link is a temporary one
if ((exactAliasMatch || hasExistingAlias) &&
(!entity.AliasLink.Active && entity.AliasLinkId != aliasLink.AliasLinkId))
{
entity.AliasLinkId = aliasLink.AliasLinkId;
entity.AliasLink = aliasLink;
//entity.CurrentServer.Logger.WriteDebug($"Updating alias link for {entity}");
await context.SaveChangesAsync();
foreach (var alias in aliases.Union(new List<EFAlias>() { entity.CurrentAlias })
.Where(_alias => !_alias.Active ||
_alias.LinkId != aliasLink.AliasLinkId))
{ {
entity.CurrentServer.Logger.WriteDebug($"{entity} has exact alias match"); entity.CurrentServer.Logger.WriteDebug($"{entity} updating alias-link id is {alias.LinkId}");
// they're using the same alias as before, so we need to make sure the current aliases is set to it alias.Active = true;
if (entity.CurrentAliasId != existingAlias.AliasId) alias.LinkId = aliasLink.AliasLinkId;
{ }
context.Update(entity);
if (existingAlias.AliasId > 0) //entity.CurrentServer.Logger.WriteDebug($"Saving updated aliases for {entity}");
{ await context.SaveChangesAsync();
entity.CurrentAliasId = existingAlias.AliasId;
}
else
{
entity.CurrentServer.Logger.WriteDebug($"Updating alias for {entity} failed");
}
}
if (!entity.CurrentAlias.Active) // todo: fix this
{ /*context.AliasLinks.Remove(entity.AliasLink);
existingAlias.Active = true; entity.AliasLink = null;
}
entity.CurrentAlias = existingAlias; //entity.CurrentServer.Logger.WriteDebug($"Removing temporary link for {entity}");
try
{
await context.SaveChangesAsync();
}
catch
{
// entity.CurrentServer.Logger.WriteDebug($"Failed to remove link for {entity}");
}*/
}
// the existing alias matches ip and name, so we can just ignore the temporary one
if (exactAliasMatch)
{
entity.CurrentServer.Logger.WriteDebug($"{entity} has exact alias match");
entity.CurrentAliasId = existingExactAlias.AliasId;
entity.CurrentAlias = existingExactAlias;
await context.SaveChangesAsync();
}
// theres no exact match, but they've played before with the GUID or IP
else if (hasExistingAlias)
{
//entity.CurrentServer.Logger.WriteDebug($"Connecting player is using a new alias {entity}");
// this happens when a temporary alias gets updated
if (entity.CurrentAlias.Name == name && entity.CurrentAlias.IPAddress == null)
{
entity.CurrentAlias.IPAddress = ip;
entity.CurrentAlias.Active = true;
//entity.CurrentServer.Logger.WriteDebug($"Updating temporary alias for {entity}");
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
// theres no exact match, but they've played before with the GUID or IP else
else if (hasExistingAlias)
{ {
// the current link is temporary so we need to update
if (!entity.AliasLink.Active)
{
entity.CurrentServer.Logger.WriteDebug($"{entity} has temporary alias so we are deleting");
foreach (var alias in aliases.Where(_alias => !_alias.Active))
{
context.Update(alias);
if (alias.IPAddress == null && alias.Name == name)
{
context.Entry(alias).State = EntityState.Deleted;
}
else
{
alias.Active = true;
alias.LinkId = aliasLink.AliasLinkId;
}
}
// we want to delete the temporary alias link
context.Entry(entity.AliasLink).State = EntityState.Deleted;
}
context.Update(entity);
entity.AliasLink = aliasLink;
entity.AliasLinkId = aliasLink.AliasLinkId;
entity.CurrentServer.Logger.WriteDebug($"Connecting player is using a new alias {entity}");
var newAlias = new EFAlias() var newAlias = new EFAlias()
{ {
DateAdded = DateTime.UtcNow, DateAdded = DateTime.UtcNow,
@ -162,65 +156,43 @@ namespace SharedLibraryCore.Services
Active = true, Active = true,
}; };
context.Aliases.Add(newAlias);
entity.CurrentAlias = newAlias; entity.CurrentAlias = newAlias;
//entity.CurrentServer.Logger.WriteDebug($"Saving new alias for {entity}");
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
}
// no record of them playing // no record of them playing
else else
{
//entity.CurrentServer.Logger.WriteDebug($"{entity} has not be seen before");
entity.AliasLink.Active = true;
entity.CurrentAlias.Active = true;
entity.CurrentAlias.IPAddress = ip;
entity.CurrentAlias.Name = name;
//entity.CurrentServer.Logger.WriteDebug($"updating new alias for {entity}");
await context.SaveChangesAsync();
}
var linkIds = aliases.Select(a => a.LinkId);
if (linkIds.Count() > 0 &&
aliases.Count(_alias => _alias.Name == name && _alias.IPAddress == ip) > 0)
{
var highestLevel = await context.Clients
.Where(c => linkIds.Contains(c.AliasLinkId))
.MaxAsync(c => c.Level);
if (entity.Level != highestLevel)
{ {
entity.CurrentServer.Logger.WriteDebug($"{entity} has not be seen before"); entity.CurrentServer.Logger.WriteDebug($"{entity} updating user level");
// todo: log level changes here
if (name != entity.CurrentAlias.Name) context.Update(entity);
{ entity.Level = highestLevel;
var newAlias = new EFAlias()
{
DateAdded = DateTime.UtcNow,
IPAddress = ip,
LinkId = entity.AliasLink.AliasLinkId,
Name = name,
Active = true
};
context.Update(entity);
context.Aliases.Add(newAlias);
entity.CurrentAlias = newAlias;
}
else
{
context.Update(entity.CurrentAlias);
entity.CurrentAlias.Active = true;
entity.CurrentAlias.IPAddress = ip;
entity.CurrentAlias.Link = entity.AliasLink;
}
context.Update(entity.AliasLink);
entity.AliasLink.Active = true;
await context.SaveChangesAsync(); await context.SaveChangesAsync();
} }
var linkIds = aliases.Select(a => a.LinkId);
if (linkIds.Count() > 0 &&
aliases.Count(_alias => _alias.Name == name && _alias.IPAddress == ip) > 0)
{
var highestLevel = await context.Clients
.Where(c => linkIds.Contains(c.AliasLinkId))
.MaxAsync(c => c.Level);
if (entity.Level != highestLevel)
{
// todo: log level changes here
context.Update(entity);
entity.Level = highestLevel;
await context.SaveChangesAsync();
}
}
} }
} }
@ -310,16 +282,36 @@ namespace SharedLibraryCore.Services
} }
} }
public async Task UpdateAlias(EFClient entity)
{
using (var context = new DatabaseContext())
{
var client = context.Clients
.Include(c => c.AliasLink)
.Include(c => c.CurrentAlias)
.First(e => e.ClientId == entity.ClientId);
client.CurrentServer = entity.CurrentServer;
await UpdateAlias(entity.Name, entity.IPAddress, client, context);
entity.CurrentAlias = client.CurrentAlias;
entity.CurrentAliasId = client.CurrentAliasId;
entity.AliasLink = client.AliasLink;
entity.AliasLinkId = client.AliasLinkId;
}
}
public async Task<EFClient> Update(EFClient entity) public async Task<EFClient> Update(EFClient entity)
{ {
using (var context = new DatabaseContext()) using (var context = new DatabaseContext())
{ {
// grab the context version of the entity // grab the context version of the entity
var client = context.Clients var client = context.Clients
.Include(c => c.AliasLink)
.Include(c => c.CurrentAlias)
.First(e => e.ClientId == entity.ClientId); .First(e => e.ClientId == entity.ClientId);
client.CurrentServer = entity.CurrentServer;
// if their level has been changed // if their level has been changed
if (entity.Level != client.Level) if (entity.Level != client.Level)
{ {
@ -332,30 +324,11 @@ namespace SharedLibraryCore.Services
// update all related clients level // update all related clients level
await matchingClients.ForEachAsync(c => await matchingClients.ForEachAsync(c =>
{ {
// todo: log that it has changed here
c.Level = entity.Level; c.Level = entity.Level;
}); });
} }
// their alias has been updated and not yet saved
if (entity.CurrentAlias.AliasId == 0)
{
client.CurrentAlias = new EFAlias()
{
Active = entity.CurrentAlias.IPAddress.HasValue ? true : false,
DateAdded = DateTime.UtcNow,
IPAddress = entity.CurrentAlias.IPAddress,
Name = entity.CurrentAlias.Name,
Link = client.AliasLink
};
}
else
{
client.CurrentAliasId = entity.CurrentAliasId;
client.IPAddress = entity.IPAddress;
client.Name = entity.Name;
}
// set remaining non-navigation properties that may have been updated // set remaining non-navigation properties that may have been updated
client.Level = entity.Level; client.Level = entity.Level;
client.LastConnection = entity.LastConnection; client.LastConnection = entity.LastConnection;

View File

@ -18,10 +18,6 @@ namespace SharedLibraryCore.Services
{ {
using (var context = new DatabaseContext()) using (var context = new DatabaseContext())
{ {
context.ChangeTracker.AutoDetectChangesEnabled = true;
context.ChangeTracker.LazyLoadingEnabled = true;
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
// make bans propogate to all aliases // make bans propogate to all aliases
if (newEntity.Type == Penalty.PenaltyType.Ban) if (newEntity.Type == Penalty.PenaltyType.Ban)
{ {
@ -95,7 +91,7 @@ namespace SharedLibraryCore.Services
IsEvadedOffense = newEntity.IsEvadedOffense IsEvadedOffense = newEntity.IsEvadedOffense
}; };
newEntity.Offender.ReceivedPenalties.Add(penalty); newEntity.Offender.ReceivedPenalties?.Add(penalty);
context.Penalties.Add(penalty); context.Penalties.Add(penalty);
} }