SMFShop > Coding

Several Modification Ideas

(1/3) > >>

Steev:
Some of these I've noticed are repeats from other threads, but not all of them, and since I had them handwritten as ideas, I'm including them all for completeness.

1) Points per post By Board
This would require in the "Modify Boards" section of the ACP change the "Count Shop Posts" into a "Points per Post".  It shouldn't be too hard of a change; the big change is altering the Boards table so that it has a MoneyPerPost smallint(5) field; but I'm probably missing something on that regard.

2) Sellable Items
Each item should have a sale price and an OnSale() function.  The function would probably only add the money to their inventory and remove the item.  Perhaps an option to allow sold items to return to the store inventory or not?

3) Items by Usergroup
I'd like to see a way to make an item limited to certain usergroups.  Buying, using, or both.  This one, though, I have absolutely no idea how to go about it, at the moment.

4) Variable Restock Rates
A couple more variables--TimeToRestock, MaxStock, EachStock.  A new field in the shop's inventory would have a LastRestocked time.  A cron job would probably be needed so that every hour it would check the last restocks against the time to restock, and if it's been at least TimeToRestock hours, it increases the shop's stock by EachStock, up to a maximum of MaxStock.  I've got a good idea of how to do this, but I see it being fairly complicated and will take some time to do.

5) Destruction Changes / Charged Items
Instead of just a static delete_on_use variable, there should be a OnCleanUp() function; a counter should be added to the inventory field as well for charged items.  The OnCleanUp() function would decide whether and how to remove the item; for instance, if the counter has reached 0, or a random chance is met.  I recently created an item with three options; on three of them, it would get deleted, and it would stay on the other three.  This would be optimal for items like that.

If anyone has already finished these, or has more ideas of how to implement them, I'd be appreciative.

Steev:
I was working on the variable restock rates today.  Here's what I've come up with so far.

First, the install, adding 4 columns to the shop_items table.

--- Code: ---<?php
// Variable Restock SMFShop Mod installation script (Build 1)
// Some code taken from SMFShop install.php by Daniel15
//Build 1 (6 April 2007, 14:35, GMT -5)

if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
require_once(dirname(__FILE__) . '/SSI.php');
// Hmm... no SSI.php and no SMF?
elseif (!defined('SMF'))
die('<b>Error:</b> Cannot install - please verify you put this in the same place as SMF\'s index.php.');

$result = db_query("SHOW COLUMNS FROM {$db_prefix}shop_items LIKE 'stock_period'", __FILE__, __LINE__);
if (mysql_num_rows($result) == 0) 
ALTER TABLE {$db_prefix}shop_items ADD `stock_period` TINYINT(4) UNSIGNED DEFAULT '1' NOT NULL", __FILE__, __LINE__);

$result = db_query("SHOW COLUMNS FROM {$db_prefix}shop_items LIKE 'stock_add'", __FILE__, __LINE__);
if (mysql_num_rows($result) == 0) 
ALTER TABLE {$db_prefix}shop_items ADD `stock_add` TINYINT(4) UNSIGNED DEFAULT '50' NOT NULL", __FILE__, __LINE__);

$result = db_query("SHOW COLUMNS FROM {$db_prefix}shop_items LIKE 'max_stock'", __FILE__, __LINE__);
if (mysql_num_rows($result) == 0) 
ALTER TABLE {$db_prefix}shop_items ADD `max_stock` TINYINT(4) UNSIGNED DEFAULT '50' NOT NULL", __FILE__, __LINE__);

$result = db_query("SHOW COLUMNS FROM {$db_prefix}shop_items LIKE 'last_stocked'", __FILE__, __LINE__);
if (mysql_num_rows($result) == 0) 
ALTER TABLE {$db_prefix}shop_items ADD `last_stocked` INT(10) UNSIGNED DEFAULT '0' NOT NULL", __FILE__, __LINE__);

?>
--- End code ---

Then, a new file, which I've named dostock.php.

--- Code: ---<?php
// Modification for:

/**********************************************\
| SMFSHOP (Shop MOD for Simple Machines Forum) |
|         (c) 2005 DanSoft Australia           |
|      http://www.dansoftaustralia.com/        |
\**********************************************/

//File: dostock.php
//      The file to check stock levels and increase, if needed.
//Build 1 (6 April 2007, 14:35, GMT -5)

// A cron job should be set up to run this file every hour.
// This file checks the current shop stock levels, and increases them
// if the appropriate time has come.  Will also decrease to desired
// maximum levels.

