Challenges/src/main/java/world/bentobox/challenges/database/object/ChallengesPlayerData.java
BONNe 8383c93cf9
Bug fix release (#228)
* Fix issue when users could not select non-block items as icons for challenges and levels. (#190)

* English update (#193)

* Organized imports

* Minor code cleanup

* Updated English locale file.

* Translate zh-CN.yml via GitLocalize (#188)

* Make default translation looking a bit nicer (#192)

* Make default translation looking a bit nicer
* Updating a few friendly names and rewording some phrases
* Add generic .gitignore

* Fix novice level

Update `chiseledmaker` name in `novice` level.

* Adapt literal style (#197)

Improve translations and process as a YAML string.

* Test coverage (#199)

* Test coverage for Challenges Command

* Added CompleteChallengeCommand test class

* Added Utils test class

* Added ChallengesGUI test class

* Fix code smells from sonarcloud analysis

* Added .gitignore

* Added Travis CI config file

* WIP ChallengesManager Test class

* Added ChallengesManager test class

* Removed debug

* Removed code smells.

* Added ChallengesAddon test class.

* Added onDisbale test

* Added new TryToComplete test class - WIP

Covers inventory challenges.

* Added Island Challenge entity tests to TryToComplete test class

* Fix a bug with challenge deletion.

If challenge has been left in a level, then system did not remove challenge from it and was kept as ghost challenge, preventing from completing level.

* Fixes tests

* Updated travis.yml

* All strings to spanish (#200)

* Translate es.yml via GitLocalize

* Translate es.yml via GitLocalize

* Translate es.yml via GitLocalize

* Fix LevelListRequestHandler.
This handler did not return list of strings but list of challenge levels, that is incorrect.
Not it should work correctly.

* Create ro.yml

* Create id.yml

* Remove blanks files now that GitLocalize is fixed.

* Initial Russian translation (#207)

* Translate ru.yml via GitLocalize

Co-authored-by: @mt-gitlocalize @IPeredero @LoveBiscuit

* Changed build character from # to b

* Add German translation (#210)

* Translate de.yml via GitLocalize

* Translate de.yml via GitLocalize

* Translate de.yml via GitLocalize

Co-authored-by: xXjojojXx <36734820+xXjojojXx@users.noreply.github.com>
Co-authored-by: FunnysBanana <51290016+FunnysBanana@users.noreply.github.com>
Co-authored-by: mt-gitlocalize <mt@gitlocalize.com>

* Czech translation. Credit @Polda18

* Added a uniqueId sanitization when creating challenges/levels
This will help fixing issues with spaces, hyphens and accents in non-English languages.

* Fixes bug with checking entities in nether and end (#219)

https://github.com/BentoBoxWorld/Challenges/issues/218

Adds test case to check for compliance.

* Add 7 new placeholders for Challenges Addon.

- `[gamemode]_challenge_total_completion_count` returns number of sum of challenge completions for user.
- `[gamemode]_challenge_completed_count` returns number of completed challenges (at least once) for user.
- `[gamemode]_challenge_uncompleted_count` returns number of uncompleted challenges for user.
- `[gamemode]_challenge_completed_level_count` returns number of completed levels for user.
- `[gamemode]_challenge_uncompleted_level_count` returns number of uncompleted levels for user.
- `[gamemode]_challenge_unlocked_level_count` returns number of unlocked levels for user.
- `[gamemode]_challenge_locked_level_count` returns number of locked levels for user.

Fixes #224

* Add 2 new placeholders:

- `[gamemode]_challenge_latest_level_name` returns latest unlocked challenge level name
- `[gamemode]_challenge_latest_level_id` returns latest unlocked challenge level id

Fixes #226

* Fix broken tests due to placeholder additions.

b5ecffb725
2958ca8b6c

* Added default perms for aoneblock

* Downgrade to 0.8.1 version

* Add option to quit from conversation by writing "cancel" in chat.
Move sanitizeInput to a GuiUtil class.

* Change latest version to 0.8.1
2020-04-22 01:19:46 +03:00

358 lines
8.9 KiB
Java

package world.bentobox.challenges.database.object;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.eclipse.jdt.annotation.NonNull;
import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.adapters.Adapter;
import world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter;
/**
* Stores the player's challenge situation
* @author tastybento
*
*/
public class ChallengesPlayerData implements DataObject
{
/**
* Constructor ChallengesPlayerData creates a new ChallengesPlayerData instance.
*/
public ChallengesPlayerData()
{
}
/**
* Creates a player data entry
*
* @param uniqueId - the player's UUID in string format
*/
public ChallengesPlayerData(String uniqueId)
{
this.uniqueId = uniqueId;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* This variable stores each player UUID as string.
*/
@Expose
private String uniqueId = "";
/**
* Challenge map, where key = unique challenge name and Value = number of times
* completed
*/
@Expose
private Map<String, Integer> challengeStatus = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
/**
* Map of challenges completion time where key is challenges unique id and value is
* timestamp when challenge was completed last time.
*/
@Expose
private Map<String, Long> challengesTimestamp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
/**
* Set of Strings that contains all challenge levels that are completed.
*/
@Expose
private Set<String> levelsDone = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
/**
* Stores history about challenge completion.
*/
@Adapter(LogEntryListAdapter.class)
@Expose
private List<LogEntry> history = new LinkedList<>();
// ---------------------------------------------------------------------
// Section: Getters
// ---------------------------------------------------------------------
/**
* @return uniqueID
* @see DataObject#getUniqueId()
*/
@Override
public String getUniqueId()
{
return uniqueId;
}
/**
* This method returns the challengeStatus value.
* @return the value of challengeStatus.
*/
public Map<String, Integer> getChallengeStatus()
{
return challengeStatus;
}
/**
* This method returns the challengesTimestamp value.
* @return the value of challengesTimestamp.
*/
public Map<String, Long> getChallengesTimestamp()
{
return challengesTimestamp;
}
/**
* This method returns the levelsDone value.
* @return the value of levelsDone.
*/
public Set<String> getLevelsDone()
{
return levelsDone;
}
/**
* This method returns the history object.
* @return the history object.
*/
public List<LogEntry> getHistory()
{
return history;
}
// ---------------------------------------------------------------------
// Section: Setters
// ---------------------------------------------------------------------
/**
* @param uniqueId - unique ID the uniqueId to set
* @see DataObject#setUniqueId(String)
*/
@Override
public void setUniqueId(String uniqueId)
{
this.uniqueId = uniqueId;
}
/**
* This method sets the challengeStatus value.
* @param challengeStatus the challengeStatus new value.
*
*/
public void setChallengeStatus(Map<String, Integer> challengeStatus)
{
this.challengeStatus = challengeStatus;
}
/**
* This method sets the challengesTimestamp value.
* @param challengesTimestamp the challengesTimestamp new value.
*
*/
public void setChallengesTimestamp(Map<String, Long> challengesTimestamp)
{
this.challengesTimestamp = challengesTimestamp;
}
/**
* This method sets the levelsDone value.
* @param levelsDone the levelsDone new value.
*
*/
public void setLevelsDone(Set<String> levelsDone)
{
this.levelsDone = levelsDone;
}
/**
* This method sets the history object value.
* @param history the history object new value.
*/
public void setHistory(List<LogEntry> history)
{
this.history = history;
}
// ---------------------------------------------------------------------
// Section: Other Methods
// ---------------------------------------------------------------------
/**
* Resets all challenges and levels in GameMode for this player
*
* @param gameMode GameMode which challenges must be reset.
*/
public void reset(@NonNull String gameMode)
{
challengeStatus.keySet().removeIf(n -> n.regionMatches(true, 0, gameMode, 0, gameMode.length()));
challengesTimestamp.keySet().removeIf(n -> n.regionMatches(true, 0, gameMode, 0, gameMode.length()));
levelsDone.removeIf(n -> n.regionMatches(true, 0, gameMode, 0, gameMode.length()));
}
/**
* Mark a challenge as having been completed. Will increment the number of times and
* timestamp
*
* @param challengeName - unique challenge name
*/
public void setChallengeDone(@NonNull String challengeName)
{
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());
}
/**
* Set the number of times a challenge has been done
*
* @param challengeName - unique challenge name
* @param times - the number of times to set
*/
public void setChallengeTimes(@NonNull String challengeName, @NonNull int times)
{
challengeStatus.put(challengeName, times);
challengesTimestamp.put(challengeName, System.currentTimeMillis());
}
/**
* Check if a challenge has been done
*
* @param challengeName - unique challenge name
* @return true if done at least once
*/
public boolean isChallengeDone(@NonNull String challengeName)
{
return this.getTimes(challengeName) > 0;
}
/**
* Check how many times a challenge has been done
*
* @param challengeName - unique challenge name
* @return - number of times
*/
public int getTimes(@NonNull String challengeName)
{
return challengeStatus.getOrDefault(challengeName, 0);
}
/**
* This method adds given level id to completed level set.
* @param uniqueId from ChallengeLevel object.
*/
public void addCompletedLevel(@NonNull String uniqueId)
{
this.levelsDone.add(uniqueId);
}
/**
* This method returns if given level is done.
* @param uniqueId of ChallengeLevel object.
* @return <code>true</code> if level is completed, otherwise <code>false</code>
*/
public boolean isLevelDone(@NonNull String uniqueId)
{
return !this.levelsDone.isEmpty() && this.levelsDone.contains(uniqueId);
}
/**
* This method adds given LogEntry to history.
*
* @param entry of type LogEntry
*/
public void addHistoryRecord(@NonNull LogEntry entry)
{
this.history.add(entry);
}
/**
* @see Object#hashCode()
* @return object hashCode value.
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode());
return result;
}
/**
* @see java.lang.Object#equals(java.lang.Object)
* @param obj Other object.
* @return boolean that indicate if objects are equals.
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof ChallengesPlayerData))
{
return false;
}
ChallengesPlayerData other = (ChallengesPlayerData) obj;
if (uniqueId == null)
{
return other.uniqueId == null;
}
else
{
return uniqueId.equalsIgnoreCase(other.uniqueId);
}
}
}