I want to make a code where after every time the user posts, they gain gold/money. Simple, right? Now, I do know that there is a SMF Shop Mod, however, I don't think this Mod will work with the adjustments I have already done with my own site. My site, is a roleplaying site, and I've already added a character creation system that revolves around the use of the forum. I've also added my own shop in which users can buy items for their characters. I have already programmed the database to give all users a column called "user_gold", which is basically how much money they have. My only problem is, I want to add gold to their count, much similar to how we add a post count for every time they post. I figured updating the post count after every time they post would be similar to how I want to add gold into the same table.
My SQL code should look something like this.
$smcFunc['db_query']('', '
UPDATE {db_prefix}members
SET user_gold = {int:money}
WHERE id_member = {int:m_Id}
LIMIT 1',
array(
'm_Id' => $user_info['id'], 'money' => 50,
)
);
I figured it would be somewhere in the Posts.php file but I could be wrong. I'm going off of a whim but I figured it would be easier to update the user_gold column that I added the same way the forum updates a user's post count. That is the basic idea I am trying to do.
Just a thought... is the gold or money amount some simple function of the number of posts they've made? If it is, it might be easier to simply use the already-existing post count for this member rather than creating a new field. Wherever you want to reference their balance, you would just feed their post count through some function. On the other hand, if the balance depends on the history of things other than just the post count, or the post count => gold balance function would be hideously slow, it might be better to maintain the balance as you go.
The post count doesn't equal to the gold balance. Simply put, I just want a member to gain 25 gold with every post they make. I was just assuming that it would be similar to how the forum updates a user's post count in the database whenever they post on the forum.
That sounds like a simple function, gold=25*posts. Come to think of it, at some point you would want members to "spend" this gold, wouldn't you? Then you would need to maintain the gold balance separately.
Lainaus käyttäjältä: Jamora1124 - elokuu 16, 2013, 03:13:41 IP
...
My SQL code should look something like this.
$smcFunc['db_query']('', '
UPDATE {db_prefix}members
SET user_gold = {int:money}
WHERE id_member = {int:m_Id}
LIMIT 1',
array(
'm_Id' => $user_info['id'], 'money' => 50,
)
);
...
I think that, if you want to make it "proper", you should use the same method SMF uses to update the members post count...
This is Post.php, when a new post is created:
// Increase the post counter for the user that created the post.
if (!empty($posterOptions['update_post_count']) && !empty($posterOptions['id']) && $msgOptions['approved'])
{
// Are you the one that happened to create this post?
if ($user_info['id'] == $posterOptions['id'])
$user_info['posts']++;
updateMemberData($posterOptions['id'], array('posts' => '+'));
}
I believe the proper way would be for you to change the function "updateMemberData" (Subs.php) in order to accept a new argument and update the DB as necessary.
This would provide you with another easy integration with the other post count operations. Ex.: Do you want to decrease gold when a topic is deleted?
Thanks, margaret! This is exactly what I was looking for. I figure I would do something like this with that part of the code.
// Increase the post counter for the user that created the post.
if (!empty($posterOptions['update_post_count']) && !empty($posterOptions['id']) && $msgOptions['approved'])
{
// Are you the one that happened to create this post?
if ($user_info['id'] == $posterOptions['id'])
$user_info['posts']++;
updateMemberData($posterOptions['id'], array('posts' => '+'));
$smcFunc['db_query']('', '
UPDATE {db_prefix}members
SET user_gold = {int:money}
WHERE id_member = {int:m_Id}
LIMIT 1',
array(
'm_Id' => $user_info['id'], 'money' => 50,
)
);
}
More or less, right? Even though, this is a bit off topic to ask but does anyone know how to display the gold amount in the topic? Probably right under the post count. Would it be in a template or would it still be in Posts.php?
hey I couldn't find that piece of code you showed in Posts.php or subs.php. I pressed Ctrl+F to find as well. I'm just trying to update a column in a table every time a user posts.
Later today I will search this when I'm on the computer.
Anyway, for a start:
- to show the user's gold you need to update Load.php so that the extra column is retrieved and you also need to update the member data array
- to increase the gold count you can do it like you said but my suggestion would be to use another approach. If I can test it today I will show you what I thought
I hope this helps -
<file name="$sourcedir/Post.php">
<operation>
<search position="before"><![CDATA[
createPost($msgOptions, $topicOptions, $posterOptions);
]]></search>
<add><![CDATA[
// Begin Wizards Shop MOD code
// New topic?
if ($newTopic)
$points = !empty($board_info['shop_pertopic']) ? $board_info['shop_pertopic'] : $modSettings['shopPointsPerTopic'];
else
$points = !empty($board_info['shop_perpost']) ? $board_info['shop_perpost'] : $modSettings['shopPointsPerPost'];
// Are bonuses allowed in this board?
// Strip out all BBCode
$plaintext = preg_replace('[\[(.*?)\]]', ' ', $_POST['message']);
// Convert all newlines into spaces
$plaintext = str_replace(array('<br />', "\r", "\n"), ' ', $plaintext);
// Convert multiple successive spaces into a single space
$plaintext = preg_replace('/\s+/', ' ', $plaintext);
// The bonus for each word...
$points += ($modSettings['shopPointsPerWord'] * str_word_count($plaintext));
// ...and bonus for each letter
$points += ($modSettings['shopPointsPerChar'] * strlen($plaintext));
// Is there a limit set, and are we over it?
if (isset($modSettings['shopPointsLimit']) && $modSettings['shopPointsLimit'] != 0 && $points > $modSettings['shopPointsLimit'])
// If so, set the number of points to the limit
$points = $modSettings['shopPointsLimit'];
// Give the user their points
$result_shop = $smcFunc['db_query']('', "
UPDATE {db_prefix}members
SET money = money + {float:points}
WHERE id_member = {int:id}
LIMIT 1",
array(
'points' => $points,
'id' => $user_info['id'],
));
cache_put_data('user_settings-' . $user_info['id'], null, 60);
// End Wizards Shop MOD code
]]></add>
</operation>
</file>
Just to resume my thought, although you should probably look into The Wizard's help ;)
So, for you to show the actual gold in messages (Display.template.php), you should first (assuming an already existing field named "user_gold", INT, in "smf_members" table):
Load.php
Find
mem.signature, mem.personal_text, mem.location, mem.gender, mem.avatar, mem.id_member, mem.member_name,
Replace with:
mem.signature, mem.personal_text, mem.location, mem.gender, mem.avatar, mem.id_member, mem.member_name, mem.user_gold,
(There are 2 locations to search for)
Find:
'local_time' => timeformat(time() + ($profile['time_offset'] - $user_info['time_offset']) * 3600, false),
Add after:
'user_gold' => $profile['user_gold'],
Then, Display.template.php
Find:
// Show how many posts they have made.
if (!isset($context['disabled_fields']['posts']))
echo '
<li class="postcount">', $txt['member_postcount'], ': ', $message['member']['posts'], '</li>';
Add after:
if (!empty($message['member']['user_gold']))
echo '
<li class="postcount">User Gold: ', $message['member']['user_gold'], '</li>';
Also note that the "User Gold" string should be in a language file, but that can be easily arranged later...
This should result in something like attached.
As for the increase of the gold amount, I will just continue ;)
Now, about the increase of gold...
As I told you, I think the easiest way would be to use the internal SMF method, therefore being easier to include additional functionalities (ex: decrease gold count)
So, in Subs.php, there is this function:
// Assumes the data has been htmlspecialchar'd.
function updateMemberData($members, $data)
{
I think the best way would be to slightly modify it, in order to know your "user_gold" column. So, if you find:
'id_theme', 'is_activated', 'id_msg_last_visit', 'id_post_group', 'total_time_logged_in', 'warning',
And replace with:
'id_theme', 'is_activated', 'id_msg_last_visit', 'id_post_group', 'total_time_logged_in', 'warning', 'user_gold',
You can easily go to Subs-Post.php and find:
updateMemberData($posterOptions['id'], array('posts' => '+'));
Adding after:
updateMemberData($posterOptions['id'], array('user_gold' => '+'));
This would allow you to update your column, the same way you update the post count (in this case, increase it). With the advantage that you can add some conditions here, so that this "update" may or may not be executed.
To finish, the way it is now you just increase "1" to each post, but you wish 25 (or any other number)
To do this, go back to Subs.php, in the same function we were changing before and find:
// Doing an increment?
if ($type == 'int' && ($val === '+' || $val === '-'))
{
$val = $var . ' ' . $val . ' 1';
And replace with:
// Doing an increment?
if ($type == 'int' && ($val === '+' || $val === '-'))
{
if ($var != 'user_gold')
$val = $var . ' ' . $val . ' 1';
else
$val = $var . ' ' . $val . ' 25';
You can adjust the "25" there.
Of course this needs to be improved. You might need to have different values for new topic/new reply, or you may want to choose which boards should give increased gold, etc...
But this should also give you a place to start.
Good luck!
The Wizard's idea might be a little over the top from what I'm trying to do but, I'll give it a shot. Though, it also looks like a snippet from his Shop Mod.
And wow margarett, this is exactly what I was trying to do. Thank you a thousand times. You have no idea how grateful I am for your assistance. Though, you mentioned something about only gaining gold in certain boards. What would be the basics of doing this?
The main part would be to create and administrative option for it. I'm sorry but I can't help you on that one (yet)...
The easiest way to do it is to manually build an array of board ids and check if the current board is in that array. If so, you would call the second "updateMemberData" and get gold updated. Of not, then that line is bypassed and nothing happens ;)
Lainaus käyttäjältä: margarett - elokuu 20, 2013, 09:10:18 AP
The main part would be to create and administrative option for it. I'm sorry but I can't help you on that one (yet)...
The easiest way to do it is to manually build an array of board ids and check if the current board is in that array. If so, you would call the second "updateMemberData" and get gold updated. Of not, then that line is bypassed and nothing happens ;)
Ok thanks. I was wondering because I don't know if you noticed or not but my forum is a roleplaying forum and I was thinking of adding a system where the user gains experience from posting but only in certain area's of the forum. I'm going to attempt to try to code it myself and well, if I mess up, I guess I can come here again. I didn't understand much of the other files outside of the CSS for SMF until now. Now, I have a better understanding of those codes. Thanks again.
EDIT/
I can't find display.template.php in the Sources file.
Because it isn't there :P
*.template.php are theme files and, as such, are located inside your themes folders ;)
I tried using the code that you gave me and now it's giving me this error:
LainaaWrong value type sent to the database. Integer expected. (p_user_gold)
Because you probably didn't create an INT column in your "members" table, maybe? ;)
I tested all of that code, so I expect it to work :P
nope, it's an INT value, i made sure to change it from VARCHAR to INT before I started using the adjustments.
Can you please attach your modified files?
modified files? What modified files?
The files you modified to try this :P
Load.php
Subs.php
Subs-Post.php
Display.template.php
By the way, when does that error occurs? When posting, when loading a page, ...?
The error occurs after posting. It updated the gold the first time, but it doesn't add any additional gold for each post.
Will look into it later, hoping still today...
No rush, dude. You've already helped quite a bit.
So, as far as I see, you had some edition issues :P
Subs.php, take a look at the first attachment. You are missing an instruction from the original function...
(https://www.simplemachines.org/community/index.php?action=dlattach;topic=509679.0;attach=223489;image)
Load.php, you just searched and replaced the first of 2 occurrences of the same line (second attachment).
(https://www.simplemachines.org/community/index.php?action=dlattach;topic=509679.0;attach=223491;image)
Display.template.php, you used double quotes on "echo" and I used single quotes. It should work nevertheless...
Thanks margarett. I put the code in and it works fine. Looks like I just overlooked something. Though, this was also my first time tampering with the other files. I usually stick to making my own pages and adding it onto the forum. The Source and template files are a whole other league from where am I coding wise.