123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package app.mar.game.challenges;
- import android.app.Activity;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.Serializable;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- import java.util.ArrayList;
- import app.mar.activities.ChallengeDriver;
- import app.mar.game.Game;
- public abstract class Challenge implements Serializable{
- public static final String CHALLENGE_NULL="null";
- protected String mType=CHALLENGE_NULL;
- protected String mName="null";
- protected char mCharPassword;
- protected int mCharPasswordIndex;
- protected boolean mDone=false;
- protected Class mStartWith = null;
- protected ArrayList<Indice> mIndices = new ArrayList<Indice>();
- protected int mLastIndiceDiscoveredIndex = 0;
- public Challenge(JSONObject root, Class start)
- {
- mStartWith=start;
- mDone = false;
- try {
- mType = root.getString("type");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- try {
- mName = root.getString("name");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- try {
- String s = root.getString("password_char");
- if(s.length()==0) throw new JSONException("password_char is an empty string");
- mCharPassword = s.charAt(0);
- mCharPasswordIndex = root.getInt("password_index");
- } catch (JSONException e) {
- mCharPassword=0;
- mCharPasswordIndex=-1;
- }
- try {
- JSONArray arr = root.getJSONArray("indices");
- for(int i=0; i<arr.length(); i++)
- mIndices.add(Indice.fromJSONObject(arr.getJSONObject(i)));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public void exec(ChallengeDriver act)
- {
- act.setFragment(mStartWith);
- }
- public String getType() {
- return mType;
- }
- public String getName() {
- return mName;
- }
- public abstract int getResultCode();
- public boolean isDone() {
- return mDone;
- }
- public void setDone()
- {
- mDone=true;
- }
- public static Class getChallengeClass(String classe)
- {
- try {
- Class cls = Class.forName("app.mar.game.challenges."+classe);
- return cls;
- }
- catch(ClassNotFoundException cnfe)
- {
- cnfe.printStackTrace();
- return null;
- }
- }
- public static Constructor<?> getChallengeConstructor(String classe)
- {
- try {
- Class[] type = { Game.class, JSONObject.class, Activity.class };
- return getChallengeClass(classe).getConstructor(type);
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static Challenge callChallengeConstructor(String classe, Game g,JSONObject o, Activity a)
- {
- Constructor constr = getChallengeConstructor(classe);
- try {
- return ((Challenge) constr.newInstance(g, o, a));
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return null;
- }
- public char getCharPassword() {
- return mCharPassword;
- }
- public int getCharPasswordIndex() {
- return mCharPasswordIndex;
- }
- public boolean hasNextIndice()
- {
- int i=0;
- for(i=0; i<mIndices.size(); i++)
- if(!mIndices.get(i).isDiscovered())
- return true;
- return false;
- }
- public Indice nextIndice()
- {
- int i=0;
- for(i=0; i<mIndices.size(); i++)
- if(!mIndices.get(i).isDiscovered())
- return mIndices.get(mLastIndiceDiscoveredIndex=i);
- mLastIndiceDiscoveredIndex=-1;
- return null;
- }
- public int getIndiceIndex(){
- return mLastIndiceDiscoveredIndex;
- }
- public ArrayList<Indice> getIndices() { return mIndices; }
- }
|