using NotPogo.Core.Rooms; using System.Net.Sockets; namespace NotPogo.Core { public sealed class Player { static int _nextId; public int Id { get; } = Interlocked.Increment(ref _nextId); public Account? Account { get; set; } public string ScreenName { get; set; } = "Guest"; public string ProfileId { get; set; } = "100000000000"; public string LoginKey { get; set; } = ""; public Room? Room { get; set; } public DateTime? LastSeen { get; set; } readonly NetworkStream _stream; readonly SemaphoreSlim _writeLock = new(1, 1); public Player(NetworkStream stream) => _stream = stream; public async Task SendAsync(params(string key, string value)[] fields) { var msg = new Dictionary(); foreach(var (k, v) in fields) msg[k] = v; await SendAsync(msg); } public async Task SendAsync(Dictionary msg) { await _writeLock.WaitAsync(); try { await TCPProtocol.WriteMessageAsync(_stream, msg); } catch(IOException) {} catch(ObjectDisposedException) {} finally { _writeLock.Release(); } } } }