public void Send(OperationRequest operationRequest)
 {
     try
     {
         byte[] data = Encoding.Default.GetBytes(JsonConvert.SerializeObject(operationRequest, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto })+ "XXXXXXXX");
         tcpClient.GetStream().Write(data, 0, data.Length);
     }
     catch (Exception ex)
     {
         peerService.DebugReturn(DebugLevel.Error, ex.Message);
         peerService.DebugReturn(DebugLevel.Error, ex.StackTrace);
     }
 }
 private void CancleReadyTask(OperationRequest operationRequest)
 {
     user.ready = false;
     OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.Correct,
                 "",
                 new Dictionary<byte, object>()
             );
     SendResponse(response);
     RoomUpdateBroadcast(user.userGroup as Room);
 }
        private void CreateRoomTask(OperationRequest operationRequest)
        {
            if (operationRequest.Parameters.Count != 3)
            {
                OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.InvalidParameter,
                        "CreateRoomTask Parameter Error",
                        new Dictionary<byte, object>()
                    );
                SendResponse(response);
            }
            else
            {
                string roomName = (string)operationRequest.Parameters[(byte)CreateRoomParameterItem.RoomName];
                bool isEncrypted = (bool)operationRequest.Parameters[(byte)CreateRoomParameterItem.IsEncrypted];
                string password = (string)operationRequest.Parameters[(byte)CreateRoomParameterItem.Password];
                Room room;

                if (CreateRoom(roomName, isEncrypted, password, out room))
                {
                    Dictionary<byte, object> parameter = new Dictionary<byte, object>
                    {
                        { (byte)CreateRoomResponseItem.RoomDataString, JsonConvert.SerializeObject(room.Serialize(), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }) }
                    };
                    OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.Correct,
                        "",
                        parameter
                    );
                    SendResponse(response);
                }
                else
                {
                    OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.InvalidOperation,
                        "建立房間失敗",
                        new Dictionary<byte, object>()
                    );
                    SendResponse(response);
                }
            }
        }
 private void BuyLandTask(OperationRequest operationRequest)
 {
     (user.playingGame.map.blocks[user.player.token.position] as LandBlock).land.Buy(user.player);
     if(user.player.money >= 0)
     {
         Dictionary<byte, object> parameter = new Dictionary<byte, object>
         {
             {(byte)BuyLandBroadcastParameterItem.PlayerName, user.player.username},
             {(byte)BuyLandBroadcastParameterItem.LandName, (user.playingGame.map.blocks[user.player.token.position] as LandBlock).land.name}
         };
         List<Peer> peers = new List<Peer>();
         foreach (ServerUser targetUser in (user.playingGame as ServerGame).users)
         {
             peers.Add(targetUser.Peer);
         }
         server.Broadcast(peers.ToArray(), BroadcastType.BuyLand, parameter);
     }
     if(user.playingGame != null)
         GameUpdateBroadcast(user.playingGame);
 }
