2012-07-16 78 views
0

我正在实施统计+成就系统。基本结构是:统计和成就系统的数据结构

  • 的成就有很多相关的统计,这个关系必须与所需统计的每个成就关联(和它的值)。例如,Achievement1需要Statistic1的值为50(或更高),Statistic2的值为100(或更高)。
  • 给定一个统计我也需要知道什么是相关成就(以检查他们的统计变化的时候。

两个统计和成就有一个唯一的ID。

我的问题是我不'T知道最新最好的数据(一个或多个)结构的(一个或多个),用于表示顺便说我使用:

SparseArray<HashMap<Statistic, Integer>> statisticsForAnAchievement; 

对于第一点,其中阵列的索引是成就ID和HashMap中包含Statistic/TargetValue对。和:

SparseArray<Collection<Achievement>> achievementsRelatedToAStatistic; 

对于第二点,其中指数是StatisticID与产品相关的成果集。

然后我需要处理这两个对象保持一致性。

是否有更容易的方式来表示?感谢

回答

1

作为Statistic(或一组Statistics)描述了一种Achievement不应该/这些Statistic /秒被存储在Achievement类?例如,改进的Achievement类:

public class Achievement { 
    SparseArray<Statistic> mStatistics = new SparseArray<Statistic>(); 

    // to get a reference to the statisctics that make this achievement 
    public SparseArray<Statistic> getStatics() { 
     return mStatistics; 
    } 

    // add a new Statistic to these Achievement 
    public void addStatistic(int statisticId, Statistic newStat) { 
     // if we don't already have this particular statistic, add it 
     // or maybe update the underlining Statistic?!? 
     if (mStatistics.get(statisticId) == null) { 
      mStatistic.add(newStat); 
     } 
    } 

    // remove the Statistic 
    public void removeStatistic(int statisticId) { 
     mStatistic.delete(statisticId); 
    } 

    // check to see if this achievment has a statistic with this id 
    public boolean hasStatistics(int statisticId) { 
     return mStatistic.get(statisticId) == null ? false : true; 
    } 

    // rest of your code 
} 

此外,Statistic类应该存储它的目标(50值Statistic1)值在它作为一个字段。

的成就有很多相关的统计,这个关系必须 关联的每个成就所要求的统计(和它 值)。例如,Achievement1需要Statistic1的值为 50(或更高),Statistic2的值为100(或更高)。

的统计数据已经存储在个成就因此,所有你需要做的是保存个成就(或成就他们自己)的ID数组/列表,这样,你将有机会获得的统计数据取得了这些成就。

鉴于一个统计我还需要知道什么是相关的成就 (为了检查他们统计变化时。

你会使用的成就上述阵列/列表,迭代和检查,看是否实现认为特别Statistic

ArrayList<Achievement> relatedAchievements = new ArrayList<Achievement>(); 
for (Achievement a : theListOfAchievments) { 
    if (a.hasStatistics(targetStatistic)) { 
      relatedAchievements.add(a); // at the end this will store the achievements related(that contain) the targetStatistic 
    } 
} 

另一种选择是有地方静态映射,其存储成就有一个Statistic,映射将在每次调用addStaticticremoveStatistic方法时得到更新。

关于你的代码,如果不需要Statistic对象,并很高兴与只是抱着它id参考,那么你可以提高statisticsForAnAchievement有:

SparseArray<SparseIntArray> statisticsForAnAchievement; 
// the index of the SparseArray is the Achievement's id 
// the index of the SparseIntArray is the Statistic's id 
// the value of the SparseIntArray is the Statistic's value