mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 23:31:13 -05:00
hopefulyl fix aliasing issue
bans are applied to an account if the accounts are linked but penallty on a different accounts
This commit is contained in:
@ -29,6 +29,7 @@ namespace SharedLibraryCore.Services
|
||||
{
|
||||
Active = false
|
||||
},
|
||||
ReceivedPenalties = new List<EFPenalty>()
|
||||
};
|
||||
|
||||
client.CurrentAlias = new Alias()
|
||||
@ -63,14 +64,15 @@ namespace SharedLibraryCore.Services
|
||||
string name = entity.Name;
|
||||
int? ip = entity.IPAddress;
|
||||
|
||||
// indicates if someone appears to have played before
|
||||
bool hasExistingAlias = false;
|
||||
|
||||
// get all aliases by IP
|
||||
// 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 != null && a.IPAddress == ip) ||
|
||||
a.LinkId == entity.AliasLinkId);
|
||||
a.LinkId == entity.AliasLinkId);
|
||||
|
||||
#if DEBUG == true
|
||||
var aliasSql = iqAliases.ToSql();
|
||||
@ -78,102 +80,97 @@ 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);
|
||||
var existingAlias = aliases.FirstOrDefault(a => a.Name == name && a.IPAddress == ip);
|
||||
bool exactAliasMatch = existingAlias != 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 exact or IP matches, create new link
|
||||
// if no matches are found, create new link
|
||||
aliasLink = aliasLink ?? new EFAliasLink();
|
||||
|
||||
// this has to be set here because we can't evalute it properly later
|
||||
hasExistingAlias = existingAlias != null;
|
||||
hasExistingAlias = aliases.Count > 0;
|
||||
|
||||
if (hasExistingAlias && !entity.AliasLink.Active)
|
||||
// the existing alias matches ip and name, so we can just ignore the temporary one
|
||||
if (exactAliasMatch)
|
||||
{
|
||||
entity.CurrentServer.Logger.WriteDebug($"Removing temporary alias for ${entity}");
|
||||
|
||||
// we want to delete the temporary alias
|
||||
context.Entry(entity.CurrentAlias).State = EntityState.Deleted;
|
||||
entity.CurrentAlias = null;
|
||||
|
||||
// we want to delete the temporary alias link
|
||||
context.Entry(entity.AliasLink).State = EntityState.Deleted;
|
||||
entity.AliasLink = null;
|
||||
|
||||
// they have an existing alias so assign it
|
||||
entity.CurrentAlias = existingAlias;
|
||||
entity.AliasLink = aliasLink;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
entity.AliasLinkId = aliasLink.AliasLinkId;
|
||||
}
|
||||
|
||||
// update the temporary alias to permanent one
|
||||
else if (!entity.AliasLink.Active)
|
||||
{
|
||||
entity.CurrentServer.Logger.WriteDebug($"Linking permanent alias for ${entity}");
|
||||
|
||||
// we want to track the current alias and link
|
||||
var alias = context.Update(entity.CurrentAlias).Entity;
|
||||
var _aliasLink = context.Update(entity.AliasLink).Entity;
|
||||
|
||||
alias.Active = true;
|
||||
alias.IPAddress = ip;
|
||||
alias.Name = name;
|
||||
_aliasLink.Active = true;
|
||||
|
||||
existingAlias = alias;
|
||||
aliasLink = _aliasLink;
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
entity.AliasLinkId = aliasLink.AliasLinkId;
|
||||
}
|
||||
|
||||
// if no existing alias create new alias
|
||||
existingAlias = existingAlias ?? new EFAlias()
|
||||
{
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = ip,
|
||||
Link = aliasLink,
|
||||
Name = name,
|
||||
};
|
||||
|
||||
if (!hasExistingAlias)
|
||||
{
|
||||
entity.CurrentServer.Logger.WriteDebug($"Connecting player does not have an existing alias {entity}");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
var linkIds = aliases.Select(a => a.LinkId);
|
||||
|
||||
if (linkIds.Count() > 0)
|
||||
// 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)
|
||||
{
|
||||
var highestLevel = await context.Clients
|
||||
.Where(c => linkIds.Contains(c.AliasLinkId))
|
||||
.MaxAsync(c => c.Level);
|
||||
context.Update(entity);
|
||||
|
||||
if (entity.Level != highestLevel)
|
||||
{
|
||||
context.Update(entity);
|
||||
entity.Level = highestLevel;
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
entity.CurrentAlias = existingAlias;
|
||||
entity.CurrentAliasId = existingAlias.AliasId;
|
||||
}
|
||||
}
|
||||
|
||||
if (entity.CurrentAlias != existingAlias ||
|
||||
entity.AliasLink != aliasLink)
|
||||
// theres no exact match, but they've played before with the GUID or IP
|
||||
else if (hasExistingAlias)
|
||||
{
|
||||
entity.CurrentAlias = existingAlias;
|
||||
entity.AliasLink = aliasLink;
|
||||
// the current link is temporary so we need to update
|
||||
if (!entity.AliasLink.Active)
|
||||
{
|
||||
// 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;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// they have an existing link
|
||||
context.Update(entity);
|
||||
entity.CurrentServer.Logger.WriteDebug($"Connecting player is using a new alias {entity}");
|
||||
entity.CurrentAlias = new EFAlias()
|
||||
{
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = ip,
|
||||
Link = aliasLink,
|
||||
LinkId = aliasLink.AliasLinkId,
|
||||
Name = name
|
||||
};
|
||||
entity.AliasLink.Children.Add(entity.CurrentAlias);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// no record of them playing
|
||||
else
|
||||
{
|
||||
context.Update(entity);
|
||||
context.Update(entity.AliasLink);
|
||||
entity.CurrentAlias = new EFAlias()
|
||||
{
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = ip,
|
||||
Link = aliasLink,
|
||||
Name = name
|
||||
};
|
||||
|
||||
entity.AliasLink.Active = true;
|
||||
entity.AliasLink.Children.Add(entity.CurrentAlias);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
var linkIds = aliases.Select(a => a.LinkId);
|
||||
|
||||
if (linkIds.Count() > 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();
|
||||
}
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@ -251,6 +248,7 @@ namespace SharedLibraryCore.Services
|
||||
context.Clients
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(c => c.AliasLink.Children)
|
||||
.Include(c => c.ReceivedPenalties)
|
||||
.FirstOrDefault(c => c.NetworkId == networkId)
|
||||
);
|
||||
|
||||
|
@ -18,38 +18,86 @@ namespace SharedLibraryCore.Services
|
||||
{
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
// create the actual EFPenalty
|
||||
EFPenalty addedEntity = new EFPenalty()
|
||||
{
|
||||
OffenderId = newEntity.Offender.ClientId,
|
||||
PunisherId = newEntity.Punisher.ClientId,
|
||||
LinkId = newEntity.Link.AliasLinkId,
|
||||
Type = newEntity.Type,
|
||||
Expires = newEntity.Expires,
|
||||
Offense = newEntity.Offense,
|
||||
When = newEntity.When,
|
||||
AutomatedOffense = newEntity.AutomatedOffense
|
||||
};
|
||||
context.ChangeTracker.AutoDetectChangesEnabled = true;
|
||||
context.ChangeTracker.LazyLoadingEnabled = true;
|
||||
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
|
||||
|
||||
// make bans propogate to all aliases
|
||||
if (addedEntity.Type == Objects.Penalty.PenaltyType.Ban)
|
||||
if (newEntity.Type == Penalty.PenaltyType.Ban)
|
||||
{
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == addedEntity.LinkId)
|
||||
.ForEachAsync(c => c.Level = Permission.Banned);
|
||||
.Include(c => c.ReceivedPenalties)
|
||||
.Where(c => c.AliasLinkId == newEntity.Link.AliasLinkId)
|
||||
.ForEachAsync(c =>
|
||||
{
|
||||
if (c.Level != Permission.Banned)
|
||||
{
|
||||
c.Level = Permission.Banned;
|
||||
c.ReceivedPenalties.Add(new EFPenalty()
|
||||
{
|
||||
Active = true,
|
||||
OffenderId = c.ClientId,
|
||||
PunisherId = newEntity.Punisher.ClientId,
|
||||
LinkId = c.AliasLinkId,
|
||||
Type = newEntity.Type,
|
||||
Expires = newEntity.Expires,
|
||||
Offense = newEntity.Offense,
|
||||
When = DateTime.UtcNow,
|
||||
AutomatedOffense = newEntity.AutomatedOffense,
|
||||
IsEvadedOffense = newEntity.IsEvadedOffense
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// make flags propogate to all aliases
|
||||
else if (addedEntity.Type == Objects.Penalty.PenaltyType.Flag)
|
||||
else if (newEntity.Type == Penalty.PenaltyType.Flag)
|
||||
{
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == addedEntity.LinkId)
|
||||
.ForEachAsync(c => c.Level = Permission.Flagged);
|
||||
.Include(c => c.ReceivedPenalties)
|
||||
.Where(c => c.AliasLinkId == newEntity.LinkId)
|
||||
.ForEachAsync(c =>
|
||||
{
|
||||
if (c.Level != Permission.Flagged)
|
||||
{
|
||||
c.Level = Permission.Flagged;
|
||||
c.ReceivedPenalties.Add(new EFPenalty()
|
||||
{
|
||||
Active = true,
|
||||
OffenderId = c.ClientId,
|
||||
PunisherId = newEntity.Punisher.ClientId,
|
||||
LinkId = c.AliasLinkId,
|
||||
Type = newEntity.Type,
|
||||
Expires = newEntity.Expires,
|
||||
Offense = newEntity.Offense,
|
||||
When = DateTime.UtcNow,
|
||||
AutomatedOffense = newEntity.AutomatedOffense,
|
||||
IsEvadedOffense = newEntity.IsEvadedOffense
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// we just want to add it to the database
|
||||
else
|
||||
{
|
||||
context.Penalties.Add(new EFPenalty()
|
||||
{
|
||||
Active = true,
|
||||
OffenderId = newEntity.Offender.ClientId,
|
||||
PunisherId = newEntity.Punisher.ClientId,
|
||||
LinkId = newEntity.Link.AliasLinkId,
|
||||
Type = newEntity.Type,
|
||||
Expires = newEntity.Expires,
|
||||
Offense = newEntity.Offense,
|
||||
When = DateTime.UtcNow,
|
||||
AutomatedOffense = newEntity.AutomatedOffense,
|
||||
IsEvadedOffense = newEntity.IsEvadedOffense
|
||||
});
|
||||
}
|
||||
|
||||
context.Penalties.Add(addedEntity);
|
||||
await context.SaveChangesAsync();
|
||||
return addedEntity;
|
||||
return newEntity;
|
||||
}
|
||||
}
|
||||
|
||||
@ -228,7 +276,8 @@ namespace SharedLibraryCore.Services
|
||||
Expression<Func<EFPenalty, bool>> filter = (p) => new Penalty.PenaltyType[]
|
||||
{
|
||||
Penalty.PenaltyType.TempBan,
|
||||
Penalty.PenaltyType.Ban, Penalty.PenaltyType.Flag
|
||||
Penalty.PenaltyType.Ban,
|
||||
Penalty.PenaltyType.Flag
|
||||
}.Contains(p.Type) &&
|
||||
p.Active &&
|
||||
(p.Expires == null || p.Expires > now);
|
||||
@ -240,7 +289,7 @@ namespace SharedLibraryCore.Services
|
||||
.Where(filter);
|
||||
|
||||
var iqIPPenalties = context.Aliases
|
||||
.Where(a => a.IPAddress == ip)
|
||||
.Where(a => a.IPAddress != null & a.IPAddress == ip)
|
||||
.SelectMany(a => a.Link.ReceivedPenalties)
|
||||
.Where(filter);
|
||||
|
||||
@ -249,7 +298,10 @@ namespace SharedLibraryCore.Services
|
||||
var ipPenaltiesSql = iqIPPenalties.ToSql();
|
||||
#endif
|
||||
|
||||
var activePenalties = (await iqLinkPenalties.ToListAsync()).Union(await iqIPPenalties.ToListAsync());
|
||||
var activePenalties = (await iqLinkPenalties.ToListAsync())
|
||||
.Union(await iqIPPenalties.ToListAsync())
|
||||
.Distinct();
|
||||
|
||||
// this is a bit more performant in memory (ordering)
|
||||
return activePenalties.OrderByDescending(p => p.When).ToList();
|
||||
}
|
||||
|
Reference in New Issue
Block a user