mirror of
https://gitee.com/xiarenalofs/squad-rain-ops-mini.git
synced 2026-08-08 14:25:33 +08:00
127 lines
4.0 KiB
C#
127 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using RainOpsMini.Models;
|
|
|
|
namespace RainOpsMini.Helpers.RCON
|
|
{
|
|
public class ChatMessage
|
|
{
|
|
public string Type { get; set; } = "";
|
|
public string EosId { get; set; } = "";
|
|
public string SteamId { get; set; } = "";
|
|
public string Name { get; set; } = "";
|
|
public string Message { get; set; } = "";
|
|
public DateTime Timestamp { get; set; }
|
|
public int? TeamId { get; set; }
|
|
public int? SquadId { get; set; }
|
|
}
|
|
|
|
public class RconDataCache
|
|
{
|
|
private readonly object _lock = new object();
|
|
|
|
public ServerInfoResponse? ServerInfo { get; private set; }
|
|
public List<SquadInfo> Squads { get; private set; } = new List<SquadInfo>();
|
|
public List<PlayerInfo> Players { get; private set; } = new List<PlayerInfo>();
|
|
public DateTime LastUpdated { get; private set; } = DateTime.MinValue;
|
|
|
|
// Cache for Steam Playtime (SteamId -> Minutes)
|
|
public Dictionary<string, int> GameTimeCache { get; private set; } = new Dictionary<string, int>();
|
|
|
|
// Chat History Cache (Max 300)
|
|
public LinkedList<ChatMessage> ChatHistory { get; private set; } = new LinkedList<ChatMessage>();
|
|
private const int MaxChatHistory = 300;
|
|
|
|
private int _refreshIntervalSeconds = 30;
|
|
public int RefreshIntervalSeconds
|
|
{
|
|
get => _refreshIntervalSeconds;
|
|
set
|
|
{
|
|
if (value < 10) _refreshIntervalSeconds = 10;
|
|
else if (value > 60) _refreshIntervalSeconds = 60;
|
|
else _refreshIntervalSeconds = value;
|
|
}
|
|
}
|
|
|
|
public PlayerInfo? GetPlayer(string steamId, string eosId)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (Players == null) return null;
|
|
return Players.FirstOrDefault(p =>
|
|
(!string.IsNullOrEmpty(steamId) && p.SteamId == steamId) ||
|
|
(!string.IsNullOrEmpty(eosId) && p.EosId == eosId));
|
|
}
|
|
}
|
|
|
|
public void AddChatMessage(ChatMessage msg)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
ChatHistory.AddFirst(msg);
|
|
if (ChatHistory.Count > MaxChatHistory)
|
|
{
|
|
ChatHistory.RemoveLast();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdateData(ServerInfoResponse? serverInfo, List<SquadInfo> squads, List<PlayerInfo> players)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
ServerInfo = serverInfo;
|
|
Squads = squads ?? new List<SquadInfo>();
|
|
Players = players ?? new List<PlayerInfo>();
|
|
LastUpdated = DateTime.Now;
|
|
}
|
|
}
|
|
|
|
public ServerData GetData()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return new ServerData
|
|
{
|
|
ServerInfo = ServerInfo,
|
|
Squads = new List<SquadInfo>(Squads), // Return copy to be safe
|
|
Players = new List<PlayerInfo>(Players),
|
|
// ChatHistory is not part of ServerData currently, need to see if I should add it there or handle separately
|
|
};
|
|
}
|
|
}
|
|
|
|
public List<ChatMessage> GetChatHistory()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return new List<ChatMessage>(ChatHistory);
|
|
}
|
|
}
|
|
|
|
public void UpdateGameTime(string steamId, int minutes)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (GameTimeCache.ContainsKey(steamId))
|
|
{
|
|
GameTimeCache[steamId] = minutes;
|
|
}
|
|
else
|
|
{
|
|
GameTimeCache.Add(steamId, minutes);
|
|
}
|
|
}
|
|
}
|
|
|
|
public Dictionary<string, int> GetGameTimes()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return new Dictionary<string, int>(GameTimeCache);
|
|
}
|
|
}
|
|
}
|
|
}
|