Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

Commit

Permalink
Fix some bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cqjjjzr committed Feb 22, 2018
1 parent 5d8b5b0 commit 11949fd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions CardSharp/GameSteps/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
32 changes: 22 additions & 10 deletions CardSharp/MessageSenderBase.cs
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
9 changes: 6 additions & 3 deletions CardSharp/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Card> ToCards(this string source)
Expand All @@ -23,18 +23,21 @@ public static IEnumerable<Card> 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)
{
Expand Down

0 comments on commit 11949fd

Please sign in to comment.