I’m implementing an Statistics + Achievements system. Basically the structure is:
- An Achievement have many related Statistics, this relations must associate each Achievement with the required Statistics (and it value). For example the Achievement1 needs the Statistic1 with value 50 (or greater) and the Statistic2 with value 100 (or greater).
- Given an Statistic I need also know what are the related Achievements (in order to check them when the Statistic changes.
Both Stats and Achievements have an unique id.
My problem is I don´t know whats the best data(s) structure(s) for representing that. By the way I’m using:
SparseArray<HashMap<Statistic, Integer>> statisticsForAnAchievement;
For the first point where the index of the array is the Achievement ID and the HashMap contains the Statistic/TargetValue pairs. And a:
SparseArray<Collection<Achievement>> achievementsRelatedToAStatistic;
For the second point where the index is the StatisticID and the item is the collection of Achievements related.
Then I need to handle both objects keeping it coherence.
Is there an easier way of representing that? Thanks
As a
Statistic(or a group ofStatistics) describes anAchievementshouldn’t that/thoseStatistic/s be stored in theAchievementclass? For example, an improvedAchievementclass:Also, the
Statisticclass should store its target(the 50 value for Statistic1) value in it as a field.The statistics are already stored in the achievments so all you have to do is store an array/list of ids of the achievments(or the achievements them self) and with this you’ll have access to the statistics that made those achievements.
You would use the above array/list of achievements, iterate them and check to see if the achievement holds that particular
Statistic:Another option is to have somewhere a static mapping that stores which achievements have a
Statistic, mapping that would get updated each time one of theaddStaticticorremoveStatisticmethods gets called.Regarding your code, if you don’t need the
Statisticobject and are happy with just holding a reference to itsidthen you could improve thestatisticsForAnAchievementwith: