* initial commit - species prototype modifications - markings points as its own file - shared humanoid component * adds a tool to convert sprite accessories to markings (in go) * removes a fmt call * converts sprite accessory to markings * adds hair and facial hair to marking categories * multiple changes - humanoid visualizer system - markings modifications for visualizer - modifications to shared humanoid component - lays out a base for humanoid system * hidden layers, ports some properties from appearance component, shrinks DefaultMarkings a little * squishes the initialize event calls into one function adds stuff to set species/skin color externally from a server message - currently laid out as if it a dirty call to a networked component, may be subject to change (server-side has not been implemented yet) * makes the sprite pipeline more obvious * apply all markings, hidden layer set replacement * ensures that markings are cleared when the new set is applied * starts refactoring markingsset (unfinished) * more additions to the markingset api * adds constructor logic to markingset * adds a method to filter out markings in a set based on a given species * fixes enumerators in markingset * adds validator into MarkingSet, fixes ForwardMarkingEnumerator * modifications to the humanoid visual system * ensuredefault in markingset * oop * fixes up data keys, populates OnAppearanceChange in visualizer * changes to humanoid component, markings marking equality is now more strict, humanoidcomponent is now implemented for client as a child of sharedhumanoidcomponent * markings are now applied the visualizer by diffing them * base sprites are now applied to humanoids from humanoidvisualizer * passes along base sprite settings to the marking application so that markings know to follow skin color/alpha or not (see: slimes) * custom base layers on humanoids * merges all data keys into one data class for humanoid visualizers * setappearance in sharedhumanoidsystem, removes custombaselayercolors * humanoidcomponent, system (empty) in server * adds some basic public API functions to HumanoidSystem * add marking, remove marking * changes appearance MarkingsSet to a List<Marking>, adds listener for PlayerSpawnCompleteEvent in HumanoidSystem * ensuredefaultmarkings, oninit for humanoids * markingmanager API changes * removes MarkingsSet * LoadProfile, adjusts randomization in humanoid appearance to account for species * base layer settings in humanoidsystem, eye color from profile * rearranges files to centralize under Humanoid namespace * more reorganization, deletes some stuff gotta break stuff to make other things work, right? goodbye SpriteAccessory... * fixes a good chunk of server-side issues still does not compile, yet * singlemarkingpicker xaml layout * singlemarkingpicker logic * magic mirror window (varying pieces of it, mostly client-oriented) * removes some imports, gives MagicMirror a BUI class (not filled in yet) * populates magic mirror BUI functionality / window callbacks * fixes up some errors in humanoidprofileeditor * changes to SingleMarkingPicker SingleMarkingPicker now accepts a List<Marking>, species, and total possible markings available in that marking category * fixes up hair pickers on humanoid profile editor * fixes the errors in markingpicker * markingsystem is now gone * fixes a bunch of build errors * so that's why i did it like that * namespace issues, adds robustxamlloader to singlemarkingpicker * another robustxamlloader * human, lizard sprites/points * prototype fixes, deletion of old spriteaccessory * component registration, fixes dwarf skin toning no, 'ReptilianToned' does not exist * removes component registration from abstract humanoid component * visualizer data now cloneable * serialize for visualizer key * zero-count edge case * missing semi-colon moment * setspecies in humanoidsystem * ensures that default markings, if empty, will cause ensuredefault to skip over that given category * tryadd instead of add * whoops * diff and apply should properly apply markings now * always ensure default, fixes double load for player spawning * apply skin color now sets the skin color property in humanoidcomponent * removes sprite from a few species prototypes * sprite changes for specific base layers based on humanoid sex * layer ordering fix, and a missing base layer should now disallow markings on that layer * anymarking base layer, adds the right leg/foot for humans * loading a profile will now clear all markings on that humanoid * adds missing layers for humans * separates species.yml into respective species prototype files * ensures that if layer visibility was changed, all markings have to be reapplied * server-side enforcement of hiding hair (and other head-related markings) when equipping things that hide hair * slime fix, clothingsystem now dictates layer visibility server side * sussy * layer settings should now ensure a marking should match the skin tone * whoops * skincolor static class and functions in UI * skin color validation in humanoidcharacterappearance * markingpicker now shows only the markings for the selected category in used * getter for slot in singlemarkingpicker now ensures slot is 0 if markings exists * FilterSpecies no longer attempts to do removal while iterating * expands for SingleMarkingPicker * humanoid base dummy has blank layers now (and snout/tail/headside/headtop) * fixes an issue with visualizer system if the marking count was different but the markings themselves were (somewhat) the same * whoops * adds edge case handlers for count differences in humanoid markings * preview now loads profile instead of directly setting appearance * moves marking set loading to update controls * clones a marking set in markingpicker by using the deep clone constructor * whoops (deep cloning a marking now copies the marking id) * adds replace function for markingset * points should now update after the markings are remove/added * merging base layer sprites into a humanoid should now clear them before merging * sets dirty range start to count only if the dirty range start was never set above 0 * fixes up some issues with singlemarkingpicker * color selector sliders in single marking picker should now expand * hair from hair pickers should now apply in profile loading (client-side) * category in singlemarkingpicker now sets the private category variable * slot selector should now populate * single marking picker buttons now have text, also shows the category name over all user-clickable elements * removes a comment * removing hair slots now sets it to bald, defaults to zero used slots if current hair is bald on hair/facial hair * random skin color, eye color * populate colors now checks if the marking count is greater than zero in singlemarkingpicker * hair/facial hair pickers now just get the first possible hair from the respective species list * different approach to random skin color * oh, that's why it wasn't working * randomize everything now just updates every single control * selecting a new marking in SingleMarkingPicker should attempt to copy over old colors, populate list now uses cache, * markingmanager now uses OnlyWhitelisted to populate by category and species * filterspecies now uses onlyWhitelist to filter markings based on whitelist or not * oops * ui fix for singlemarkingpicker, ensures that cache is not null if it is null when populatelist is called * order of operations for the horizontal expand for add/remove * hair pickers should now update when you add/remove the hair slot * fixes variable naming error in character appearance * loc string fix in singlemarkingpicker * lizards, vox now have onlyWhitelist, vox restriction for hair/facialhairs * having zero possible hairs should no longer cause an exception in randomization * setting species should now update hair pickers * ignore categories for marking picker * and a clear as well for the category button * places that functionality in its own function instead * adds eye base sprite, vox now also have their own custom eye sprites * loading a profile client-side should do FilterSpecies for markings now * client-side load profile does filter species after adding in the hairs now * magic mirror * callbacks now call the callback instead of adding it on construct * whoops * in removemarking too * adds missing synchronize calls * comments out an updateinterface call in magic mirror * magic mirror window title, minimum sizing * fixes minsize, adds warning for players who try to set their hair for species that have no hair * removes spaces in xaml * namespace changes/organization * whoopsie (merge conflicts) * re-enables identity from humanoid component * damagevisuals now uses the enum given to it instead of the layerstate given on that layer tied to the enum * removes commas from json * changes to visuals system so the change is consistent * chest * reptilian * visualizer system now handles body sprite setting/coloration, similar to how characterappearance did it not a big fan of this * adds a check in applybasesprites * adding/removing parts should now make them invisible on a humanoid * body part removal/adding now enumerates over sublayers instead * synchro now runs in bodycomponent startup * parts instead of slots * humanoidcompnent check * switches from rsi to actualrsi * removes all the body stuff (too slow) * cleans up resolves from humanoid visualizer system * merging sprites now checks if the base sprites have been modified or not (through things like species changes, or custom base sprite changes) * not forgetting that one again * merging now returns an actual dirty value * replaces the sequenceequal with a more accurate solution * permanent layers, layer visibility on add/remove part in body * should send all hidden layers over now * isdirty in visualizer system for base layers * isdirty checks count as well * ok, IsDirty should now set the base layers if the merged sprites are different * equals override in HumanoidSpritePrototypes.cs temporary until record prototypes :heck: * makes fields readonly, equates IDs instead * adds forced markings through marking picker * forced in humanoidsystem api, ignorespecies in markingpicker * marking bui * makes that serializable as well * ignore species/forced toggles now work * adds icon to modifier verb, interface and keys to humanoid bases * needs the actual enum value to open, no? * makes the key the actual key * actions now propagate upwards * ignore species when set now repopulates markingpicker * modifiable base layers in the markings window * oops! * layout changes * info box should now appear * adds ignorespecies for marking picker, collapsible for base layer section of appearance modification window * collapsible layout moment * if base layers have changed, all markings are now dirty (and if a base layer is missing, the marking is still 'applied' but it's now just invisible * small change to marking visibility * small changes to modifier UI * markings now match skin on zombification * zombie stuff * makes the line edit in marking modifier window more obvious * disables vox on round start * horizontal expand on the single label in base layer modifiers * humanoid profiles in prototypes * randomhumanoidappearance won't work if the humanoid has a profile already stored * removes unused code * documentation in humanoidsystem server-side * documentation in shared/client * whoops * converts accessory into marking in locale files (also adds marking loc string into single marking picker) * be gone, shared humanoid appearance system from the last upstream merge * species ignore on randomization (defaults to no ignored species) * more upstream merge parts that bypassed any errors before merge * addresses review (also just adds typeserializers in some places) * submodule moment * upstream merge issues
987 lines
34 KiB
C#
987 lines
34 KiB
C#
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Content.Server.Administration.Logs;
|
|
using Content.Shared.Administration.Logs;
|
|
using Content.Shared.Humanoid;
|
|
using Content.Shared.Humanoid.Markings;
|
|
using Content.Shared.Preferences;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Robust.Shared.Enums;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Server.Database
|
|
{
|
|
public abstract class ServerDbBase
|
|
{
|
|
#region Preferences
|
|
public async Task<PlayerPreferences?> GetPlayerPreferencesAsync(NetUserId userId)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var prefs = await db.DbContext
|
|
.Preference
|
|
.Include(p => p.Profiles).ThenInclude(h => h.Jobs)
|
|
.Include(p => p.Profiles).ThenInclude(h => h.Antags)
|
|
.Include(p => p.Profiles).ThenInclude(h => h.Traits)
|
|
.AsSingleQuery()
|
|
.SingleOrDefaultAsync(p => p.UserId == userId.UserId);
|
|
|
|
if (prefs is null) return null;
|
|
|
|
var maxSlot = prefs.Profiles.Max(p => p.Slot) + 1;
|
|
var profiles = new Dictionary<int, ICharacterProfile>(maxSlot);
|
|
foreach (var profile in prefs.Profiles)
|
|
{
|
|
profiles[profile.Slot] = ConvertProfiles(profile);
|
|
}
|
|
|
|
return new PlayerPreferences(profiles, prefs.SelectedCharacterSlot, Color.FromHex(prefs.AdminOOCColor));
|
|
}
|
|
|
|
public async Task SaveSelectedCharacterIndexAsync(NetUserId userId, int index)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
await SetSelectedCharacterSlotAsync(userId, index, db.DbContext);
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task SaveCharacterSlotAsync(NetUserId userId, ICharacterProfile? profile, int slot)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
if (profile is null)
|
|
{
|
|
await DeleteCharacterSlot(db.DbContext, userId, slot);
|
|
await db.DbContext.SaveChangesAsync();
|
|
return;
|
|
}
|
|
|
|
if (profile is not HumanoidCharacterProfile humanoid)
|
|
{
|
|
// TODO: Handle other ICharacterProfile implementations properly
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
var entity = ConvertProfiles(humanoid, slot);
|
|
|
|
var prefs = await db.DbContext
|
|
.Preference
|
|
.Include(p => p.Profiles)
|
|
.SingleAsync(p => p.UserId == userId.UserId);
|
|
|
|
var oldProfile = prefs
|
|
.Profiles
|
|
.SingleOrDefault(h => h.Slot == entity.Slot);
|
|
|
|
if (oldProfile is not null)
|
|
{
|
|
prefs.Profiles.Remove(oldProfile);
|
|
}
|
|
|
|
prefs.Profiles.Add(entity);
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
private static async Task DeleteCharacterSlot(ServerDbContext db, NetUserId userId, int slot)
|
|
{
|
|
var profile = await db.Profile.Include(p => p.Preference)
|
|
.Where(p => p.Preference.UserId == userId.UserId && p.Slot == slot)
|
|
.SingleOrDefaultAsync();
|
|
|
|
if (profile == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
db.Profile.Remove(profile);
|
|
}
|
|
|
|
public async Task<PlayerPreferences> InitPrefsAsync(NetUserId userId, ICharacterProfile defaultProfile)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var profile = ConvertProfiles((HumanoidCharacterProfile) defaultProfile, 0);
|
|
var prefs = new Preference
|
|
{
|
|
UserId = userId.UserId,
|
|
SelectedCharacterSlot = 0,
|
|
AdminOOCColor = Color.Red.ToHex()
|
|
};
|
|
|
|
prefs.Profiles.Add(profile);
|
|
|
|
db.DbContext.Preference.Add(prefs);
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
return new PlayerPreferences(new[] {new KeyValuePair<int, ICharacterProfile>(0, defaultProfile)}, 0, Color.FromHex(prefs.AdminOOCColor));
|
|
}
|
|
|
|
public async Task DeleteSlotAndSetSelectedIndex(NetUserId userId, int deleteSlot, int newSlot)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
await DeleteCharacterSlot(db.DbContext, userId, deleteSlot);
|
|
await SetSelectedCharacterSlotAsync(userId, newSlot, db.DbContext);
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task SaveAdminOOCColorAsync(NetUserId userId, Color color)
|
|
{
|
|
await using var db = await GetDb();
|
|
var prefs = await db.DbContext
|
|
.Preference
|
|
.Include(p => p.Profiles)
|
|
.SingleAsync(p => p.UserId == userId.UserId);
|
|
prefs.AdminOOCColor = color.ToHex();
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
private static async Task SetSelectedCharacterSlotAsync(NetUserId userId, int newSlot, ServerDbContext db)
|
|
{
|
|
var prefs = await db.Preference.SingleAsync(p => p.UserId == userId.UserId);
|
|
prefs.SelectedCharacterSlot = newSlot;
|
|
}
|
|
|
|
private static HumanoidCharacterProfile ConvertProfiles(Profile profile)
|
|
{
|
|
var jobs = profile.Jobs.ToDictionary(j => j.JobName, j => (JobPriority) j.Priority);
|
|
var antags = profile.Antags.Select(a => a.AntagName);
|
|
var traits = profile.Traits.Select(t => t.TraitName);
|
|
|
|
var sex = Sex.Male;
|
|
if (Enum.TryParse<Sex>(profile.Sex, true, out var sexVal))
|
|
sex = sexVal;
|
|
|
|
var clothing = ClothingPreference.Jumpsuit;
|
|
if (Enum.TryParse<ClothingPreference>(profile.Clothing, true, out var clothingVal))
|
|
clothing = clothingVal;
|
|
|
|
var backpack = BackpackPreference.Backpack;
|
|
if (Enum.TryParse<BackpackPreference>(profile.Backpack, true, out var backpackVal))
|
|
backpack = backpackVal;
|
|
|
|
var gender = sex == Sex.Male ? Gender.Male : Gender.Female;
|
|
if (Enum.TryParse<Gender>(profile.Gender, true, out var genderVal))
|
|
gender = genderVal;
|
|
|
|
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
|
var markingsRaw = profile.Markings?.Deserialize<List<string>>();
|
|
|
|
List<Marking> markings = new();
|
|
if (markingsRaw != null)
|
|
{
|
|
foreach (var marking in markingsRaw)
|
|
{
|
|
var parsed = Marking.ParseFromDbString(marking);
|
|
|
|
if (parsed is null) continue;
|
|
|
|
markings.Add(parsed);
|
|
}
|
|
}
|
|
|
|
return new HumanoidCharacterProfile(
|
|
profile.CharacterName,
|
|
profile.FlavorText,
|
|
profile.Species,
|
|
profile.Age,
|
|
sex,
|
|
gender,
|
|
new HumanoidCharacterAppearance
|
|
(
|
|
profile.HairName,
|
|
Color.FromHex(profile.HairColor),
|
|
profile.FacialHairName,
|
|
Color.FromHex(profile.FacialHairColor),
|
|
Color.FromHex(profile.EyeColor),
|
|
Color.FromHex(profile.SkinColor),
|
|
markings
|
|
),
|
|
clothing,
|
|
backpack,
|
|
jobs,
|
|
(PreferenceUnavailableMode) profile.PreferenceUnavailable,
|
|
antags.ToList(),
|
|
traits.ToList()
|
|
);
|
|
}
|
|
|
|
private static Profile ConvertProfiles(HumanoidCharacterProfile humanoid, int slot)
|
|
{
|
|
var appearance = (HumanoidCharacterAppearance) humanoid.CharacterAppearance;
|
|
List<string> markingStrings = new();
|
|
foreach (var marking in appearance.Markings)
|
|
{
|
|
markingStrings.Add(marking.ToString());
|
|
}
|
|
var markings = JsonSerializer.SerializeToDocument(markingStrings);
|
|
|
|
var entity = new Profile
|
|
{
|
|
CharacterName = humanoid.Name,
|
|
FlavorText = humanoid.FlavorText,
|
|
Species = humanoid.Species,
|
|
Age = humanoid.Age,
|
|
Sex = humanoid.Sex.ToString(),
|
|
Gender = humanoid.Gender.ToString(),
|
|
HairName = appearance.HairStyleId,
|
|
HairColor = appearance.HairColor.ToHex(),
|
|
FacialHairName = appearance.FacialHairStyleId,
|
|
FacialHairColor = appearance.FacialHairColor.ToHex(),
|
|
EyeColor = appearance.EyeColor.ToHex(),
|
|
SkinColor = appearance.SkinColor.ToHex(),
|
|
Clothing = humanoid.Clothing.ToString(),
|
|
Backpack = humanoid.Backpack.ToString(),
|
|
Markings = markings,
|
|
Slot = slot,
|
|
PreferenceUnavailable = (DbPreferenceUnavailableMode) humanoid.PreferenceUnavailable
|
|
};
|
|
entity.Jobs.AddRange(
|
|
humanoid.JobPriorities
|
|
.Where(j => j.Value != JobPriority.Never)
|
|
.Select(j => new Job {JobName = j.Key, Priority = (DbJobPriority) j.Value})
|
|
);
|
|
entity.Antags.AddRange(
|
|
humanoid.AntagPreferences
|
|
.Select(a => new Antag {AntagName = a})
|
|
);
|
|
entity.Traits.AddRange(
|
|
humanoid.TraitPreferences
|
|
.Select(t => new Trait {TraitName = t})
|
|
);
|
|
|
|
return entity;
|
|
}
|
|
#endregion
|
|
|
|
#region User Ids
|
|
public async Task<NetUserId?> GetAssignedUserIdAsync(string name)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var assigned = await db.DbContext.AssignedUserId.SingleOrDefaultAsync(p => p.UserName == name);
|
|
return assigned?.UserId is { } g ? new NetUserId(g) : default(NetUserId?);
|
|
}
|
|
|
|
public async Task AssignUserIdAsync(string name, NetUserId netUserId)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
db.DbContext.AssignedUserId.Add(new AssignedUserId
|
|
{
|
|
UserId = netUserId.UserId,
|
|
UserName = name
|
|
});
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
#endregion
|
|
|
|
#region Bans
|
|
/*
|
|
* BAN STUFF
|
|
*/
|
|
/// <summary>
|
|
/// Looks up a ban by id.
|
|
/// This will return a pardoned ban as well.
|
|
/// </summary>
|
|
/// <param name="id">The ban id to look for.</param>
|
|
/// <returns>The ban with the given id or null if none exist.</returns>
|
|
public abstract Task<ServerBanDef?> GetServerBanAsync(int id);
|
|
|
|
/// <summary>
|
|
/// Looks up an user's most recent received un-pardoned ban.
|
|
/// This will NOT return a pardoned ban.
|
|
/// One of <see cref="address"/> or <see cref="userId"/> need to not be null.
|
|
/// </summary>
|
|
/// <param name="address">The ip address of the user.</param>
|
|
/// <param name="userId">The id of the user.</param>
|
|
/// <param name="hwId">The HWId of the user.</param>
|
|
/// <returns>The user's latest received un-pardoned ban, or null if none exist.</returns>
|
|
public abstract Task<ServerBanDef?> GetServerBanAsync(
|
|
IPAddress? address,
|
|
NetUserId? userId,
|
|
ImmutableArray<byte>? hwId);
|
|
|
|
/// <summary>
|
|
/// Looks up an user's ban history.
|
|
/// This will return pardoned bans as well.
|
|
/// One of <see cref="address"/> or <see cref="userId"/> need to not be null.
|
|
/// </summary>
|
|
/// <param name="address">The ip address of the user.</param>
|
|
/// <param name="userId">The id of the user.</param>
|
|
/// <param name="hwId">The HWId of the user.</param>
|
|
/// <param name="includeUnbanned">Include pardoned and expired bans.</param>
|
|
/// <returns>The user's ban history.</returns>
|
|
public abstract Task<List<ServerBanDef>> GetServerBansAsync(
|
|
IPAddress? address,
|
|
NetUserId? userId,
|
|
ImmutableArray<byte>? hwId,
|
|
bool includeUnbanned);
|
|
|
|
public abstract Task AddServerBanAsync(ServerBanDef serverBan);
|
|
public abstract Task AddServerUnbanAsync(ServerUnbanDef serverUnban);
|
|
#endregion
|
|
|
|
#region Role Bans
|
|
/*
|
|
* ROLE BANS
|
|
*/
|
|
/// <summary>
|
|
/// Looks up a role ban by id.
|
|
/// This will return a pardoned role ban as well.
|
|
/// </summary>
|
|
/// <param name="id">The role ban id to look for.</param>
|
|
/// <returns>The role ban with the given id or null if none exist.</returns>
|
|
public abstract Task<ServerRoleBanDef?> GetServerRoleBanAsync(int id);
|
|
|
|
/// <summary>
|
|
/// Looks up an user's role ban history.
|
|
/// This will return pardoned role bans based on the <see cref="includeUnbanned"/> bool.
|
|
/// Requires one of <see cref="address"/>, <see cref="userId"/>, or <see cref="hwId"/> to not be null.
|
|
/// </summary>
|
|
/// <param name="address">The IP address of the user.</param>
|
|
/// <param name="userId">The NetUserId of the user.</param>
|
|
/// <param name="hwId">The Hardware Id of the user.</param>
|
|
/// <param name="includeUnbanned">Whether expired and pardoned bans are included.</param>
|
|
/// <returns>The user's role ban history.</returns>
|
|
public abstract Task<List<ServerRoleBanDef>> GetServerRoleBansAsync(IPAddress? address,
|
|
NetUserId? userId,
|
|
ImmutableArray<byte>? hwId,
|
|
bool includeUnbanned);
|
|
|
|
public abstract Task AddServerRoleBanAsync(ServerRoleBanDef serverRoleBan);
|
|
public abstract Task AddServerRoleUnbanAsync(ServerRoleUnbanDef serverRoleUnban);
|
|
#endregion
|
|
|
|
#region Playtime
|
|
public async Task<List<PlayTime>> GetPlayTimes(Guid player)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
return await db.DbContext.PlayTime
|
|
.Where(p => p.PlayerId == player)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task UpdatePlayTimes(IReadOnlyCollection<PlayTimeUpdate> updates)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
// Ideally I would just be able to send a bunch of UPSERT commands, but EFCore is a pile of garbage.
|
|
// So... In the interest of not making this take forever at high update counts...
|
|
// Bulk-load play time objects for all players involved.
|
|
// This allows us to semi-efficiently load all entities we need in a single DB query.
|
|
// Then we can update & insert without further round-trips to the DB.
|
|
|
|
var players = updates.Select(u => u.User.UserId).Distinct().ToArray();
|
|
var dbTimes = (await db.DbContext.PlayTime
|
|
.Where(p => players.Contains(p.PlayerId))
|
|
.ToArrayAsync())
|
|
.GroupBy(p => p.PlayerId)
|
|
.ToDictionary(g => g.Key, g => g.ToDictionary(p => p.Tracker, p => p));
|
|
|
|
foreach (var (user, tracker, time) in updates)
|
|
{
|
|
if (dbTimes.TryGetValue(user.UserId, out var userTimes)
|
|
&& userTimes.TryGetValue(tracker, out var ent))
|
|
{
|
|
// Already have a tracker in the database, update it.
|
|
ent.TimeSpent = time;
|
|
continue;
|
|
}
|
|
|
|
// No tracker, make a new one.
|
|
var playTime = new PlayTime
|
|
{
|
|
Tracker = tracker,
|
|
PlayerId = user.UserId,
|
|
TimeSpent = time
|
|
};
|
|
|
|
db.DbContext.PlayTime.Add(playTime);
|
|
}
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Player Records
|
|
/*
|
|
* PLAYER RECORDS
|
|
*/
|
|
public async Task UpdatePlayerRecord(
|
|
NetUserId userId,
|
|
string userName,
|
|
IPAddress address,
|
|
ImmutableArray<byte> hwId)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var record = await db.DbContext.Player.SingleOrDefaultAsync(p => p.UserId == userId.UserId);
|
|
if (record == null)
|
|
{
|
|
db.DbContext.Player.Add(record = new Player
|
|
{
|
|
FirstSeenTime = DateTime.UtcNow,
|
|
UserId = userId.UserId,
|
|
});
|
|
}
|
|
|
|
record.LastSeenTime = DateTime.UtcNow;
|
|
record.LastSeenAddress = address;
|
|
record.LastSeenUserName = userName;
|
|
record.LastSeenHWId = hwId.ToArray();
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<PlayerRecord?> GetPlayerRecordByUserName(string userName, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
// Sort by descending last seen time.
|
|
// So if, due to account renames, we have two people with the same username in the DB,
|
|
// the most recent one is picked.
|
|
var record = await db.DbContext.Player
|
|
.OrderByDescending(p => p.LastSeenTime)
|
|
.FirstOrDefaultAsync(p => p.LastSeenUserName == userName, cancel);
|
|
|
|
return record == null ? null : MakePlayerRecord(record);
|
|
}
|
|
|
|
public async Task<PlayerRecord?> GetPlayerRecordByUserId(NetUserId userId, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var record = await db.DbContext.Player
|
|
.SingleOrDefaultAsync(p => p.UserId == userId.UserId, cancel);
|
|
|
|
return record == null ? null : MakePlayerRecord(record);
|
|
}
|
|
|
|
protected abstract PlayerRecord MakePlayerRecord(Player player);
|
|
#endregion
|
|
|
|
#region Connection Logs
|
|
/*
|
|
* CONNECTION LOG
|
|
*/
|
|
public abstract Task<int> AddConnectionLogAsync(
|
|
NetUserId userId,
|
|
string userName,
|
|
IPAddress address,
|
|
ImmutableArray<byte> hwId,
|
|
ConnectionDenyReason? denied);
|
|
|
|
public async Task AddServerBanHitsAsync(int connection, IEnumerable<ServerBanDef> bans)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
foreach (var ban in bans)
|
|
{
|
|
db.DbContext.ServerBanHit.Add(new ServerBanHit
|
|
{
|
|
ConnectionId = connection, BanId = ban.Id!.Value
|
|
});
|
|
}
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Admin Ranks
|
|
/*
|
|
* ADMIN RANKS
|
|
*/
|
|
public async Task<Admin?> GetAdminDataForAsync(NetUserId userId, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
return await db.DbContext.Admin
|
|
.Include(p => p.Flags)
|
|
.Include(p => p.AdminRank)
|
|
.ThenInclude(p => p!.Flags)
|
|
.AsSplitQuery() // tests fail because of a random warning if you dont have this!
|
|
.SingleOrDefaultAsync(p => p.UserId == userId.UserId, cancel);
|
|
}
|
|
|
|
public abstract Task<((Admin, string? lastUserName)[] admins, AdminRank[])>
|
|
GetAllAdminAndRanksAsync(CancellationToken cancel);
|
|
|
|
public async Task<AdminRank?> GetAdminRankDataForAsync(int id, CancellationToken cancel = default)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
return await db.DbContext.AdminRank
|
|
.Include(r => r.Flags)
|
|
.SingleOrDefaultAsync(r => r.Id == id, cancel);
|
|
}
|
|
|
|
public async Task RemoveAdminAsync(NetUserId userId, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var admin = await db.DbContext.Admin.SingleAsync(a => a.UserId == userId.UserId, cancel);
|
|
db.DbContext.Admin.Remove(admin);
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
public async Task AddAdminAsync(Admin admin, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
db.DbContext.Admin.Add(admin);
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
public async Task UpdateAdminAsync(Admin admin, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var existing = await db.DbContext.Admin.Include(a => a.Flags).SingleAsync(a => a.UserId == admin.UserId, cancel);
|
|
existing.Flags = admin.Flags;
|
|
existing.Title = admin.Title;
|
|
existing.AdminRankId = admin.AdminRankId;
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
public async Task RemoveAdminRankAsync(int rankId, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var admin = await db.DbContext.AdminRank.SingleAsync(a => a.Id == rankId, cancel);
|
|
db.DbContext.AdminRank.Remove(admin);
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
public async Task AddAdminRankAsync(AdminRank rank, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
db.DbContext.AdminRank.Add(rank);
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
public virtual async Task<int> AddNewRound(Server server, params Guid[] playerIds)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var players = await db.DbContext.Player
|
|
.Where(player => playerIds.Contains(player.UserId))
|
|
.ToListAsync();
|
|
|
|
var round = new Round
|
|
{
|
|
Players = players,
|
|
ServerId = server.Id
|
|
};
|
|
|
|
db.DbContext.Round.Add(round);
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
return round.Id;
|
|
}
|
|
|
|
public async Task<Round> GetRound(int id)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var round = await db.DbContext.Round
|
|
.Include(round => round.Players)
|
|
.SingleAsync(round => round.Id == id);
|
|
|
|
return round;
|
|
}
|
|
|
|
public async Task AddRoundPlayers(int id, Guid[] playerIds)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var round = await db.DbContext.Round
|
|
.Include(round => round.Players)
|
|
.SingleAsync(round => round.Id == id);
|
|
|
|
var players = await db.DbContext.Player
|
|
.Where(player => playerIds.Contains(player.UserId))
|
|
.ToListAsync();
|
|
|
|
var playerSet = new HashSet<Guid>(round.Players.Select(player => player.UserId));
|
|
foreach (var player in players)
|
|
{
|
|
if (playerSet.Contains(player.UserId))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
round.Players.Add(player);
|
|
}
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var existing = await db.DbContext.AdminRank
|
|
.Include(r => r.Flags)
|
|
.SingleAsync(a => a.Id == rank.Id, cancel);
|
|
|
|
existing.Flags = rank.Flags;
|
|
existing.Name = rank.Name;
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
|
}
|
|
#endregion
|
|
|
|
#region Admin Logs
|
|
|
|
public async Task<(Server, bool existed)> AddOrGetServer(string serverName)
|
|
{
|
|
await using var db = await GetDb();
|
|
var server = await db.DbContext.Server
|
|
.Where(server => server.Name.Equals(serverName))
|
|
.SingleOrDefaultAsync();
|
|
|
|
if (server != default)
|
|
return (server, true);
|
|
|
|
server = new Server
|
|
{
|
|
Name = serverName
|
|
};
|
|
|
|
db.DbContext.Server.Add(server);
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
return (server, false);
|
|
}
|
|
|
|
public virtual async Task AddAdminLogs(List<QueuedLog> logs)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var entities = new Dictionary<int, AdminLogEntity>();
|
|
|
|
foreach (var (log, entityData) in logs)
|
|
{
|
|
var logEntities = new List<AdminLogEntity>(entityData.Count);
|
|
foreach (var (id, name) in entityData)
|
|
{
|
|
var entity = entities.GetOrNew(id);
|
|
entity.Name = name;
|
|
logEntities.Add(entity);
|
|
}
|
|
|
|
log.Entities = logEntities;
|
|
db.DbContext.AdminLog.Add(log);
|
|
}
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
private async Task<IQueryable<AdminLog>> GetAdminLogsQuery(ServerDbContext db, LogFilter? filter = null)
|
|
{
|
|
IQueryable<AdminLog> query = db.AdminLog;
|
|
|
|
if (filter == null)
|
|
{
|
|
return query.OrderBy(log => log.Date);
|
|
}
|
|
|
|
if (filter.Round != null)
|
|
{
|
|
query = query.Where(log => log.RoundId == filter.Round);
|
|
}
|
|
|
|
if (filter.Search != null)
|
|
{
|
|
query = query.Where(log => log.Message.Contains(filter.Search));
|
|
}
|
|
|
|
if (filter.Types != null)
|
|
{
|
|
query = query.Where(log => filter.Types.Contains(log.Type));
|
|
}
|
|
|
|
if (filter.Impacts != null)
|
|
{
|
|
query = query.Where(log => filter.Impacts.Contains(log.Impact));
|
|
}
|
|
|
|
if (filter.Before != null)
|
|
{
|
|
query = query.Where(log => log.Date < filter.Before);
|
|
}
|
|
|
|
if (filter.After != null)
|
|
{
|
|
query = query.Where(log => log.Date > filter.After);
|
|
}
|
|
|
|
if (filter.AnyPlayers != null)
|
|
{
|
|
var players = await db.AdminLogPlayer
|
|
.Where(player => filter.AnyPlayers.Contains(player.PlayerUserId))
|
|
.ToListAsync();
|
|
|
|
if (players.Count > 0)
|
|
{
|
|
query = from log in query
|
|
join player in db.AdminLogPlayer on log.Id equals player.LogId
|
|
where filter.AnyPlayers.Contains(player.Player.UserId)
|
|
select log;
|
|
}
|
|
}
|
|
|
|
if (filter.AllPlayers != null)
|
|
{
|
|
// TODO ADMIN LOGGING
|
|
}
|
|
|
|
query = query.Distinct();
|
|
|
|
if (filter.LastLogId != null)
|
|
{
|
|
query = filter.DateOrder switch
|
|
{
|
|
DateOrder.Ascending => query.Where(log => log.Id < filter.LastLogId),
|
|
DateOrder.Descending => query.Where(log => log.Id > filter.LastLogId),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(filter),
|
|
$"Unknown {nameof(DateOrder)} value {filter.DateOrder}")
|
|
};
|
|
}
|
|
|
|
query = filter.DateOrder switch
|
|
{
|
|
DateOrder.Ascending => query.OrderBy(log => log.Date),
|
|
DateOrder.Descending => query.OrderByDescending(log => log.Date),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(filter),
|
|
$"Unknown {nameof(DateOrder)} value {filter.DateOrder}")
|
|
};
|
|
|
|
if (filter.Limit != null)
|
|
{
|
|
query = query.Take(filter.Limit.Value);
|
|
}
|
|
|
|
return query;
|
|
}
|
|
|
|
public async IAsyncEnumerable<string> GetAdminLogMessages(LogFilter? filter = null)
|
|
{
|
|
await using var db = await GetDb();
|
|
var query = await GetAdminLogsQuery(db.DbContext, filter);
|
|
|
|
await foreach (var log in query.Select(log => log.Message).AsAsyncEnumerable())
|
|
{
|
|
yield return log;
|
|
}
|
|
}
|
|
|
|
public async IAsyncEnumerable<SharedAdminLog> GetAdminLogs(LogFilter? filter = null)
|
|
{
|
|
await using var db = await GetDb();
|
|
var query = await GetAdminLogsQuery(db.DbContext, filter);
|
|
query = query.Include(log => log.Players);
|
|
|
|
await foreach (var log in query.AsAsyncEnumerable())
|
|
{
|
|
var players = new Guid[log.Players.Count];
|
|
for (var i = 0; i < log.Players.Count; i++)
|
|
{
|
|
players[i] = log.Players[i].PlayerUserId;
|
|
}
|
|
|
|
yield return new SharedAdminLog(log.Id, log.Type, log.Impact, log.Date, log.Message, players);
|
|
}
|
|
}
|
|
|
|
public async IAsyncEnumerable<JsonDocument> GetAdminLogsJson(LogFilter? filter = null)
|
|
{
|
|
await using var db = await GetDb();
|
|
var query = await GetAdminLogsQuery(db.DbContext, filter);
|
|
|
|
await foreach (var json in query.Select(log => log.Json).AsAsyncEnumerable())
|
|
{
|
|
yield return json;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Whitelist
|
|
|
|
public async Task<bool> GetWhitelistStatusAsync(NetUserId player)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
return await db.DbContext.Whitelist.AnyAsync(w => w.UserId == player);
|
|
}
|
|
|
|
public async Task AddToWhitelistAsync(NetUserId player)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
db.DbContext.Whitelist.Add(new Whitelist { UserId = player });
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task RemoveFromWhitelistAsync(NetUserId player)
|
|
{
|
|
await using var db = await GetDb();
|
|
var entry = await db.DbContext.Whitelist.SingleAsync(w => w.UserId == player);
|
|
db.DbContext.Whitelist.Remove(entry);
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<DateTime?> GetLastReadRules(NetUserId player)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
return await db.DbContext.Player
|
|
.Where(dbPlayer => dbPlayer.UserId == player)
|
|
.Select(dbPlayer => dbPlayer.LastReadRules)
|
|
.SingleOrDefaultAsync();
|
|
}
|
|
|
|
public async Task SetLastReadRules(NetUserId player, DateTime date)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var dbPlayer = await db.DbContext.Player.Where(dbPlayer => dbPlayer.UserId == player).SingleOrDefaultAsync();
|
|
if (dbPlayer == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
dbPlayer.LastReadRules = date;
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Uploaded Resources Logs
|
|
|
|
public async Task AddUploadedResourceLogAsync(NetUserId user, DateTime date, string path, byte[] data)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
db.DbContext.UploadedResourceLog.Add(new UploadedResourceLog() { UserId = user, Date = date, Path = path, Data = data });
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task PurgeUploadedResourceLogAsync(int days)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var date = DateTime.Now.Subtract(TimeSpan.FromDays(days));
|
|
|
|
await foreach (var log in db.DbContext.UploadedResourceLog
|
|
.Where(l => date > l.Date)
|
|
.AsAsyncEnumerable())
|
|
{
|
|
db.DbContext.UploadedResourceLog.Remove(log);
|
|
}
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Admin Notes
|
|
|
|
public virtual async Task<int> AddAdminNote(AdminNote note)
|
|
{
|
|
await using var db = await GetDb();
|
|
db.DbContext.AdminNotes.Add(note);
|
|
await db.DbContext.SaveChangesAsync();
|
|
return note.Id;
|
|
}
|
|
|
|
public async Task<AdminNote?> GetAdminNote(int id)
|
|
{
|
|
await using var db = await GetDb();
|
|
return await db.DbContext.AdminNotes
|
|
.Where(note => note.Id == id)
|
|
.Include(note => note.Round)
|
|
.Include(note => note.CreatedBy)
|
|
.Include(note => note.LastEditedBy)
|
|
.Include(note => note.DeletedBy)
|
|
.Include(note => note.Player)
|
|
.SingleOrDefaultAsync();
|
|
}
|
|
|
|
public async Task<List<AdminNote>> GetAdminNotes(Guid player)
|
|
{
|
|
await using var db = await GetDb();
|
|
return await db.DbContext.AdminNotes
|
|
.Where(note => note.PlayerUserId == player)
|
|
.Where(note => !note.Deleted)
|
|
.Include(note => note.Round)
|
|
.Include(note => note.CreatedBy)
|
|
.Include(note => note.LastEditedBy)
|
|
.Include(note => note.Player)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task DeleteAdminNote(int id, Guid deletedBy, DateTime deletedAt)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var note = await db.DbContext.AdminNotes.Where(note => note.Id == id).SingleAsync();
|
|
|
|
note.Deleted = true;
|
|
note.DeletedById = deletedBy;
|
|
note.DeletedAt = deletedAt;
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task EditAdminNote(int id, string message, Guid editedBy, DateTime editedAt)
|
|
{
|
|
await using var db = await GetDb();
|
|
|
|
var note = await db.DbContext.AdminNotes.Where(note => note.Id == id).SingleAsync();
|
|
note.Message = message;
|
|
note.LastEditedById = editedBy;
|
|
note.LastEditedAt = editedAt;
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
protected abstract Task<DbGuard> GetDb();
|
|
|
|
protected abstract class DbGuard : IAsyncDisposable
|
|
{
|
|
public abstract ServerDbContext DbContext { get; }
|
|
|
|
public abstract ValueTask DisposeAsync();
|
|
}
|
|
|
|
}
|
|
}
|