Ejemplo n.º 1
0
        public object Status(HttpRequestMessage request, string messageMode = null)
        {
            int amount;

            if (messageMode == "all")
            {
                PlayerHost.SetMessageIndex(0);
            }

            else if (int.TryParse(messageMode, out amount))
            {
                PlayerHost.GetMessages();
                PlayerHost.SetMessageIndex(Math.Max(PlayerHost.LastMesageIndex - amount, 0));
            }


            return(new
            {
                Gaming = PlayerHost.Gaming,
                RespondingUrl = PlayerHost.Responding == null ? null : "Responding/" + PlayerHost.Responding.RespondingID,

                GameInformation = GetGameInformation(),
                Messages = PlayerHost.GetMessages(),
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建 Responding 对象
        /// </summary>
        /// <param name="playerHost">玩家宿主</param>
        /// <param name="promptText">响应提示文字</param>
        /// <param name="token">取消标识</param>
        protected Responding(PlayerHost playerHost, string promptText, CancellationToken token)
        {
            RespondingID = Guid.NewGuid();

            lock (playerHost.SyncRoot)
            {
                if (playerHost.Responding != null)
                {
                    if (playerHost.Responding.IsCompleted)
                    {
                        playerHost.Responding = null;
                    }

                    else
                    {
                        throw new InvalidOperationException();
                    }
                }


                PlayerHost           = playerHost;
                PromptText           = promptText;
                TaskCompletionSource = new TaskCompletionSource <T>();

                token.Register(OnCancelled);
                playerHost.Responding = this;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 创建游戏房间
        /// </summary>
        /// <param name="name">房间名称</param>
        /// <param name="privateRoom">是否为私有房间</param>
        /// <param name="owner">房间所有人</param>
        /// <returns></returns>
        public static async Task <GameRoom> CreateGame(PlayerHost owner, string name, bool privateRoom)
        {
            if (nameRegex.IsMatch(name) == false)
            {
                throw new ArgumentException("游戏名称必须由10个英文字母或者5个汉字组成", "name");
            }


            GameRoom room;

            lock ( _sync )
            {
                if (_games.Contains(name))
                {
                    throw new InvalidOperationException("游戏房间已经存在");
                }


                room = GameRoom.Create(name, privateRoom);
                _games.Add(room);
            }

            try
            {
                await room.Initialize(owner);
            }
            catch
            {
                _games.Remove(room);
                throw;
            }

            return(room);
        }
Ejemplo n.º 4
0
 public object Exit()
 {
     lock (PlayerHost.SyncRoot)
     {
         PlayerHost.Release();
         return(new HttpResponseMessage(HttpStatusCode.OK));
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 创建一个新的玩家宿主
 /// </summary>
 /// <returns>新的玩家宿主</returns>
 public static PlayerHost CreatePlayerHost()
 {
     lock ( sync )
     {
         var instance = new PlayerHost(PlayerNameManager.CreateName());
         players.Add(instance);
         instance.ShowInitializeInfo();
         return(instance);
     }
 }
Ejemplo n.º 6
0
        public object Name(string name)
        {
            if (PlayerHost.TrySetName(name))
            {
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }

            else
            {
                return(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 初始化游戏房间
        /// </summary>
        /// <param name="initializer">游戏创建者</param>
        internal async Task Initialize(PlayerHost initializer)
        {
            try
            {
                var options = GameManager.RegisteredGames.Select(item => Option.Create(item, item.GameName, item.GameDescription)).ToArray();
                if (options.Any() == false)
                {
                    initializer.WriteSystemMessage("当前没有已经注册的游戏,无法创建房间");
                    throw new InvalidOperationException();
                }

                var factory = await initializer.Console.Choose("请选择游戏类型:", options, CancellationToken.None);
                await InitializeGame(factory.CreateGame(), initializer);
            }
            catch
            {
                GameRoomsManager.ReleaseRoom(this);
            }
        }
Ejemplo n.º 8
0
        public object Create(string name, bool @private = false)
        {
            lock (PlayerHost.SyncRoot)
            {
                CheckGameming();

                Task.Run(async() =>
                {
                    try
                    {
                        await GameRoomsManager.CreateGame(PlayerHost, name, @private);
                    }
                    catch (Exception e)
                    {
                        PlayerHost.WriteMessage(GameMessage.Error(e.ToString()));
                    }
                });
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
        }
Ejemplo n.º 9
0
        public async Task <object> PostResponding(Guid id)
        {
            var responding = PlayerHost.Responding;

            if (responding == null)
            {
                return(NotFound());
            }

            if (Request.Content.Headers.ContentType.MediaType != "text/responding")
            {
                return(BadRequest());
            }


            var message = await Request.Content.ReadAsStringAsync();

            PlayerHost.OnResponse(message);

            return("OK");
        }
Ejemplo n.º 10
0
 public OptionsResponding(PlayerHost playerHost, string promptText, Option[] options, CancellationToken token)
     : base(playerHost, promptText, token)
 {
     Options = options;
 }
Ejemplo n.º 11
0
 public TextMessageResponding(PlayerHost playerHost, string promptText, CancellationToken token) : base(playerHost, promptText, token)
 {
 }
Ejemplo n.º 12
0
 public static void Release(PlayerHost playerHost)
 {
     players.Remove(playerHost);
     PlayerNameManager.RemoveName(playerHost.Name);
 }
Ejemplo n.º 13
0
 public PlayerConsole(PlayerHost playerHost)
     : base(playerHost)
 {
     PlayerHost = playerHost;
     SyncRoot   = new object();
 }
Ejemplo n.º 14
0
 public object QuitGame()
 {
     PlayerHost.QuitGame();
     return(new HttpResponseMessage(HttpStatusCode.OK));
 }