2017-11-25 20:29:58 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-04-26 20:19:42 -04:00
|
|
|
|
using SharedLibraryCore.Objects;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Services
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
public class PenaltyService : Interfaces.IEntityService<EFPenalty>
|
|
|
|
|
{
|
2018-04-09 15:17:10 -04:00
|
|
|
|
public async Task<EFPenalty> Create(EFPenalty newEntity)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2018-04-09 15:17:10 -04:00
|
|
|
|
// create the actual EFPenalty
|
|
|
|
|
EFPenalty addedEntity = new EFPenalty()
|
|
|
|
|
{
|
|
|
|
|
OffenderId = newEntity.Offender.ClientId,
|
|
|
|
|
PunisherId = newEntity.Punisher.ClientId,
|
|
|
|
|
LinkId = newEntity.Link.AliasLinkId,
|
|
|
|
|
Type = newEntity.Type,
|
|
|
|
|
Active = true,
|
|
|
|
|
Expires = newEntity.Expires,
|
|
|
|
|
Offense = newEntity.Offense,
|
|
|
|
|
When = newEntity.When,
|
2018-05-30 21:50:20 -04:00
|
|
|
|
AutomatedOffense = newEntity.AutomatedOffense
|
2018-04-09 15:17:10 -04:00
|
|
|
|
};
|
2018-02-17 01:13:38 -05:00
|
|
|
|
|
2018-04-09 15:17:10 -04:00
|
|
|
|
if (addedEntity.Expires == DateTime.MaxValue)
|
|
|
|
|
addedEntity.Expires = DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString());
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
|
|
|
|
// make bans propogate to all aliases
|
2018-04-09 15:17:10 -04:00
|
|
|
|
if (addedEntity.Type == Objects.Penalty.PenaltyType.Ban)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
|
|
|
|
await context.Clients
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.Where(c => c.AliasLinkId == addedEntity.LinkId)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.ForEachAsync(c => c.Level = Objects.Player.Permission.Banned);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make flags propogate to all aliases
|
2018-04-09 15:17:10 -04:00
|
|
|
|
else if (addedEntity.Type == Objects.Penalty.PenaltyType.Flag)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
|
|
|
|
await context.Clients
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.Where(c => c.AliasLinkId == addedEntity.LinkId)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.ForEachAsync(c => c.Level = Objects.Player.Permission.Flagged);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-09 15:17:10 -04:00
|
|
|
|
context.Penalties.Add(addedEntity);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
await context.SaveChangesAsync();
|
2018-04-09 15:17:10 -04:00
|
|
|
|
return addedEntity;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> CreateProxy()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> Delete(EFPenalty entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
|
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
throw await Task.FromResult(new Exception());
|
|
|
|
|
/*
|
2018-02-14 14:01:26 -05:00
|
|
|
|
return await Task.FromResult(new List<EFPenalty>());
|
|
|
|
|
// fixme: this is so slow!
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return await Task.Run(() =>
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2018-02-07 00:19:06 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
return context.Penalties
|
|
|
|
|
.Include(p => p.Offender)
|
|
|
|
|
.Include(p => p.Punisher)
|
|
|
|
|
.Where(expression)
|
|
|
|
|
.Where(p => p.Active)
|
|
|
|
|
.ToList();
|
2018-04-05 00:38:45 -04:00
|
|
|
|
});*/
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> Get(int entityID)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public Task<EFPenalty> GetUnique(long entityProperty)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> Update(EFPenalty entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-04 00:22:10 -04:00
|
|
|
|
public async Task<IList<EFPenalty>> GetRecentPenalties(int count, int offset, Penalty.PenaltyType showOnly = Penalty.PenaltyType.Any)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
return await context.Penalties
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Include(p => p.Offender.CurrentAlias)
|
|
|
|
|
.Include(p => p.Punisher.CurrentAlias)
|
2018-05-04 00:22:10 -04:00
|
|
|
|
.Where(p => showOnly == Penalty.PenaltyType.Any ? p.Type != Penalty.PenaltyType.Any : p.Type == showOnly)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.Where(p => p.Active)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
.OrderByDescending(p => p.When)
|
|
|
|
|
.Skip(offset)
|
|
|
|
|
.Take(count)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<EFPenalty>> GetClientPenaltiesAsync(int clientId)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
return await context.Penalties
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(p => p.OffenderId == clientId)
|
|
|
|
|
.Where(p => p.Active)
|
|
|
|
|
.Include(p => p.Offender.CurrentAlias)
|
|
|
|
|
.Include(p => p.Punisher.CurrentAlias)
|
|
|
|
|
.ToListAsync();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 14:01:26 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get a read-only copy of client penalties
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="clientI"></param>
|
|
|
|
|
/// <param name="victim">Retreive penalties for clients receiving penalties, other wise given</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<List<ProfileMeta>> ReadGetClientPenaltiesAsync(int clientId, bool victim = true)
|
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
{
|
2018-04-28 17:39:45 -04:00
|
|
|
|
context.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
|
|
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
2018-02-14 14:01:26 -05:00
|
|
|
|
if (victim)
|
|
|
|
|
{
|
2018-04-13 02:32:30 -04:00
|
|
|
|
var now = DateTime.UtcNow;
|
2018-02-14 14:01:26 -05:00
|
|
|
|
var iqPenalties = from penalty in context.Penalties.AsNoTracking()
|
|
|
|
|
where penalty.OffenderId == clientId
|
|
|
|
|
join victimClient in context.Clients.AsNoTracking()
|
|
|
|
|
on penalty.OffenderId equals victimClient.ClientId
|
2018-04-26 20:19:42 -04:00
|
|
|
|
join victimAlias in context.Aliases.AsNoTracking()
|
2018-02-14 14:01:26 -05:00
|
|
|
|
on victimClient.CurrentAliasId equals victimAlias.AliasId
|
2018-04-26 20:19:42 -04:00
|
|
|
|
join punisherClient in context.Clients.AsNoTracking()
|
2018-02-14 14:01:26 -05:00
|
|
|
|
on penalty.PunisherId equals punisherClient.ClientId
|
2018-04-26 20:19:42 -04:00
|
|
|
|
join punisherAlias in context.Aliases.AsNoTracking()
|
2018-02-14 14:01:26 -05:00
|
|
|
|
on punisherClient.CurrentAliasId equals punisherAlias.AliasId
|
|
|
|
|
//orderby penalty.When descending
|
|
|
|
|
select new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Event.Penalty",
|
2018-02-21 20:29:23 -05:00
|
|
|
|
Value = new PenaltyInfo
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
2018-06-05 17:31:36 -04:00
|
|
|
|
Id = penalty.PenaltyId,
|
2018-02-14 14:01:26 -05:00
|
|
|
|
OffenderName = victimAlias.Name,
|
|
|
|
|
OffenderId = victimClient.ClientId,
|
|
|
|
|
PunisherName = punisherAlias.Name,
|
|
|
|
|
PunisherId = penalty.PunisherId,
|
|
|
|
|
Offense = penalty.Offense,
|
2018-04-13 02:32:30 -04:00
|
|
|
|
Type = penalty.Type.ToString(),
|
2018-05-30 21:50:20 -04:00
|
|
|
|
TimeRemaining = now > penalty.Expires ? "" : penalty.Expires.ToString(),
|
|
|
|
|
AutomatedOffense = penalty.AutomatedOffense
|
2018-02-14 14:01:26 -05:00
|
|
|
|
},
|
2018-03-09 03:01:12 -05:00
|
|
|
|
When = penalty.When,
|
2018-04-28 17:39:45 -04:00
|
|
|
|
Sensitive = penalty.Type == Penalty.PenaltyType.Flag
|
2018-02-14 14:01:26 -05:00
|
|
|
|
};
|
|
|
|
|
// fixme: is this good and fast?
|
2018-04-13 02:32:30 -04:00
|
|
|
|
var list = await iqPenalties.ToListAsync();
|
|
|
|
|
list.ForEach(p =>
|
|
|
|
|
{
|
2018-04-28 17:39:45 -04:00
|
|
|
|
// todo: why does this have to be done?
|
|
|
|
|
if (((PenaltyInfo)p.Value).Type.Length < 2)
|
|
|
|
|
((PenaltyInfo)p.Value).Type = ((Penalty.PenaltyType)Convert.ToInt32(((PenaltyInfo)p.Value).Type)).ToString();
|
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
var pi = ((PenaltyInfo)p.Value);
|
|
|
|
|
if (pi.TimeRemaining.Length > 0)
|
|
|
|
|
pi.TimeRemaining = (DateTime.Parse(((PenaltyInfo)p.Value).TimeRemaining) - now).TimeSpanText();
|
2018-04-28 17:39:45 -04:00
|
|
|
|
|
|
|
|
|
});
|
2018-04-13 02:32:30 -04:00
|
|
|
|
return list;
|
2018-02-14 14:01:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var iqPenalties = from penalty in context.Penalties.AsNoTracking()
|
|
|
|
|
where penalty.PunisherId == clientId
|
|
|
|
|
join victimClient in context.Clients.AsNoTracking()
|
|
|
|
|
on penalty.OffenderId equals victimClient.ClientId
|
|
|
|
|
join victimAlias in context.Aliases
|
|
|
|
|
on victimClient.CurrentAliasId equals victimAlias.AliasId
|
|
|
|
|
join punisherClient in context.Clients
|
|
|
|
|
on penalty.PunisherId equals punisherClient.ClientId
|
|
|
|
|
join punisherAlias in context.Aliases
|
|
|
|
|
on punisherClient.CurrentAliasId equals punisherAlias.AliasId
|
|
|
|
|
//orderby penalty.When descending
|
|
|
|
|
select new ProfileMeta()
|
|
|
|
|
{
|
|
|
|
|
Key = "Event.Penalty",
|
2018-02-21 20:29:23 -05:00
|
|
|
|
Value = new PenaltyInfo
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
2018-06-05 17:31:36 -04:00
|
|
|
|
Id = penalty.PenaltyId,
|
2018-02-14 14:01:26 -05:00
|
|
|
|
OffenderName = victimAlias.Name,
|
|
|
|
|
OffenderId = victimClient.ClientId,
|
|
|
|
|
PunisherName = punisherAlias.Name,
|
|
|
|
|
PunisherId = penalty.PunisherId,
|
|
|
|
|
Offense = penalty.Offense,
|
2018-05-30 21:50:20 -04:00
|
|
|
|
Type = penalty.Type.ToString(),
|
|
|
|
|
AutomatedOffense = penalty.AutomatedOffense
|
2018-02-14 14:01:26 -05:00
|
|
|
|
},
|
2018-05-14 13:55:10 -04:00
|
|
|
|
When = penalty.When,
|
|
|
|
|
Sensitive = penalty.Type == Penalty.PenaltyType.Flag
|
2018-02-14 14:01:26 -05:00
|
|
|
|
};
|
|
|
|
|
// fixme: is this good and fast?
|
2018-04-26 20:19:42 -04:00
|
|
|
|
var list = await iqPenalties.ToListAsync();
|
|
|
|
|
|
|
|
|
|
list.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
// todo: why does this have to be done?
|
|
|
|
|
if (((PenaltyInfo)p.Value).Type.Length < 2)
|
|
|
|
|
((PenaltyInfo)p.Value).Type = ((Penalty.PenaltyType)Convert.ToInt32(((PenaltyInfo)p.Value).Type)).ToString();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return list;
|
2018-02-14 14:01:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int aliasId, int ip = 0)
|
2018-02-15 23:01:28 -05:00
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
{
|
2018-04-13 02:32:30 -04:00
|
|
|
|
var iqPenalties = await (from link in context.AliasLinks
|
|
|
|
|
where link.AliasLinkId == aliasId
|
|
|
|
|
join penalty in context.Penalties
|
|
|
|
|
on link.AliasLinkId equals penalty.LinkId
|
|
|
|
|
where penalty.Active
|
|
|
|
|
select penalty).ToListAsync();
|
|
|
|
|
if (ip != 0)
|
|
|
|
|
{
|
|
|
|
|
iqPenalties.AddRange(await (from alias in context.Aliases
|
|
|
|
|
where alias.IPAddress == ip
|
|
|
|
|
join penalty in context.Penalties
|
|
|
|
|
on alias.LinkId equals penalty.LinkId
|
|
|
|
|
where penalty.Active
|
|
|
|
|
select penalty).ToListAsync());
|
|
|
|
|
}
|
|
|
|
|
return iqPenalties;
|
2018-02-15 23:01:28 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
public async Task RemoveActivePenalties(int aliasLinkId)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var penalties = await context.Penalties
|
|
|
|
|
.Include(p => p.Link.Children)
|
|
|
|
|
.Where(p => p.LinkId == aliasLinkId)
|
|
|
|
|
.Where(p => p.Expires > now)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
penalties.ForEach(async p =>
|
|
|
|
|
{
|
|
|
|
|
p.Active = false;
|
2017-11-29 19:35:50 -05:00
|
|
|
|
// reset the player levels
|
2018-04-28 01:22:18 -04:00
|
|
|
|
if (p.Type == Penalty.PenaltyType.Ban)
|
2018-04-26 16:26:03 -04:00
|
|
|
|
{
|
|
|
|
|
using (var internalContext = new DatabaseContext())
|
|
|
|
|
{
|
|
|
|
|
await internalContext.Clients
|
|
|
|
|
.Where(c => c.AliasLinkId == p.LinkId)
|
2018-04-28 17:39:45 -04:00
|
|
|
|
.ForEachAsync(c => c.Level = Player.Permission.User);
|
2018-04-28 01:22:18 -04:00
|
|
|
|
await internalContext.SaveChangesAsync();
|
2018-04-26 16:26:03 -04:00
|
|
|
|
}
|
2018-04-28 01:22:18 -04:00
|
|
|
|
|
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
}
|
2017-11-25 20:29:58 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|