Challenge.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package app.mar.game.challenges;
  2. import android.app.Activity;
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import java.io.Serializable;
  7. import java.lang.reflect.Constructor;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.util.ArrayList;
  10. import app.mar.activities.ChallengeDriver;
  11. import app.mar.game.Game;
  12. public abstract class Challenge implements Serializable{
  13. public static final String CHALLENGE_NULL="null";
  14. protected String mType=CHALLENGE_NULL;
  15. protected String mName="null";
  16. protected char mCharPassword;
  17. protected int mCharPasswordIndex;
  18. protected boolean mDone=false;
  19. protected Class mStartWith = null;
  20. protected ArrayList<Indice> mIndices = new ArrayList<Indice>();
  21. protected int mLastIndiceDiscoveredIndex = 0;
  22. public Challenge(JSONObject root, Class start)
  23. {
  24. mStartWith=start;
  25. mDone = false;
  26. try {
  27. mType = root.getString("type");
  28. } catch (JSONException e) {
  29. e.printStackTrace();
  30. }
  31. try {
  32. mName = root.getString("name");
  33. } catch (JSONException e) {
  34. e.printStackTrace();
  35. }
  36. try {
  37. String s = root.getString("password_char");
  38. if(s.length()==0) throw new JSONException("password_char is an empty string");
  39. mCharPassword = s.charAt(0);
  40. mCharPasswordIndex = root.getInt("password_index");
  41. } catch (JSONException e) {
  42. mCharPassword=0;
  43. mCharPasswordIndex=-1;
  44. }
  45. try {
  46. JSONArray arr = root.getJSONArray("indices");
  47. for(int i=0; i<arr.length(); i++)
  48. mIndices.add(Indice.fromJSONObject(arr.getJSONObject(i)));
  49. } catch (JSONException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. public void exec(ChallengeDriver act)
  54. {
  55. act.setFragment(mStartWith);
  56. }
  57. public String getType() {
  58. return mType;
  59. }
  60. public String getName() {
  61. return mName;
  62. }
  63. public abstract int getResultCode();
  64. public boolean isDone() {
  65. return mDone;
  66. }
  67. public void setDone()
  68. {
  69. mDone=true;
  70. }
  71. public static Class getChallengeClass(String classe)
  72. {
  73. try {
  74. Class cls = Class.forName("app.mar.game.challenges."+classe);
  75. return cls;
  76. }
  77. catch(ClassNotFoundException cnfe)
  78. {
  79. cnfe.printStackTrace();
  80. return null;
  81. }
  82. }
  83. public static Constructor<?> getChallengeConstructor(String classe)
  84. {
  85. try {
  86. Class[] type = { Game.class, JSONObject.class, Activity.class };
  87. return getChallengeClass(classe).getConstructor(type);
  88. } catch (NoSuchMethodException e) {
  89. e.printStackTrace();
  90. }
  91. return null;
  92. }
  93. public static Challenge callChallengeConstructor(String classe, Game g,JSONObject o, Activity a)
  94. {
  95. Constructor constr = getChallengeConstructor(classe);
  96. try {
  97. return ((Challenge) constr.newInstance(g, o, a));
  98. } catch (InstantiationException e) {
  99. e.printStackTrace();
  100. } catch (IllegalAccessException e) {
  101. e.printStackTrace();
  102. } catch (InvocationTargetException e) {
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. public char getCharPassword() {
  108. return mCharPassword;
  109. }
  110. public int getCharPasswordIndex() {
  111. return mCharPasswordIndex;
  112. }
  113. public boolean hasNextIndice()
  114. {
  115. int i=0;
  116. for(i=0; i<mIndices.size(); i++)
  117. if(!mIndices.get(i).isDiscovered())
  118. return true;
  119. return false;
  120. }
  121. public Indice nextIndice()
  122. {
  123. int i=0;
  124. for(i=0; i<mIndices.size(); i++)
  125. if(!mIndices.get(i).isDiscovered())
  126. return mIndices.get(mLastIndiceDiscoveredIndex=i);
  127. mLastIndiceDiscoveredIndex=-1;
  128. return null;
  129. }
  130. public int getIndiceIndex(){
  131. return mLastIndiceDiscoveredIndex;
  132. }
  133. public ArrayList<Indice> getIndices() { return mIndices; }
  134. }