Save pin to PG

This commit is contained in:
P0nk
2024-09-27 06:50:07 +02:00
parent 0f2ef341ce
commit f33d4fbc1c
5 changed files with 63 additions and 22 deletions

View File

@@ -446,14 +446,6 @@ public class Client extends ChannelInboundHandlerAdapter {
public void setPin(String pin) { public void setPin(String pin) {
this.pin = pin; this.pin = pin;
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pin = ? WHERE id = ?")) {
ps.setString(1, pin);
ps.setInt(2, accId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} }
public String getPin() { public String getPin() {

View File

@@ -55,4 +55,17 @@ public class AccountRepository {
.execute(); .execute();
} }
} }
public boolean setPin(int accountId, String pin) {
String sql = """
UPDATE account
SET pin = :pin
WHERE id = :id""";
try (Handle handle = connection.getHandle()) {
return handle.createUpdate(sql)
.bind("id", accountId)
.bind("pin", pin)
.execute() > 0;
}
}
} }

View File

@@ -291,7 +291,7 @@ public final class PacketProcessor {
registerHandler(RecvOpcode.DELETE_CHAR, new DeleteCharHandler()); registerHandler(RecvOpcode.DELETE_CHAR, new DeleteCharHandler());
registerHandler(RecvOpcode.VIEW_ALL_CHAR, new ViewAllCharHandler()); registerHandler(RecvOpcode.VIEW_ALL_CHAR, new ViewAllCharHandler());
registerHandler(RecvOpcode.PICK_ALL_CHAR, new ViewAllCharSelectedHandler()); registerHandler(RecvOpcode.PICK_ALL_CHAR, new ViewAllCharSelectedHandler());
registerHandler(RecvOpcode.REGISTER_PIN, new RegisterPinHandler()); registerHandler(RecvOpcode.REGISTER_PIN, new RegisterPinHandler(channelDeps.accountService()));
registerHandler(RecvOpcode.GUEST_LOGIN, new GuestLoginHandler()); registerHandler(RecvOpcode.GUEST_LOGIN, new GuestLoginHandler());
registerHandler(RecvOpcode.REGISTER_PIC, new RegisterPicHandler()); registerHandler(RecvOpcode.REGISTER_PIC, new RegisterPicHandler());
registerHandler(RecvOpcode.CHAR_SELECT_WITH_PIC, new CharSelectedWithPicHandler()); registerHandler(RecvOpcode.CHAR_SELECT_WITH_PIC, new CharSelectedWithPicHandler());

View File

@@ -25,27 +25,35 @@ import client.Client;
import net.AbstractPacketHandler; import net.AbstractPacketHandler;
import net.packet.InPacket; import net.packet.InPacket;
import net.server.coordinator.session.SessionCoordinator; import net.server.coordinator.session.SessionCoordinator;
import service.AccountService;
import tools.PacketCreator; import tools.PacketCreator;
/* /**
* @author Rob * @author Rob
* @author Ponk
*/ */
public final class RegisterPinHandler extends AbstractPacketHandler { public final class RegisterPinHandler extends AbstractPacketHandler {
private final AccountService accountService;
public RegisterPinHandler(final AccountService accountService) {
this.accountService = accountService;
}
@Override @Override
public final void handlePacket(InPacket p, Client c) { public void handlePacket(InPacket p, Client c) {
byte c2 = p.readByte(); boolean cancel = p.readByte() == 0;
if (c2 == 0) { if (cancel) {
SessionCoordinator.getInstance().closeSession(c, false); SessionCoordinator.getInstance().closeSession(c, false);
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN); c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
} else { return;
}
String pin = p.readString(); String pin = p.readString();
if (pin != null) { accountService.setPin(c.getAccID(), pin);
c.setPin(pin); c.setPin(pin);
c.sendPacket(PacketCreator.pinRegistered()); c.sendPacket(PacketCreator.pinRegistered());
SessionCoordinator.getInstance().closeSession(c, false); SessionCoordinator.getInstance().closeSession(c, false);
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN); c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
} }
}
}
} }

View File

@@ -97,4 +97,32 @@ public class AccountService {
accountRepository.setTos(accountId, true); accountRepository.setTos(accountId, true);
return true; return true;
} }
public void setPin(int accountId, String pin) {
setPinMysql(accountId, pin);
setPinPostgres(accountId, pin);
}
private void setPinMysql(int accountId, String pin) {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pin = ? WHERE id = ?")) {
ps.setString(1, pin);
ps.setInt(2, accountId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void setPinPostgres(int accountId, String pin) {
boolean success = false;
try {
success = accountRepository.setPin(accountId, pin);
} catch (Exception e) {
log.error("Failed to set pin due to error - account:{}, pin:{}", accountId, pin, e);
}
if (!success) {
log.warn("Failed to set pin due to no updated rows - account:{}, pin:{}", accountId, pin);
}
}
} }