Initial
This commit is contained in:
74
LightlessSyncServer/LightlessSyncServer/Utils/Extensions.cs
Normal file
74
LightlessSyncServer/LightlessSyncServer/Utils/Extensions.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using LightlessSync.API.Data;
|
||||
using LightlessSync.API.Data.Enum;
|
||||
using LightlessSync.API.Data.Extensions;
|
||||
using LightlessSyncShared.Models;
|
||||
using static LightlessSyncServer.Hubs.LightlessHub;
|
||||
|
||||
namespace LightlessSyncServer.Utils;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static GroupData ToGroupData(this Group group)
|
||||
{
|
||||
return new GroupData(group.GID, group.Alias);
|
||||
}
|
||||
|
||||
public static UserData ToUserData(this GroupPair pair)
|
||||
{
|
||||
return new UserData(pair.GroupUser.UID, pair.GroupUser.Alias);
|
||||
}
|
||||
|
||||
public static UserData ToUserData(this User user)
|
||||
{
|
||||
return new UserData(user.UID, user.Alias);
|
||||
}
|
||||
|
||||
public static IndividualPairStatus ToIndividualPairStatus(this UserInfo userInfo)
|
||||
{
|
||||
if (userInfo.IndividuallyPaired) return IndividualPairStatus.Bidirectional;
|
||||
if (!userInfo.IndividuallyPaired && userInfo.GIDs.Contains(Constants.IndividualKeyword, StringComparer.Ordinal)) return IndividualPairStatus.OneSided;
|
||||
return IndividualPairStatus.None;
|
||||
}
|
||||
|
||||
public static GroupPermissions ToEnum(this Group group)
|
||||
{
|
||||
var permissions = GroupPermissions.NoneSet;
|
||||
permissions.SetPreferDisableAnimations(group.PreferDisableAnimations);
|
||||
permissions.SetPreferDisableSounds(group.PreferDisableSounds);
|
||||
permissions.SetPreferDisableVFX(group.PreferDisableVFX);
|
||||
permissions.SetDisableInvites(!group.InvitesEnabled);
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public static GroupUserPreferredPermissions ToEnum(this GroupPairPreferredPermission groupPair)
|
||||
{
|
||||
var permissions = GroupUserPreferredPermissions.NoneSet;
|
||||
permissions.SetDisableAnimations(groupPair.DisableAnimations);
|
||||
permissions.SetDisableSounds(groupPair.DisableSounds);
|
||||
permissions.SetPaused(groupPair.IsPaused);
|
||||
permissions.SetDisableVFX(groupPair.DisableVFX);
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public static GroupPairUserInfo ToEnum(this GroupPair groupPair)
|
||||
{
|
||||
var groupUserInfo = GroupPairUserInfo.None;
|
||||
groupUserInfo.SetPinned(groupPair.IsPinned);
|
||||
groupUserInfo.SetModerator(groupPair.IsModerator);
|
||||
return groupUserInfo;
|
||||
}
|
||||
|
||||
public static UserPermissions ToUserPermissions(this UserPermissionSet? permissions, bool setSticky = false)
|
||||
{
|
||||
if (permissions == null) return UserPermissions.NoneSet;
|
||||
|
||||
UserPermissions perm = UserPermissions.NoneSet;
|
||||
perm.SetPaused(permissions.IsPaused);
|
||||
perm.SetDisableAnimations(permissions.DisableAnimations);
|
||||
perm.SetDisableSounds(permissions.DisableSounds);
|
||||
perm.SetDisableVFX(permissions.DisableVFX);
|
||||
if (setSticky)
|
||||
perm.SetSticky(permissions.Sticky);
|
||||
return perm;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using LightlessSync.API.SignalR;
|
||||
using LightlessSyncServer.Hubs;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace LightlessSyncServer.Utils;
|
||||
|
||||
public class LightlessHubLogger
|
||||
{
|
||||
private readonly LightlessHub _hub;
|
||||
private readonly ILogger<LightlessHub> _logger;
|
||||
|
||||
public LightlessHubLogger(LightlessHub hub, ILogger<LightlessHub> logger)
|
||||
{
|
||||
_hub = hub;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public static object[] Args(params object[] args)
|
||||
{
|
||||
return args;
|
||||
}
|
||||
|
||||
public void LogCallInfo(object[] args = null, [CallerMemberName] string methodName = "")
|
||||
{
|
||||
string formattedArgs = args != null && args.Length != 0 ? "|" + string.Join(":", args) : string.Empty;
|
||||
_logger.LogInformation("{uid}:{method}{args}", _hub.UserUID, methodName, formattedArgs);
|
||||
}
|
||||
|
||||
public void LogCallWarning(object[] args = null, [CallerMemberName] string methodName = "")
|
||||
{
|
||||
string formattedArgs = args != null && args.Length != 0 ? "|" + string.Join(":", args) : string.Empty;
|
||||
_logger.LogWarning("{uid}:{method}{args}", _hub.UserUID, methodName, formattedArgs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace LightlessSyncServer.Utils;
|
||||
|
||||
public enum PauseInfo
|
||||
{
|
||||
NoConnection,
|
||||
Paused,
|
||||
Unpaused,
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace LightlessSyncServer.Utils;
|
||||
|
||||
public record PauseState
|
||||
{
|
||||
public string GID { get; set; }
|
||||
public bool IsPaused => IsSelfPaused || IsOtherPaused;
|
||||
public bool IsSelfPaused { get; set; }
|
||||
public bool IsOtherPaused { get; set; }
|
||||
}
|
||||
58
LightlessSyncServer/LightlessSyncServer/Utils/PausedEntry.cs
Normal file
58
LightlessSyncServer/LightlessSyncServer/Utils/PausedEntry.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
namespace LightlessSyncServer.Utils;
|
||||
|
||||
public record PausedEntry
|
||||
{
|
||||
public string UID { get; set; }
|
||||
public List<PauseState> PauseStates { get; set; } = new();
|
||||
|
||||
public PauseInfo IsDirectlyPaused => PauseStateWithoutGroups == null ? PauseInfo.NoConnection
|
||||
: PauseStates.First(g => g.GID == null).IsPaused ? PauseInfo.Paused : PauseInfo.Unpaused;
|
||||
|
||||
public PauseInfo IsPausedPerGroup => !PauseStatesWithoutDirect.Any() ? PauseInfo.NoConnection
|
||||
: PauseStatesWithoutDirect.All(p => p.IsPaused) ? PauseInfo.Paused : PauseInfo.Unpaused;
|
||||
|
||||
private IEnumerable<PauseState> PauseStatesWithoutDirect => PauseStates.Where(f => f.GID != null);
|
||||
private PauseState PauseStateWithoutGroups => PauseStates.SingleOrDefault(p => p.GID == null);
|
||||
|
||||
public bool IsPaused
|
||||
{
|
||||
get
|
||||
{
|
||||
var isDirectlyPaused = IsDirectlyPaused;
|
||||
bool result;
|
||||
if (isDirectlyPaused != PauseInfo.NoConnection)
|
||||
{
|
||||
result = isDirectlyPaused == PauseInfo.Paused;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = IsPausedPerGroup == PauseInfo.Paused;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public PauseInfo IsOtherPausedForSpecificGroup(string gid)
|
||||
{
|
||||
var state = PauseStatesWithoutDirect.SingleOrDefault(g => string.Equals(g.GID, gid, StringComparison.Ordinal));
|
||||
if (state == null) return PauseInfo.NoConnection;
|
||||
return state.IsOtherPaused ? PauseInfo.Paused : PauseInfo.Unpaused;
|
||||
}
|
||||
|
||||
public PauseInfo IsPausedForSpecificGroup(string gid)
|
||||
{
|
||||
var state = PauseStatesWithoutDirect.SingleOrDefault(g => string.Equals(g.GID, gid, StringComparison.Ordinal));
|
||||
if (state == null) return PauseInfo.NoConnection;
|
||||
return state.IsPaused ? PauseInfo.Paused : PauseInfo.NoConnection;
|
||||
}
|
||||
|
||||
public PauseInfo IsPausedExcludingGroup(string gid)
|
||||
{
|
||||
var states = PauseStatesWithoutDirect.Where(f => !string.Equals(f.GID, gid, StringComparison.Ordinal)).ToList();
|
||||
if (!states.Any()) return PauseInfo.NoConnection;
|
||||
var result = states.All(p => p.IsPaused);
|
||||
if (result) return PauseInfo.Paused;
|
||||
return PauseInfo.Unpaused;
|
||||
}
|
||||
}
|
||||
12
LightlessSyncServer/LightlessSyncServer/Utils/UserPair.cs
Normal file
12
LightlessSyncServer/LightlessSyncServer/Utils/UserPair.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace LightlessSyncServer.Hubs;
|
||||
|
||||
public partial class LightlessHub
|
||||
{
|
||||
private record UserPair
|
||||
{
|
||||
public string UserUID { get; set; }
|
||||
public string OtherUserUID { get; set; }
|
||||
public bool UserPausedOther { get; set; }
|
||||
public bool OtherPausedUser { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user