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

Commit

Permalink
fake player
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyl18 committed Feb 17, 2018
1 parent 47e7fd4 commit 1a459f0
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 28 deletions.
14 changes: 5 additions & 9 deletions CardSharp.GUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CardSharp.GameComponents;

namespace CardSharp.GUI
{
Expand All @@ -25,19 +26,14 @@ private static void RunTest(ref int count)
{
var sw = Stopwatch.StartNew();
var desk = Desk.GetOrCreateDesk(Rng.NextDouble().ToString(CultureInfo.InvariantCulture));
desk.AddPlayer(new Player("Player1"));
desk.AddPlayer(new Player("Player2"));
desk.AddPlayer(new Player("Player3"));
desk.AddPlayer(new FakePlayer());
desk.AddPlayer(new FakePlayer());
desk.AddPlayer(new FakePlayer());

//Task.Run(() => { ShowMessage(desk); });

//ParseMessage(desk);

desk.ParseCommand(desk.CurrentPlayer.PlayerId, "开始游戏");
desk.ParseCommand(desk.CurrentPlayer.PlayerId, "抢地主");
desk.ParseCommand(desk.CurrentPlayer.PlayerId, "托管");
desk.ParseCommand(desk.CurrentPlayer.PlayerId, "托管");
desk.ParseCommand(desk.CurrentPlayer.PlayerId, "托管");
desk.Start();

Console.WriteLine($"Test successful: {count} / 20000, used {sw.ElapsedMilliseconds}ms");
//Console.WriteLine(desk.Message);
Expand Down
2 changes: 1 addition & 1 deletion CardSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Global
{90D28A68-F43B-4127-8166-3F54FAC05C6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90D28A68-F43B-4127-8166-3F54FAC05C6A}.Release|Any CPU.Build.0 = Release|Any CPU
{0AF6659C-8F03-467D-A470-E0B130D12C6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AF6659C-8F03-467D-A470-E0B130D12C6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AF6659C-8F03-467D-A470-E0B130D12C6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E88658D4-E429-4D42-9427-7CFD25B2905B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E88658D4-E429-4D42-9427-7CFD25B2905B}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -31,6 +30,7 @@ Global
{48AFBFAC-C4C5-4ECD-A361-76F82A7B16EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48AFBFAC-C4C5-4ECD-A361-76F82A7B16EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48AFBFAC-C4C5-4ECD-A361-76F82A7B16EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48AFBFAC-C4C5-4ECD-A361-76F82A7B16EC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions CardSharp/CardSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="GameComponents\CardType.cs" />
<Compile Include="Constants.cs" />
<Compile Include="GameComponents\Desk.cs" />
<Compile Include="GameComponents\FakePlayer.cs" />
<Compile Include="GameComponents\IDesk.cs" />
<Compile Include="GameComponents\IMessageSender.cs" />
<Compile Include="GameComponents\IPlayerConfig.cs" />
Expand Down
4 changes: 3 additions & 1 deletion CardSharp/GameComponents/Desk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ private void SendCards()
public void SetLandlord(Player player)
{
player.Type = PlayerType.Landlord;
_currentParser = new CommandParser(this);
var parser = new CommandParser(this);
_currentParser = parser;
parser.Prepare(this);
}

public void BoardcastCards()
Expand Down
27 changes: 27 additions & 0 deletions CardSharp/GameComponents/FakePlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CardSharp.GameComponents
{
public class FakePlayer : Player
{
public FakePlayer() : base(GenerateName())
{
HostedEnabled = true;
}

private static readonly Random Rng = new Random("fork you kamijoutoma".GetHashCode());
private static string GenerateName()
{
return $"机器人{Rng.Next(100)}";
}

public override string ToAtCode()
{
return $"{PlayerId}";
}
}
}
2 changes: 1 addition & 1 deletion CardSharp/GameComponents/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override bool Equals(object obj)
return !(player1 == player2);
}

public string ToAtCode()
public virtual string ToAtCode()
{
#if !DEBUG
return $"[CQ:at,qq={PlayerId}]";
Expand Down
31 changes: 19 additions & 12 deletions CardSharp/GameSteps/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ public class CommandParser : Samsara, ICommandParser
public CommandParser(Desk desk)
{
CurrentIndex = desk.PlayerList.FindIndex(p => p.Type == PlayerType.Landlord);
desk.AddMessage($"{desk.GetPlayerFromIndex(CurrentIndex).ToAtCode()}出牌");
}

public void Prepare(Desk desk)
{
desk.AddMessage($"{desk.CurrentPlayer.ToAtCode()}出牌");
RunHostedCheck(desk);
}

public void Parse(Desk desk, Player player, string command)
Expand Down Expand Up @@ -105,12 +110,6 @@ public void Parse(Desk desk, Player player, string command)
if (cardsCommand.IsValidCardString())
if (Rules.Rules.IsCardsMatch(cardsCommand.ToCards(), desk))
{
if (player.Cards.Count == 0)
{
PlayerWin(desk, player);
return;
}

player.SendCards(desk);
if (player.Cards.Count <= Constants.BoardcastCardNumThreshold)
desk.AddMessageLine($"{player.ToAtCode()} 只剩{player.Cards.Count}张牌啦~");
Expand All @@ -135,8 +134,14 @@ public void Parse(Desk desk, Player player, string command)
}
}

if (desk.CurrentPlayer.Cards.Count == 0) {
PlayerWin(desk, player);
return;
}

RunHostedCheck(desk);
if (RunHostedCheck(desk))
return;

RunAutoPassCheck(desk);
}

Expand All @@ -152,10 +157,10 @@ private void RunAutoPassCheck(Desk desk)
}
}