示例#5
0
        protected override void OnOperationRequest(OperationRequest operationRequest)
        {
            server.logger.Info(guid.ToString() + " : " + (OperationType)operationRequest.OperationCode);
            switch(operationRequest.OperationCode)
            {
                #region login
                case (byte)OperationType.Login:
                    {
                        LoginTask(operationRequest);
                    }
                    break;
                #endregion

                #region get lobby data
                case (byte)OperationType.GetLobbyData:
                    {
                        GetLobbyDataTask(operationRequest);
                    }
                    break;
                #endregion

                #region send message
                case (byte)OperationType.SendMessage:
                    {
                        SendMessageTask(operationRequest);
                    }
                    break;
                #endregion

                #region create room
                case (byte)OperationType.CreateRoom:
                    {
                        CreateRoomTask(operationRequest);
                    }
                    break;
                #endregion

                #region join room
                case (byte)OperationType.JoinRoom:
                    {
                        JoinRoomTask(operationRequest);
                    }
                    break;
                #endregion

                #region ready for game
                case (byte)OperationType.ReadyForGame:
                    {
                        ReadyForGameTask(operationRequest);
                    }
                    break;
                #endregion

                #region cancle ready
                case (byte)OperationType.CancleReady:
                    {
                        CancleReadyTask(operationRequest);
                    }
                    break;
                #endregion

                #region start game
                case (byte)OperationType.StartGame:
                    {
                        StartGameTask(operationRequest);
                    }
                    break;
                #endregion

                #region exit game
                case (byte)OperationType.ExitRoom:
                    {
                        ExitRoomTask(operationRequest);
                    }
                    break;
                #endregion

                #region get game data
                case (byte)OperationType.GetGameData:
                    {
                        GetGameDataTask(operationRequest);
                    }
                    break;
                #endregion

                #region roll dice
                case (byte)OperationType.RollDice:
                    {
                        RollDiceTask(operationRequest);
                    }
                    break;
                #endregion

                #region buy land
                case (byte)OperationType.BuyLand:
                    {
                        BuyLandTask(operationRequest);
                    }
                    break;
                #endregion

                #region upgrade land
                case (byte)OperationType.UpgradeLand:
                    {
                        UpgradeLandTask(operationRequest);
                    }
                    break;
                #endregion

                //#region get player data
                //case (byte)OperationType.GetPlayerData:
                //    {
                //        GetPlayerDataTask(operationRequest);
                //    }
                //    break;
                //#endregion

                #region give up
                case (byte)OperationType.GiveUp:
                    {
                        GiveUpTask(operationRequest);
                    }
                    break;
                #endregion

                #region end turn
                case (byte)OperationType.EndTurn:
                    {
                        EndTurnTask(operationRequest);
                    }
                    break;
                #endregion

                case (byte)OperationType.LogOut:
                    {
                        LogOutTask(operationRequest);
                    }
                    break;
            }
        }
 private void StartGameTask(OperationRequest operationRequest)
 {
     if(/*!(user.userGroup as Room).users.Any(x=>x.Value.ready) && */server.CreateGame(user.userGroup.users.Values.ToList()))
     {
         OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.Correct,
                 "",
                 new Dictionary<byte, object>()
             );
         SendResponse(response);
     }
     else
     {
         OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.PermissionDeny,
                 "開始遊戲錯誤",
                 new Dictionary<byte, object>()
             );
         SendResponse(response);
     }
 }
        private void SendMessageTask(OperationRequest operationRequest)
        {
            if (operationRequest.Parameters.Count != 1)
            {
                OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.InvalidParameter,
                        "SendMessageTask Parameter Error",
                        new Dictionary<byte, object>()
                    );
                SendResponse(response);
            }
            else
            {
                string message = (string)operationRequest.Parameters[(byte)SendMessageParameterItem.Message];

                if (SendMessageBroadcast(message))
                {
                    OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.Correct,
                        "",
                        new Dictionary<byte, object>()
                    );
                    SendResponse(response);
                }
                else
                {
                    OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.NotExist,
                        "Send target not exist",
                        new Dictionary<byte, object>()
                    );
                    SendResponse(response);
                }
            }
        }
 private void RollDiceTask(OperationRequest operationRequest)
 {
     int result = user.playingGame.RollDice();
     OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.Correct,
                 "",
                 new Dictionary<byte, object>()
             );
     SendResponse(response);
     Dictionary<byte, object> parameter = new Dictionary<byte, object>
     {
         {(byte)RollDiceResultParameterItem.DiceNumber, result}
     };
     List<Peer> peers = new List<Peer>();
     foreach (ServerUser targetUser in (user.playingGame as ServerGame).users)
     {
         peers.Add(targetUser.Peer);
     }
     server.Broadcast(peers.ToArray(), BroadcastType.RollDiceResult, parameter);
     user.playingGame.players[user.playingGame.turnCounter% user.playingGame.players.Count].Move(result);
     GameUpdateBroadcast(user.playingGame);
 }
 private void ExitRoomTask(OperationRequest operationRequest)
 {
     ExitRoom();
     OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.Correct,
                 "",
                 new Dictionary<byte, object>()
             );
     SendResponse(response);
 }
 private void LoginTask(OperationRequest operationRequest)
 {
     if (operationRequest.Parameters.Count != 1)
     {
         OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.InvalidParameter,
                 "LoginTask Parameter Error",
                 new Dictionary<byte, object>()
             );
         server.logger.Info(string.Format("{0} LoginTask Parameter Error", guid));
         SendResponse(response);
     }
     else
     {
         string userName = (string)operationRequest.Parameters[(byte)LoginParameterItem.UserName];
         if(server.UserOnline(user = new ServerUser(userName, false, this)))
         {
             LobbyUpdateBroadcast(server.lobby);
             Dictionary<byte, object> parameter = new Dictionary<byte, object>
             {
                 { (byte)LoginResponseItem.UserName, user.userName }
             };
             OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.Correct,
                 "",
                 parameter
             );
             server.logger.Info(string.Format("{0} 登入成功", user.userName));
             SendResponse(response);
         }
         else
         {
             OperationResponse response = new OperationResponse
                 (
                     operationRequest.OperationCode,
                     (byte)ReturnCode.InvalidOperation,
                     "此帳號已經登入!",
                     new Dictionary<byte, object>()
                 );
             server.logger.Info(string.Format("{0} 此帳號已經登入", guid));
             SendResponse(response);
         }
     }
 }
        private void JoinRoomTask(OperationRequest operationRequest)
        {
            if (operationRequest.Parameters.Count != 2)
            {
                OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.InvalidParameter,
                        "JoinRoomTask Parameter Error",
                        new Dictionary<byte, object>()
                    );
                SendResponse(response);
            }
            else
            {
                int roomID = (int)(long)operationRequest.Parameters[(byte)JoinRoomParameterItem.RoomID];
                string password = (string)operationRequest.Parameters[(byte)JoinRoomParameterItem.Password];
                Room room;

                if (JoinRoom(roomID, password, out room))
                {
                    Dictionary<byte, object> parameter = new Dictionary<byte, object>
                    {
                        { (byte)JoinRoomResponseItem.RoomDataString, JsonConvert.SerializeObject(room?.Serialize(), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }) }
                    };
                    OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.Correct,
                        "",
                        parameter
                    );
                    SendResponse(response);
                }
                else
                {
                    OperationResponse response = new OperationResponse
                    (
                        operationRequest.OperationCode,
                        (byte)ReturnCode.InvalidOperation,
                        "加入房間失敗",
                        new Dictionary<byte, object>()
                    );
                    SendResponse(response);
                }
            }
        }
 private void GiveUpTask(OperationRequest operationRequest)
 {
 }
 private void GetLobbyDataTask(OperationRequest operationRequest)
 {
     Dictionary<byte, object> parameter = new Dictionary<byte, object>
     {
         { (byte)GetLobbyDataResponseItem.LobbyDataString, JsonConvert.SerializeObject(server.lobby.Serialize(), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }) }
     };
     OperationResponse response = new OperationResponse
                 (
                     operationRequest.OperationCode,
                     (byte)ReturnCode.Correct,
                     "",
                     parameter
                 );
     server.logger.Info(string.Format("{0} 取得了大廳資料", user.userName));
     SendResponse(response);
 }
 private void GetGameDataTask(OperationRequest operationRequest)
 {
 }
 private void LogOutTask(OperationRequest operationRequest)
 {
     server.UserOffline(user);
     LobbyUpdateBroadcast(server.lobby);
     OperationResponse response = new OperationResponse
            (
                operationRequest.OperationCode,
                (byte)ReturnCode.Correct,
                "",
                new Dictionary<byte, object>()
            );
     server.logger.Info(string.Format("{0} 登出成功", user.userName));
     SendResponse(response);
 }
示例#16
0
 protected abstract void OnOperationRequest(OperationRequest operationRequest);
 private void EndTurnTask(OperationRequest operationRequest)
 {
     user.playingGame.EndTurn();
     GameUpdateBroadcast(user.playingGame);
 }