2021-03-22 01:30:50 +01:00
|
|
|
using System.Collections.Immutable;
|
2020-09-29 14:26:00 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2021-11-22 19:08:27 +01:00
|
|
|
using System.Text.Json;
|
2020-11-10 16:50:28 +01:00
|
|
|
using System.Threading;
|
2020-09-29 14:26:00 +02:00
|
|
|
using System.Threading.Tasks;
|
2021-11-22 19:08:27 +01:00
|
|
|
using Content.Server.Administration.Logs;
|
|
|
|
|
using Content.Shared.Administration.Logs;
|
Humanoid appearance refactor (#10882)
* 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
2022-09-22 15:19:00 -07:00
|
|
|
using Content.Shared.Humanoid;
|
|
|
|
|
using Content.Shared.Humanoid.Markings;
|
2020-09-29 14:26:00 +02:00
|
|
|
using Content.Shared.Preferences;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-02-22 00:07:46 +00:00
|
|
|
using Robust.Shared.Enums;
|
2020-09-29 14:26:00 +02:00
|
|
|
using Robust.Shared.Network;
|
2021-11-22 19:08:27 +01:00
|
|
|
using Robust.Shared.Utility;
|
2020-09-29 14:26:00 +02:00
|
|
|
|
|
|
|
|
namespace Content.Server.Database
|
|
|
|
|
{
|
|
|
|
|
public abstract class ServerDbBase
|
|
|
|
|
{
|
2021-02-14 11:59:56 -03:00
|
|
|
#region Preferences
|
2020-09-29 14:26:00 +02:00
|
|
|
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)
|
2022-09-10 17:40:06 +02:00
|
|
|
.Include(p => p.Profiles).ThenInclude(h => h.Traits)
|
2021-03-31 12:41:23 -07:00
|
|
|
.AsSingleQuery()
|
2020-09-29 14:26:00 +02:00
|
|
|
.SingleOrDefaultAsync(p => p.UserId == userId.UserId);
|
|
|
|
|
|
|
|
|
|
if (prefs is null) return null;
|
|
|
|
|
|
2020-10-06 15:13:16 +02:00
|
|
|
var maxSlot = prefs.Profiles.Max(p => p.Slot) + 1;
|
|
|
|
|
var profiles = new Dictionary<int, ICharacterProfile>(maxSlot);
|
2020-09-29 14:26:00 +02:00
|
|
|
foreach (var profile in prefs.Profiles)
|
|
|
|
|
{
|
|
|
|
|
profiles[profile.Slot] = ConvertProfiles(profile);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 11:59:56 -03:00
|
|
|
return new PlayerPreferences(profiles, prefs.SelectedCharacterSlot, Color.FromHex(prefs.AdminOOCColor));
|
2020-09-29 14:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SaveSelectedCharacterIndexAsync(NetUserId userId, int index)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
2020-10-07 18:02:10 +02:00
|
|
|
await SetSelectedCharacterSlotAsync(userId, index, db.DbContext);
|
2020-09-29 14:26:00 +02:00
|
|
|
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SaveCharacterSlotAsync(NetUserId userId, ICharacterProfile? profile, int slot)
|
|
|
|
|
{
|
2020-10-07 18:02:10 +02:00
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
2020-09-29 14:26:00 +02:00
|
|
|
if (profile is null)
|
|
|
|
|
{
|
2021-10-13 21:11:20 +02:00
|
|
|
await DeleteCharacterSlot(db.DbContext, userId, slot);
|
2020-10-07 18:02:10 +02:00
|
|
|
await db.DbContext.SaveChangesAsync();
|
2020-09-29 14:26:00 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 14:33:31 +01:00
|
|
|
if (profile is not HumanoidCharacterProfile humanoid)
|
2020-09-29 14:26:00 +02:00
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
|
2020-11-26 14:33:31 +01:00
|
|
|
if (oldProfile is not null)
|
2020-09-29 14:26:00 +02:00
|
|
|
{
|
|
|
|
|
prefs.Profiles.Remove(oldProfile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prefs.Profiles.Add(entity);
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 21:11:20 +02:00
|
|
|
private static async Task DeleteCharacterSlot(ServerDbContext db, NetUserId userId, int slot)
|
2020-09-29 14:26:00 +02:00
|
|
|
{
|
2021-10-13 21:11:20 +02:00
|
|
|
var profile = await db.Profile.Include(p => p.Preference)
|
|
|
|
|
.Where(p => p.Preference.UserId == userId.UserId && p.Slot == slot)
|
|
|
|
|
.SingleOrDefaultAsync();
|
|
|
|
|
|
2021-11-09 15:05:07 +01:00
|
|
|
if (profile == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 21:11:20 +02:00
|
|
|
db.Profile.Remove(profile);
|
2020-09-29 14:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2021-02-14 11:59:56 -03:00
|
|
|
SelectedCharacterSlot = 0,
|
|
|
|
|
AdminOOCColor = Color.Red.ToHex()
|
2020-09-29 14:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
prefs.Profiles.Add(profile);
|
|
|
|
|
|
|
|
|
|
db.DbContext.Preference.Add(prefs);
|
|
|
|
|
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
|
2021-02-14 11:59:56 -03:00
|
|
|
return new PlayerPreferences(new[] {new KeyValuePair<int, ICharacterProfile>(0, defaultProfile)}, 0, Color.FromHex(prefs.AdminOOCColor));
|
2020-09-29 14:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 18:02:10 +02:00
|
|
|
public async Task DeleteSlotAndSetSelectedIndex(NetUserId userId, int deleteSlot, int newSlot)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
2021-10-13 21:11:20 +02:00
|
|
|
await DeleteCharacterSlot(db.DbContext, userId, deleteSlot);
|
2020-10-07 18:02:10 +02:00
|
|
|
await SetSelectedCharacterSlotAsync(userId, newSlot, db.DbContext);
|
|
|
|
|
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 11:59:56 -03:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 18:02:10 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 14:26:00 +02:00
|
|
|
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);
|
2022-09-10 17:40:06 +02:00
|
|
|
var traits = profile.Traits.Select(t => t.TraitName);
|
2020-12-24 13:42:40 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2021-01-03 15:22:14 +00:00
|
|
|
var backpack = BackpackPreference.Backpack;
|
|
|
|
|
if (Enum.TryParse<BackpackPreference>(profile.Backpack, true, out var backpackVal))
|
|
|
|
|
backpack = backpackVal;
|
|
|
|
|
|
2020-12-24 13:42:40 +00:00
|
|
|
var gender = sex == Sex.Male ? Gender.Male : Gender.Female;
|
|
|
|
|
if (Enum.TryParse<Gender>(profile.Gender, true, out var genderVal))
|
|
|
|
|
gender = genderVal;
|
|
|
|
|
|
2022-05-05 11:23:48 +02:00
|
|
|
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
|
|
|
|
var markingsRaw = profile.Markings?.Deserialize<List<string>>();
|
|
|
|
|
|
2022-05-05 01:07:42 -07:00
|
|
|
List<Marking> markings = new();
|
2022-05-05 11:23:48 +02:00
|
|
|
if (markingsRaw != null)
|
2022-05-05 01:07:42 -07:00
|
|
|
{
|
2022-05-05 11:23:48 +02:00
|
|
|
foreach (var marking in markingsRaw)
|
2022-05-05 01:07:42 -07:00
|
|
|
{
|
2022-05-05 11:23:48 +02:00
|
|
|
var parsed = Marking.ParseFromDbString(marking);
|
2022-05-05 01:07:42 -07:00
|
|
|
|
2022-05-05 11:23:48 +02:00
|
|
|
if (parsed is null) continue;
|
2022-05-05 01:07:42 -07:00
|
|
|
|
2022-05-05 11:23:48 +02:00
|
|
|
markings.Add(parsed);
|
2022-05-05 01:07:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 14:26:00 +02:00
|
|
|
return new HumanoidCharacterProfile(
|
|
|
|
|
profile.CharacterName,
|
2022-05-14 08:58:45 +10:00
|
|
|
profile.FlavorText,
|
2022-01-08 19:53:14 -06:00
|
|
|
profile.Species,
|
2020-09-29 14:26:00 +02:00
|
|
|
profile.Age,
|
2020-12-24 13:42:40 +00:00
|
|
|
sex,
|
|
|
|
|
gender,
|
2020-09-29 14:26:00 +02:00
|
|
|
new HumanoidCharacterAppearance
|
|
|
|
|
(
|
|
|
|
|
profile.HairName,
|
|
|
|
|
Color.FromHex(profile.HairColor),
|
|
|
|
|
profile.FacialHairName,
|
|
|
|
|
Color.FromHex(profile.FacialHairColor),
|
|
|
|
|
Color.FromHex(profile.EyeColor),
|
2022-05-05 01:07:42 -07:00
|
|
|
Color.FromHex(profile.SkinColor),
|
Humanoid appearance refactor (#10882)
* 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
2022-09-22 15:19:00 -07:00
|
|
|
markings
|
2020-09-29 14:26:00 +02:00
|
|
|
),
|
2020-12-24 13:42:40 +00:00
|
|
|
clothing,
|
2021-01-03 15:22:14 +00:00
|
|
|
backpack,
|
2020-09-29 14:26:00 +02:00
|
|
|
jobs,
|
|
|
|
|
(PreferenceUnavailableMode) profile.PreferenceUnavailable,
|
2022-09-10 17:40:06 +02:00
|
|
|
antags.ToList(),
|
|
|
|
|
traits.ToList()
|
2020-09-29 14:26:00 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Profile ConvertProfiles(HumanoidCharacterProfile humanoid, int slot)
|
|
|
|
|
{
|
|
|
|
|
var appearance = (HumanoidCharacterAppearance) humanoid.CharacterAppearance;
|
2022-05-05 01:07:42 -07:00
|
|
|
List<string> markingStrings = new();
|
|
|
|
|
foreach (var marking in appearance.Markings)
|
|
|
|
|
{
|
|
|
|
|
markingStrings.Add(marking.ToString());
|
|
|
|
|
}
|
2022-05-05 11:23:48 +02:00
|
|
|
var markings = JsonSerializer.SerializeToDocument(markingStrings);
|
2020-09-29 14:26:00 +02:00
|
|
|
|
|
|
|
|
var entity = new Profile
|
|
|
|
|
{
|
|
|
|
|
CharacterName = humanoid.Name,
|
2022-05-14 08:58:45 +10:00
|
|
|
FlavorText = humanoid.FlavorText,
|
2022-01-08 19:53:14 -06:00
|
|
|
Species = humanoid.Species,
|
2020-09-29 14:26:00 +02:00
|
|
|
Age = humanoid.Age,
|
|
|
|
|
Sex = humanoid.Sex.ToString(),
|
2020-12-24 13:42:40 +00:00
|
|
|
Gender = humanoid.Gender.ToString(),
|
2021-03-28 08:26:32 +02:00
|
|
|
HairName = appearance.HairStyleId,
|
2020-09-29 14:26:00 +02:00
|
|
|
HairColor = appearance.HairColor.ToHex(),
|
2021-03-28 08:26:32 +02:00
|
|
|
FacialHairName = appearance.FacialHairStyleId,
|
2020-09-29 14:26:00 +02:00
|
|
|
FacialHairColor = appearance.FacialHairColor.ToHex(),
|
|
|
|
|
EyeColor = appearance.EyeColor.ToHex(),
|
|
|
|
|
SkinColor = appearance.SkinColor.ToHex(),
|
2020-12-24 13:42:40 +00:00
|
|
|
Clothing = humanoid.Clothing.ToString(),
|
2021-01-03 15:22:14 +00:00
|
|
|
Backpack = humanoid.Backpack.ToString(),
|
2022-05-05 01:07:42 -07:00
|
|
|
Markings = markings,
|
2020-09-29 14:26:00 +02:00
|
|
|
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})
|
|
|
|
|
);
|
2022-09-10 17:40:06 +02:00
|
|
|
entity.Traits.AddRange(
|
|
|
|
|
humanoid.TraitPreferences
|
|
|
|
|
.Select(t => new Trait {TraitName = t})
|
|
|
|
|
);
|
2020-09-29 14:26:00 +02:00
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
2021-02-14 11:59:56 -03:00
|
|
|
#endregion
|
2020-09-29 14:26:00 +02:00
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
#region User Ids
|
2020-09-29 14:26:00 +02:00
|
|
|
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();
|
|
|
|
|
}
|
2021-11-11 17:54:02 +01:00
|
|
|
#endregion
|
2020-09-29 14:26:00 +02:00
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
#region Bans
|
2020-09-29 14:26:00 +02:00
|
|
|
/*
|
|
|
|
|
* BAN STUFF
|
|
|
|
|
*/
|
2021-02-13 17:51:54 +01:00
|
|
|
/// <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>
|
2021-03-22 01:30:50 +01:00
|
|
|
/// <param name="hwId">The HWId of the user.</param>
|
2021-02-13 17:51:54 +01:00
|
|
|
/// <returns>The user's latest received un-pardoned ban, or null if none exist.</returns>
|
2021-03-22 01:30:50 +01:00
|
|
|
public abstract Task<ServerBanDef?> GetServerBanAsync(
|
|
|
|
|
IPAddress? address,
|
|
|
|
|
NetUserId? userId,
|
|
|
|
|
ImmutableArray<byte>? hwId);
|
2021-02-13 17:51:54 +01:00
|
|
|
|
|
|
|
|
/// <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>
|
2021-03-22 01:30:50 +01:00
|
|
|
/// <param name="hwId">The HWId of the user.</param>
|
2022-02-02 22:57:11 +01:00
|
|
|
/// <param name="includeUnbanned">Include pardoned and expired bans.</param>
|
2021-02-13 17:51:54 +01:00
|
|
|
/// <returns>The user's ban history.</returns>
|
2021-03-22 01:30:50 +01:00
|
|
|
public abstract Task<List<ServerBanDef>> GetServerBansAsync(
|
|
|
|
|
IPAddress? address,
|
|
|
|
|
NetUserId? userId,
|
2022-02-02 22:57:11 +01:00
|
|
|
ImmutableArray<byte>? hwId,
|
|
|
|
|
bool includeUnbanned);
|
2021-02-13 17:51:54 +01:00
|
|
|
|
2020-09-29 14:26:00 +02:00
|
|
|
public abstract Task AddServerBanAsync(ServerBanDef serverBan);
|
2021-02-13 17:51:54 +01:00
|
|
|
public abstract Task AddServerUnbanAsync(ServerUnbanDef serverUnban);
|
2023-04-03 02:24:55 +02:00
|
|
|
|
|
|
|
|
protected static async Task<ServerBanExemptFlags?> GetBanExemptionCore(DbGuard db, NetUserId? userId)
|
|
|
|
|
{
|
|
|
|
|
if (userId == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var exemption = await db.DbContext.BanExemption
|
|
|
|
|
.SingleOrDefaultAsync(e => e.UserId == userId.Value.UserId);
|
|
|
|
|
|
|
|
|
|
return exemption?.Flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UpdateBanExemption(NetUserId userId, ServerBanExemptFlags flags)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
|
|
|
|
if (flags == 0)
|
|
|
|
|
{
|
|
|
|
|
// Delete whatever is there.
|
|
|
|
|
await db.DbContext.BanExemption.Where(u => u.UserId == userId.UserId).ExecuteDeleteAsync();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var exemption = await db.DbContext.BanExemption.SingleOrDefaultAsync(u => u.UserId == userId.UserId);
|
|
|
|
|
if (exemption == null)
|
|
|
|
|
{
|
|
|
|
|
exemption = new ServerBanExemption
|
|
|
|
|
{
|
|
|
|
|
UserId = userId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.DbContext.BanExemption.Add(exemption);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exemption.Flags = flags;
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ServerBanExemptFlags> GetBanExemption(NetUserId userId)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
|
|
|
|
var flags = await GetBanExemptionCore(db, userId);
|
|
|
|
|
return flags ?? ServerBanExemptFlags.None;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
#endregion
|
2020-09-29 14:26:00 +02:00
|
|
|
|
2022-02-21 14:11:39 -08:00
|
|
|
#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
|
|
|
|
|
|
2022-08-07 08:00:42 +02:00
|
|
|
#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
|
|
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
#region Player Records
|
2020-09-29 14:26:00 +02:00
|
|
|
/*
|
|
|
|
|
* PLAYER RECORDS
|
|
|
|
|
*/
|
2021-11-11 17:54:02 +01:00
|
|
|
public async Task UpdatePlayerRecord(
|
2021-03-22 01:30:50 +01:00
|
|
|
NetUserId userId,
|
|
|
|
|
string userName,
|
|
|
|
|
IPAddress address,
|
2021-11-11 17:54:02 +01:00
|
|
|
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();
|
|
|
|
|
}
|
2020-09-29 14:26:00 +02:00
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
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
|
2020-09-29 14:26:00 +02:00
|
|
|
/*
|
|
|
|
|
* CONNECTION LOG
|
|
|
|
|
*/
|
2022-02-02 22:57:11 +01:00
|
|
|
public abstract Task<int> AddConnectionLogAsync(
|
2021-03-22 01:30:50 +01:00
|
|
|
NetUserId userId,
|
|
|
|
|
string userName,
|
|
|
|
|
IPAddress address,
|
2022-02-02 22:57:11 +01:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
#endregion
|
2020-09-29 14:26:00 +02:00
|
|
|
|
2021-11-11 17:54:02 +01:00
|
|
|
#region Admin Ranks
|
2020-10-30 16:06:48 +01:00
|
|
|
/*
|
2021-11-11 17:54:02 +01:00
|
|
|
* ADMIN RANKS
|
2020-10-30 16:06:48 +01:00
|
|
|
*/
|
2020-11-10 16:50:28 +01:00
|
|
|
public async Task<Admin?> GetAdminDataForAsync(NetUserId userId, CancellationToken cancel)
|
2020-10-30 16:06:48 +01:00
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
2020-11-02 00:22:53 +01:00
|
|
|
return await db.DbContext.Admin
|
|
|
|
|
.Include(p => p.Flags)
|
|
|
|
|
.Include(p => p.AdminRank)
|
2020-11-02 00:23:34 +01:00
|
|
|
.ThenInclude(p => p!.Flags)
|
2022-01-04 06:37:06 -07:00
|
|
|
.AsSplitQuery() // tests fail because of a random warning if you dont have this!
|
2020-11-10 16:50:28 +01:00
|
|
|
.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();
|
|
|
|
|
|
2020-11-13 03:23:13 +01:00
|
|
|
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;
|
2020-11-10 16:50:28 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 18:36:48 +01:00
|
|
|
public virtual async Task<int> AddNewRound(Server server, params Guid[] playerIds)
|
2021-11-22 19:08:27 +01:00
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
|
|
|
|
var players = await db.DbContext.Player
|
|
|
|
|
.Where(player => playerIds.Contains(player.UserId))
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
var round = new Round
|
|
|
|
|
{
|
2022-03-13 18:36:48 +01:00
|
|
|
Players = players,
|
|
|
|
|
ServerId = server.Id
|
2021-11-22 19:08:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2023-05-07 03:13:42 -07:00
|
|
|
// ReSharper disable once SuggestVarOrType_Elsewhere
|
|
|
|
|
Dictionary<Guid, int> players = await db.DbContext.Player
|
2021-11-22 19:08:27 +01:00
|
|
|
.Where(player => playerIds.Contains(player.UserId))
|
2023-05-07 03:13:42 -07:00
|
|
|
.ToDictionaryAsync(player => player.UserId, player => player.Id);
|
2021-11-22 19:08:27 +01:00
|
|
|
|
2023-05-07 03:13:42 -07:00
|
|
|
foreach (var player in playerIds)
|
2021-12-23 22:13:15 +01:00
|
|
|
{
|
2023-05-07 03:13:42 -07:00
|
|
|
await db.DbContext.Database.ExecuteSqlAsync($"""
|
|
|
|
|
INSERT INTO player_round (players_id, rounds_id) VALUES ({players[player]}, {id}) ON CONFLICT DO NOTHING
|
|
|
|
|
""");
|
2021-12-23 22:13:15 +01:00
|
|
|
}
|
2021-11-22 19:08:27 +01:00
|
|
|
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 16:50:28 +01:00
|
|
|
public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
|
|
|
|
|
2020-11-13 03:23:13 +01:00
|
|
|
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;
|
2020-11-10 16:50:28 +01:00
|
|
|
|
|
|
|
|
await db.DbContext.SaveChangesAsync(cancel);
|
2020-10-30 16:06:48 +01:00
|
|
|
}
|
2021-11-11 17:54:02 +01:00
|
|
|
#endregion
|
2020-09-29 14:26:00 +02:00
|
|
|
|
2021-11-22 19:08:27 +01:00
|
|
|
#region Admin Logs
|
|
|
|
|
|
2022-08-07 08:00:42 +02:00
|
|
|
public async Task<(Server, bool existed)> AddOrGetServer(string serverName)
|
2022-03-13 18:36:48 +01:00
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
2022-08-07 08:00:42 +02:00
|
|
|
var server = await db.DbContext.Server
|
|
|
|
|
.Where(server => server.Name.Equals(serverName))
|
|
|
|
|
.SingleOrDefaultAsync();
|
|
|
|
|
|
2022-03-13 18:36:48 +01:00
|
|
|
if (server != default)
|
2022-08-07 08:00:42 +02:00
|
|
|
return (server, true);
|
2022-03-13 18:36:48 +01:00
|
|
|
|
|
|
|
|
server = new Server
|
|
|
|
|
{
|
|
|
|
|
Name = serverName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.DbContext.Server.Add(server);
|
|
|
|
|
|
|
|
|
|
await db.DbContext.SaveChangesAsync();
|
|
|
|
|
|
2022-08-07 08:00:42 +02:00
|
|
|
return (server, false);
|
2022-03-13 18:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-22 19:08:27 +01:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 02:36:39 +02:00
|
|
|
private static IQueryable<AdminLog> GetAdminLogsQuery(ServerDbContext db, LogFilter? filter = null)
|
2021-11-22 19:08:27 +01:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 10:09:35 -06:00
|
|
|
if (filter.IncludePlayers)
|
2021-11-22 19:08:27 +01:00
|
|
|
{
|
2023-02-28 10:09:35 -06:00
|
|
|
if (filter.AnyPlayers != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(log =>
|
|
|
|
|
log.Players.Any(p => filter.AnyPlayers.Contains(p.PlayerUserId)) ||
|
|
|
|
|
log.Players.Count == 0 && filter.IncludeNonPlayers);
|
|
|
|
|
}
|
2021-11-22 19:08:27 +01:00
|
|
|
|
2023-02-28 10:09:35 -06:00
|
|
|
if (filter.AllPlayers != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(log =>
|
|
|
|
|
log.Players.All(p => filter.AllPlayers.Contains(p.PlayerUserId)) ||
|
|
|
|
|
log.Players.Count == 0 && filter.IncludeNonPlayers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-11-22 19:08:27 +01:00
|
|
|
{
|
2023-02-28 10:09:35 -06:00
|
|
|
query = query.Where(log => log.Players.Count == 0);
|
2021-11-22 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}")
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-27 21:16:04 -07:00
|
|
|
const int hardLogLimit = 500_000;
|
2021-11-22 19:08:27 +01:00
|
|
|
if (filter.Limit != null)
|
|
|
|
|
{
|
2022-09-27 21:16:04 -07:00
|
|
|
query = query.Take(Math.Min(filter.Limit.Value, hardLogLimit));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
query = query.Take(hardLogLimit);
|
2021-11-22 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async IAsyncEnumerable<string> GetAdminLogMessages(LogFilter? filter = null)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
2023-05-02 02:36:39 +02:00
|
|
|
var query = GetAdminLogsQuery(db.DbContext, filter);
|
2021-11-22 19:08:27 +01:00
|
|
|
|
|
|
|
|
await foreach (var log in query.Select(log => log.Message).AsAsyncEnumerable())
|
|
|
|
|
{
|
|
|
|
|
yield return log;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 02:07:12 +01:00
|
|
|
public async IAsyncEnumerable<SharedAdminLog> GetAdminLogs(LogFilter? filter = null)
|
2021-11-22 19:08:27 +01:00
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
2023-05-02 02:36:39 +02:00
|
|
|
var query = GetAdminLogsQuery(db.DbContext, filter);
|
2021-11-24 19:57:27 +01:00
|
|
|
query = query.Include(log => log.Players);
|
2021-11-22 19:08:27 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 02:07:12 +01:00
|
|
|
yield return new SharedAdminLog(log.Id, log.Type, log.Impact, log.Date, log.Message, players);
|
2021-11-22 19:08:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async IAsyncEnumerable<JsonDocument> GetAdminLogsJson(LogFilter? filter = null)
|
|
|
|
|
{
|
|
|
|
|
await using var db = await GetDb();
|
2023-05-02 02:36:39 +02:00
|
|
|
var query = GetAdminLogsQuery(db.DbContext, filter);
|
2021-11-22 19:08:27 +01:00
|
|
|
|
|
|
|
|
await foreach (var json in query.Select(log => log.Json).AsAsyncEnumerable())
|
|
|
|
|
{
|
|
|
|
|
yield return json;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-01-04 06:37:06 -07:00
|
|
|
#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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-26 20:16:57 +01:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 06:37:06 -07:00
|
|
|
#endregion
|
|
|
|
|
|
2022-03-26 12:46:37 +01:00
|
|
|
#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
|
|
|
|
|
|
2022-04-16 20:57:50 +02:00
|
|
|
#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
|
|
|
|
|
|
2020-09-29 14:26:00 +02:00
|
|
|
protected abstract Task<DbGuard> GetDb();
|
|
|
|
|
|
|
|
|
|
protected abstract class DbGuard : IAsyncDisposable
|
|
|
|
|
{
|
|
|
|
|
public abstract ServerDbContext DbContext { get; }
|
|
|
|
|
|
|
|
|
|
public abstract ValueTask DisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|