Android Open Source - MythTrack Hero Helper






From Project

Back to project page MythTrack.

License

The source code is released under:

MIT License

If you think the Android project MythTrack 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

/**
 * HeroHelper simplifies the management of the "Heroes" table in the database.
 *// w w w .  jav a2s.  c  o  m
 * @author Nolan Jurgens
 */

package nolanjurgens.mythtrack.app;

// IMPORTS /////////////////////////////////////////////////////////////////////////////////////////
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;

import nolanjurgens.mythtrack.R;
import nolanjurgens.mythtrack.provider.MythTrackContract;

////////////////////////////////////////////////////////////////////////////////////////////////////
// CLASS - HeroHelper                                                                             //
////////////////////////////////////////////////////////////////////////////////////////////////////
public class HeroHelper
{
  // CONSTANTS /////////////////////////////////////////////////////////////////////////////////////

  public static final int EQUIPMENT_CHANGE_SUCCEEDED = 1;
  public static final int EQUIPMENT_CHANGE_WOULD_KILL = 2;

  // FIELDS ////////////////////////////////////////////////////////////////////////////////////////

  /** The parent context.*/
  private Context context;
  /** Object for accessing Android resources.*/
  private Resources resources;
  /** Helper for accessing the "Items" table.*/
  private ItemHelper items;

  // METHODS ///////////////////////////////////////////////////////////////////////////////////////

  /**
   * Constructor
   * @param context The parent activity/context.
   */
  public HeroHelper(Context context)
  {
    // Get handle to the context and resources.
    this.context = context;
    resources = context.getResources();

    // Initialize the item helper.
    items = new ItemHelper(context);
  }


