Ejemplo n.º 1
0
    public void Login(DataPacket packet)
    {
        Console.WriteLine(packet.client.RemoteEndPoint.ToString() + " 로그인");

        AccountPacket accountPacket = new AccountPacket(packet.msg);
        AccountData   accountData   = accountPacket.GetData();

        Console.WriteLine("아이디 : " + accountData.Id + "비밀번호 : " + accountData.Password);

        Result result = Result.Fail;

        try
        {
            if (database.AccountData.Contains(accountData.Id))
            {
                if (((AccountData)database.AccountData[accountData.Id]).Password == accountData.Password)
                {
                    List <string> id = new List <string>(loginUser.Values);


                    if (!loginUser.ContainsValue(accountData.Id))
                    {
                        result = Result.Success;
                        Console.WriteLine("로그인 성공");
                        loginUser.Add(packet.client, accountData.Id);
                        userState.Add(accountData.Id, new UserState(accountData.Id, -1));

                        database.AddUserData(accountData.Id);
                    }
                    else
                    {
                        Console.WriteLine("현재 접속중인 아이디입니다.");

                        if (CompareIP(GetSocket(accountData.Id).RemoteEndPoint.ToString(), packet.client.RemoteEndPoint.ToString()))
                        {
                            loginUser.Remove(GetSocket(accountData.Id));

                            try
                            {
                                if (userState.ContainsKey(accountData.Id))
                                {
                                    if (userState[accountData.Id].state >= 0)
                                    {
                                        roomManager.ExitRoom(userState[accountData.Id].state, packet.client);
                                    }

                                    userState.Remove(accountData.Id);
                                }
                            }
                            catch
                            {
                                Console.WriteLine("DataHandler::GameClose.ContainsKey - roomManager 에러");
                                Console.WriteLine("방에 입장하지 않았습니다.");
                            }

                            database.DeleteUserData(accountData.Id);

                            Console.WriteLine("현재 접속중 해제");
                        }

                        result = Result.Fail;
                    }
                }
                else
                {
                    Console.WriteLine("패스워드가 맞지 않습니다.");
                    result = Result.Fail;
                }
            }
            else
            {
                Console.WriteLine("존재하지 않는 아이디입니다.");
                result = Result.Fail;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("DataHandler::Login.ContainsValue 에러" + e.Message);
            result = Result.Fail;
        }

        ResultData   resultData       = new ResultData((byte)result);
        ResultPacket resultDataPacket = new ResultPacket(resultData);

        resultDataPacket.SetPacketId((int)ServerPacketId.LoginResult);

        byte[] msg = CreatePacket(resultDataPacket);
        packet = new DataPacket(msg, packet.client);

        lock (sendLock)
        {
            sendMsgs.Enqueue(packet);
        }
    }