private void PerformCharacterCreate(SubPacket subPacket) { Database db = new Database(); CharacterCreatePacket cp = new CharacterCreatePacket(subPacket.data); int error = db.AddCharacterToDb(ap.userName, cp); if (error == -1) { SubPacket success = new SubPacket(GamePacketOpCode.CreateCharacterSuccess, 0, 0, System.Text.Encoding.Unicode.GetBytes("Character created successfully"), SubPacketTypes.GamePacket); BasePacket basePacket = BasePacket.CreatePacket(success, client.authenticated, false); client.QueuePacket(basePacket); //created successfully } else { ErrorPacket ep = new ErrorPacket(); if (error == (int)ErrorCodes.DuplicateCharacter) { var packetToSend = ep.buildPacket(GamePacketOpCode.CreateCharacterError, ErrorCodes.DuplicateCharacter, "Character with that name already exists"); } if (error == (int)ErrorCodes.UnknownDatabaseError) { var packetToSend = ep.buildPacket(GamePacketOpCode.CreateCharacterError, ErrorCodes.UnknownDatabaseError, "Unknown database error occurred"); QueueErrorPacket(packetToSend); } } }
//client in this case will be WorldServer private void ProcessHandshake(SubPacket receivedPacket) { //search connected clients for address //send characterid and address from receivedPacket back to worldserver HandshakePacket received = new HandshakePacket(receivedPacket.data); foreach (var connection in LoginServer.mConnectionList) { //Console.WriteLine("id present: "+characterIdPresentInClient(received.CharacterId, connection)); //Console.WriteLine("client id: " + received.CharacterId); //Console.WriteLine("connection id: " + connection.CharacterId[0]); if (connection.GetIp() == received.ClientAddress && characterIdPresentInClient(received.CharacterId, connection)) { //TODO: Separate this into a method AcknowledgePacket ack = new AcknowledgePacket(true, received.ClientAddress, received.CharacterId); SubPacket sp = new SubPacket(GamePacketOpCode.Acknowledgement, 0, 0, ack.GetBytes(), SubPacketTypes.GamePacket); BasePacket successPacketToSend = BasePacket.CreatePacket(sp, true, false); AckResponseToWorldServer(successPacketToSend); return; } } AcknowledgePacket ackFailure = new AcknowledgePacket(true, received.ClientAddress, received.CharacterId); SubPacket fail = new SubPacket(GamePacketOpCode.Acknowledgement, 0, 0, ackFailure.GetBytes(), SubPacketTypes.GamePacket); BasePacket failPacketToSend = BasePacket.CreatePacket(fail, true, false); AckResponseToWorldServer(failPacketToSend); }
private void ProcessCharacterListQueryPacket(SubPacket receivedPacket) { Database db = new Database(); CharacterQueryPacket cq = new CharacterQueryPacket(); string accountName = cq.ReadAccountName(receivedPacket); Console.WriteLine("account name for CL: " + accountName); var accountId = db.GetAccountIdFromAccountName(accountName); Console.WriteLine("ID for CL: " + accountId); var characterList = db.GetListOfCharacters(accountId); var packets = cq.BuildResponsePacket(characterList); Console.WriteLine("Character packeted authenticated = " + client.authenticated); BasePacket packetsToSend = BasePacket.CreatePacket(packets, client.authenticated, false); Console.WriteLine("---Character Query Packet---"); for (var i = 0; i < characterList.Count; i++) { int characterId = int.Parse(characterList[i][0]); client.CharacterIds[i] = characterId; } packetsToSend.debugPrintPacket(); client.QueuePacket(packetsToSend); }
private void PerformCharacterCreate(SubPacket subPacket) { LoginDatabase db = new LoginDatabase(); CharacterCreatePacket cp = new CharacterCreatePacket(subPacket.data); try { db.AddCharacterToDb(ap.userName, cp); SubPacket success = new SubPacket(GamePacketOpCode.CreateCharacterSuccess, 0, 0, System.Text.Encoding.Unicode.GetBytes("Character created successfully"), SubPacketTypes.GamePacket); BasePacket basePacket = BasePacket.CreatePacket(success, client.authenticated, false); client.QueuePacket(basePacket); } catch (MySqlException e) { ErrorPacket ep = new ErrorPacket(); SubPacket packetToSend; if (e.Number == (int)ErrorCodes.DuplicateCharacter) { packetToSend = ep.buildPacket(GamePacketOpCode.CreateCharacterError, ErrorCodes.DuplicateCharacter, "Character with that name already exists"); } else { packetToSend = ep.buildPacket(GamePacketOpCode.CreateCharacterError, ErrorCodes.UnknownDatabaseError, "Unknown database error occurred"); } QueueErrorPacket(packetToSend); } }
private void ProcessAccountPacket(ClientConnection client, SubPacket packet) { ap = new AccountPacket(); ErrorPacket ep = new ErrorPacket(); ap.Read(packet.GetAccountHeaderBytes(), packet.data); if (!ap.register)//if account is logging in { Database db = new Database(); List <string> account = db.CheckUserInDb(ap.userName, ap.password); switch (account.Count) { case 0: var packetToSend = ep.buildPacket(GamePacketOpCode.AccountError, ErrorCodes.NoAccount, "Account does not exist"); Console.WriteLine("Attempted log in for username: {0} pw: {1}, account does not exist", ap.userName, ap.password); QueueErrorPacket(packetToSend); break; case 1: //password incorrect packetToSend = ep.buildPacket(GamePacketOpCode.AccountError, ErrorCodes.WrongPassword, "Wrong username or password"); Console.WriteLine("Attempted log in for username: {0} pw: {1}, password incorrect", ap.userName, ap.password); QueueErrorPacket(packetToSend); break; case 2: //user and password found Console.WriteLine("Username: {0} Password: {1} has logged in successfully", account[0], account[1]); SubPacket success = new SubPacket(GamePacketOpCode.AccountSuccess, 0, 0, System.Text.Encoding.Unicode.GetBytes("Login Successful"), SubPacketTypes.GamePacket); client.authenticated = true; BasePacket basePacket = BasePacket.CreatePacket(success, client.authenticated, false); client.QueuePacket(basePacket); break; default: throw new Exception("somehow found more than 2 colums in DB"); } } else //account is registering { Database db = new Database(); var succeeded = db.AddUserToDb(ap.userName, ap.password); if (succeeded) { Console.WriteLine("Username: {0} Password: {1} has been registered successfully", ap.userName, ap.password); SubPacket success = new SubPacket(GamePacketOpCode.RegisterSuccess, 0, 0, System.Text.Encoding.Unicode.GetBytes("Registration Successful"), SubPacketTypes.GamePacket); BasePacket basePacket = BasePacket.CreatePacket(success, false, false); client.QueuePacket(basePacket); } else { var packetToSend = ep.buildPacket(GamePacketOpCode.AccountError, ErrorCodes.DuplicateAccount, "Account already registered"); QueueErrorPacket(packetToSend); } } }
private void ProcessCharacterDeleteRequest(SubPacket receivedPacket) { Database db = new Database(); CharacterDeletePacket deletePacket = new CharacterDeletePacket(receivedPacket); int error = db.DeleteCharacterFromDb(deletePacket.CharId); if (error == -1) { SubPacket success = new SubPacket(GamePacketOpCode.CharacterDeleteSuccess, 0, 0, System.Text.Encoding.Unicode.GetBytes("Character deleted successfully"), SubPacketTypes.GamePacket); BasePacket basePacket = BasePacket.CreatePacket(success, client.authenticated, false); client.QueuePacket(basePacket); } else { //send error packet here } }
private void QueueErrorPacket(SubPacket subPacket) { BasePacket errorBasePacket = BasePacket.CreatePacket(subPacket, client.authenticated, false); client.QueuePacket(errorBasePacket); }