LoginState enum
This commit is contained in:
@@ -582,19 +582,20 @@ public class Client extends ChannelInboundHandlerAdapter {
|
|||||||
return accId;
|
return accId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLoginState(int newState) {
|
public void onChangedLoginState(LoginState newState) {
|
||||||
if (newState == LoginState.LOGGED_OUT) {
|
switch (newState) {
|
||||||
|
case LoginState.LOGGED_OUT -> {
|
||||||
loggedIn = false;
|
loggedIn = false;
|
||||||
inServerTransition = false;
|
inServerTransition = false;
|
||||||
setAccID(0);
|
}
|
||||||
} else if (newState == LoginState.SERVER_TRANSITION) {
|
case LoginState.SERVER_TRANSITION -> {
|
||||||
loggedIn = false;
|
loggedIn = false;
|
||||||
inServerTransition = true;
|
inServerTransition = true;
|
||||||
} else if (newState == LoginState.LOGGED_IN) {
|
}
|
||||||
|
case LoginState.LOGGED_IN -> {
|
||||||
loggedIn = true;
|
loggedIn = true;
|
||||||
inServerTransition = false;
|
inServerTransition = false;
|
||||||
} else {
|
}
|
||||||
throw new IllegalArgumentException("Invalid login state: " + newState);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,29 @@
|
|||||||
package client;
|
package client;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Ponk
|
* @author Ponk
|
||||||
*/
|
*/
|
||||||
public class LoginState {
|
public enum LoginState {
|
||||||
public static final byte LOGGED_OUT = 0;
|
LOGGED_OUT(0),
|
||||||
public static final byte SERVER_TRANSITION = 1;
|
SERVER_TRANSITION(1),
|
||||||
public static final byte LOGGED_IN = 2;
|
LOGGED_IN(2);
|
||||||
|
|
||||||
|
private final byte value;
|
||||||
|
|
||||||
|
LoginState(int value) {
|
||||||
|
this.value = (byte) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<LoginState> fromValue(int value) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.filter(v -> v.getValue() == value)
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package database.account;
|
package database.account;
|
||||||
|
|
||||||
|
import client.LoginState;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -11,7 +12,7 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
@Builder
|
@Builder
|
||||||
public record Account(int id, String name, String password, boolean acceptedTos, Byte gender, LocalDate birthdate,
|
public record Account(int id, String name, String password, boolean acceptedTos, Byte gender, LocalDate birthdate,
|
||||||
String pin, String pic, byte chrSlots, byte loginState, LocalDateTime lastLogin, boolean banned,
|
String pin, String pic, byte chrSlots, LoginState loginState, LocalDateTime lastLogin, boolean banned,
|
||||||
LocalDateTime tempBanTimestamp) {
|
LocalDateTime tempBanTimestamp) {
|
||||||
public Account {
|
public Account {
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package database.account;
|
package database.account;
|
||||||
|
|
||||||
|
import client.LoginState;
|
||||||
import database.PgDatabaseConnection;
|
import database.PgDatabaseConnection;
|
||||||
import org.jdbi.v3.core.Handle;
|
import org.jdbi.v3.core.Handle;
|
||||||
|
|
||||||
@@ -126,7 +127,7 @@ public class AccountRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setLoginState(int accountId, byte loginState, Instant lastLogin) {
|
public boolean setLoginState(int accountId, LoginState loginState, Instant lastLogin) {
|
||||||
String sql = """
|
String sql = """
|
||||||
UPDATE account
|
UPDATE account
|
||||||
SET login_state = :loginState, last_login = :lastLogin
|
SET login_state = :loginState, last_login = :lastLogin
|
||||||
@@ -134,7 +135,7 @@ public class AccountRepository {
|
|||||||
try (Handle handle = connection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
return handle.createUpdate(sql)
|
return handle.createUpdate(sql)
|
||||||
.bind("id", accountId)
|
.bind("id", accountId)
|
||||||
.bind("loginState", loginState)
|
.bind("loginState", loginState.getValue())
|
||||||
.bind("lastLogin", lastLogin)
|
.bind("lastLogin", lastLogin)
|
||||||
.execute() > 0;
|
.execute() > 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package database.account;
|
package database.account;
|
||||||
|
|
||||||
|
import client.LoginState;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.jdbi.v3.core.mapper.RowMapper;
|
import org.jdbi.v3.core.mapper.RowMapper;
|
||||||
import org.jdbi.v3.core.statement.StatementContext;
|
import org.jdbi.v3.core.statement.StatementContext;
|
||||||
|
|
||||||
@@ -11,6 +13,7 @@ import java.util.Optional;
|
|||||||
/**
|
/**
|
||||||
* @author Ponk
|
* @author Ponk
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class AccountRowMapper implements RowMapper<Account> {
|
public class AccountRowMapper implements RowMapper<Account> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -27,7 +30,7 @@ public class AccountRowMapper implements RowMapper<Account> {
|
|||||||
.orElse(null))
|
.orElse(null))
|
||||||
.acceptedTos(rs.getBoolean("tos_accepted"))
|
.acceptedTos(rs.getBoolean("tos_accepted"))
|
||||||
.chrSlots(rs.getByte("chr_slots"))
|
.chrSlots(rs.getByte("chr_slots"))
|
||||||
.loginState(rs.getByte("login_state"))
|
.loginState(getLoginState(rs.getByte("login_state")))
|
||||||
.lastLogin(Optional.ofNullable(rs.getTimestamp("last_login"))
|
.lastLogin(Optional.ofNullable(rs.getTimestamp("last_login"))
|
||||||
.map(Timestamp::toLocalDateTime)
|
.map(Timestamp::toLocalDateTime)
|
||||||
.orElse(null))
|
.orElse(null))
|
||||||
@@ -37,4 +40,12 @@ public class AccountRowMapper implements RowMapper<Account> {
|
|||||||
.orElse(null))
|
.orElse(null))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static LoginState getLoginState(byte dbValue) {
|
||||||
|
Optional<LoginState> loginState = LoginState.fromValue(dbValue);
|
||||||
|
if (loginState.isEmpty()) {
|
||||||
|
throw new IllegalStateException("Invalid login state: " + dbValue);
|
||||||
|
}
|
||||||
|
return loginState.get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ public final class PlayerLoggedinHandler extends AbstractPacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int state = account.loginState();
|
LoginState state = account.loginState();
|
||||||
if (state != LoginState.SERVER_TRANSITION) {
|
if (state != LoginState.SERVER_TRANSITION) {
|
||||||
c.setPlayer(null);
|
c.setPlayer(null);
|
||||||
c.setAccID(0);
|
c.setAccID(0);
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ public final class LoginPasswordHandler implements PacketHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (account.loginState() > LoginState.LOGGED_OUT) {
|
if (account.loginState() != LoginState.LOGGED_OUT) {
|
||||||
c.sendPacket(PacketCreator.getLoginFailed(7));
|
c.sendPacket(PacketCreator.getLoginFailed(7));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ public class AccountService {
|
|||||||
throw new IllegalStateException("Unable to set logged in - no account");
|
throw new IllegalStateException("Unable to set logged in - no account");
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentState = account.loginState();
|
LoginState currentState = account.loginState();
|
||||||
if (currentState != LoginState.LOGGED_OUT && currentState != LoginState.SERVER_TRANSITION) {
|
if (currentState != LoginState.LOGGED_OUT && currentState != LoginState.SERVER_TRANSITION) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -242,22 +242,22 @@ public class AccountService {
|
|||||||
setLoginState(c, LoginState.SERVER_TRANSITION);
|
setLoginState(c, LoginState.SERVER_TRANSITION);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLoginState(Client c, byte newState) {
|
private void setLoginState(Client c, LoginState newState) {
|
||||||
saveLoginState(c.getAccID(), newState);
|
saveLoginState(c.getAccID(), newState);
|
||||||
c.setLoginState(newState);
|
c.onChangedLoginState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveLoginState(int accountId, byte newState) {
|
private void saveLoginState(int accountId, LoginState newState) {
|
||||||
setLoginStateMysql(accountId, newState);
|
setLoginStateMysql(accountId, newState);
|
||||||
setLoginStatePostgres(accountId, newState);
|
setLoginStatePostgres(accountId, newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLoginStateMysql(int accountId, byte newState) {
|
private void setLoginStateMysql(int accountId, LoginState newState) {
|
||||||
try (Connection con = DatabaseConnection.getConnection();
|
try (Connection con = DatabaseConnection.getConnection();
|
||||||
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = ?, lastlogin = ? WHERE id = ?")) {
|
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = ?, lastlogin = ? WHERE id = ?")) {
|
||||||
// using sql currenttime here could potentially break the login, thanks Arnah for pointing this out
|
// using sql currenttime here could potentially break the login, thanks Arnah for pointing this out
|
||||||
|
|
||||||
ps.setInt(1, newState);
|
ps.setInt(1, newState.getValue());
|
||||||
ps.setTimestamp(2, new java.sql.Timestamp(Server.getInstance().getCurrentTime()));
|
ps.setTimestamp(2, new java.sql.Timestamp(Server.getInstance().getCurrentTime()));
|
||||||
ps.setInt(3, accountId);
|
ps.setInt(3, accountId);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
@@ -266,7 +266,7 @@ public class AccountService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLoginStatePostgres(int accountId, byte newState) {
|
private void setLoginStatePostgres(int accountId, LoginState newState) {
|
||||||
Instant loginTime = Instant.now();
|
Instant loginTime = Instant.now();
|
||||||
boolean success = accountRepository.setLoginState(accountId, newState, loginTime);
|
boolean success = accountRepository.setLoginState(accountId, newState, loginTime);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
|
|||||||
Reference in New Issue
Block a user