Save pin to PG
This commit is contained in:
@@ -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() {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
||||||
|
|||||||
@@ -25,21 +25,31 @@ 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());
|
||||||
|
|
||||||
@@ -47,5 +57,3 @@ public final class RegisterPinHandler extends AbstractPacketHandler {
|
|||||||
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
|
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user