Skip to content

Commit

Permalink
Ranks and Ladders: Changed the way the ladder and rank file names are…
Browse files Browse the repository at this point in the history
… saved. Instead of using their IDs, it is now using their names.

The file names are converted upon loading.  Once loaded, and converted, the prison config setup cannot be reverted, but the old settings and files will be saved in the saved directories and can be restored if needed.
Rank IDs and Ladder IDs are no longer used internally.  They are being kept for now, but will be removed in the future.
  • Loading branch information
rbluer committed Sep 9, 2024
1 parent 9c3d55a commit 64d2cae
Show file tree
Hide file tree
Showing 14 changed files with 350 additions and 63 deletions.
5 changes: 5 additions & 0 deletions docs/changelog_v3.3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ These change logs represent the work that has been going on within prison.
# 3.3.0-alpha.19 2024-09-08


* **Ranks and Ladders: Changed the way the ladder and rank file names are saved. Instead of using their IDs, it is now using their names.**
The file names are converted upon loading. Once loaded, and converted, the prison config setup cannot be reverted, but the old settings and files will be saved in the saved directories and can be restored if needed.
Rank IDs and Ladder IDs are no longer used internally. They are being kept for now, but will be removed in the future.


* **Mines: Air Block Counts: Improvement. Rewrote the way prison is handling the air block counts because they were causing the servers to fall behind on TPS by a significant amount.**
The server, depending upon how many mines, and how large they were, the system (spigot, paper, etc) would warn that it was running behind by 50 ticks to a few hundred ticks. The primary reason was that all the mines would be submitted to run almost at the same time, in different threads, so it would starve all available TPS.
These changes were a complete rewrite on how the jobs are submitted and the mines are processed. Now it's just one initial job submission, instead of each mine submitting it's own task. And that one new job steps through each mine, one at time, counting the blocks, and then moving on to the next mine. As such, we are not ensuring only one task is trying to run at the same time, thus allowing other services to get sufficient access to the processing that they need. Where a mine-heavy test server was reporting falling behind by 600+ ticks, after these changes there were no warnings. During, and right after the air counts, the server is reporting a solid 20 TPS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,100 @@ public List<Document> getAll() {
@Override
public Optional<Document> get(String key) {

String suffix = key.endsWith(FILE_SUFFIX_JSON) ? "" : FILE_SUFFIX_JSON;
File dbFile = new File(collDir, key + suffix);
File dbFile = getFile(key);
Document doc = (Document) readJsonFile(dbFile, new Document());

return Optional.ofNullable(doc);
}

@Override
public void save(Document document)
{
save((String)document.get("name"), document);
}
// @Override
// public void save(Document document)
// {
// save((String)document.get("name"), document);
// }

@Override
public void save(String filename, Document document)
{
String suffix = filename.endsWith(FILE_SUFFIX_JSON) ? "" : FILE_SUFFIX_JSON;
File dbFile = new File(collDir, filename + suffix);
// @Override
// public void save(String filename, Document document)
// {
// String suffix = filename.endsWith(FILE_SUFFIX_JSON) ? "" : FILE_SUFFIX_JSON;
// File dbFile = new File(collDir, filename + suffix);
// saveJsonFile( dbFile, document );
// }


@Override
public void save(String filename, Document document,
String oldFilename, String fileType ) {

File dbFile = getFile(filename);
saveJsonFile( dbFile, document );
}

if ( oldFilename != null ) {

// Since the new file should have been saved by now...
File oldDbFile = getFile(oldFilename);

// If both the new file and old file exists, then need to remove the old file:
if ( dbFile.exists() && dbFile.length() > 0 && oldDbFile.exists() ) {

boolean deleted = oldDbFile.delete();

if ( deleted ) {

Output.get().logInfo(
"&3%s File Converted: &7%s &3--> &7%s",
fileType,
oldFilename + FILE_SUFFIX_JSON,
filename + FILE_SUFFIX_JSON
);
}
else {
Output.get().logInfo(
"&3The old %s file could not be removed: "
+ "Old file &7%s &3. " +
"Reason unknown (check logs?). "
+ "New file name &7%s. [%s]",
fileType,
oldFilename + FILE_SUFFIX_JSON,
filename + FILE_SUFFIX_JSON,
oldDbFile.getAbsolutePath()
);

}
}
}
}


private File getFile(String name)
{
String suffix = name.endsWith(FILE_SUFFIX_JSON) ? "" : FILE_SUFFIX_JSON;
File dbFile = new File(collDir, name + suffix);
return dbFile;
}

@Override
public boolean exists(String name)
{
File dbFile = getFile(name);
return dbFile.exists();
}

@Override
public boolean delete(String name)
{
String suffix = name.endsWith(FILE_SUFFIX_JSON) ? "" : FILE_SUFFIX_JSON;
File dbFile = new File(collDir, name + suffix);
return virtualDelete( dbFile );
File dbFile = getFile(name);
return dbFile.exists() ? virtualDelete( dbFile ) : false;
}

@Override
public File backup( String name )
{
String suffix = name.endsWith(FILE_SUFFIX_JSON) ? "" : FILE_SUFFIX_JSON;
File dbFile = new File(collDir, name + suffix);
File backupFile = virtualBackup( dbFile );
File dbFile = getFile(name);
File backupFile = dbFile.exists() ? virtualBackup( dbFile ) : null;

return backupFile;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class Rank

private transient final StatsRankPlayerBalance statsPlayerBlance;

private transient boolean dirty = false;


public Rank() {
super();
Expand Down Expand Up @@ -314,8 +316,12 @@ public String toString() {
return "Rank: " + id + " " + name;
}

public String filename() {
return "rank_" + id;
public String filenameNew() {
return "rank_" + getName();
}

public String filenameOld() {
return getId() == -1 ? null : "rank_" + getId();
}


Expand Down Expand Up @@ -559,4 +565,11 @@ public List<RankPlayer> getPlayers() {
return players;
}

public boolean isDirty() {
return dirty;
}
public void setDirty(boolean dirty) {
this.dirty = dirty;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class RankLadder
private double rankCostMultiplierPerRank = 0.0d;
private boolean applyRankCostMultiplierToLadder = true;

private boolean dirty = false;
private transient boolean dirty = false;


public RankLadder() {
Expand Down Expand Up @@ -213,7 +213,10 @@ public boolean isPrestiges() {

public Document toDocument() {
Document ret = new Document();
ret.put("id", this.id);

if ( this.id != -1 ) {
ret.put("id", this.id);
}
ret.put("name", this.name);

List<String> cmds = new ArrayList<>();
Expand All @@ -232,7 +235,12 @@ public Document toDocument() {
LinkedTreeMap<String, Object> rnk = new LinkedTreeMap<String, Object>();

// rnk.put( "position", rank.getPosition() );
rnk.put( "rankId", rank.getId() );

// Do not save the rank ID in the ladder file:
// if ( rank.getId() != -1 ) {
// rnk.put( "rankId", rank.getId() );
// }

rnk.put( "rankName", rank.getName());

ranksLocal.add( rnk );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ public interface Collection {
public Optional<Document> get(String key);


public void save(Document document);
public void save(String filename, Document document);
// public void save(Document document);
// public void save(String filename, Document document);
public void save(String filename, Document document,
String oldFilename, String fileType);


boolean exists(String name);

public boolean delete(String name);

public File backup(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class ConversionUtil
{

public static int doubleToInt(Object d) {
return Math.toIntExact(Math.round((double) d));
return d == null ? -1 : Math.toIntExact(Math.round((double) d));
}

public static long doubleToLong(Object d) {
return Math.round((double) d);
return d == null ? -1 : Math.round((double) d);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2774,8 +2774,8 @@ else if ( source == null || "virtual".equalsIgnoreCase( source ) ) {
File backupFile = pMines.getMineManager().backupMine( m );

String messageBu = "Mine &7" + m.getName() + " &3has " +
( backupFile.exists() ? "been successfully" : "failed to be" ) +
" backed up: " + backupFile.getAbsolutePath();
( backupFile != null && backupFile.exists() ? "been successfully" : "failed to be" ) +
" backed up: " + (backupFile != null ? backupFile.getAbsolutePath() : "none");
sender.sendMessage( messageBu );
Output.get().logInfo( messageBu );

Expand Down Expand Up @@ -2960,8 +2960,8 @@ public void backupMineCommand(CommandSender sender,
File backupFile = pMines.getMineManager().backupMine( m );

String messageBu = "Mine &7" + m.getName() + " &3has " +
( backupFile.exists() ? "been successfully" : "failed to be" ) +
" backed up: " + backupFile.getAbsolutePath();
( backupFile != null && backupFile.exists() ? "been successfully" : "failed to be" ) +
" backed up: " + (backupFile != null ? backupFile.getAbsolutePath() : "none");

if ( sender.isPlayer() ) {
sender.sendMessage( messageBu );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private void loadMines( long offsetTimingMs ) {
* PrisonMines}
*/
public void saveMine(Mine mine) {
coll.save(mine.toDocument());
coll.save( mine.getName(), mine.toDocument(), null, "Mine" );
}

public void saveMines(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ public Rank createRank( Document document )
public Document toDocument( Rank rank ) {
Document ret = new Document();
// ret.put("position", this.position );
ret.put("id", rank.getId());

if ( rank.getId() != -1 ) {
ret.put("id", rank.getId());
}

ret.put("name", rank.getName() );
ret.put("tag", (rank.getTag() == null ? "none" : rank.getTag()) );
ret.put("cost", rank.getCost() );
Expand Down
Loading

0 comments on commit 64d2cae

Please sign in to comment.