  /**
   * Add a new hero to the database.
   * @param heroName The hero's name.
   * @param heroClassID The hero's class ID.
   * @return The URI for the new hero.
   */
  public Uri addHero(String heroName, int heroClassID)
  {
    ContentValues newHero = new ContentValues();

    // Set the name and class ID.
    newHero.put(MythTrackContract.Heroes.COLUMN_NAME, heroName);
    newHero.put(MythTrackContract.Heroes.COLUMN_CLASS_ID, heroClassID);

    // Set base stats by class.
    switch(heroClassID)
    {
      case MythTrackContract.Heroes.ACOLYTE:
      {
        // Set the Acolyte's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.FAITH);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.APPRENTICE:
      {
        // Set the Apprentice's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.ARCANE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 5);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 5);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.ARCHER:
      {
        // Set the Archer's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.NATURE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 5);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 5);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.BRIGAND:
      {
        // Set the Brigand's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.GUILE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.DRUID:
      {
        // Set the Druid's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.NATURE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 3);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 3);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.SKALD:
      {
        // Set the Skald's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.FAITH);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 5);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 5);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.SOLDIER:
      {
        // Set the Soldier's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.RAGE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 7);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 7);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.SPRIGGAN:
      {
        // Set the Spriggan's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.NATURE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 3);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 3);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      case MythTrackContract.Heroes.TRICKSTER:
      {
        // Set the Trickster's base stats.
        newHero.put(MythTrackContract.Heroes.COLUMN_ATTRIBUTE, MythTrackContract.Heroes.GUILE);
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 2);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 6);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 3);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        // Initialize the equipment slots as empty.
        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
      default:
      {
        // Put zeros in all fields when class doesn't match up.
        newHero.put(MythTrackContract.Heroes.COLUMN_MOVEMENT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_BASE_VITALITY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_COURAGE, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_THREAT, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_GOLD, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, 0);

        newHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_HELM, 0);
        newHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, 0);

        // No status effects.
        newHero.put(MythTrackContract.Heroes.COLUMN_STATUS_EFFECTS, 0);

        // No title.
        newHero.put(MythTrackContract.Heroes.COLUMN_TITLE, 0);

        break;
      }
    }

    // Save the URI for the added hero.
    Uri heroURI = context.getContentResolver().insert(MythTrackContract.Heroes.CONTENT_URI,
      newHero);

    // Parse the ID for the added hero.
    long heroID = Long.parseLong(heroURI.getLastPathSegment());

    // Get a cursor containing all starting gear.
    Cursor startingGear = context.getContentResolver().query(MythTrackContract.Items.CONTENT_URI,
      MythTrackContract.Items.PROJECTION_STARTING_GEAR,
      MythTrackContract.Items.SELECTION_STARTING_GEAR, null, null);

    // Get the item ID and name columns.
    final int idColumn = startingGear.getColumnIndex(MythTrackContract.Items._ID);
    final int nameColumn = startingGear.getColumnIndex(MythTrackContract.Items.COLUMN_NAME);

    // Equipment to update hero with.
    ContentValues heroEquipment = new ContentValues();

    // TODO - Add expansion classes' starting gear.

    // Set starting gear (equipment and inventory) by class.
    switch(heroClassID)
    {
      case MythTrackContract.Heroes.ACOLYTE:
      {
        // Set the Acolyte's starting gear.
        while(startingGear.moveToNext())
        {
          String currentItemName = startingGear.getString(nameColumn);

          // Primary
          if(currentItemName.equals(resources.getString(R.string.acolyte_starting_primary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_PRIMARY, inventoryID);
          }
          // Secondary
          if(currentItemName.equals(resources.getString(R.string.acolyte_starting_secondary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_SECONDARY, inventoryID);
          }
          // Armor
          if(currentItemName.equals(resources.getString(R.string.acolyte_starting_armor)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_ARMOR, inventoryID);
          }
        }

        break;
      }
      case MythTrackContract.Heroes.APPRENTICE:
      {
        // Set the Apprentice's starting gear.
        while(startingGear.moveToNext())
        {
          String currentItemName = startingGear.getString(nameColumn);

          // Primary
          if(currentItemName.equals(resources.getString(R.string.apprentice_starting_primary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_PRIMARY, inventoryID);
          }
          // Secondary
          if(currentItemName.equals(resources.getString(R.string.apprentice_starting_secondary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_SECONDARY, inventoryID);
          }
          // Armor
          if(currentItemName.equals(resources.getString(R.string.apprentice_starting_armor)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_ARMOR, inventoryID);
          }
        }

        break;
      }
      case MythTrackContract.Heroes.ARCHER:
      {
        // Set the Archer's starting gear.
        while(startingGear.moveToNext())
        {
          String currentItemName = startingGear.getString(nameColumn);

          // Primary
          if(currentItemName.equals(resources.getString(R.string.archer_starting_primary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_PRIMARY, inventoryID);
          }
          // Secondary
          if(currentItemName.equals(resources.getString(R.string.archer_starting_secondary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_SECONDARY, inventoryID);
          }
          // Armor
          if(currentItemName.equals(resources.getString(R.string.archer_starting_armor)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_ARMOR, inventoryID);
          }
        }

        break;
      }
      case MythTrackContract.Heroes.BRIGAND:
      {
        // Set the Brigand's starting gear.
        while(startingGear.moveToNext())
        {
          String currentItemName = startingGear.getString(nameColumn);

          // Primary
          if(currentItemName.equals(resources.getString(R.string.brigand_starting_primary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_PRIMARY, inventoryID);
          }
          // Armor
          if(currentItemName.equals(resources.getString(R.string.brigand_starting_armor)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_ARMOR, inventoryID);
          }
          // Accessory
          if(currentItemName.equals(resources.getString(R.string.brigand_starting_accessory)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, inventoryID);
          }
        }

        break;
      }
      case MythTrackContract.Heroes.SOLDIER:
      {
        // Set the Soldier's starting gear.
        while(startingGear.moveToNext())
        {
          String currentItemName = startingGear.getString(nameColumn);

          // Primary
          if(currentItemName.equals(resources.getString(R.string.soldier_starting_primary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_PRIMARY, inventoryID);
          }
          // Secondary
          if(currentItemName.equals(resources.getString(R.string.soldier_starting_secondary)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_SECONDARY, inventoryID);
          }
          // Armor
          if(currentItemName.equals(resources.getString(R.string.soldier_starting_armor)))
          {
            // Add the item to the hero's inventory.
            long itemID = startingGear.getLong(idColumn);
            ContentValues inventoryItem = new ContentValues();
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID);
            inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID);
            Uri inventoryURI = context.getContentResolver().insert(
              MythTrackContract.Inventories.CONTENT_URI, inventoryItem);

            // Parse the ID for the added inventory item.
            long inventoryID = Long.parseLong(inventoryURI.getLastPathSegment());

            // Equip the item.
            heroEquipment.put(MythTrackContract.Heroes.COLUMN_ARMOR, inventoryID);
          }
        }

        break;
      }
      default:
      {
        // If class doesn't match, don't add any gear.
        break;
      }
    }

    // Close the cursor.
    startingGear.close();

    // Update the hero's equipped items with the inventory IDs.
    if(heroEquipment.size() != 0)
    {
      context.getContentResolver().update(heroURI, heroEquipment, null, null);
    }

    return heroURI;
  }


  /**
   * Changes the specified hero's equipment of the specified type (both inventory ID and Item) to
   * the given inventory ID and corresponding its Item. Use an ID of zero to unequip an item
   * without equipping something else in its place.
   * @param hero The hero whose equipment is being changed.
   * @param inventoryID The inventory ID of the item to equip.
   * @param itemType The type of item being equipped.
   * @return Result of equipment change.
   */
  public int changeEquipment(Hero hero, long inventoryID, int itemType)
  {
    long itemID;
    
    // Get the item ID for the inventory ID.
    if(inventoryID == 0)
    {
      itemID = 0;
    }
    else
    {
      Uri inventoryURI = Uri.withAppendedPath(MythTrackContract.Inventories.CONTENT_URI,
        Long.toString(inventoryID));
      Cursor inventoryCursor = context.getContentResolver().query(inventoryURI,
        MythTrackContract.Inventories.PROJECTION_ITEM_LOAD, null, null, null);
      int itemIDColumn
        = inventoryCursor.getColumnIndex(MythTrackContract.Inventories.COLUMN_ITEM_ID);
      if(inventoryCursor.getCount() == 0)
      {
        itemID = 0;
      }
      else
      {
        inventoryCursor.moveToFirst();
        itemID = inventoryCursor.getLong(itemIDColumn);
      }
      inventoryCursor.close();
    }

    // Get the Item.
    Item item = items.getItem(itemID);

    // Check if equipment switch would kill the hero.
    int vitality = hero.getVitality();
    int currentItemVitalityModifier = 0;
    int newItemVitalityModifier = item.getVitalityModifier();
    switch(itemType)
    {
      case MythTrackContract.Items.PRIMARY:
      case MythTrackContract.Items.TWO_HANDED:
      {
        if(hero.getPrimaryItem() != null)
        {
          currentItemVitalityModifier = hero.getPrimaryItem().getVitalityModifier();
        }
        break;
      }
      case MythTrackContract.Items.SECONDARY:
      {
        if(hero.getSecondaryItem() != null)
        {
          currentItemVitalityModifier = hero.getSecondaryItem().getVitalityModifier();
        }
        break;
      }
      case MythTrackContract.Items.ARMOR:
      {
        if(hero.getArmorItem() != null)
        {
          currentItemVitalityModifier = hero.getArmorItem().getVitalityModifier();
        }
        break;
      }
      case MythTrackContract.Items.HELM:
      {
        if(hero.getHelmItem() != null)
        {
          currentItemVitalityModifier = hero.getHelmItem().getVitalityModifier();
        }
        break;
      }
      case MythTrackContract.Items.ACCESSORY:
      {
        if(hero.getAccessoryItem() != null)
        {
          currentItemVitalityModifier = hero.getAccessoryItem().getVitalityModifier();
        }
        break;
      }
    }

    int difference = currentItemVitalityModifier - newItemVitalityModifier;

    // Check if the vitality drop larger than or equal to the current vitality.
    // The first condition is necessary because during hero loads this method gets called, but
    // the vitality will still be at zero.
    if(vitality != 0 && difference >= vitality)
    {
      return EQUIPMENT_CHANGE_WOULD_KILL;
    }


    // Check if the hero is already using a two-handed item.
    boolean twoHanding = false;
    if(hero.getPrimaryItem() != null)
    {
      twoHanding = hero.getPrimaryItem().getType() == MythTrackContract.Items.TWO_HANDED;
    }

    switch(itemType)
    {
      case MythTrackContract.Items.PRIMARY:
      {
        hero.setPrimaryID(inventoryID);
        hero.setPrimaryItem(item);
        // If the hero was two-handing already, remove the secondary.
        if(twoHanding)
        {
          Item emptyItem = items.getItem(0);
          hero.setSecondaryID(0);
          hero.setSecondaryItem(emptyItem);
        }
        break;
      }
      case MythTrackContract.Items.SECONDARY:
      {
        hero.setSecondaryID(inventoryID);
        hero.setSecondaryItem(item);
        // If the hero was two-handing already, remove the primary.
        if(twoHanding)
        {
          Item emptyItem = items.getItem(0);
          hero.setPrimaryID(0);
          hero.setPrimaryItem(emptyItem);
        }
        break;
      }
      case MythTrackContract.Items.TWO_HANDED:
      {
        hero.setPrimaryID(inventoryID);
        hero.setPrimaryItem(item);
        hero.setSecondaryID(inventoryID);
        hero.setSecondaryItem(item);
        break;
      }
      case MythTrackContract.Items.ARMOR:
      {
        hero.setArmorID(inventoryID);
        hero.setArmorItem(item);
        break;
      }
      case MythTrackContract.Items.HELM:
      {
        hero.setHelmID(inventoryID);
        hero.setHelmItem(item);
        break;
      }
      case MythTrackContract.Items.ACCESSORY:
      {
        hero.setAccessoryID(inventoryID);
        hero.setAccessoryItem(item);
        break;
      }
    }

    return EQUIPMENT_CHANGE_SUCCEEDED;
  }


  /**
   * Delete the hero with the given ID.
   * @param heroID ID of the hero to be deleted.
   * @return True if the hero was deleted.
   */
  public boolean deleteHero(long heroID)
  {
    // Get a URI to the hero's entry.
    Uri heroURI = Uri.withAppendedPath(MythTrackContract.Heroes.CONTENT_URI, Long.toString(heroID));
    // Delete the entry.
    boolean heroDeleted = (context.getContentResolver().delete(heroURI, null, null) > 0);
    // If a hero is deleted, also delete all of the hero's items out of the inventory table.
    if(heroDeleted)
    {
      String selection = MythTrackContract.Inventories.COLUMN_HERO_ID + " = " + heroID;
      context.getContentResolver().delete(MythTrackContract.Inventories.CONTENT_URI, selection,
        null);
    }

    return heroDeleted;
  }


  /**
   * Load the database information for the given hero ID into the hero object.
   * @param hero The hero object that is being loaded.
   * @param heroID The ID of the hero whose data is being loaded.
   * @return True if the hero loaded.
   */
  public boolean loadHero(Hero hero, long heroID)
  {
    // Get a cursor with all columns for the target hero.
    Uri heroURI = Uri.withAppendedPath(MythTrackContract.Heroes.CONTENT_URI,
      Long.toString(heroID));
    Cursor targetHero = context.getContentResolver().query(heroURI,
      MythTrackContract.Heroes.PROJECTION_LOAD, null, null, null);
    // The cursor should have exactly one hero/row.
    if(targetHero.getCount() == 1)
    {
      // Load that row.
      targetHero.moveToFirst();
    }
    // If there's more than one or none, there's a problem.
    else
    {
      targetHero.close();
      return false;
    }

    // Load the values from the database into the hero object.

    // ID
    int idColumn = targetHero.getColumnIndex(MythTrackContract.Heroes._ID);
    long id = targetHero.getLong(idColumn);
    hero.setID(id);

    // Name
    int nameColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_NAME);
    String name = targetHero.getString(nameColumn);
    hero.setName(name);

    // Class ID
    int classIDColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_CLASS_ID);
    int classID = targetHero.getInt(classIDColumn);
    hero.setClassID(classID);

    // Movement Points
    int movementColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_MOVEMENT);
    int movement = targetHero.getInt(movementColumn);
    hero.setMovement(movement);

    // Base Vitality
    int baseVitalityColumn
      = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_BASE_VITALITY);
    int baseVitality = targetHero.getInt(baseVitalityColumn);
    hero.setBaseVitality(baseVitality);

    // Courage
    int courageColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_COURAGE);
    int courage = targetHero.getInt(courageColumn);
    hero.setCourage(courage);

    // Threat
    int threatColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_THREAT);
    int threat = targetHero.getInt(threatColumn);
    hero.setThreat(threat);

    // Vitality
    int vitalityColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_VITALITY);
    int vitality = targetHero.getInt(vitalityColumn);
    hero.setVitality(vitality);

    // Gold
    int goldColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_GOLD);
    int gold = targetHero.getInt(goldColumn);
    hero.setGold(gold);

    // Serendipity
    int serendipityColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_SERENDIPITY);
    int serendipity = targetHero.getInt(serendipityColumn);
    hero.setSerendipity(serendipity);

    // Primary/Secondary/Two-Handed Item
    int primaryColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_PRIMARY);
    long primaryID = targetHero.getLong(primaryColumn);
    int secondaryColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_SECONDARY);
    long secondaryID = targetHero.getLong(secondaryColumn);

