Save pin to PG
This commit is contained in:
@@ -446,14 +446,6 @@ public class Client extends ChannelInboundHandlerAdapter {
|
||||
|
||||
public void setPin(String 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() {
|
||||
|
||||
@@ -55,4 +55,17 @@ public class AccountRepository {
|
||||
.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,7 +291,7 @@ public final class PacketProcessor {
|
||||
registerHandler(RecvOpcode.DELETE_CHAR, new DeleteCharHandler());
|
||||
registerHandler(RecvOpcode.VIEW_ALL_CHAR, new ViewAllCharHandler());
|
||||
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.REGISTER_PIC, new RegisterPicHandler());
|
||||
registerHandler(RecvOpcode.CHAR_SELECT_WITH_PIC, new CharSelectedWithPicHandler());
|
||||
|
||||
@@ -25,27 +25,35 @@ import client.Client;
|
||||
import net.AbstractPacketHandler;
|
||||
import net.packet.InPacket;
|
||||
import net.server.coordinator.session.SessionCoordinator;
|
||||
import service.AccountService;
|
||||
import tools.PacketCreator;
|
||||
|
||||
/*
|
||||
/**
|
||||
* @author Rob
|
||||
* @author Ponk
|
||||
*/
|
||||
public final class RegisterPinHandler extends AbstractPacketHandler {
|
||||
private final AccountService accountService;
|
||||
|
||||
public RegisterPinHandler(final AccountService accountService) {
|
||||
this.accountService = accountService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void handlePacket(InPacket p, Client c) {
|
||||
byte c2 = p.readByte();
|
||||
if (c2 == 0) {
|
||||
public void handlePacket(InPacket p, Client c) {
|
||||
boolean cancel = p.readByte() == 0;
|
||||
if (cancel) {
|
||||
SessionCoordinator.getInstance().closeSession(c, false);
|
||||
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
|
||||
} else {
|
||||
String pin = p.readString();
|
||||
if (pin != null) {
|
||||
c.setPin(pin);
|
||||
c.sendPacket(PacketCreator.pinRegistered());
|
||||
|
||||
SessionCoordinator.getInstance().closeSession(c, false);
|
||||
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
String pin = p.readString();
|
||||
accountService.setPin(c.getAccID(), pin);
|
||||
c.setPin(pin);
|
||||
c.sendPacket(PacketCreator.pinRegistered());
|
||||
|
||||
SessionCoordinator.getInstance().closeSession(c, false);
|
||||
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,4 +97,32 @@ public class AccountService {
|
||||
accountRepository.setTos(accountId, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user