Author Topic: Several Modification Ideas  (Read 7371 times)

Offline Steev

Several Modification Ideas
« on: April 06, 2007, 08:52:35 am »
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.

Offline Steev

Re: Several Modification Ideas
« Reply #1 on: April 07, 2007, 05:33:03 am »
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: [Select]
<?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_periodTINYINT(4UNSIGNED 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_stockTINYINT(4UNSIGNED 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__);

?>

Then, a new file, which I've named dostock.php.
Code: [Select]
<?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"])) 
?>


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?

Offline Daniel15

Re: Several Modification Ideas
« Reply #2 on: April 07, 2007, 07:37:19 pm »
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.
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?
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: [Select]
echo '<pre>', print_r($context, true), '</pre>';.

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 :)
« Last Edit: April 07, 2007, 07:42:17 pm by Daniel15 »

Offline Steev

Re: Several Modification Ideas
« Reply #3 on: April 08, 2007, 11:12:10 am »
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.

Offline Daniel15

Re: Several Modification Ideas
« Reply #4 on: April 08, 2007, 11:26:01 am »
Quote
Awesome.  I'll have to grab the dev version, then.
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?
Bonuses are just the base points.

« Last Edit: April 08, 2007, 11:27:44 am by Daniel15 »

Offline Steev

Re: Several Modification Ideas
« Reply #5 on: April 10, 2007, 12:57:57 pm »
I've made a project for the Variable Restock Rates, and will probably be making another one shortly for the Resellable items...but I have one question, since these are my first SMF mods...

Is there an easy way to make the .xml files for the install, or do I have to do the diffs and copy-paste everything manually?

Offline JRSofty

Re: Several Modification Ideas
« Reply #6 on: April 10, 2007, 03:44:30 pm »
I personally think the copy/paste method works well. I tried using a package making software and was not happy with it at all. It wouldn't open an old package that it had already created, among other things.

Currently I do everything in my programming Text editor and I'm happy with that.

Offline Daniel15

Re: Several Modification Ideas
« Reply #7 on: April 10, 2007, 05:16:41 pm »
Quote
Is there an easy way to make the .xml files for the install, or do I have to do the diffs and copy-paste everything manually?
You need to copy-and-paste everything manually. It's not too hard, and good GUI diff tools (like WinMerge for Windows, or Meld for Linux) help quite a bit.

Like JRSofty, I do almost everything in my text editor (gedit on Linux, and Notepad++ on Windows). Most of the time, I don't need a diff tool, as I mark all my changes (eg. with // Begin SMFShop code and // End SMFShop code) so I can just search the file for "SMFShop" :)

Offline Steev

Re: Several Modification Ideas
« Reply #8 on: April 10, 2007, 09:21:51 pm »
Fair enough.  I've been marking my changes too (6 years in software development almost force you to), but the diff in EditPadPro gives me a quick glance at the changes to make it even easier. :)

Thanks for the help, guys.

Offline Steev

Re: Several Modification Ideas
« Reply #9 on: April 11, 2007, 03:18:10 am »
New question, same topic, and again, a thanks in advance for the help.

In ShopAdmin.template.php, I have the following:
Code: [Select]
<table>
<tr>
<td align="right"><label for="itemname">', $txt['shop_name'], ':</label></td>
<td><input name="itemname" id="itemname" type="text" value="', $context['shop_edit']['name'], '" size="80"  style="width: 100%" /></td>
</tr><tr>
<td align="right" valign="top"><label for="itemdesc">', $txt['shop_description'], ':</label></td>
<td><textarea name="itemdesc" id="itemdesc" cols="40" rows="6" style="width: 100%">', $context['shop_edit']['desc'], '</textarea></td>
</tr><tr>
<td align="right"><label for="itemprice">', $txt['shop_price'], ':</label></td>
<td>', $modSettings['shopCurrencyPrefix'], '<input name="itemprice" id="itemprice" type="text" value="', $context['shop_edit']['price'], '" size="5" />', $modSettings['shopCurrencySuffix'], '</td>
</tr><tr>
<td align="right"><label for="itemstock">', $txt['shop_stock'], ':</label></td>
<td><input name="itemstock" id="itemstock" type="text" value="', $context['shop_edit']['stock'], '" size="5" /></td>
</tr>';
//BEGIN CODE ADDED FOR Variable Restock Rates
echo '<tr>
<td align="right"><label for="stock_period">', $txt['shop_stock_period'], ':</label></td>
<td><input name="stock_period" id="stock_period" type="text" value="', $context['shop_item']['stock_period'], '"size="5" /></td>
</tr><tr>
<td align="right"><label for="stock_add">', $txt['shop_stock_add'], ':</label></td>
<td><input name="stock_add" id="stock_add" type="text" value="', $context['shop_item']['stock_add'], '"size="5" /></td>
</tr><tr>
<td align="right"><label for="max_stock">', $txt['shop_max_stock'], ':</label></td>
<td><input name="max_stock" id="max_stock" type="text" value="', $context['shop_item']['max_stock'], '"size="5" /></td>
</tr>';
//END CODE ADDED FOR Variable Restock Rates
echo '<tr>
<td align="right"><label for="cat">', $txt['shop_category'], ':</label></td>
<td>
The form continues, but that gets all of my stuff in there.

In ShopAdmin.php, I have the following:\
Code: [Select]
// Update the item information
// QUERY UPDATED FOR Variable Restock Rates
db_query("
UPDATE {$db_prefix}shop_items
SET name = '{$_POST['itemname']}',
`desc` = '{$_POST['itemdesc']}',
price = {$_POST['itemprice']},
stock = {$_POST['itemstock']},
image = '{$_POST['icon']}',
delete_after_use = {$delete},
category = {$_POST['cat']},
stock_period = {$_POST['stock_period']},
stock_add = {$_POST['stock_add']},
max_stock = {$_POST['max_stock']},
{$additional}
WHERE id = {$_POST['id']}
LIMIT 1", __FILE__, __LINE__);

When I run it, it lets me get to the edit page, but the three new text fields are empty.  And when I click Edit, I get a MYSQL error at the end of that ShopAdmin.php block.

I know I have some $context stuff in the template; did I miss a location where I need to add this data?  I'm assuming that it's the $context being missing that's causing data to not make it over to the query, which is causing the query to fail.  Can anyone help?

Edit: Removed extra tabs.

EditX2: Fixed the issue.  It was two problems rolled into one.  I was using $context['shop_item'] instead of ['shop_edit'] in the template, and I had an extra comma in the query, since $additional begins with a comma if it's needed.

I've now got this installed on my site, and things are working swimmingly at the moment.  Thanks again.
« Last Edit: April 12, 2007, 01:48:14 am by Steev »

Offline Conrad123

Re: Several Modification Ideas
« Reply #10 on: May 23, 2017, 06:48:52 pm »
Come here I learned a lot of things, I will add this site to my label, continue to refuel