Auto-create account in both MySQL and PG
This commit is contained in:
@@ -9,6 +9,7 @@ import database.character.CharacterSaver;
|
||||
import database.drop.DropProvider;
|
||||
import lombok.Builder;
|
||||
import server.shop.ShopFactory;
|
||||
import service.AccountService;
|
||||
import service.BanService;
|
||||
import service.NoteService;
|
||||
import service.TransitionService;
|
||||
@@ -20,6 +21,7 @@ import java.util.Objects;
|
||||
*/
|
||||
@Builder
|
||||
public record ChannelDependencies(
|
||||
AccountService accountService,
|
||||
CharacterCreator characterCreator, CharacterLoader characterLoader, CharacterSaver characterSaver,
|
||||
NoteService noteService, FredrickProcessor fredrickProcessor, MakerProcessor makerProcessor,
|
||||
DropProvider dropProvider, CommandsExecutor commandsExecutor, ShopFactory shopFactory,
|
||||
@@ -27,6 +29,7 @@ public record ChannelDependencies(
|
||||
) {
|
||||
|
||||
public ChannelDependencies {
|
||||
Objects.requireNonNull(accountService);
|
||||
Objects.requireNonNull(characterCreator);
|
||||
Objects.requireNonNull(characterLoader);
|
||||
Objects.requireNonNull(characterSaver);
|
||||
|
||||
@@ -281,7 +281,8 @@ public final class PacketProcessor {
|
||||
registerHandler(RecvOpcode.SERVERLIST_REREQUEST, new ServerlistRequestHandler());
|
||||
registerHandler(RecvOpcode.CHARLIST_REQUEST, new CharlistRequestHandler());
|
||||
registerHandler(RecvOpcode.CHAR_SELECT, new CharSelectedHandler());
|
||||
registerHandler(RecvOpcode.LOGIN_PASSWORD, new LoginPasswordHandler(channelDeps.transitionService()));
|
||||
registerHandler(RecvOpcode.LOGIN_PASSWORD, new LoginPasswordHandler(channelDeps.accountService(),
|
||||
channelDeps.transitionService()));
|
||||
registerHandler(RecvOpcode.RELOG, new RelogRequestHandler());
|
||||
registerHandler(RecvOpcode.SERVERLIST_REQUEST, new ServerlistRequestHandler());
|
||||
registerHandler(RecvOpcode.SERVERSTATUS_REQUEST, new ServerStatusRequestHandler());
|
||||
@@ -291,7 +292,7 @@ public final class PacketProcessor {
|
||||
registerHandler(RecvOpcode.VIEW_ALL_CHAR, new ViewAllCharHandler());
|
||||
registerHandler(RecvOpcode.PICK_ALL_CHAR, new ViewAllCharSelectedHandler());
|
||||
registerHandler(RecvOpcode.REGISTER_PIN, new RegisterPinHandler());
|
||||
registerHandler(RecvOpcode.GUEST_LOGIN, new GuestLoginHandler(channelDeps.transitionService()));
|
||||
registerHandler(RecvOpcode.GUEST_LOGIN, new GuestLoginHandler());
|
||||
registerHandler(RecvOpcode.REGISTER_PIC, new RegisterPicHandler());
|
||||
registerHandler(RecvOpcode.CHAR_SELECT_WITH_PIC, new CharSelectedWithPicHandler());
|
||||
registerHandler(RecvOpcode.SET_GENDER, new SetGenderHandler());
|
||||
|
||||
@@ -44,6 +44,7 @@ import constants.net.OpcodeConstants;
|
||||
import constants.net.ServerConstants;
|
||||
import database.PgDatabaseConfig;
|
||||
import database.PgDatabaseConnection;
|
||||
import database.account.AccountRepository;
|
||||
import database.character.CharacterLoader;
|
||||
import database.character.CharacterRepository;
|
||||
import database.character.CharacterSaver;
|
||||
@@ -88,6 +89,7 @@ import server.expeditions.ExpeditionBossLog;
|
||||
import server.life.PlayerNPC;
|
||||
import server.quest.Quest;
|
||||
import server.shop.ShopFactory;
|
||||
import service.AccountService;
|
||||
import service.BanService;
|
||||
import service.NoteService;
|
||||
import service.TransitionService;
|
||||
@@ -1015,6 +1017,7 @@ public class Server {
|
||||
DropProvider dropProvider = new DropProvider(new DropRepository(connection));
|
||||
ShopFactory shopFactory = new ShopFactory(new ShopDao(connection));
|
||||
ChannelDependencies channelDependencies = ChannelDependencies.builder()
|
||||
.accountService(new AccountService(new AccountRepository(connection)))
|
||||
.characterCreator(new CharacterCreator(connection, characterRepository))
|
||||
.characterLoader(new CharacterLoader(monsterCardRepository))
|
||||
.characterSaver(characterSaver)
|
||||
|
||||
@@ -22,25 +22,21 @@
|
||||
package net.server.handlers.login;
|
||||
|
||||
import client.Client;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.AbstractPacketHandler;
|
||||
import net.packet.InPacket;
|
||||
import service.TransitionService;
|
||||
import tools.PacketCreator;
|
||||
|
||||
/*
|
||||
* @author David
|
||||
*/
|
||||
@Slf4j
|
||||
public final class GuestLoginHandler extends AbstractPacketHandler {
|
||||
private final TransitionService transitionService;
|
||||
|
||||
public GuestLoginHandler(TransitionService transitionService) {
|
||||
this.transitionService = transitionService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void handlePacket(InPacket p, Client c) {
|
||||
c.sendPacket(PacketCreator.sendGuestTOS());
|
||||
//System.out.println(slea.toString());
|
||||
new LoginPasswordHandler(transitionService).handlePacket(p, c);
|
||||
log.error("Unexpected guest login. How did you trigger this? It shouldn't be possible in v83. Packet: {}", p);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import net.server.coordinator.session.Hwid;
|
||||
import net.server.world.World;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import service.AccountService;
|
||||
import service.TransitionService;
|
||||
import tools.BCrypt;
|
||||
import tools.DatabaseConnection;
|
||||
@@ -42,17 +43,18 @@ import tools.PacketCreator;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
|
||||
public final class LoginPasswordHandler implements PacketHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(LoginPasswordHandler.class);
|
||||
|
||||
private final AccountService accountService;
|
||||
private final TransitionService transitionService;
|
||||
|
||||
public LoginPasswordHandler(TransitionService transitionService) {
|
||||
public LoginPasswordHandler(AccountService accountService, TransitionService transitionService) {
|
||||
this.accountService = accountService;
|
||||
this.transitionService = transitionService;
|
||||
}
|
||||
|
||||
@@ -80,21 +82,10 @@ public final class LoginPasswordHandler implements PacketHandler {
|
||||
|
||||
|
||||
if (YamlConfig.config.server.AUTOMATIC_REGISTER && loginok == 5) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO accounts (name, password, birthday, tempban) VALUES (?, ?, ?, ?);", Statement.RETURN_GENERATED_KEYS)) { //Jayd: Added birthday, tempban
|
||||
ps.setString(1, login);
|
||||
ps.setString(2, BCrypt.hashpw(pwd, BCrypt.gensalt(12)));
|
||||
ps.setDate(3, Date.valueOf(DefaultDates.getBirthday()));
|
||||
ps.setTimestamp(4, Timestamp.valueOf(DefaultDates.getTempban()));
|
||||
ps.executeUpdate();
|
||||
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
rs.next();
|
||||
c.setAccID(rs.getInt(1));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
c.setAccID(-1);
|
||||
e.printStackTrace();
|
||||
try {
|
||||
int accountId = createAccountPostgres(login, pwd);
|
||||
createAccountMysql(accountId, login, pwd);
|
||||
c.setAccID(accountId);
|
||||
} finally {
|
||||
loginok = c.login(login, pwd, hwid);
|
||||
}
|
||||
@@ -126,6 +117,24 @@ public final class LoginPasswordHandler implements PacketHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private int createAccountPostgres(String name, String password) {
|
||||
return accountService.createNew(name, password);
|
||||
}
|
||||
|
||||
private void createAccountMysql(int id, String name, String password) {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO accounts (id, name, password, birthday, tempban) VALUES (?, ?, ?, ?, ?);")) {
|
||||
ps.setInt(1, id);
|
||||
ps.setString(2, name);
|
||||
ps.setString(3, BCrypt.hashpw(password, BCrypt.gensalt(12)));
|
||||
ps.setDate(4, Date.valueOf(DefaultDates.getBirthday()));
|
||||
ps.setTimestamp(5, Timestamp.valueOf(DefaultDates.getTempban()));
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkChar(Client c) { // issue with multiple chars from same account login found by shavit, resinate
|
||||
if (!YamlConfig.config.server.USE_CHARACTER_ACCOUNT_CHECK) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user