1. littledot5566
  2. LocalTreasure

Commits

littledot5566  committed 14f4aed

*Fixed several bugs causing FC. *Implemented custom distance limit to prevent cheating.

  • Participants
  • Parent commits 6e53991
  • Branches master

Comments (0)

Files changed (9)

File release/TIL.txt

View file
 
 > Android SQLiteDatabase.rawQuery() cannot substitue table names and column names with "?".
 X: db.rawQuery("select * from ? where ?=?", new String[]{"profile", "pID", "1"});
-O: db.rawQuery(String.format("select * from %s where %s=%d", "profile", "pID", 1));
+O: db.rawQuery(String.format("select * from %s where %s=%d", "profile", "pID", 1));
+
+> Floating point numbers have a maximum value of 10E9.

File res/layout/selectdistance_act_layout.xml

View file
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:hint="@string/distance_et_custom"
-            android:inputType="number"
+            android:inputType="numberDecimal"
             android:visibility="gone" />
 
         <Button

File src/nctuw/littledot/localtreasure/ExpeditionActivity.java

View file
 			Bundle extras = getIntent().getExtras();
 			String source = extras.getString(Const.BUNDLE_SOURCE);
 
-			if (source.equals(SelectDistanceActivity.class)) {
-				// initialize Expedition
+			if (source.equals(SelectDistanceActivity.class.getName())) {
+				// initialize new Expedition
 				mExpedition = new Expedition();
 				mExpedition.setPID(mSP.getActiveProfileID());
 				mExpedition.setStartTS(System.currentTimeMillis());
 				destMarker.addLocation(mExpedition.getDestLoc(), "resume-Dest",
 						mExpedition.getDestLoc().toString());
 
+				isCalibrated = true;
 			}
 			Leg.a(mExpedition.toString());
 
 		// determine if player is close enough to win
 		if (location.distanceTo(mExpedition.getDestLoc()) < mExpedition
 				.getDistance() * 0.05) {
+			Leg.a("apart=" + location.distanceTo(mExpedition.getDestLoc())
+					+ " required=" + mExpedition.getDistance() * 0.05);
 			mExpedition.setEndTS(System.currentTimeMillis());
 			mExpedition.setPrize(Treasure.generateReward());
 

File src/nctuw/littledot/localtreasure/components/ExpeditionAdapter.java

-package nctuw.littledot.localtreasure.components;
-
-import java.util.ArrayList;
-
-import nctuw.littledot.localtreasure.R;
-import nctuw.littledot.localtreasure.database.Database;
-import nctuw.littledot.localtreasure.database.Expedition;
-import nctuw.littledot.localtreasure.database.Profile;
-import nctuw.littledot.util.Leg;
-import android.content.ContentValues;
-import android.content.Context;
-import android.database.Cursor;
-import android.database.DatabaseUtils;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.BaseAdapter;
-import android.widget.TextView;
-
-public class ExpeditionAdapter extends BaseAdapter {
-	private Context mCtx;
-	private Database mDB;
-
-	private ArrayList<ContentValues> mData = new ArrayList<ContentValues>();
-
-	public ExpeditionAdapter(Context ctx) {
-		mCtx = ctx;
-		mDB = new Database(ctx).open(Database.MODE_READ);
-
-		Cursor cur = mDB.rawQuery(String
-				.format("select * from %s, %s where %s.%s=%s.%s order by %s",
-						Expedition.TABLE_NAME, Profile.TABLE_NAME,
-						Expedition.TABLE_NAME, Expedition.KEY_USER_ID,
-						Profile.TABLE_NAME, Profile.KEY_ID,
-						Expedition.KEY_START_TIMESTAMP));
-
-		if (!cur.moveToFirst())
-			throw new IllegalAccessError("bad sql");
-
-		while (!cur.isAfterLast()) {
-			Leg.a(DatabaseUtils.dumpCurrentRowToString(cur));
-			ContentValues kv = Expedition.toContentValues(cur);
-			kv.putAll(Profile.toContentValues(cur));
-			mData.add(kv);
-
-			cur.moveToNext();
-		}
-
-		cur.close();
-		mDB.close();
-	}
-
-	public int getCount() {
-		return mData.size();
-	}
-
-	public ContentValues getItem(int position) {
-		return mData.get(position);
-	}
-
-	public long getItemId(int position) {
-		return mData.get(position).getAsLong(Expedition.KEY_ID);
-	}
-
-	public View getView(int position, View view, ViewGroup parent) {
-		if (view == null) {
-			LayoutInflater inflater = (LayoutInflater) mCtx
-					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-			view = inflater.inflate(R.layout.history_item, parent, false);
-		}
-
-		ContentValues kv = mData.get(position);
-
-		TextView tvUser = (TextView) view.findViewById(R.id.hItem_tv_user);
-		TextView tvStTime = (TextView) view.findViewById(R.id.hItem_tv_stTime);
-
-		tvUser.setText(kv.getAsString(Profile.KEY_NAME));
-		tvStTime.setText(kv.getAsLong(Expedition.KEY_START_TIMESTAMP).toString());
-
-		return view;
-	}
-}

File src/nctuw/littledot/localtreasure/components/HistoryActivity.java

View file
 public class HistoryActivity extends Activity {
 	private ListView lvHistory;
 
-	private ExpeditionAdapter mAdapter;
+	private HistoryActivityAdapter mAdapter;
 
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		setContentView(R.layout.history_act_layout);
 		lvHistory = (ListView) findViewById(R.id.history_lv_expeditions);
 
-		mAdapter = new ExpeditionAdapter(this);
+		mAdapter = new HistoryActivityAdapter(this);
 		lvHistory.setAdapter(mAdapter);
 	}
 

File src/nctuw/littledot/localtreasure/components/HistoryActivityAdapter.java

View file
+package nctuw.littledot.localtreasure.components;
+
+import java.util.ArrayList;
+
+import nctuw.littledot.localtreasure.R;
+import nctuw.littledot.localtreasure.database.Database;
+import nctuw.littledot.localtreasure.database.Expedition;
+import nctuw.littledot.localtreasure.database.Profile;
+import nctuw.littledot.util.Leg;
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.DatabaseUtils;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.TextView;
+
+public class HistoryActivityAdapter extends BaseAdapter {
+	private Context mCtx;
+	private Database mDB;
+
+	private ArrayList<ContentValues> mData = new ArrayList<ContentValues>();
+
+	private ArrayList<String> dbg = new ArrayList<String>();
+
+	public HistoryActivityAdapter(Context ctx) {
+		mCtx = ctx;
+		mDB = new Database(ctx).open(Database.MODE_READ);
+
+		Cursor cur = mDB.rawQuery(String
+				.format("select * from %s, %s where %s.%s=%s.%s order by %s",
+						Expedition.TABLE_NAME, Profile.TABLE_NAME,
+						Expedition.TABLE_NAME, Expedition.KEY_USER_ID,
+						Profile.TABLE_NAME, Profile.KEY_ID,
+						Expedition.KEY_START_TIMESTAMP));
+
+		if (!cur.moveToFirst())
+			throw new IllegalAccessError("bad sql");
+
+		while (!cur.isAfterLast()) {
+			Leg.a(DatabaseUtils.dumpCurrentRowToString(cur));
+			ContentValues kv = Expedition.toContentValues(cur);
+			kv.putAll(Profile.toContentValues(cur));
+			mData.add(kv);
+			dbg.add(DatabaseUtils.dumpCurrentRowToString(cur));
+
+			cur.moveToNext();
+		}
+
+		cur.close();
+		mDB.close();
+	}
+
+	public int getCount() {
+		return mData.size();
+	}
+
+	public ContentValues getItem(int position) {
+		return mData.get(position);
+	}
+
+	public long getItemId(int position) {
+		return mData.get(position).getAsLong(Expedition.KEY_ID);
+	}
+
+	public View getView(int position, View view, ViewGroup parent) {
+		if (view == null) {
+			LayoutInflater inflater = (LayoutInflater) mCtx
+					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+			view = inflater.inflate(R.layout.history_item, parent, false);
+		}
+
+		ContentValues kv = mData.get(position);
+
+		TextView tvUser = (TextView) view.findViewById(R.id.hItem_tv_user);
+		TextView tvStTime = (TextView) view.findViewById(R.id.hItem_tv_stTime);
+
+		// tvUser.setText(kv.getAsString(Profile.KEY_NAME));
+		// tvStTime.setText(kv.getAsLong(Expedition.KEY_START_TIMESTAMP).toString());
+		tvUser.setText(dbg.get(position));
+
+		return view;
+	}
+}

File src/nctuw/littledot/localtreasure/components/ProfileActivity.java

View file
 	private TextView tvTimeTotal;
 	private GridView gvTreasure;
 
-	private Database mDB = new Database(this);
+	private Database mDB;
 	private Profile mProfile;
 
 	@Override
 		mProfile = bundle.getParcelable(Const.BUNDLE_PROFILE);
 		Leg.a(mProfile.toString());
 
+		mDB = new Database(this).open(Database.MODE_WRITE);
+
 		tvName.setText(mProfile.getName());
 		tvExpeditionTotal.setText("Total expeditions="
 				+ mProfile.getTotalExpeditions());

File src/nctuw/littledot/localtreasure/components/SelectDistanceActivity.java

View file
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.Toast;
 
 public class SelectDistanceActivity extends Activity {
 
 		if (et.getVisibility() == View.GONE) {
 			et.setVisibility(View.VISIBLE);
 			but.setText("OK!");
+
 		} else {
-			Intent intent = new Intent(this, ExpeditionActivity.class);
-			intent.putExtra(Const.BUNDLE_SOURCE, getClass().getName());
-			intent.putExtra(Const.BUNDLE_DISTANCE,
-					Float.parseFloat(et.getText().toString()));
-			startActivity(intent);
+			float d = Float.parseFloat(et.getText().toString());
+
+			// cannot > 1/2 * circumference of Earth (40075km)
+			if (d > 20000000) {
+				d = 20000000;
+				et.setText("" + d);
+				Toast.makeText(this, "Custom distance must < 20000km",
+						Toast.LENGTH_LONG).show();
+
+			} else {
+				Intent intent = new Intent(this, ExpeditionActivity.class);
+				intent.putExtra(Const.BUNDLE_SOURCE, getClass().getName());
+				intent.putExtra(Const.BUNDLE_DISTANCE, d);
+				startActivity(intent);
+			}
+
 		}
 	}
 }

File src/nctuw/littledot/localtreasure/database/Expedition.java

View file
 			+ KEY_USER_ID // long
 			+ " integer not null,"
 			+ KEY_DISTANCE // float
-			+ " real not null default 0,"
+			+ " integer not null,"
 			+ KEY_TRAVELED // float
 			+ " real default 0,"
 			+ KEY_START_LAT // double
 	 */
 	public Expedition(Cursor cur) {
 		Leg.dumpCursor(cur);
-		Location loc = new Location("");
+		Location loc;
 		int i;
 
 		setEID(eID);
 			setTravelled(cur.getFloat(i));
 
 		if ((i = Database.getIndex(cur, Expedition.KEY_START_LAT)) > -1) {
+			loc = new Location("");
 			loc.setLatitude(cur.getDouble(i));
 			loc.setLongitude(cur.getDouble(cur
 					.getColumnIndex(Expedition.KEY_START_LONG)));
 		}
 
 		if ((i = Database.getIndex(cur, Expedition.KEY_END_LAT)) > -1) {
+			loc = new Location("");
 			loc.setLatitude(cur.getDouble(i));
 			loc.setLongitude(cur.getDouble(cur
 					.getColumnIndex(Expedition.KEY_END_LONG)));
 	}
 
 	public String toString() {
-		StringBuilder sb = new StringBuilder("ID=").append(eID);
-		sb.append(" pID=").append(pID);
+		String sb = "ID=" + eID + " pID=" + pID;
 
 		if (mStartLoc != null)
-			sb.append(" stLoc=(").append(mStartLoc.getLatitude()).append(",")
-					.append(mStartLoc.getLongitude()).append(")");
+			sb += " stLoc=(" + mStartLoc.getLatitude() + ","
+					+ mStartLoc.getLongitude() + ")";
 		if (mDestLoc != null)
-			sb.append(" destLoc=(").append(mDestLoc.getLatitude()).append(",")
-					.append(mDestLoc.getLongitude()).append(")");
-		sb.append(" dist=").append(mDistance);
-		sb.append(" tra=").append(mTravelled);
-		sb.append(" stTS=").append(new Date(mStartTS).toString());
-		sb.append(" endTS=").append(new Date(mEndTS).toString());
+			sb += " destLoc=(" + mDestLoc.getLatitude() + ","
+					+ mDestLoc.getLongitude() + ")";
+		sb += " dist=" + mDistance
+				+ " tra=" + mTravelled
+				+ " stTS=" + new Date(mStartTS).toString()
+				+ " endTS=" + new Date(mEndTS).toString();
 		if (mPrize != null)
-			sb.append(" prize=").append(mPrize.toString());
+			sb += " prize=" + mPrize.toString();
 
 		return sb.toString();
 	}