Skip to content

Commit

Permalink
many such updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LolaLollipop committed Dec 30, 2023
1 parent 8a45fee commit 7eea1dc
Show file tree
Hide file tree
Showing 29 changed files with 313 additions and 130 deletions.
1 change: 0 additions & 1 deletion RueI/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace RueI;

using System.Reflection;
using System.Runtime.CompilerServices;

/*********\
Expand Down
62 changes: 44 additions & 18 deletions RueI/RueI/Displays/AutoGiving/AutoGivers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PlayerRoles;

using RueI.Displays;
using RueI.Displays.Scheduling;
using RueI.Elements;

/// <summary>
Expand All @@ -27,12 +28,16 @@ public record ElemRefResolver<T>(IElemReference<T> elemRef, Func<T> creator)
/// </summary>
public class AutoElement
{
private record PeriodicUpdate(TimeSpan time, int priority, JobToken token);

private static readonly List<AutoElement> AutoGivers = new();

private readonly Element? element;

private readonly Func<DisplayCore, Element>? creator;
private readonly IElemReference<Element>? reference;
private readonly IElemReference<Element> reference;

private PeriodicUpdate? periodicUpdate;

static AutoElement()
{
Expand All @@ -58,10 +63,12 @@ private AutoElement(Roles roles, IElemReference<Element> reference, Func<Display
/// Initializes a new instance of the <see cref="AutoElement"/> class.
/// </summary>
/// <param name="roles">The <see cref="Roles"/> to use for the <see cref="AutoElement"/>.</param>
/// <param name="reference">The <see cref="IElemReference{T}"/> to use.</param>
/// <param name="element">The element to automatically give.</param>
private AutoElement(Roles roles, Element element)
private AutoElement(Roles roles, IElemReference<Element> reference, Element element)
{
this.element = element;
this.reference = reference;
Roles = roles;

AutoGivers.Add(this);
Expand All @@ -82,21 +89,20 @@ private AutoElement(Roles roles, Element element)
public static AutoElement Create<T>(Roles roles, T element)
where T : Element
{
return new AutoElement(roles, element);
return new AutoElement(roles, DisplayCore.GetReference<T>(), element);
}

/// <summary>
/// Creates a new <see cref="AutoElement"/> using a <see cref="IElemReference{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the <see cref="Element"/>.</typeparam>
/// <param name="roles">The <see cref="Roles"/> to use for the <see cref="AutoElement"/>.</param>
/// <param name="reference">The <see cref="IElemReference{T}"/> to use.</param>
/// <param name="creator">A <see cref="Func{T, TResult}"/> that creates the elements.</param>
/// <returns>A new <see cref="AutoElement"/>.</returns>
public static AutoElement Create<T>(Roles roles, IElemReference<T> reference, Func<DisplayCore, T> creator)
public static AutoElement Create<T>(Roles roles, Func<DisplayCore, T> creator)
where T : Element
{
return new AutoElement(roles, reference, (core) => creator(core));
return new AutoElement(roles, DisplayCore.GetReference<T>(), (core) => creator(core));
}

/// <summary>
Expand All @@ -107,22 +113,37 @@ public virtual void Disable()
AutoGivers.Remove(this);
}

/// <summary>
/// Schedules an update for all players with one of the <see cref="Roles"/> every <paramref name="span"/>.
/// </summary>
/// <param name="span">How often to schedule an update.</param>
/// <param name="priority">The priority of the update.</param>
/// <returns>A reference to this <see cref="AutoElement"/>.</returns>
public AutoElement UpdateEvery(TimeSpan span, int priority = 35)
{
periodicUpdate = new(span, priority, new());
return this;
}

/// <summary>
/// Gives this <see cref="AutoElement"/> to a <see cref="DisplayCore"/>.
/// </summary>
/// <param name="core">The <see cref="DisplayCore"/> to give to.</param>
protected virtual void GiveTo(DisplayCore core)
{
ServerConsole.AddLog("Hello");
if (element != null)
{
if (!core.AnonymousDisplay.Elements.Contains(element))
{
core.AnonymousDisplay.Elements.Add(element);
}
core.AddAsReference(reference, element);
}
else
{
core.AddAsReference(reference!, creator!(core));
core.AddAsReference(reference, creator!(core));
}

if (periodicUpdate != null)
{
ScheduleUpdate(core, periodicUpdate);
}
}

Expand All @@ -132,13 +153,11 @@ protected virtual void GiveTo(DisplayCore core)
/// <param name="core">The <see cref="DisplayCore"/> to give to.</param>
protected virtual void RemoveFrom(DisplayCore core)
{
if (element != null)
{
core.AnonymousDisplay.Elements.Remove(element);
}
else
core.RemoveReference(reference);

if (periodicUpdate != null)
{
core.RemoveReference(reference!);
core.Scheduler.KillJob(periodicUpdate.token);
}
}

Expand All @@ -156,9 +175,16 @@ private static void OnRoleChanged(ReferenceHub hub, PlayerRoleBase prevRole, Pla
}
else
{

autoElement.RemoveFrom(core);
}
}

core.Update(35);
}
}

private static void ScheduleUpdate(DisplayCore core, PeriodicUpdate update)
{
core.Scheduler.Schedule(update.time, () => ScheduleUpdate(core, update), update.token);
}
}
5 changes: 5 additions & 0 deletions RueI/RueI/Displays/AutoGiving/Roles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
/// <summary>
/// Provides a means for describing multiple <see cref="RoleTypeId"/>s.
/// </summary>
/// <remarks>
/// The purpose of the <see cref="Roles"/> enum is to enable roles to be treated like a <see cref="FlagsAttribute"/> enum. Normally,
/// <see cref="RoleTypeId"/> cannot be treated like bit flags, so this acts as a fast and convenient way to do so.
/// </remarks>
/// <include file='docs.xml' path='docs/displays/members[@name="roles"]/Roles/*'/>
[Flags]
public enum Roles
{
Expand Down
4 changes: 2 additions & 2 deletions RueI/RueI/Displays/Display.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using RueI.Displays.Interfaces;

/// <summary>
/// Represents a display attached to a <see cref="DisplayCore"/>.
/// Represents a basic display attached to a <see cref="DisplayCore"/>.
/// </summary>
/// <include file='docs.xml' path='docs/members[@name="display"]/Display/*'/>
/// <include file='docs.xml' path='docs/displays/members[@name="display"]/Display/*'/>
public class Display : DisplayBase, IElementContainer
{
/// <summary>
Expand Down
10 changes: 3 additions & 7 deletions RueI/RueI/Displays/DisplayCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using RueI.Extensions;

/// <summary>
/// Is responsible for managing all of the <see cref="DisplayBase"/>s for a <see cref="ReferenceHub"/>.
/// Manages all of the <see cref="DisplayBase"/>s for a <see cref="ReferenceHub"/>.
/// </summary>
public class DisplayCore
{
Expand Down Expand Up @@ -33,7 +33,6 @@ protected DisplayCore(ReferenceHub hub)
}

Scheduler = new(this);
AnonymousDisplay = new(this);
}

/// <summary>
Expand All @@ -46,11 +45,6 @@ protected DisplayCore(ReferenceHub hub)
/// </summary>
internal static Dictionary<ReferenceHub, DisplayCore> DisplayCores { get; } = new();

/// <summary>
/// Gets a display of anonymous <see cref="Element"/>s added to this display.
/// </summary>
internal Display AnonymousDisplay { get; }

/// <summary>
/// Gets the <see cref="ReferenceHub"/> that this display is for.
/// </summary>
Expand Down Expand Up @@ -96,6 +90,7 @@ public static IElemReference<T> GetReference<T>()
/// <param name="priority">The priority of the update - defaults to 100.</param>
public void Update(int priority = 100)
{
ServerConsole.AddLog("updating");
if (IgnoreUpdate)
{
return;
Expand Down Expand Up @@ -174,6 +169,7 @@ public void RemoveReference<T>(IElemReference<T> reference)
internal void InternalUpdate()
{
string text = ElemCombiner.Combine(GetAllElements());
ServerConsole.AddLog(text);
UnityAlternative.Provider.ShowHint(Hub, text);
Events.Events.OnDisplayUpdated(new(this));
}
Expand Down
16 changes: 10 additions & 6 deletions RueI/RueI/Displays/ElemCombiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class ElemCombiner
/// <returns>A <see cref="string"/> with all of the combined <see cref="Element"/>s.</returns>
public static string Combine(IEnumerable<Element> enumElems)
{
ServerConsole.AddLog("hello world!!!");
List<Element> elements = ListPool<Element>.Shared.Rent(enumElems);

if (!elements.Any())
Expand All @@ -32,18 +33,19 @@ public static string Combine(IEnumerable<Element> enumElems)

float lastPosition = 0;
float lastOffset = 0;

ServerConsole.AddLog("im kind of a big deal");
elements.Sort(CompareElement);

for (int i = 0; i < elements.Count; i++)
{
Element curElement = elements[i];

ServerConsole.AddLog(curElement.ZIndex.ToString());
ParsedData parsedData = curElement.GetParsedData();
ServerConsole.AddLog(curElement.ZIndex.ToString());
float funcPos = curElement.GetFunctionalPosition();
if (curElement.Options.HasFlagFast(Elements.Enums.ElementOptions.PreserveSpacing))
{
funcPos -= parsedData.offset;
funcPos -= parsedData.Offset;
}

if (i != 0)
Expand All @@ -57,16 +59,18 @@ public static string Combine(IEnumerable<Element> enumElems)
totalOffset += funcPos;
}

sb.Append(parsedData.content);
sb.Append(parsedData.Content);

totalOffset += parsedData.offset;
totalOffset += parsedData.Offset;
lastPosition = funcPos;
lastOffset = parsedData.offset;
lastOffset = parsedData.Offset;
ServerConsole.AddLog("What the fuck");
}

ListPool<Element>.Shared.Return(elements);
sb.Insert(0, $"<line-height={totalOffset}px>\n</line-height>");
sb.Append(Constants.ZeroWidthSpace);
ServerConsole.AddLog(sb.ToString());
return StringBuilderPool.Shared.ToStringReturn(sb);
}

Expand Down
7 changes: 5 additions & 2 deletions RueI/RueI/Displays/IDGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
/// <summary>
/// Generates new, unique IDs.
/// </summary>
/// <remarks>
/// This class is not thread safe.
/// </remarks>
internal static class IDGenerator
{
private static int nextID = 0;

/// <summary>
/// Gets a new ID.
/// Gets a new and unique ID.
/// </summary>
/// <returns>A new ID.</returns>
/// <returns>A new, unique ID.</returns>
internal static int GetID() => nextID++;
}
4 changes: 4 additions & 0 deletions RueI/RueI/Displays/Scheduling/JobToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/// <summary>
/// Represents a reference to any number of <see cref="Records.ScheduledJob"/>.
/// </summary>
/// <remarks>
/// A <see cref="JobToken"/> provides a unique identifier for a <see cref="Records.ScheduledJob"/> within any number of <see cref="Scheduler"/>s. In other words, a <see cref="JobToken"/> can reference multiple (or no) <see cref="Records.ScheduledJob"/>, but only a single <see cref="Records.ScheduledJob"/> with the given <see cref="JobToken"/> can exist in a <see cref="Scheduler"/>.
/// </remarks>
/// <seealso cref="Records.ScheduledJob"/>
public class JobToken
{
/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions RueI/RueI/Displays/Scheduling/RateLimiter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Diagnostics;

namespace RueI.Displays.Scheduling;
namespace RueI.Displays.Scheduling;

/// <summary>
/// Provides a way to ratelimit actions or detect ratelimits.
/// </summary>
/// <seealso cref="Scheduler"/>
public class RateLimiter
{
private DateTimeOffset lastConsumed = DateTime.UtcNow;
Expand Down
2 changes: 2 additions & 0 deletions RueI/RueI/Displays/Scheduling/Records/ScheduledJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/// <summary>
/// Defines a scheduled job for a <see cref="Scheduler"/>.
/// </summary>
/// <seealso cref="Scheduler"/>
/// <seealso cref="JobToken"/>
public class ScheduledJob : IComparable<ScheduledJob>
{
/// <summary>
Expand Down
Loading

0 comments on commit 7eea1dc

Please sign in to comment.