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>
<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
<PackageId>RaidMax.IW4MAdmin.Application</PackageId>
<Version>2.2.3.0</Version>
<Version>2.2.3.1</Version>
<Authors>RaidMax</Authors>
<Company>Forever None</Company>
<Product>IW4MAdmin</Product>
<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>
<PackageProjectUrl>https://raidmax.org/IW4MAdmin</PackageProjectUrl>
<RepositoryUrl>https://github.com/RaidMax/IW4M-Admin</RepositoryUrl>
@ -31,8 +31,8 @@
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<TieredCompilation>true</TieredCompilation>
<AssemblyVersion>2.2.3.0</AssemblyVersion>
<FileVersion>2.2.3.0</FileVersion>
<AssemblyVersion>2.2.3.1</AssemblyVersion>
<FileVersion>2.2.3.1</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

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

View File

@ -36,6 +36,13 @@ namespace SharedLibraryCore.Database
this.ChangeTracker.LazyLoadingEnabled = false;
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)

View File

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

View File

@ -66,7 +66,7 @@ namespace SharedLibraryCore
//Returns list of all current players
public List<EFClient> GetClientsAsList()
{
return Clients.FindAll(x => x != null);
return Clients.FindAll(x => x != null && x.NetworkId != 0);
}
/// <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
#if DEBUG == false
if (entity.IsBot)
@ -59,18 +60,9 @@ namespace SharedLibraryCore.Services
return;
}
#endif
using (var context = new DatabaseContext())
{
string name = entity.Name;
int? ip = entity.IPAddress;
// 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);
@ -80,79 +72,81 @@ namespace SharedLibraryCore.Services
var aliases = await iqAliases.ToListAsync();
// see if they have a matching IP + Name but new NetworkId
var existingAlias = aliases.FirstOrDefault(a => a.Name == name && a.IPAddress == ip);
bool exactAliasMatch = existingAlias != null;
var existingExactAlias = aliases.FirstOrDefault(a => a.Name == name && a.IPAddress == ip);
bool exactAliasMatch = existingExactAlias != null;
// if existing alias matches link them
EFAliasLink aliasLink = existingAlias?.Link;
EFAliasLink aliasLink = existingExactAlias?.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();
// if no matches are found, use our current one
aliasLink = aliasLink ?? entity.AliasLink;
hasExistingAlias = aliases.Count > 0;
bool hasExistingAlias = aliases.Count > 0;
// 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} updating alias-link id is {alias.LinkId}");
alias.Active = true;
alias.LinkId = aliasLink.AliasLinkId;
}
//entity.CurrentServer.Logger.WriteDebug($"Saving updated aliases for {entity}");
await context.SaveChangesAsync();
// todo: fix this
/*context.AliasLinks.Remove(entity.AliasLink);
entity.AliasLink = null;
//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");
// they're using the same alias as before, so we need to make sure the current aliases is set to it
if (entity.CurrentAliasId != existingAlias.AliasId)
{
context.Update(entity);
if (existingAlias.AliasId > 0)
{
entity.CurrentAliasId = existingAlias.AliasId;
}
else
{
entity.CurrentServer.Logger.WriteDebug($"Updating alias for {entity} failed");
}
}
if (!entity.CurrentAlias.Active)
{
existingAlias.Active = true;
}
entity.CurrentAlias = existingAlias;
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)
{
// 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");
//entity.CurrentServer.Logger.WriteDebug($"Connecting player is using a new alias {entity}");
foreach (var alias in aliases.Where(_alias => !_alias.Active))
// this happens when a temporary alias gets updated
if (entity.CurrentAlias.Name == name && entity.CurrentAlias.IPAddress == null)
{
context.Update(alias);
if (alias.IPAddress == null && alias.Name == name)
{
context.Entry(alias).State = EntityState.Deleted;
entity.CurrentAlias.IPAddress = ip;
entity.CurrentAlias.Active = true;
//entity.CurrentServer.Logger.WriteDebug($"Updating temporary alias for {entity}");
await context.SaveChangesAsync();
}
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()
{
DateAdded = DateTime.UtcNow,
@ -162,44 +156,23 @@ namespace SharedLibraryCore.Services
Active = true,
};
context.Aliases.Add(newAlias);
entity.CurrentAlias = newAlias;
//entity.CurrentServer.Logger.WriteDebug($"Saving new alias for {entity}");
await context.SaveChangesAsync();
}
}
// no record of them playing
else
{
entity.CurrentServer.Logger.WriteDebug($"{entity} has not be seen before");
//entity.CurrentServer.Logger.WriteDebug($"{entity} has not be seen before");
if (name != entity.CurrentAlias.Name)
{
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.AliasLink.Active = true;
entity.CurrentAlias.Active = true;
entity.CurrentAlias.IPAddress = ip;
entity.CurrentAlias.Link = entity.AliasLink;
}
context.Update(entity.AliasLink);
entity.AliasLink.Active = true;
entity.CurrentAlias.Name = name;
//entity.CurrentServer.Logger.WriteDebug($"updating new alias for {entity}");
await context.SaveChangesAsync();
}
@ -214,14 +187,13 @@ namespace SharedLibraryCore.Services
if (entity.Level != highestLevel)
{
entity.CurrentServer.Logger.WriteDebug($"{entity} updating user level");
// todo: log level changes here
context.Update(entity);
entity.Level = highestLevel;
await context.SaveChangesAsync();
}
}
}
}
public async Task<EFClient> Delete(EFClient entity)
@ -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)
{
using (var context = new DatabaseContext())
{
// grab the context version of the entity
var client = context.Clients
.Include(c => c.AliasLink)
.Include(c => c.CurrentAlias)
.First(e => e.ClientId == entity.ClientId);
client.CurrentServer = entity.CurrentServer;
// if their level has been changed
if (entity.Level != client.Level)
{
@ -332,30 +324,11 @@ namespace SharedLibraryCore.Services
// update all related clients level
await matchingClients.ForEachAsync(c =>
{
// todo: log that it has changed here
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
client.Level = entity.Level;
client.LastConnection = entity.LastConnection;

View File

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