Save gender to PG

This commit is contained in:
P0nk
2024-09-27 23:08:12 +02:00
parent 5abae50be5
commit 439280947c
6 changed files with 91 additions and 26 deletions

View File

@@ -87,8 +87,6 @@ public class Client extends ChannelInboundHandlerAdapter {
private static final int MAX_FAILED_LOGIN_ATTEMPTS = 5;
private static final int MAX_CHR_SLOTS = 15;
public static final byte NO_GENDER = 10;
public static final int LOGIN_NOTLOGGEDIN = 0;
public static final int LOGIN_SERVER_TRANSITION = 1;
public static final int LOGIN_LOGGEDIN = 2;
@@ -299,7 +297,7 @@ public class Client extends ChannelInboundHandlerAdapter {
this.characterSlots = account.chrSlots();
this.pin = account.pin();
this.pic = account.pic();
this.gender = Objects.requireNonNullElse(account.gender(), NO_GENDER);
this.gender = Objects.requireNonNullElse(account.gender(), Gender.NOT_SET);
Calendar calendar = Calendar.getInstance();
LocalDate birthdate = account.birthdate();
calendar.set(birthdate.getYear(), birthdate.getMonthValue() - 1, birthdate.getDayOfMonth());
@@ -988,18 +986,11 @@ public class Client extends ChannelInboundHandlerAdapter {
return gender;
}
public void setGender(byte m) {
this.gender = m;
// TODO: move to AccountService
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET gender = ? WHERE id = ?")) {
ps.setByte(1, gender);
ps.setInt(2, accId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
public void setGender(byte gender) {
if (gender != Gender.MALE && gender != Gender.FEMALE) {
throw new IllegalArgumentException("Invalid gender: " + gender);
}
this.gender = gender;
}
private void announceDisableServerMessage() {

View File

@@ -0,0 +1,10 @@
package client;
/**
* @author Ponk
*/
public class Gender {
public static final byte MALE = 0;
public static final byte FEMALE = 1;
public static final byte NOT_SET = 10;
}

View File

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

View File

@@ -295,7 +295,7 @@ public final class PacketProcessor {
registerHandler(RecvOpcode.GUEST_LOGIN, new GuestLoginHandler());
registerHandler(RecvOpcode.REGISTER_PIC, new RegisterPicHandler(channelDeps.accountService()));
registerHandler(RecvOpcode.CHAR_SELECT_WITH_PIC, new CharSelectedWithPicHandler());
registerHandler(RecvOpcode.SET_GENDER, new SetGenderHandler());
registerHandler(RecvOpcode.SET_GENDER, new SetGenderHandler(channelDeps.accountService()));
registerHandler(RecvOpcode.VIEW_ALL_WITH_PIC, new ViewAllCharSelectedWithPicHandler());
registerHandler(RecvOpcode.VIEW_ALL_PIC_REGISTER, new ViewAllCharRegisterPicHandler(
channelDeps.accountService()));

View File

@@ -23,30 +23,57 @@
package net.server.handlers.login;
import client.Client;
import client.Gender;
import net.AbstractPacketHandler;
import net.packet.InPacket;
import net.server.Server;
import net.server.coordinator.session.SessionCoordinator;
import service.AccountService;
import tools.PacketCreator;
/**
* @author kevintjuh93
* @author Ponk
*/
public class SetGenderHandler extends AbstractPacketHandler {
private final AccountService accountService;
public SetGenderHandler(AccountService accountService) {
this.accountService = accountService;
}
@Override
public void handlePacket(InPacket p, Client c) {
if (c.getGender() == Client.NO_GENDER) { //Packet shouldn't come if Gender isn't 10.
byte confirmed = p.readByte();
if (confirmed == 0x01) {
c.setGender(p.readByte());
c.sendPacket(PacketCreator.getAuthSuccess(c));
Server.getInstance().registerLoginState(c);
} else {
SessionCoordinator.getInstance().closeSession(c, false);
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
}
if (c.getGender() != Gender.NOT_SET) { // Packet shouldn't come if Gender isn't 10.
close(c);
return;
}
byte confirmed = p.readByte();
if (confirmed != 0x01) {
close(c);
return;
}
byte gender = p.readByte();
if (gender != Gender.MALE && gender != Gender.FEMALE) {
close(c);
return;
}
if (!accountService.setGender(c.getAccID(), gender)) {
close(c);
return;
}
c.setGender(gender);
c.sendPacket(PacketCreator.getAuthSuccess(c));
Server.getInstance().registerLoginState(c);
}
private void close(Client c) {
SessionCoordinator.getInstance().closeSession(c, false);
c.updateLoginState(Client.LOGIN_NOTLOGGEDIN);
}
}

View File

@@ -106,6 +106,30 @@ public class AccountService {
return true;
}
public boolean setGender(int accountId, byte gender) {
setGenderMysql(accountId, gender);
return setGenderPostgres(accountId, gender);
}
private void setGenderMysql(int accountId, byte gender) {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET gender = ? WHERE id = ?")) {
ps.setByte(1, gender);
ps.setInt(2, accountId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
private boolean setGenderPostgres(int accountId, byte gender) {
boolean success = accountRepository.setGender(accountId, gender);
if (!success) {
log.warn("Failed to set gender, account:{}, gender:{}", accountId, gender);
}
return success;
}
public void setPin(int accountId, String pin) {
setPinMysql(accountId, pin);
setPinPostgres(accountId, pin);