diff --git a/src/Hypnonema.Server/ServerScript.cs b/src/Hypnonema.Server/ServerScript.cs index 7188333..a839adb 100644 --- a/src/Hypnonema.Server/ServerScript.cs +++ b/src/Hypnonema.Server/ServerScript.cs @@ -29,10 +29,13 @@ public class ServerScript : BaseScript private LiteDatabase database; - private List lastKnownState; + private ScreenDuiStateList lastKnownState = new ScreenDuiStateList(); + + private int syncInterval = 5000; private LiteCollection screenCollection; + public ServerScript() { this.RegisterEventHandler("onResourceStart", new Action(this.OnResourceStart)); @@ -105,12 +108,19 @@ private void OnClientInitialize([FromSource] Player p) } var q = this.screenCollection.Find(s => s.AlwaysOn); + + // if we didn't receive a syncInterval for 3 times, we flush the lastKnownState + if (DateTime.UtcNow > (this.lastKnownState.Timestamp + new TimeSpan(0,0,0,0, this.syncInterval*3))) + { + this.lastKnownState = new ScreenDuiStateList(); + } + p.TriggerEvent( ClientEvents.Initialize, JsonConvert.SerializeObject(q), filesCount, this.IsPlayerAllowed(p), - JsonConvert.SerializeObject(this.lastKnownState)); + JsonConvert.SerializeObject(this.lastKnownState.StateList)); } catch (Exception e) { @@ -277,6 +287,8 @@ private void OnResourceStart(string resourceName) this.cmdName = ConfigReader.GetConfigKeyValue(resourceName, "hypnonema_command_name", 0, "hypnonema") .Replace(" ", string.Empty); + this.syncInterval = ConfigReader.GetConfigKeyValue(resourceName, "hypnonema_sync_interval", 0, 5000); + if (this.cmdName != "hypnonema") Logger.WriteLine( $"Using {this.cmdName} as command name. Type /{this.cmdName} to open the NUI window.", @@ -321,7 +333,11 @@ private void OnStateTick([FromSource] Player p, string jsonState) where screen != null select new ScreenDuiState { Screen = screen, State = duiState }).ToList(); - if (lastState.Any()) this.lastKnownState = lastState; + if (lastState.Any()) + { + this.lastKnownState.StateList = lastState; + this.lastKnownState.Timestamp = DateTime.UtcNow; + } } private void OnStopVideo([FromSource] Player p, string screenName) diff --git a/src/Hypnonema.Shared/ScreenDuiState.cs b/src/Hypnonema.Shared/ScreenDuiState.cs index 39f73de..2c0ae36 100644 --- a/src/Hypnonema.Shared/ScreenDuiState.cs +++ b/src/Hypnonema.Shared/ScreenDuiState.cs @@ -1,5 +1,8 @@ namespace Hypnonema.Shared { + using System; + using System.Collections.Generic; + using Hypnonema.Shared.Models; public class ScreenDuiState @@ -8,4 +11,11 @@ public class ScreenDuiState public Screen Screen { get; set; } } + + public class ScreenDuiStateList + { + public List StateList { get; set; } = new List(); + + public DateTime Timestamp { get; set; } + } } \ No newline at end of file