private void GetLobbyDataResponseTask(OperationResponse operationResponse)
 {
     if (operationResponse.ReturnCode == (byte)ReturnCode.Correct)
     {
         string lobbyDataString = (string)operationResponse.Parameters[(byte)GetLobbyDataResponseItem.LobbyDataString];
         GameGlobal.lobby = JsonConvert.DeserializeObject<Lobby>(lobbyDataString, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
         if (OnGetLobbyData != null)
             OnGetLobbyData(GameGlobal.lobby);
     }
 }
 private void LogOutResponseTask(OperationResponse operationResponse)
 {
     GameGlobal.LoginStatus = false;
     GameGlobal.userName = "";
     GameGlobal.lobby = null;
     GameGlobal.room = null;
     GameGlobal.playingGame = null;
     if (OnLogOut != null)
         OnLogOut();
 }
 internal void SendResponse(OperationResponse operationResponse)
 {
     try
     {
         byte[] data = Encoding.Default.GetBytes(JsonConvert.SerializeObject(new CommunicationParameter((byte)ParamaterType.OperationResponse, operationResponse), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto })+ "XXXXXXXX");
         tcpClient.GetStream().Write(data, 0, data.Length);
     }
     catch (Exception ex)
     {
         server.logger.Error(guid.ToString() + " : " + ex.Message);
         server.logger.Error(guid.ToString() + " : " + 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 JoinRoomResponseTask(OperationResponse operationResponse)
 {
     if (operationResponse.ReturnCode == (byte)ReturnCode.Correct)
     {
         string roomDataString = (string)operationResponse.Parameters[(byte)JoinRoomResponseItem.RoomDataString];
         GameGlobal.room = JsonConvert.DeserializeObject<Room>(roomDataString, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
         if (OnJoinRoom != null)
             OnJoinRoom(true);
     }
     else
     {
         if(OnAlert != null)
             OnAlert(operationResponse.DebugMessage);
     }
 }
        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 LoginResponseTask(OperationResponse operationResponse)
 {
     if (operationResponse.ReturnCode == (byte)ReturnCode.Correct)
     {
         GameGlobal.LoginStatus = true;
         GameGlobal.userName = (string)operationResponse.Parameters[(byte)LoginResponseItem.UserName];
         if (OnLoginResponse != null)
             OnLoginResponse(true);
     }
     else
     {
         GameGlobal.LoginStatus = false;
         if (OnAlert != null)
             OnAlert(operationResponse.DebugMessage);
         if (OnLoginResponse != null)
             OnLoginResponse(false);
     }
 }
 private void GiveUpResponseTask(OperationResponse operationResponse)
 {
 }
        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 ExitGameResponseTask(OperationResponse operationResponse)
 {
 }
 private void SendMessageResponseTask(OperationResponse operationResponse)
 {
 }
 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 RollDiceResponseTask(OperationResponse operationResponse)
 {
     if (operationResponse.ReturnCode == (byte)ReturnCode.Correct)
     {
     }
     else
     {
         if (OnAlert != null)
             OnAlert(operationResponse.DebugMessage);
     }
 }
 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 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);
 }
 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);
         }
     }
 }
 public void OnOperationResponse(OperationResponse operationResponse)
 {
     DebugReturn(DebugLevel.Info, "operationResponse : " + ((OperationType)operationResponse.OperationCode).ToString());
     switch(operationResponse.OperationCode)
     {
         case (byte)OperationType.Login:
             LoginResponseTask(operationResponse);
             break;
         case (byte)OperationType.GetLobbyData:
             GetLobbyDataResponseTask(operationResponse);
             break;
         case (byte)OperationType.JoinRoom:
             JoinRoomResponseTask(operationResponse);
             break;
         case (byte)OperationType.CreateRoom:
             CreateRoomResponseTask(operationResponse);
             break;
         case (byte)OperationType.RollDice:
             RollDiceResponseTask(operationResponse);
             break;
         case (byte)OperationType.LogOut:
             LogOutResponseTask(operationResponse);
             break;
     }
 }
 private void ExitRoomTask(OperationRequest operationRequest)
 {
     ExitRoom();
     OperationResponse response = new OperationResponse
             (
                 operationRequest.OperationCode,
                 (byte)ReturnCode.Correct,
                 "",
                 new Dictionary<byte, object>()
             );
     SendResponse(response);
 }