コード例 #1
0
ファイル: NunitClient.cs プロジェクト: JasoonS/TotalTempo2
        private static JoinRandomGameResponse GetJoinRandomGameResponse(OperationResponse op)
        {
            var res = new JoinRandomGameResponse();

            res.GameId  = GetParameter <string>(op, Operations.ParameterCode.GameId, false);
            res.Address = GetParameter <string>(op, Operations.ParameterCode.Address, false);
            res.NodeId  = GetParameter <byte>(op, Operations.ParameterCode.NodeId, true);
            return(res);
        }
コード例 #2
0
        protected static JoinRandomGameResponse GetJoinRandomGameResponse(OperationResponse op)
        {
            var res = new JoinRandomGameResponse
            {
                GameId  = GetParameter <string>(op, ParameterCode.RoomName, false),
                Address = GetParameter <string>(op, ParameterCode.Address, false),
            };

            return(res);
        }
コード例 #3
0
        protected virtual OperationResponse HandleJoinRandomGame(MasterClientPeer peer, OperationRequest operationRequest)
        {
            // validate the operation request
            var operation = new JoinRandomGameRequest(peer.Protocol, operationRequest);
            OperationResponse response;

            if (OperationHelper.ValidateOperation(operation, log, out response) == false)
            {
                return(response);
            }

            // try to find a match
            GameState game;
            string    errorMessage;
            var       result = this.GameList.TryGetRandomGame(operation, peer, out game, out errorMessage);

            //create game
            if (result == ErrorCode.NoMatchFound && operation.CreateIfNotExists)
            {
                //random GameId if not set
                if (string.IsNullOrEmpty(operation.GameId))
                {
                    operation.GameId = Guid.NewGuid().ToString();
                    operationRequest.Parameters[(byte)ParameterKey.GameId] = operation.GameId;
                }

                //replace GameProperties (used for filter) with values for game creation
                if (operation.Properties != null)
                {
                    operationRequest.Parameters[(byte)ParameterKey.GameProperties] = operation.Properties;
                }

                //create - we try to use current HandleCreateGame implementation
                var createGameResponse = HandleCreateGame(peer, operationRequest);

                if (createGameResponse.ReturnCode != 0)
                {
                    return(new OperationResponse {
                        OperationCode = operationRequest.OperationCode, ReturnCode = createGameResponse.ReturnCode, DebugMessage = createGameResponse.DebugMessage
                    });
                }

                var joinRandomGameResponse = new JoinRandomGameResponse
                {
                    GameId  = operation.GameId,
                    Address = (string)createGameResponse.Parameters[(byte)ParameterKey.Address],
                };
                //NodeId is not used anywhere atm
                if (createGameResponse.Parameters.ContainsKey((byte)ParameterKey.NodeId))
                {
                    joinRandomGameResponse.NodeId = (byte)createGameResponse.Parameters[(byte)ParameterKey.NodeId];
                }

                return(new OperationResponse(operationRequest.OperationCode, joinRandomGameResponse)
                {
                    //TODO other ReturnCode if game was created
                    ReturnCode = (byte)ErrorCode.Ok
                });
            }

            if (result != ErrorCode.Ok)
            {
                if (string.IsNullOrEmpty(errorMessage))
                {
                    errorMessage = "No match found";
                }

                response = new OperationResponse {
                    OperationCode = operationRequest.OperationCode, ReturnCode = (short)result, DebugMessage = errorMessage
                };
                return(response);
            }

            // match found, add peer to game and notify the peer
            game.AddPeer(peer);

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Found match: connectionId={0}, userId={1}, gameId={2}", peer.ConnectionId, peer.UserId, game.Id);
            }

            this.ScheduleCheckJoinTimeOuts();

            var joinResponse = this.GetJoinRandomGameResponse(peer, game);

            return(new OperationResponse(operationRequest.OperationCode, joinResponse));
        }