    // Check for two-handed item.
    if(primaryID == secondaryID)
    {
      changeEquipment(hero, primaryID, MythTrackContract.Items.TWO_HANDED);
    }
    else
    {
      changeEquipment(hero, primaryID, MythTrackContract.Items.PRIMARY);
      changeEquipment(hero, secondaryID, MythTrackContract.Items.SECONDARY);
    }

    // Armor Item
    int armorColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_ARMOR);
    long armorID = targetHero.getLong(armorColumn);
    changeEquipment(hero, armorID, MythTrackContract.Items.ARMOR);

    // Helm Item
    int helmColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_HELM);
    long helmID = targetHero.getLong(helmColumn);
    changeEquipment(hero, helmID, MythTrackContract.Items.HELM);

    // Accessory Item
    int accessoryColumn = targetHero.getColumnIndex(MythTrackContract.Heroes.COLUMN_ACCESSORY);
    long accessoryID = targetHero.getLong(accessoryColumn);
    changeEquipment(hero, accessoryID, MythTrackContract.Items.ACCESSORY);

    // Close the hero cursor.
    targetHero.close();

    return true;
  }


  /**
   * Save the updated stats from the hero object into the database.
   * @param hero The hero that is being updated.
   */
  public void saveHeroStats(Hero hero)
  {
    Uri heroURI = Uri.withAppendedPath(MythTrackContract.Heroes.CONTENT_URI,
      Long.toString(hero.getID()));
    ContentValues updatedHero = new ContentValues();

    // Add the hero stats into the update data.
    updatedHero.put(MythTrackContract.Heroes.COLUMN_GOLD, hero.getGold());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_SERENDIPITY, hero.getSerendipity());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_THREAT, hero.getThreat());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_VITALITY, hero.getVitality());

    // Add the hero equipment into the update data.
    updatedHero.put(MythTrackContract.Heroes.COLUMN_PRIMARY, hero.getPrimaryID());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_SECONDARY, hero.getSecondaryID());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_ARMOR, hero.getArmorID());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_HELM, hero.getHelmID());
    updatedHero.put(MythTrackContract.Heroes.COLUMN_ACCESSORY, hero.getAccessoryID());

    // Update the database.
    context.getContentResolver().update(heroURI, updatedHero, null, null);
  }


  /**
   * Adjust the hero's threat when their threat modifier changes.
   * @param hero The hero whose threat is being modified.
   */
  public void updateThreat(Hero hero)
  {
    // Adjust threat based on equipment changes.
    int minThreat = hero.getMinThreat();
    int maxThreat = hero.getMaxThreat();
    int threat = hero.getThreat();
    if(threat < minThreat)
    {
      hero.setThreat(minThreat);
    }
    if(threat > maxThreat)
    {
      hero.setThreat(maxThreat);
    }
  }
  
  
  /**
   * Adjust the hero's vitality when their vitality modifier changes.
   * @param hero The hero whose vitality is being modified.
   * @param previousVitalityModifier The sum of the vitality bonuses before the change.
   */
  public void updateVitality(Hero hero, int previousVitalityModifier)
  {
    // Adjust vitality and base vitality based on equipment changes.
    int vitalityDifference = hero.getVitalityModifier() - previousVitalityModifier;
    int vitality = hero.getVitality();
    if(vitalityDifference != 0 && vitality >= 0)
    {
      hero.setVitality(vitality + vitalityDifference);
    }
    // Vitality can't be negative.
    if(hero.getVitality() < 0)
    {
      hero.setVitality(0);
    }
  }

}




