예제 #1
0
        /// <summary>
        /// 广播一个游戏事件
        /// </summary>
        /// <param name="gameEvent">要广播的游戏事件</param>
        /// <returns>用于等待事件广播完毕的 Task 对象</returns>
        public virtual async Task SendGameEvent(IGameEvent gameEvent)
        {
            var parallelEvent = gameEvent as IParallelGameEvent;


            if (parallelEvent != null)
            {
                //并行广播游戏事件
                var tasks = Players.Cast <CardGamePlayer>().Select(item => item.OnGameEvent(parallelEvent)).ToArray();
                await Task.WhenAll(tasks);
            }
            else
            {
                foreach (var player in Players.Cast <CardGamePlayer>())
                {
                    await player.OnGameEvent(gameEvent);
                }
            }

            var handledEvent = gameEvent as IGameNeedHandledEvent;

            if (handledEvent != null)
            {
                await handledEvent.HandleEvent();
            }
        }