/// <summary> /// 玩家出牌 /// </summary> /// <param name="player">轮到出牌的玩家</param> /// <param name="token">取消标识</param> /// <returns>获取一个 Task,用于等待这个玩家处理结束</returns> protected virtual async Task PlayerPlay(GameRoundEvent roundEvnet, CardGamePlayer player, CancellationToken token) { await OnBeforePlay(roundEvnet, player, token); await player.Play(roundEvnet, token); await OnAfterPlay(roundEvnet, player, token); }
/// <summary> /// 开始一个回合 /// </summary> /// <param name="token">取消标识</param> /// <returns>获取一个 Task,用于等待这个回合游戏结束</returns> protected virtual async Task PlayAround(CancellationToken token) { AnnounceSystemMessage("第 {0} 回合", Rounds); var roundEvent = new GameRoundEvent(Rounds); foreach (CardGamePlayer player in Players) { await PlayerPlay(roundEvent, player, token); token.ThrowIfCancellationRequested(); } Rounds++; }
/// <summary> /// 玩家进行该回合的操作 /// </summary> /// <param name="token">取消标识</param> /// <returns>获取一个用于等待玩家处理完成的 Task</returns> public virtual async Task Play(GameRoundEvent roundEvent, CancellationToken token) { Game.AnnounceMessage("轮到 {0} 出牌", PlayerName); var round = new PlayerRoundEvent(roundEvent, this); await OnGameEvent(round); await OnBeforePlayCard(round, token); try { await PlayCard(round, token); } catch (TimeoutException) { await OnTimeout(); } await OnAfterPlayCard(round, token); }
/// <summary> /// 重写 PlayerPlay 方法,在每个玩家操作完毕后,检查所有玩家是否都还活着 /// </summary> /// <param name="player">正在执行操作的玩家</param> /// <param name="token">取消标识</param> /// <returns>获取一个 Task 用于等待操作完成</returns> protected override async Task PlayerPlay(GameRoundEvent roundEvent, CardGamePlayer player, CancellationToken token) { await base.PlayerPlay(roundEvent, player, token); await EnsurePlayersAlive(); }
/// <summary> /// 创建 PlayerRoundEvent 对象 /// </summary> /// <param name="player">当前出牌的玩家</param> public PlayerRoundEvent(GameRoundEvent roundEvent, CardGamePlayer player) { Player = player; RoundEvent = roundEvent; }
protected virtual Task OnAfterPlay(GameRoundEvent roundEvent, CardGamePlayer player, CancellationToken token) { return(Task.CompletedTask); }