예제 #1
0
        /// <summary>
        /// >マッチング時に与えられたルーム名をもとにゲーム準備を宣言
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task GameReadyAsync(GameReadyRequest request)
        {
            IsConnected = true;
            MineId      = request.MineId;
            EnemyId     = request.EnemyId;
            WithOther   = request.WithOther;

            // ゲームループ作成
            ServerMainGameLoop.LoopData data = new ServerMainGameLoop.LoopData(MineId, EnemyId, this, NetworkInput);
            ServerMainGameLoop.CreateNew(looperPool, logger, data);

            // ゲーム部屋に放り込む
            gameRoom = await Group.AddAsync(request.RoomName);

            if (WithOther)
            {   // 2P
                var memberNum = await gameRoom.GetMemberCountAsync();

                if (memberNum == 2)
                {   // 2人揃ったらゲームを開始してよい
                    logger.LogInformation($"GameReady at {MineId}");
                    var param = new Dictionary <string, IReadOnlyList <short> >();
                    // 互いの情報取得
                    var mineLoop = ServerMainGameLoop.TryTakeGame(MineId);
                    var enemLoop = ServerMainGameLoop.TryTakeGame(EnemyId);
                    param[MineId]  = mineLoop.PlayingState.Board;
                    param[EnemyId] = enemLoop.PlayingState.Board;

                    var res = new GameReadyResponse {
                        RoomName = request.RoomName, PlayerBoards = param
                    };
                    Broadcast(gameRoom).OnGameReady(res);
                }
            }
            else
            {   // 1P / 速攻でスタート
                logger.LogInformation($"GameReady at {MineId}");
                var loop  = ServerMainGameLoop.TryTakeGame(request.MineId);
                var param = new Dictionary <string, IReadOnlyList <short> >();
                param[MineId] = loop.PlayingState.Board;
                BroadcastToSelf(gameRoom).OnGameReady(new GameReadyResponse {
                    RoomName = request.RoomName, PlayerBoards = param
                });
            }
        }
        /// <summary>
        /// 接続・切断
        /// </summary>
        private async void toGameReadyAsync()
        {
            if (!isJoin)
            {   // 接続開始
                var request = new GameReadyRequest {
                    MineId = loopParam.MineId, EnemyId = loopParam.EnemyId, RoomName = loopParam.RoomName, WithOther = loopParam.WithOther
                };
                await hub.ServerImpl.GameReadyAsync(request);

                isJoin = true;
            }
            else
            {   // 切断
                await hub.ServerImpl.LeaveAsync();

                // UI初期化
                isJoin = false;
            }
        }