Save pic to PG
This commit is contained in:
@@ -470,14 +470,6 @@ public class Client extends ChannelInboundHandlerAdapter {
|
||||
|
||||
public void setPic(String pic) {
|
||||
this.pic = pic;
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pic = ? WHERE id = ?")) {
|
||||
ps.setString(1, pic);
|
||||
ps.setInt(2, accId);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getPic() {
|
||||
|
||||
@@ -68,4 +68,17 @@ public class AccountRepository {
|
||||
.execute() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setPic(int accountId, String pic) {
|
||||
String sql = """
|
||||
UPDATE account
|
||||
SET pic = :pic
|
||||
WHERE id = :id""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createUpdate(sql)
|
||||
.bind("id", accountId)
|
||||
.bind("pic", pic)
|
||||
.execute() > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,11 +293,12 @@ public final class PacketProcessor {
|
||||
registerHandler(RecvOpcode.PICK_ALL_CHAR, new ViewAllCharSelectedHandler());
|
||||
registerHandler(RecvOpcode.REGISTER_PIN, new RegisterPinHandler(channelDeps.accountService()));
|
||||
registerHandler(RecvOpcode.GUEST_LOGIN, new GuestLoginHandler());
|
||||
registerHandler(RecvOpcode.REGISTER_PIC, new RegisterPicHandler());
|
||||
registerHandler(RecvOpcode.REGISTER_PIC, new RegisterPicHandler(channelDeps.accountService()));
|
||||
registerHandler(RecvOpcode.CHAR_SELECT_WITH_PIC, new CharSelectedWithPicHandler());
|
||||
registerHandler(RecvOpcode.SET_GENDER, new SetGenderHandler());
|
||||
registerHandler(RecvOpcode.VIEW_ALL_WITH_PIC, new ViewAllCharSelectedWithPicHandler());
|
||||
registerHandler(RecvOpcode.VIEW_ALL_PIC_REGISTER, new ViewAllCharRegisterPicHandler());
|
||||
registerHandler(RecvOpcode.VIEW_ALL_PIC_REGISTER, new ViewAllCharRegisterPicHandler(
|
||||
channelDeps.accountService()));
|
||||
}
|
||||
|
||||
private void registerChannelHandlers() {
|
||||
|
||||
@@ -10,6 +10,7 @@ import net.server.coordinator.session.SessionCoordinator.AntiMulticlientResult;
|
||||
import net.server.world.World;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import service.AccountService;
|
||||
import tools.PacketCreator;
|
||||
|
||||
import java.net.InetAddress;
|
||||
@@ -18,14 +19,10 @@ import java.net.UnknownHostException;
|
||||
public final class RegisterPicHandler extends AbstractPacketHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(RegisterPicHandler.class);
|
||||
|
||||
private static int parseAntiMulticlientError(AntiMulticlientResult res) {
|
||||
return switch (res) {
|
||||
case REMOTE_PROCESSING -> 10;
|
||||
case REMOTE_LOGGEDIN -> 7;
|
||||
case REMOTE_NO_MATCH -> 17;
|
||||
case COORDINATOR_ERROR -> 8;
|
||||
default -> 9;
|
||||
};
|
||||
private final AccountService accountService;
|
||||
|
||||
public RegisterPicHandler(AccountService accountService) {
|
||||
this.accountService = accountService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,6 +64,7 @@ public final class RegisterPicHandler extends AbstractPacketHandler {
|
||||
|
||||
String pic = p.readString();
|
||||
if (c.getPic() == null || c.getPic().equals("")) {
|
||||
accountService.setPic(c.getAccID(), pic);
|
||||
c.setPic(pic);
|
||||
|
||||
c.setWorld(server.getCharacterWorld(charId));
|
||||
@@ -94,4 +92,14 @@ public final class RegisterPicHandler extends AbstractPacketHandler {
|
||||
SessionCoordinator.getInstance().closeSession(c, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int parseAntiMulticlientError(AntiMulticlientResult res) {
|
||||
return switch (res) {
|
||||
case REMOTE_PROCESSING -> 10;
|
||||
case REMOTE_LOGGEDIN -> 7;
|
||||
case REMOTE_NO_MATCH -> 17;
|
||||
case COORDINATOR_ERROR -> 8;
|
||||
default -> 9;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import net.server.coordinator.session.SessionCoordinator.AntiMulticlientResult;
|
||||
import net.server.world.World;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import service.AccountService;
|
||||
import tools.PacketCreator;
|
||||
import tools.Randomizer;
|
||||
|
||||
@@ -19,14 +20,10 @@ import java.net.UnknownHostException;
|
||||
public final class ViewAllCharRegisterPicHandler extends AbstractPacketHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(ViewAllCharRegisterPicHandler.class);
|
||||
|
||||
private static int parseAntiMulticlientError(AntiMulticlientResult res) {
|
||||
return switch (res) {
|
||||
case REMOTE_PROCESSING -> 10;
|
||||
case REMOTE_LOGGEDIN -> 7;
|
||||
case REMOTE_NO_MATCH -> 17;
|
||||
case COORDINATOR_ERROR -> 8;
|
||||
default -> 9;
|
||||
};
|
||||
private final AccountService accountService;
|
||||
|
||||
public ViewAllCharRegisterPicHandler(AccountService accountService) {
|
||||
this.accountService = accountService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,6 +75,7 @@ public final class ViewAllCharRegisterPicHandler extends AbstractPacketHandler {
|
||||
c.setChannel(channel);
|
||||
|
||||
String pic = p.readString();
|
||||
accountService.setPic(c.getAccID(), pic);
|
||||
c.setPic(pic);
|
||||
|
||||
String[] socket = server.getInetSocket(c, c.getWorld(), channel);
|
||||
@@ -95,4 +93,14 @@ public final class ViewAllCharRegisterPicHandler extends AbstractPacketHandler {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static int parseAntiMulticlientError(AntiMulticlientResult res) {
|
||||
return switch (res) {
|
||||
case REMOTE_PROCESSING -> 10;
|
||||
case REMOTE_LOGGEDIN -> 7;
|
||||
case REMOTE_NO_MATCH -> 17;
|
||||
case COORDINATOR_ERROR -> 8;
|
||||
default -> 9;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,14 +115,40 @@ public class AccountService {
|
||||
}
|
||||
|
||||
private void setPinPostgres(int accountId, String pin) {
|
||||
boolean success = false;
|
||||
try {
|
||||
success = accountRepository.setPin(accountId, pin);
|
||||
boolean success = accountRepository.setPin(accountId, pin);
|
||||
if (!success) {
|
||||
log.warn("Failed to set pin (no updated rows) - account:{}, pin:{}", 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);
|
||||
}
|
||||
|
||||
public void setPic(int accountId, String pic) {
|
||||
setPicMysql(accountId, pic);
|
||||
setPicPostgres(accountId, pic);
|
||||
}
|
||||
|
||||
private void setPicMysql(int accountId, String pic) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET pic = ? WHERE id = ?")) {
|
||||
ps.setString(1, pic);
|
||||
ps.setInt(2, accountId);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void setPicPostgres(int accountId, String pic) {
|
||||
try {
|
||||
boolean success = accountRepository.setPic(accountId, pic);
|
||||
if (!success) {
|
||||
log.warn("Failed to set pic (no updated rows) - account:{}, pic:{}", accountId, pic);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to set pic - account:{}, pin:{}", accountId, pic, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user