Android Open Source - minecraft-connect-4 Stats






From Project

Back to project page minecraft-connect-4.

License

The source code is released under:

MIT License

If you think the Android project minecraft-connect-4 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

/* COPYRIGHT (C) 2014 Aleksandr Belkin. All Rights Reserved. */
package sq.squ1rr.mcc4;
//w w w.  j  a  va 2 s. c o m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Class saves and shows the statistics.
 * @author squ1rr
 */
public class Stats extends SQLiteOpenHelper {
    /*
     * Stats IDs
     */
    public static final int STATS_GAMES       = 0;
    public static final int STATS_PVPS        = 1;
    public static final int STATS_WINS        = 2;
    public static final int STATS_DEFEATS     = 3;
    public static final int STATS_TIME        = 4;
    public static final int STATS_DRAWS       = 5;
    
    /*
     * Database constants
     */
    /** database version */
    private static final int DATABASE_VERSION = 1;
     
    /** database name */
    private static final String DATABASE_NAME = "Stats";
 
    /** database table */
    private static final String TABLE_STATS = "stats";
    
    /*
     * Columns
     */
    private static final String KEY_ID = "id";
    private static final String KEY_VALUE = "value";
    
    /**
     * Create database
     * @param context
     */
    public Stats(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    /*
     * (non-Javadoc)
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_STATS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_VALUE + " INTEGER)";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
 
    /*
     * (non-Javadoc)
     * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS);
        onCreate(db);
    }
    
    /**
     * Insert a row in a table
     * @param id
     * @param value
     */
    private void insert(int id, int value) {
        SQLiteDatabase db = this.getWritableDatabase();
     
        ContentValues values = new ContentValues();
        values.put(KEY_ID, id);
        values.put(KEY_VALUE, value);
     
        db.insert(TABLE_STATS, null, values);
        db.close();
    }
    
    /**
     * Update a rew in a table
     * @param id
     * @param value
     * @return
     */
    private int update(int id, int value) {
        SQLiteDatabase db = this.getWritableDatabase();
        
        ContentValues values = new ContentValues();
        values.put(KEY_VALUE, value);
     
        int result = db.update(TABLE_STATS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(id) });
        
        db.close();
        
        return result;
    }
    
    /**
     * Clear all info
     */
    public void deleteAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_STATS, null, null);
        db.close();
    }
    
    /**
     * Gets a row value if it is present, -1 otherwise
     * @param id
     * @return
     */
    public int get(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
     
        int result = -1;
        Cursor cursor = db.query(TABLE_STATS, new String[] { KEY_VALUE },
                KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if(cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            result = cursor.getInt(0);
        }
        
        if(cursor != null) cursor.close();

        db.close();
        return result;
    }
    
    /**
     * Updates a row if it's present (by adding value to it),
     * adds new row otherwise
     * @param id
     * @param value
     */
    public void add(int id, int value) {
        int old = get(id);
        if(old != -1) {
            value += old;
            update(id, value);
        } else {
            insert(id, value);
        }
    }
}




Java Source Code List

sq.squ1rr.mcc4.AboutMenu.java
sq.squ1rr.mcc4.BaseActivity.java
sq.squ1rr.mcc4.GameActivity.java
sq.squ1rr.mcc4.MainMenuActivity.java
sq.squ1rr.mcc4.MainMenu.java
sq.squ1rr.mcc4.MenuLayout.java
sq.squ1rr.mcc4.OptionsMenu.java
sq.squ1rr.mcc4.QuickGameMenu.java
sq.squ1rr.mcc4.StatsMenu.java
sq.squ1rr.mcc4.Stats.java
sq.squ1rr.mcc4.ai.Ai.java
sq.squ1rr.mcc4.ai.EasyAi.java
sq.squ1rr.mcc4.ai.HardAi.java
sq.squ1rr.mcc4.ai.NormalAi.java
sq.squ1rr.mcc4.ai.PeacefulAi.java
sq.squ1rr.mcc4.board.BoardDialogue.java
sq.squ1rr.mcc4.board.BoardLogic.java
sq.squ1rr.mcc4.board.BoardRenderer.java
sq.squ1rr.mcc4.board.BoardView.java
sq.squ1rr.mcc4.board.GameBoard.java
sq.squ1rr.mcc4.board.Texture.java
sq.squ1rr.mcc4.gl.Rectangle.java
sq.squ1rr.mcc4.gl.SpriteBatch.java
sq.squ1rr.mcc4.gl.SpriteString.java
sq.squ1rr.mcc4.gl.Sprite.java
sq.squ1rr.mcc4.layout.LayoutManager.java
sq.squ1rr.mcc4.layout.McButton.java
sq.squ1rr.mcc4.layout.McGroup.java
sq.squ1rr.mcc4.layout.McSelector.java
sq.squ1rr.mcc4.layout.McStyle.java
sq.squ1rr.mcc4.layout.McText.java
sq.squ1rr.mcc4.layout.McToggler.java
sq.squ1rr.mcc4.layout.McToken.java
sq.squ1rr.mcc4.rules.GameRules.java
sq.squ1rr.mcc4.rules.Player.java
sq.squ1rr.mcc4.rules.Rule.java
sq.squ1rr.mcc4.util.GlobalConstants.java