if(!isset($_SERVER["HTTP_HOST"])) 
{

include("../../SSI.php");

$result = db_query("
SELECT id, stock, stock_period, stock_add, max_stock, last_stocked
FROM {$db_prefix}shop_items" , __FILE__, __LINE__);

// There should be multiple rows.
while ($row = mysql_fetch_assoc($result))
{
$id = $row['id']
$current_stock = $row['stock'];
$stock_period = $row['stock_period'];
$stock_add = $row['stock_add'];
$max_stock = $row['max_stock'];
$last_stocked = $row['last_stocked'];

// Check if we need to restock the item.
$current_time = time();
$period_seconds = $stock_period * 60 * 60; // 60 seconds by 60 minutes by hours.
if ($current_time >= $last_stocked + $period_seconds)
{
// We have passed our stock period.
// Check to see if our current stock is less than we're willing to keep around.
if ($current_stock < $max_stock)
{
// It is, so add some.
$current_stock = $current_stock + $stock_add;
} // if ($current_stock < $max_stock)
} // if ($current_time >= $last_stocked + $period_seconds)
// Now make sure we're not over the most we want to hold.
$current_stock = $current_stock > $max_stock ? $max_stock : $current_stock; // Limit to max stock.

// Update the table with the new information.
$result2 = db_query ("
UPDATE {$db_prefix}shop_items
SET stock = {$current_stock},
last_stocked={$current_time}
WHERE id=$id" , __FILE__, __LINE__);

// Sanity check on table update?
// if (mysql_affected_rows ($result2) != 1) echo 'ERROR DETECTED<br>';
// This is set to be a cron, so no output needed.  Sanity check skipped.

mysql_free_result($result2);
} // while ($row = mysql_fetch_assoc($result))

mysql_free_result($result);

} // if(!isset($_SERVER["HTTP_HOST"])) 
?>

--- End code ---

Obviously, this isn't yet complete--options for those four would need to be added to the admininstration section, and probably the item_engine as well.  But as I got that far, I realized there was one thing I didn't know.

$context seems to be used everywhere.  As you can see, I've used individual variables, as that's how I'm used to doing PHP.  How is $context supposed to be used?

Daniel15:
Please feel free to use my development site at dev.dansoftaustralia.net - See http://www.daniel15.com/forum/index.php/topic,737.0.html for a full list of its features. In particular, you'll find the Subversion support very handy :D


--- Quote ---1) Points per post By Board
This would require in the "Modify Boards" section of the ACP change the "Count Shop Posts" into a "Points per Post".  It shouldn't be too hard of a change; the big change is altering the Boards table so that it has a MoneyPerPost smallint(5) field; but I'm probably missing something on that regard.
--- End quote ---
I'm already working on that... It's already in the development version of SMFShop :)
http://www.daniel15.com/forum/index.php/topic,554.msg3996.html#msg3996


--- Quote ---$context seems to be used everywhere.  As you can see, I've used individual variables, as that's how I'm used to doing PHP.  How is $context supposed to be used?
--- End quote ---
For what you're doing, individual variables are fine :)
$context is used in SMF itself, and contains all the data needed for the current page. Essentially, SMF's code is split up into two sections: The main "back-end" code (Sources/*.php) used to get all the data, and the template code (Themes/default/*.template.php) used to actually display all the data. The source files do all the processing, and the templates simply display this information. The $context array is the method used to pass data from the source files to the template. Its contents change depending on your page view (if you're viewing a topic, it displays information on that topic).

If you want to see a list of context variables, put this in a template somewhere (eg. main_above in index.template.php):

--- Code: ---echo '<pre>', print_r($context, true), '</pre>';
--- End code ---
.

Also, for your code, I suggest you take a look at the coding guidelines, available (temporarilly, until the actual guidelines page goes up) at http://custom.simplemachines.org/mods/guidelines.php. It explains the SMF Coding Style, which SMF mods should follow :)
Edit: In case that link doesn't work for you, please go to http://www.simplemachines.org/community/index.php?topic=159824.0 for a new version of the guidelines :)

Steev:
Awesome.  I'll have to grab the dev version, then. :)  Out of curiosity, are the bonuses per board as well, or just the base points?  I have all the bonuses at 0, so it doesn't matter either way, but I'm curious.

I've grabbed the coding guidelines, but not until after I wrote that code.  I think I got most of it, though.

Daniel15:

--- Quote ---Awesome.  I'll have to grab the dev version, then.
--- End quote ---
Well, I don't really have a page up for the development version yet. However, there's some installable packages at http://smfshop.dev.dansoftaustralia.net/snapshots/, the SVN repository is at http://dev.dansoftaustralia.net/svn/smfshop/trunk/, and the SVN viewer (ViewVC) is at http://dev.dansoftaustralia.net/plugins/scmsvn/viewcvs.php/?root=smfshop :)

More information on SVN and the development edition at http://www.daniel15.com/forum/index.php/topic,198.0.html ;)


--- Quote ---Out of curiosity, are the bonuses per board as well, or just the base points?
--- End quote ---
Bonuses are just the base points.

Navigation

[0] Message Index

[#] Next page

Go to full version