Esempio n. 1
0
        public void SendGameData(UserClient player, int playerId, MsgType messageType, bool messageState, GameDataType gameDataType, string gameDataObject = "")
        {
            var gameServerStream = new GameServerStream()
            {
                MessageType  = messageType,
                MessageState = messageState,
                Message      = JsonConvert.SerializeObject(new GameData()
                {
                    GameId         = GameId,
                    PlayerId       = playerId,
                    GameDataType   = gameDataType,
                    GameDataObject = gameDataObject
                })
            };

            player.responseQueue.Enqueue(gameServerStream);

            player.Visitors.ForEach(p => {
                p.responseQueue.Enqueue(gameServerStream);
            });

            if (true)
            {
                var gameLogPath = UserById(playerId) == Player1 ? _player1GameLogName : _player2GameLogName;

                using (StreamWriter outputFile = new StreamWriter(Path.Combine(Environment.CurrentDirectory, gameLogPath), true))
                {
                    outputFile.WriteLine(JsonConvert.SerializeObject(gameServerStream));
                }
            }
        }
Esempio n. 2
0
        public MatchGameService(GameServerServiceImpl gameServerService, int index, UserClient player1, UserClient player2)
        {
            _gameServerService = gameServerService;
            _random            = new Random();
            GameId             = index;

            Player1             = player1;
            Player1.GameId      = GameId;
            Player1.PlayerId    = 1;
            Player1.PlayerState = PlayerState.None;

            Player2             = player2;
            Player2.GameId      = GameId;
            Player2.PlayerId    = 2;
            Player2.PlayerState = PlayerState.None;

            _id    = 2;
            _token = $"matchgame{GameId}";

            _game = null;
        }
        public override Task <AuthReply> Authentication(AuthRequest request, ServerCallContext context)
        {
            // invalid accountname
            if (request.AccountName == null || request.AccountName.Length < 3)
            {
                Log.Warn($"{request.AccountName} is invalid!");
                return(Task.FromResult(new AuthReply()
                {
                    RequestState = false
                }));
            }

            var user = _registredUsers.Values.ToList().Find(p => p.AccountName == request.AccountName);

            // already authentificated accounts
            if (user != null)
            {
                if (user.Peer.Equals(context.Peer))
                {
                    Log.Warn($"{request.AccountName} is already registred, with the same peer!");
                    return(Task.FromResult(new AuthReply {
                        RequestState = false
                    }));
                }

                // TODO same account with a new connection
                Log.Warn($"{request.AccountName} is already registred, with a different peer!");
                return(Task.FromResult(new AuthReply {
                    RequestState = false
                }));
            }

            var sessionId = NextSessionIndex;
            var userInfo  = new UserClient
            {
                Peer        = context.Peer,
                Token       = Helper.ComputeSha256Hash(sessionId + request.AccountName + context.Peer),
                SessionId   = sessionId,
                AccountName = request.AccountName,
                UserState   = UserState.Connected,
                GameId      = -1,
                DeckData    = string.Empty,
                PlayerState = PlayerState.None,
                PlayerId    = -1
            };


            // failed registration
            if (!_registredUsers.TryAdd(userInfo.Token, userInfo))
            {
                Log.Warn($"failed to register user with account {request.AccountName}!");
                return(Task.FromResult(new AuthReply {
                    RequestState = false
                }));
            }

            Log.Info($"Successfully registred user with account {request.AccountName}!");

            var reply = new AuthReply
            {
                RequestState   = true,
                RequestMessage = string.Empty,
                SessionId      = userInfo.SessionId,
                SessionToken   = userInfo.Token
            };

            return(Task.FromResult(reply));
        }
Esempio n. 4
0
 public void SendGameData(UserClient player, MsgType messageType, bool messageState, GameDataType gameDataType, string gameDataObject = "")
 {
     SendGameData(player, player.PlayerId, messageType, messageState, gameDataType, gameDataObject);
 }
Esempio n. 5
0
        internal void ProcessGameData(GameData gameData)
        {
            UserClient userInfoData = UserById(gameData.PlayerId);

            if (userInfoData == null)
            {
                Stop();
                return;
            }

            switch (gameData.GameDataType)
            {
            case GameDataType.PowerOption:
                PowerOptionChoice powerOptionChoice = JsonConvert.DeserializeObject <PowerOptionChoice>(gameData.GameDataObject);
                PlayerTask        optionTask        = SabberStoneConverter.CreatePlayerTaskOption(_game, powerOptionChoice.PowerOption, powerOptionChoice.Target, powerOptionChoice.Position, powerOptionChoice.SubOption);

                _game.Process(optionTask);

                //if (powerOptionChoice.PowerOption.OptionType == OptionType.END_TURN)
                //{
                //    Log.Info($"State[{_game.State}]-T[{_game.Turn}] Hero1: {_game.Player1.Hero.Health} HP // Hero2: {_game.Player2.Hero.Health} HP");
                //}

                SendActionToPlayers(gameData.PlayerId, gameData.GameDataType, gameData.GameDataObject);
                SendHistoryToPlayers();

                if (_game.State == State.RUNNING)
                {
                    SendOptionsOrChoicesToPlayers();
                }
                else
                {
                    Stop();
                }
                break;

            case GameDataType.PowerChoice:
                var powerChoices = JsonConvert.DeserializeObject <PowerChoices>(gameData.GameDataObject);
                var choiceTask   = SabberStoneConverter.CreatePlayerTaskChoice(_game, gameData.PlayerId, powerChoices.ChoiceType, powerChoices.Entities);

                _game.Process(choiceTask);

                // if mulligan has been finished!
                if (_game.Step == Step.BEGIN_MULLIGAN &&
                    _game.Player1.MulliganState == Mulligan.DONE &&
                    _game.Player2.MulliganState == Mulligan.DONE)
                {
                    _game.MainBegin();
                }

                SendActionToPlayers(gameData.PlayerId, gameData.GameDataType, gameData.GameDataObject);
                SendHistoryToPlayers();

                if (_game.State == State.RUNNING)
                {
                    SendOptionsOrChoicesToPlayers();
                }
                else
                {
                    Stop();
                }
                break;

            case GameDataType.Concede:
                break;

            default:
                break;
            }
        }