Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Keydet - syyskuu 08, 2013, 12:02:15 AP

Otsikko: Integrating SMFShop & Living Avatars
Kirjoitti: Keydet - syyskuu 08, 2013, 12:02:15 AP
Hello, everyone. Long time, lurker, finally stumped on code that I can't hammer out on my own. If this is supposed to be in Scripting help, please forgive me. Took my best gander at where to put this.

I am taking my turn at attempting to integrate SMFShop and Living Avatars, both well-known mods. I have already connected the mods to use the same "money" system, but want SMFShop to take on the responsibility of all item sales I have. It gets mighty confusing once I add an item a players wardrobe on Living Avatars, so here is my workaround:

Items will be bind on equip. Players will buy them from the SMFShop, but won't be able to equip them until they "use" them, effectively sending them to the Living Avatars mod and then the item is deleted with traditional SMFShop scripting.

This code I am attempting to create below must accomplish two things: it must reject the item use if the user already has the item in their Living Avatar wardrobe, or add the item to the wardrobe if it's not already there. This uses a lot of database references that are tripping me up.

Index.php in Living Avatars is what currently handles this process for it's own shop, if you need to reference that code. Please help, thank you in advance!


function onUse()
{
define('IN_PRGM', true);
define('ROOT_PATH', './');

global $DB, $user_info, $forum_info_arr;

  if($item_info[1] = $DB->query_first("SELECT item_id FROM {items_purchased} WHERE item_id = $item_id AND user_id = $user_info[user_id]"))
  {
    return 'You have already purchased this item.';
}

if(isset($error_message))
  {
    DisplayMessage($error_message, true);
    DisplayItems();
  }
  else // no errors, lets continue
{
$DB->query("INSERT INTO {items_purchased} (item_id, pack_id, group_id, user_id, points_spent, purchase_date)
                VALUES ($item[item_id], $item[pack_id], $item[group_id], $user_info[user_id], $item[cost], " . TIME_NOW . ")");
               
                $DB->query("UPDATE ".$forum_info_arr['forum_table_prefix'] . "members SET money = money - $item[cost] WHERE ID_MEMBER =    $user_info[user_id]");
               
                $points_remaining = $user_info['points'] - $item['cost'];
               
                DisplayMessage('Item successfully bound to your character!');
}
}



Living Avatars: http://www.livingavatars.com/
SMFShop: http://custom.simplemachines.org/mods/index.php?mod=65
Otsikko: Re: Integrating SMFShop & Living Avatars
Kirjoitti: Keydet - syyskuu 08, 2013, 09:25:19 IP
I've attached the code that writes item to the player's inventory when it's purchased within the LA interface. It seems that the OnUse() function should just have to mirror that to work, but I'm terrible with arrays and it's killing me. I've also struck through some checks that do not need to happen when this item is "used," since it will already be bought.

Lainaa// ###############################################################################
// PURCHASE ITEM
// ###############################################################################

function PurchaseItem()
{
  global $DB, $user_info, $forum_info_arr;
 
  if(!$user_info['logged_in'])
  {
    DisplayMessage('You must be logged in under you forum to purchase items.', true);
    DisplayItems();
    return;
  }

  $item_id = $_GET['item_id'];
  if(!is_numeric($item_id)) {
     echo 'Error: Invalid item id';
   exit;
  }
  // get item info
  $item = $DB->query_first("SELECT * FROM {items} WHERE item_id = $item_id");

// make sure the user can afford this item
  if($user_info['points'] < $item['cost'])
  {
    $error_message = 'You do not have enough points to purchase this item.';
  }

// make sure this item is not sold out
  if($item['purchase_limit'] != 0 AND $item['purchase_count'] >= $item['purchase_limit'])
  {
    $error_message = 'Sorry, this item is sold out.';
  }

  // make sure the user hasn't already purchased this item
  if($itempurchased = $DB->query_first("SELECT item_id FROM {items_purchased} WHERE item_id = $item_id AND user_id = $user_info[user_id]"))
  {
    $error_message = 'You have already purchased this item.';
  }

  if(isset($error_message))
  {
    DisplayMessage($error_message, true);
    DisplayItems();
  }

  else // no errors, lets continue
  {
    // insert item into items purchased table
    $DB->query("INSERT INTO {items_purchased} (item_id, pack_id, group_id, user_id, points_spent, purchase_date)
                VALUES ($item[item_id], $item[pack_id], $item[group_id], $user_info[user_id], $item[cost], " . TIME_NOW . ")");

    // update item purchase count
    $DB->query("UPDATE {items} SET purchase_count = purchase_count + 1 WHERE item_id = $item[item_id]");

    $DB->query("UPDATE ".$forum_info_arr['forum_table_prefix'] . "members SET money = money - $item[cost] WHERE ID_MEMBER = $user_info[user_id]");

    $points_remaining = $user_info['points'] - $item['cost'];

    echo '<script type="text/javascript">
          document.getElementById("points_remaining").innerHTML = "' . $points_remaining  . '";
          </script>';

    DisplayMessage('Item Purchased!');
    DisplayItems($purchased_item_id = $item['item_id']);
  }
}