feat: add world chat UI
This commit is contained in:
@@ -21,6 +21,7 @@ namespace XWorld.Server.Gateway
|
||||
private readonly Matchmaker _matchmaker;
|
||||
private readonly ChannelsNS.Channel<ServerCommand> _cmds =
|
||||
ChannelsNS.Channel.CreateUnbounded<ServerCommand>();
|
||||
private const int MaxWorldChatTextLength = 200;
|
||||
private readonly Dictionary<string, List<int>> _roomPlayers = new Dictionary<string, List<int>>();
|
||||
private readonly Dictionary<int, LobbyPlayerMsg> _lobbyPlayers = new Dictionary<int, LobbyPlayerMsg>();
|
||||
private readonly Dictionary<string, int> _playerCountCache = new Dictionary<string, int>(); // "gameId@version" -> playerCount
|
||||
@@ -156,6 +157,9 @@ namespace XWorld.Server.Gateway
|
||||
case FrameworkOpcode.LobbyLeave:
|
||||
if (_lobbyPlayers.Remove(pid)) BroadcastLobbyState();
|
||||
break;
|
||||
case FrameworkOpcode.WorldChatSend:
|
||||
HandleWorldChatSend(pid, frame.Payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,6 +217,41 @@ namespace XWorld.Server.Gateway
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleWorldChatSend(int pid, byte[] payload)
|
||||
{
|
||||
if (!_sessions.IsConnected(pid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var req = WorldChatSendMsg.Decode(payload);
|
||||
string text = (req.Text ?? string.Empty).Trim();
|
||||
if (text.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (text.Length > MaxWorldChatTextLength)
|
||||
{
|
||||
text = text.Substring(0, MaxWorldChatTextLength);
|
||||
}
|
||||
|
||||
string name = _lobbyPlayers.TryGetValue(pid, out var player) && !string.IsNullOrWhiteSpace(player.Name)
|
||||
? player.Name
|
||||
: $"P{pid}";
|
||||
var msg = new WorldChatMessageMsg
|
||||
{
|
||||
PlayerId = pid,
|
||||
PlayerName = name,
|
||||
Text = text,
|
||||
ServerTimeMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
};
|
||||
byte[] encoded = msg.Encode();
|
||||
foreach (int targetPid in _sessions.ConnectedPlayerIds())
|
||||
{
|
||||
SendFramework(targetPid, FrameworkOpcode.WorldChatMessage, encoded);
|
||||
}
|
||||
}
|
||||
|
||||
private void BroadcastLobbyState()
|
||||
{
|
||||
if (_lobbyPlayers.Count == 0) return;
|
||||
|
||||
@@ -38,6 +38,19 @@ namespace XWorld.Server.Gateway
|
||||
|
||||
public Session Get(int pid) => _byPid.TryGetValue(pid, out var s) ? s : null;
|
||||
|
||||
public IReadOnlyList<int> ConnectedPlayerIds()
|
||||
{
|
||||
List<int> players = null;
|
||||
foreach (var kv in _byPid)
|
||||
{
|
||||
if (kv.Value.Connected)
|
||||
{
|
||||
(players ??= new List<int>()).Add(kv.Key);
|
||||
}
|
||||
}
|
||||
return players ?? (IReadOnlyList<int>)Array.Empty<int>();
|
||||
}
|
||||
|
||||
/// <summary>移除断线且"严格越过"重连窗口的会话(currentTick - DisconnectedAtTick > windowTicks)。返回被移除的会话;无则空数组。</summary>
|
||||
public IReadOnlyList<Session> SweepExpired(long currentTick, long windowTicks)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user