private void RunHostedCheck(Desk desk)
private bool RunHostedCheck(Desk desk)
{
var cp = desk.CurrentPlayer;
if (!cp.HostedEnabled) return;
if (!cp.HostedEnabled) return false;

if (desk.LastSuccessfulSender == cp)
{
Expand All @@ -175,12 +180,14 @@ private void RunHostedCheck(Desk desk)
case true:
desk.AddMessageLine($" {cp.ToAtCode()} 托管出牌 {cards.ToFormatString()}");
Parse(desk, cp, $"{string.Join("", cards.Select(card => card.ToString()))}");
return;
return true;
case false:
desk.AddMessageLine($" {cp.ToAtCode()} 托管过牌");
Parse(desk, cp, "pass");
return;
return true;
}

return true;
}

private static void PlayerWin(Desk desk, Player player)
Expand Down
8 changes: 7 additions & 1 deletion CardSharp/GameSteps/LandlordDiscuss.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using CardSharp.GameComponents;
using CardSharp.GameSteps;

namespace CardSharp
Expand All @@ -12,7 +13,12 @@ public class LandlordDiscuss : Samsara, ICommandParser
public LandlordDiscuss(IEnumerable<Card> landlordCards, Desk desk)
{
_landlordCards = landlordCards;
desk.AddMessage($"开始游戏, {desk.GetPlayerFromIndex(CurrentIndex).ToAtCode()}你要抢地主吗?[抢地主/不抢]");
var player = desk.GetPlayerFromIndex(CurrentIndex);
desk.AddMessage($"开始游戏, {player.ToAtCode()}你要抢地主吗?[抢地主/不抢]");
if (player is FakePlayer)
{
Parse(desk, player, "");
}
}

public void Parse(Desk desk, Player player, string command)
Expand Down
10 changes: 9 additions & 1 deletion CardSharp/GameSteps/WaitingParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CardSharp.GameComponents;
using System.Linq;
using CardSharp.GameComponents;

namespace CardSharp.GameSteps
{
Expand Down Expand Up @@ -26,6 +27,13 @@ public void Parse(Desk desk, Player player, string command)
else
desk.AddMessage("人数不够.");
break;
case "添加机器人":
desk.AddPlayer(new FakePlayer());
break;
case "移除机器人":
if (desk.Players.Any(p => p is FakePlayer))
desk.RemovePlayer(desk.Players.First(p=> p is FakePlayer));
break;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CardSharp/MessageSenderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void ClearMessage()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddMessageLine(string msg = "")
{
if (!Message.EndsWith(Environment.NewLine))
if (!Message?.EndsWith(Environment.NewLine) == true)
{
AddMessage(Environment.NewLine);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Linq;
using CardSharp;
using CardSharp.GameComponents;
using Newbe.Mahua;

namespace Origind.Card.Game.MahuaEvents
Expand All @@ -27,7 +28,7 @@ public void ProcessGroupMessage(GroupMessageReceivedContext context)
var message = context.Message;
var desk = Desk.GetOrCreateDesk(deskid);
desk.ParseCommand(playerid, message);
desk.PlayerList.Where(player => player.Message != null).ToList().ForEach(player =>
desk.PlayerList.Where(player => player.Message != null && !(player is FakePlayer)).ToList().ForEach(player =>
{
_mahuaApi.SendPrivateMessage(player.PlayerId, player.Message);
player.ClearMessage();
Expand Down

0 comments on commit 1a459f0

Please sign in to comment.