Java Source Code List

nolanjurgens.mythtrack.app.BuyItemDialog.java
nolanjurgens.mythtrack.app.CreateHeroDialog.java
nolanjurgens.mythtrack.app.DeleteHeroDialog.java
nolanjurgens.mythtrack.app.EquipmentInfoDialog.java
nolanjurgens.mythtrack.app.HeroDisplayFragment.java
nolanjurgens.mythtrack.app.HeroHelper.java
nolanjurgens.mythtrack.app.HeroList.java
nolanjurgens.mythtrack.app.HeroTrackerActivity.java
nolanjurgens.mythtrack.app.Hero.java
nolanjurgens.mythtrack.app.InventoryList.java
nolanjurgens.mythtrack.app.ItemHelper.java
nolanjurgens.mythtrack.app.ItemList.java
nolanjurgens.mythtrack.app.Item.java
nolanjurgens.mythtrack.app.MainActivity.java
nolanjurgens.mythtrack.app.RemoveItemDialog.java
nolanjurgens.mythtrack.app.SettingsActivity.java
nolanjurgens.mythtrack.app.SettingsFragment.java
nolanjurgens.mythtrack.app.StatInfoDialog.java
nolanjurgens.mythtrack.app.StatPickerDialog.java
nolanjurgens.mythtrack.app.TitleHelper.java
nolanjurgens.mythtrack.app.Title.java
nolanjurgens.mythtrack.provider.MythTrackBackup.java
nolanjurgens.mythtrack.provider.MythTrackContract.java
nolanjurgens.mythtrack.provider.MythTrackDatabase.java