Android Open Source - dragon-wars Database






From Project

Back to project page dragon-wars.

License

The source code is released under:

GNU General Public License

If you think the Android project dragon-wars listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.group7.dragonwars.engine.Database;
/*  w  w w.j  a  v a  2 s  . c  om*/
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;

import java.util.ArrayList;
import java.util.List;

public final class Database {
    public class Entry {
        public Double DAMAGEDEALT;
        public Double DAMAGERECEIVED;
        public Double DISTANCETRAVELLED;
        public Integer GOLDCOLLECTED;
        public Integer UNITSKILLED;
        public Integer UNITSMADE;
    }

    private static final String DATABASE_NAME = "dragonwars.db";
    private static final String DATABASE_TABLE_NAME = "statistics";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table if not exists " + DATABASE_TABLE_NAME + " (" +
        " GAMETIME INT NOT NULL," +
        " DAMAGEDEALT DOUBLE NOT NULL," +
        " DAMAGERECEIVED DOUBLE NOT NULL," +
        " DISTANCETRAVELLED DOUBLE NOT NULL," +
        " GOLDCOLLECTED INT NOT NULL," +
        " UNITSKILLED INT NOT NULL," +
        " UNITSMADE INT NOT NULL," +
        " PRIMARY KEY(GAMETIME)" +
        ");";


    private SQLiteDatabase database;

    public Database(final Context con) {
        database = con.openOrCreateDatabase(DATABASE_NAME, 0, null);
        CreateTable();
    }

    public void CreateTable() {
        //creates the high_scores table
        database.execSQL(DATABASE_CREATE);
    }

    public void DeleteTable() {
        Close();
    }

    //should be called when the DB is no longer needed
    public void Close() {
        database.close();
    }

    //use to add a new high score to the database
    public void AddEntry(final Double damageDealt, final Double damageReceived,
                         final Double distanceTravelled,
                         final Integer goldCollected, final Integer unitsKilled,
                         final Integer unitsMade) {
        //create content values
        ContentValues values = new ContentValues();

        values.put("GAMETIME", System.currentTimeMillis());
        values.put("DAMAGEDEALT", damageDealt);
        values.put("DAMAGERECEIVED", damageReceived);
        values.put("DISTANCETRAVELLED", distanceTravelled);
        values.put("GOLDCOLLECTED", goldCollected);
        values.put("UNITSKILLED", unitsKilled);
        values.put("UNITSMADE", unitsMade);

        //add content values as a row
        database.insert(DATABASE_TABLE_NAME, null, values);
    }

    public List<Entry> GetEntries() {
        //gets all entries from the high scores table
        List<Entry> entries = new ArrayList<Entry>();

        //get cursor to DB from query
        String[] query = {"GAMETIME", "DAMAGEDEALT", "DAMAGERECEIVED",
                          "DISTANCETRAVELLED", "GOLDCOLLECTED",
                          "UNITSKILLED", "UNITSMADE"
                         };

        Cursor cursor = database.query(
                            DATABASE_TABLE_NAME,
                            query,
                            null, null, null, null, null);

        //count the number of entries
        Integer numberOfEntries = cursor.getCount();
        cursor.moveToFirst();

        for(Integer entry = 0; entry < numberOfEntries; entry++) {
            Entry record = new Entry();
            /* We don't care about the game time */
            record.DAMAGEDEALT       = cursor.getDouble(1);
            record.DAMAGERECEIVED    = cursor.getDouble(2);
            record.DISTANCETRAVELLED = cursor.getDouble(3);
            record.GOLDCOLLECTED     = cursor.getInt(4);
            record.UNITSKILLED       = cursor.getInt(5);
            record.UNITSMADE         = cursor.getInt(6);
            entries.add(record);
            cursor.moveToNext();
        }

        return entries;
    }

    public Entry GetSummedEntries() {
        List<Entry> entries = GetEntries();
        Entry rec = new Entry();
        rec.DAMAGEDEALT      = 0.0;;
        rec.DAMAGERECEIVED   = 0.0;;
        rec.DISTANCETRAVELLED = 0.0;;
        rec.GOLDCOLLECTED    = 0;
        rec.UNITSKILLED      = 0;
        rec.UNITSMADE        = 0;

        for (Entry ent : entries) {
            rec.DAMAGEDEALT      += ent.DAMAGEDEALT;
            rec.DAMAGERECEIVED   += ent.DAMAGERECEIVED;
            rec.DISTANCETRAVELLED += ent.DISTANCETRAVELLED;
            rec.GOLDCOLLECTED    += ent.GOLDCOLLECTED;
            rec.UNITSKILLED      += ent.UNITSKILLED;
            rec.UNITSMADE        += ent.UNITSMADE;
        }

        return rec;
    }
}




Java Source Code List

com.group7.dragonwars.DrawingThread.java
com.group7.dragonwars.GameActivity.java
com.group7.dragonwars.GameView.java
com.group7.dragonwars.HelpActivity.java
com.group7.dragonwars.IsAiAdapter.java
com.group7.dragonwars.MainMenuActivity.java
com.group7.dragonwars.MapSelectActivity.java
com.group7.dragonwars.PlayerSelectActivity.java
com.group7.dragonwars.Results.java
com.group7.dragonwars.StatisticsActivity.java
com.group7.dragonwars.engine.BasicMapInfo.java
com.group7.dragonwars.engine.BitmapChanger.java
com.group7.dragonwars.engine.Building.java
com.group7.dragonwars.engine.DrawableMapObject.java
com.group7.dragonwars.engine.FloatPair.java
com.group7.dragonwars.engine.FuncEx.java
com.group7.dragonwars.engine.Func.java
com.group7.dragonwars.engine.GameField.java
com.group7.dragonwars.engine.GameFinishedException.java
com.group7.dragonwars.engine.GameMap.java
com.group7.dragonwars.engine.GameState.java
com.group7.dragonwars.engine.InformationState.java
com.group7.dragonwars.engine.Logic.java
com.group7.dragonwars.engine.MapReader.java
com.group7.dragonwars.engine.Pair.java
com.group7.dragonwars.engine.PlayerAI.java
com.group7.dragonwars.engine.Player.java
com.group7.dragonwars.engine.Position.java
com.group7.dragonwars.engine.RangedUnit.java
com.group7.dragonwars.engine.Statistics.java
com.group7.dragonwars.engine.Unit.java
com.group7.dragonwars.engine.Database.Database.java
com.group7.dragonwars.engine.GoalArbitration.AtomicAction.java
com.group7.dragonwars.engine.GoalArbitration.AttackAt.java
com.group7.dragonwars.engine.GoalArbitration.BuildUnit.java
com.group7.dragonwars.engine.GoalArbitration.MoveTo.java
com.group7.dragonwars.engine.GoalArbitration.Node.java
com.group7.dragonwars.engine.GoalArbitration.StateTree.java