diff --git a/CardSharp/GameSteps/CommandParser.cs b/CardSharp/GameSteps/CommandParser.cs index f93461a..84b4b48 100644 --- a/CardSharp/GameSteps/CommandParser.cs +++ b/CardSharp/GameSteps/CommandParser.cs @@ -100,6 +100,7 @@ private void ParsePlayerSubmitCard(Desk desk, Player player, string command) if (command.StartsWith("出")) { desk.AddMessage("出的命令已经被替换为直接出牌. 如<出34567>被替换为<34567>"); + command = command.Substring(1); } var cardsCommand = command.ToUpper(); if (cardsCommand.IsValidCardString()) diff --git a/CardSharp/MessageSenderBase.cs b/CardSharp/MessageSenderBase.cs index 8e3c562..632e245 100644 --- a/CardSharp/MessageSenderBase.cs +++ b/CardSharp/MessageSenderBase.cs @@ -1,33 +1,45 @@ using System; using System.Runtime.CompilerServices; +using System.Threading; namespace CardSharp { public abstract class MessageSenderBase : IMessageSender { - private readonly object _locker = new object(); - public string Message { get; private set; } + private volatile string _message; + private readonly ReaderWriterLockSlim _rwlock = new ReaderWriterLockSlim(); - public virtual void AddMessage(string msg) + public string Message { - lock (_locker) + get + { + _rwlock.EnterReadLock(); + var str = _message; + _rwlock.ExitReadLock(); + return str; + } + set { - Message += msg; + _rwlock.EnterWriteLock(); + _message = value; + _rwlock.ExitWriteLock(); } } + public virtual void AddMessage(string msg) + { + Message += msg; + } + public void ClearMessage() { - lock (_locker) - { - Message = null; - } + Message = null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddMessageLine(string msg = "") { - if (!Message?.EndsWith(Environment.NewLine) == true) + if (Message?.EndsWith(Environment.NewLine) != true) { AddMessage(Environment.NewLine); } diff --git a/CardSharp/StringExtensions.cs b/CardSharp/StringExtensions.cs index 2b2c23d..e9be497 100644 --- a/CardSharp/StringExtensions.cs +++ b/CardSharp/StringExtensions.cs @@ -14,7 +14,7 @@ public static string AddNewLine(this string source) public static bool IsValidCardString(this string source) { - return Regex.IsMatch(source, "([2-9]|10|A|王|鬼|J|Q|K).*"); + return Regex.IsMatch(source, "^([2-9]|10|A|王|鬼|J|Q|K).*$"); } public static IEnumerable ToCards(this string source) @@ -23,18 +23,21 @@ public static IEnumerable ToCards(this string source) for (var index = 0; index < source.Length; index++) { var chara = source[index]; - if (int.TryParse(chara.ToString(), out var num)) + if (chara >= '0' && chara <= '9') + { + var num = chara - '0'; if (num >= 3 && num <= 9) { yield return new Card(num - px); continue; } - else if (num == 1) + if (num == 1) { index++; yield return new Card(Constants.Cards.C10); continue; } + } switch (chara) {