86 lines
2.3 KiB
C#
86 lines
2.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class AnalyticsSessionState
|
|
{
|
|
private SessionStatus _status;
|
|
|
|
public string SessionId { get; private set; }
|
|
|
|
public float StartRealtimeSeconds { get; private set; }
|
|
|
|
public int HeartbeatIndex { get; private set; }
|
|
|
|
public int TotalBuildingsPlaced { get; private set; }
|
|
|
|
public bool Started => _status != SessionStatus.NotStarted;
|
|
|
|
public bool DemoCompleted => _status == SessionStatus.DemoCompleted;
|
|
|
|
public void Begin(float realtimeSeconds)
|
|
{
|
|
_status = SessionStatus.Started;
|
|
SessionId = Guid.NewGuid().ToString("N");
|
|
StartRealtimeSeconds = realtimeSeconds;
|
|
HeartbeatIndex = 0;
|
|
TotalBuildingsPlaced = 0;
|
|
}
|
|
|
|
public bool TryRecordBuildingConstruction(BuildingDefinition definition)
|
|
{
|
|
if (!Started || !definition) return false;
|
|
|
|
TotalBuildingsPlaced++;
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool CanRecordHouseUpgrade(Building building)
|
|
{
|
|
return Started && building.Definition.IsHouse;
|
|
}
|
|
|
|
public bool TryAdvanceHeartbeat(float realtimeSeconds, float heartbeatIntervalSeconds, out int heartbeatIndex, out float playtimeSeconds)
|
|
{
|
|
heartbeatIndex = 0;
|
|
playtimeSeconds = 0;
|
|
|
|
if (!Started || DemoCompleted) return false;
|
|
|
|
var nextHeartbeatTime = StartRealtimeSeconds + (HeartbeatIndex + 1) * heartbeatIntervalSeconds;
|
|
if (realtimeSeconds < nextHeartbeatTime) return false;
|
|
|
|
heartbeatIndex = ++HeartbeatIndex;
|
|
playtimeSeconds = GetPlaytimeSeconds(realtimeSeconds);
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool TryMarkDemoCompleted()
|
|
{
|
|
if (!Started || DemoCompleted) return false;
|
|
|
|
_status = SessionStatus.DemoCompleted;
|
|
|
|
return true;
|
|
}
|
|
|
|
public float GetPlaytimeSeconds(float realtimeSeconds)
|
|
{
|
|
if (!Started) return 0;
|
|
|
|
return Mathf.Max(realtimeSeconds - StartRealtimeSeconds, 0);
|
|
}
|
|
|
|
private enum SessionStatus
|
|
{
|
|
NotStarted,
|
|
|
|
Started,
|
|
|
|
DemoCompleted
|
|
}
|
|
}
|
|
} |