Skip to content

Commit

Permalink
fixed ttl component
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Oct 14, 2024
1 parent b1188fc commit 60d98a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
20 changes: 11 additions & 9 deletions Runtime/ArenaClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,6 @@ private void CreateUpdateObject(ArenaObjectJson msg, object indata, object menuC
aobj = gobj.AddComponent(typeof(ArenaObject)) as ArenaObject;
arenaObjs[msg.object_id] = gobj;
aobj.Created = true;
if (msg.persist.HasValue)
aobj.persist = (bool)msg.persist;
aobj.messageType = msg.type;
if (msg.ttl != null)
{
if (!gobj.TryGetComponent<ArenaTtl>(out var ttl))
ttl = gobj.AddComponent<ArenaTtl>();
ttl.SetTtlDeleteTimer((float)msg.ttl);
}
#if UNITY_EDITOR
// local create context auto-select
if (menuCommand != null)
Expand All @@ -577,6 +568,17 @@ private void CreateUpdateObject(ArenaObjectJson msg, object indata, object menuC
#endif
}

// apply storage-level object properties
if (msg.persist.HasValue)
aobj.persist = (bool)msg.persist;
aobj.messageType = msg.type;
if (msg.ttl != null)
{
if (!gobj.TryGetComponent<ArenaTtl>(out var ttl))
ttl = gobj.AddComponent<ArenaTtl>();
ttl.SetTtlDeleteTimer((float)msg.ttl);
}

JObject jData = JObject.Parse(JsonConvert.SerializeObject(indata));
//new JsonSerializerSettings
//{
Expand Down
16 changes: 6 additions & 10 deletions Runtime/ArenaTtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,27 @@ namespace ArenaUnity
[RequireComponent(typeof(ArenaObject))]
public class ArenaTtl : MonoBehaviour
{
DateTime? expiration = null;
long? expiration = null;

private void Start()
{
}

public void SetTtlDeleteTimer(float seconds)
{
DateTime now = DateTime.Now;
int secOnly = (int)Math.Truncate(seconds);
int msOnly = (int)Math.Truncate((seconds - secOnly) * 1000);
TimeSpan time = new(0, 0, 0, secOnly, msOnly);
expiration = now.Add(time);
expiration = DateTimeOffset.Now.ToUnixTimeMilliseconds() + (long)(seconds * 1000);
}

private void Update()
{
Debug.Log($"ttl up: {expiration - DateTime.Now}");
if (expiration != null && expiration > DateTime.Now)
if (expiration != null && DateTimeOffset.Now.ToUnixTimeMilliseconds() > expiration)
{
var aobj = GetComponent<ArenaObject>();
if (aobj != null)
{
aobj.externalDelete = true;

Destroy(gameObject);
Destroy(gameObject);
}
}
}

Expand Down

0 comments on commit 60d98a6

Please sign in to comment.