예제 #1
0
        public object Join(string name)
        {
            lock (PlayerHost.SyncRoot)
            {
                CheckGameming();

                var game = GameRoomsManager.GetGame(name);
                if (game == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.NotFound));
                }

                game.JoinGame(PlayerHost);
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
        }
예제 #2
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);
            }
        }
예제 #3
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));
            }
        }
예제 #4
0
 /// <summary>
 /// 释放游戏
 /// </summary>
 /// <param name="game"></param>
 public override void ReleaseGame(GameBase game)
 {
     base.ReleaseGame(game);
     GameRoomsManager.ReleaseRoom(this);
 }
예제 #5
0
        public object List()
        {
            var games = GameRoomsManager.PublicGames();

            return(games.Select(item => new { Name = item.RoomName, State = item.GameHostState, Players = item.Game.Players.Select(player => player.PlayerName) }));
        }