Skip to content

Commit

Permalink
Rework TryToComplete.
Browse files Browse the repository at this point in the history
Implement ability to complete challenge multiple times at once.
To do it, I split everything in checking/removing/rewarding steps.

In checking step, it calculates if it is possible to complete with minimal requirements and then calculates maximal repeating factor.
In removing step, it removes everything that is necessary.
In rewarding step, it give rewards by necessary factor (multilayer).

I rework item/block/entity removing as factors may be influenced at the last element, so I improve everything by memory usage. Create necessary sets/lists/queues for faster access to already collected items.
  • Loading branch information
BONNe committed May 3, 2019
1 parent c702dd4 commit b3ce8f5
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 341 deletions.
25 changes: 20 additions & 5 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,23 @@ private boolean isChallengeComplete(String storageDataID, String challengeID)
* @param challengeID - challengeID
*/
private void setChallengeComplete(@NonNull String storageDataID, @NonNull String challengeID)
{
this.setChallengeComplete(storageDataID, challengeID, 1);
}


/**
* Sets the challenge with given ID as complete and increments the number of times it has been
* completed
*
* @param storageDataID - playerData ID
* @param challengeID - challengeID
* @param count - how many times challenge is completed
*/
private void setChallengeComplete(@NonNull String storageDataID, @NonNull String challengeID, int count)
{
this.addPlayerData(storageDataID);
this.playerCacheData.get(storageDataID).setChallengeDone(challengeID);
this.playerCacheData.get(storageDataID).addChallengeDone(challengeID, count);
// Save
this.savePlayerData(storageDataID);
}
Expand Down Expand Up @@ -842,9 +856,9 @@ public boolean isChallengeComplete(UUID user, World world, String challengeID)
* @param world - World where completion must be called.
* @param challenge - That must be completed.
*/
public void setChallengeComplete(User user, World world, Challenge challenge)
public void setChallengeComplete(User user, World world, Challenge challenge, int completionCount)
{
this.setChallengeComplete(user.getUniqueId(), world, challenge);
this.setChallengeComplete(user.getUniqueId(), world, challenge, completionCount);
}


Expand All @@ -854,21 +868,22 @@ public void setChallengeComplete(User user, World world, Challenge challenge)
* @param world - World where completion must be called.
* @param challenge - That must be completed.
*/
public void setChallengeComplete(UUID userID, World world, Challenge challenge)
public void setChallengeComplete(UUID userID, World world, Challenge challenge, int completionCount)
{
String storageID = this.getDataUniqueID(userID, Util.getWorld(world));
this.setChallengeComplete(storageID, challenge.getUniqueId());
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE").
data("user-id", userID.toString()).
data("challenge-id", challenge.getUniqueId()).
data("completion-count", Integer.toString(completionCount)).
build());

// Fire event that user completes challenge
Bukkit.getServer().getPluginManager().callEvent(
new ChallengeCompletedEvent(challenge.getUniqueId(),
userID,
false,
1));
completionCount));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,21 @@ public void reset(@NonNull String worldName)
*/
public void setChallengeDone(@NonNull String challengeName)
{
int times = challengeStatus.getOrDefault(challengeName, 0) + 1;
challengeStatus.put(challengeName, times);
this.addChallengeDone(challengeName, 1);
}


/**
* Mark a challenge as having been completed. Will increment the number of times and
* timestamp
*
* @param challengeName - unique challenge name
* @param times - how many new times should be added
*/
public void addChallengeDone(@NonNull String challengeName, int times)
{
int newTimes = challengeStatus.getOrDefault(challengeName, 0) + times;
challengeStatus.put(challengeName, newTimes);
challengesTimestamp.put(challengeName, System.currentTimeMillis());
}

Expand Down

0 comments on commit b3ce8f5

Please sign in to comment.