Separate PG from MySQL chr saving
This commit is contained in:
@@ -8438,7 +8438,7 @@ public class Character extends AbstractCharacterObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void saveCharacter(Connection con) throws SQLException {
|
private void saveCharacter(Connection con) throws SQLException {
|
||||||
SaveStats stats = getSaveStats();
|
CharacterStats stats = getCharacterStats();
|
||||||
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, gachaexp = ?, hp = ?, mp = ?, maxhp = ?, maxmp = ?, sp = ?, ap = ?, gm = ?, skincolor = ?, gender = ?, job = ?, hair = ?, face = ?, map = ?, meso = ?, hpMpUsed = ?, spawnpoint = ?, party = ?, buddyCapacity = ?, messengerid = ?, messengerposition = ?, mountlevel = ?, mountexp = ?, mounttiredness= ?, equipslots = ?, useslots = ?, setupslots = ?, etcslots = ?, monsterbookcover = ?, vanquisherStage = ?, dojoPoints = ?, lastDojoStage = ?, finishedDojoTutorial = ?, vanquisherKills = ?, matchcardwins = ?, matchcardlosses = ?, matchcardties = ?, omokwins = ?, omoklosses = ?, omokties = ?, dataString = ?, fquest = ?, jailexpire = ?, partnerId = ?, marriageItemId = ?, lastExpGainTime = ?, ariantPoints = ?, partySearch = ? WHERE id = ?", Statement.RETURN_GENERATED_KEYS)) {
|
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, gachaexp = ?, hp = ?, mp = ?, maxhp = ?, maxmp = ?, sp = ?, ap = ?, gm = ?, skincolor = ?, gender = ?, job = ?, hair = ?, face = ?, map = ?, meso = ?, hpMpUsed = ?, spawnpoint = ?, party = ?, buddyCapacity = ?, messengerid = ?, messengerposition = ?, mountlevel = ?, mountexp = ?, mounttiredness= ?, equipslots = ?, useslots = ?, setupslots = ?, etcslots = ?, monsterbookcover = ?, vanquisherStage = ?, dojoPoints = ?, lastDojoStage = ?, finishedDojoTutorial = ?, vanquisherKills = ?, matchcardwins = ?, matchcardlosses = ?, matchcardties = ?, omokwins = ?, omoklosses = ?, omokties = ?, dataString = ?, fquest = ?, jailexpire = ?, partnerId = ?, marriageItemId = ?, lastExpGainTime = ?, ariantPoints = ?, partySearch = ? WHERE id = ?", Statement.RETURN_GENERATED_KEYS)) {
|
||||||
ps.setInt(1, stats.level());
|
ps.setInt(1, stats.level());
|
||||||
ps.setInt(2, stats.fame());
|
ps.setInt(2, stats.fame());
|
||||||
@@ -8504,8 +8504,8 @@ public class Character extends AbstractCharacterObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SaveStats getSaveStats() {
|
public CharacterStats getCharacterStats() {
|
||||||
SaveStats.SaveStatsBuilder builder = SaveStats.builder()
|
CharacterStats.CharacterStatsBuilder builder = CharacterStats.builder()
|
||||||
.id(id)
|
.id(id)
|
||||||
.level(level)
|
.level(level)
|
||||||
.fame(fame)
|
.fame(fame)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package client;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
public record SaveStats(
|
public record CharacterStats(
|
||||||
int id,
|
int id,
|
||||||
int level,
|
int level,
|
||||||
int fame,
|
int fame,
|
||||||
@@ -1,19 +1,26 @@
|
|||||||
package database.character;
|
package database.character;
|
||||||
|
|
||||||
import client.Character;
|
import client.Character;
|
||||||
|
import database.PgDatabaseConnection;
|
||||||
import database.monsterbook.MonsterCardRepository;
|
import database.monsterbook.MonsterCardRepository;
|
||||||
|
import org.jdbi.v3.core.Handle;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import tools.DatabaseConnection;
|
import tools.DatabaseConnection;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
public class CharacterSaver {
|
public class CharacterSaver {
|
||||||
private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class);
|
private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class);
|
||||||
|
private final PgDatabaseConnection pgConnection;
|
||||||
private final MonsterCardRepository monsterCardRepository;
|
private final MonsterCardRepository monsterCardRepository;
|
||||||
|
|
||||||
public CharacterSaver(MonsterCardRepository monsterCardRepository) {
|
public CharacterSaver(PgDatabaseConnection pgConnection,
|
||||||
|
MonsterCardRepository monsterCardRepository) {
|
||||||
|
this.pgConnection = pgConnection;
|
||||||
this.monsterCardRepository = monsterCardRepository;
|
this.monsterCardRepository = monsterCardRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +30,12 @@ public class CharacterSaver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.debug("Saving chr {}", chr.getName());
|
log.debug("Saving chr {}", chr.getName());
|
||||||
|
saveToMysql(chr);
|
||||||
|
saveToPostgres(chr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveToMysql(Character chr) {
|
||||||
|
Instant before = Instant.now();
|
||||||
try (Connection con = DatabaseConnection.getConnection()) {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
con.setAutoCommit(false);
|
con.setAutoCommit(false);
|
||||||
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
|
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
|
||||||
@@ -38,11 +51,24 @@ public class CharacterSaver {
|
|||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error("Error saving chr {}, level: {}, job: {}", chr.getName(), chr.getLevel(), chr.getJob().getId(), e);
|
log.error("Error saving chr {}, level: {}, job: {}", chr.getName(), chr.getLevel(), chr.getJob().getId(), e);
|
||||||
return;
|
}
|
||||||
|
Duration saveDuration = Duration.between(before, Instant.now());
|
||||||
|
log.debug("Saved {} to MySQL in {} ms", chr.getName(), saveDuration.toMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Saving monster cards to both MySQL and Postgres for now
|
private void saveToPostgres(Character chr) {
|
||||||
monsterCardRepository.save(chr.getId(), chr.getMonsterBook().getCards());
|
Instant before = Instant.now();
|
||||||
|
|
||||||
|
try (Handle handle = pgConnection.getHandle()) {
|
||||||
|
handle.useTransaction(h -> doPostgresSave(h, chr));
|
||||||
|
}
|
||||||
|
|
||||||
|
Duration saveDuration = Duration.between(before, Instant.now());
|
||||||
|
log.debug("Saved {} to Postgres in {} ms", chr.getName(), saveDuration.toMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doPostgresSave(Handle handle, Character chr) {
|
||||||
|
monsterCardRepository.save(handle, chr.getId(), chr.getMonsterBook().getCards());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ public class MonsterCardRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(int chrId, List<MonsterCard> cards) {
|
public void save(Handle handle, int chrId, List<MonsterCard> cards) {
|
||||||
try (Handle handle = connection.getHandle()) {
|
try {
|
||||||
PreparedBatch batch = handle.prepareBatch("""
|
PreparedBatch batch = handle.prepareBatch("""
|
||||||
INSERT INTO monster_card (chr_id, card_id, level)
|
INSERT INTO monster_card (chr_id, card_id, level)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?)
|
||||||
|
|||||||
@@ -1004,7 +1004,7 @@ public class Server {
|
|||||||
private ChannelDependencies registerChannelDependencies(PgDatabaseConnection connection) {
|
private ChannelDependencies registerChannelDependencies(PgDatabaseConnection connection) {
|
||||||
MonsterCardRepository monsterCardRepository = new MonsterCardRepository(connection);
|
MonsterCardRepository monsterCardRepository = new MonsterCardRepository(connection);
|
||||||
CharacterLoader characterLoader = new CharacterLoader(monsterCardRepository);
|
CharacterLoader characterLoader = new CharacterLoader(monsterCardRepository);
|
||||||
CharacterSaver characterSaver = new CharacterSaver(monsterCardRepository);
|
CharacterSaver characterSaver = new CharacterSaver(connection, monsterCardRepository);
|
||||||
TransitionService transitionService = new TransitionService(characterSaver);
|
TransitionService transitionService = new TransitionService(characterSaver);
|
||||||
BanService banService = new BanService(transitionService);
|
BanService banService = new BanService(transitionService);
|
||||||
NoteService noteService = new NoteService(new NoteDao(connection));
|
NoteService noteService = new NoteService(new NoteDao(connection));
|
||||||
|
|||||||
Reference in New Issue
Block a user