Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Jamora1124 - elokuu 31, 2013, 11:12:40 AP

Otsikko: Creating a Level Up System
Kirjoitti: Jamora1124 - elokuu 31, 2013, 11:12:40 AP
I'm going to lay down the basic functions of the code I have created so far. It's a little complicated, so without some background information it may get just a tad bit complicated.

Okay, so in the Posts.template.php, I successfully created a code where it displays radio boxes before you post, so you can "Select Your Character" you are posting with in that post. (The basic system is that every post you gain experience).

Here is a snippet from Posts.template.php.
if($board == 3 or $board == 4 or $board == 5 or $board == 6) {
$db=mysqli_connect("(removed)","(removed)","(removed)","(removed)");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($db,"SELECT * FROM smf_rpg_characters WHERE member_name='" .$context['user']['name']. "'");
echo "<dt class='clear_left'>Select Character: </dt><dd>
<input type='hidden' name='character' value='0' checked>";
while($row = mysqli_fetch_array($result))
  {
echo "<input type='radio' name='character' value='" .$row['character_id']. "'><a href='http://rpgforumtest.freeiz.com/view_character.php?character_id=" .$row['character_id']. "'><img src='" .$row['avatar']. "' width='75' height='75' style='border: 1px solid #000'></a> " .$row['l_name']. " " .$row['m_name']. " " .$row['f_name']. "<hr class='clear' />"; }
echo "</dd></dl><hr class='clear' />";
mysqli_close($con); }
else {
echo '<input type="hidden" name="character" value="0" checked></dd></dl><hr class="clear" />'; }


Which creates the first screenshot. And now, I created a code that checks if the user has leveled up or not, and I'm trying to insert it into Post.php underneath the createPost() function. Though, it doesn't seem to work. Here is my code.

//Fetches the radio value from the post if the poster entered it.
if ($_REQUEST['message']['character'] >= 1) {
$select = $smcFunc['db_query']('', '
SELECT * FROM {db_prefix}rpg_characters
WHERE character_id = {string:id}
',
array(
'id' => $_REQUEST['message']['character'],
)
);
$words = 50; //Words are at 50 for now just as a test...
while($row = $smcFunc['db_fetch_assoc']($select))
{
$character_id = $_REQUEST['message']['character'];
$exp = $row['exp'];
$mexp = $row['mexp'];
$newexp = 0;
$newmexp = 0;
$lvl = $row['level'];

if($lvl >= 1) {
$exprate = 1.1;
$newexp = ceil($exprate*$words+$exp);
}

if($lvl >= 5) {
$exprate = 1.2;
$newexp = ceil($exprate*$words+$exp);
}

if($newexp > $mexp) {
$exp = $mexp - $newexp;
$lvl = $lvl +1;
$mexp = $lvl * 1000;

$updatecharacter = $smcFunc['db_query']('', '
UPDATE {db_prefix}rpg_characters SET level = {string:level}, exp = {string:exp}, mexp = {string:mexp} WHERE character_id = {int:c_id}',
array( 'level' => $lvl, 'exp' => $exp, 'mexp' => $mexp, 'c_id' => $character_id,));
}
else {
$updatecharacter = $smcFunc['db_query']('', '
UPDATE {db_prefix}rpg_characters SET exp = {string:exp} WHERE character_id = {int:c_id}',
array( 'exp' => $exp, 'c_id' => $character_id,));
}
}
}


Any suggestions as for what to do? The script itself doesn't have any errors however, it's not running properly. It's suppose to update information in the database for the table {db_prefix}rpg_characters but, it doesn't do that at all. Tbh, I'm not sure if my code is picking up the character id of the radio boxes. Any help?
Otsikko: Re: Creating a Level Up System
Kirjoitti: onepiece - elokuu 31, 2013, 02:14:51 IP
Instead of $_REQUEST['message']['character'] shouldn't it be just $_REQUEST['character'] in Post.php?
Otsikko: Re: Creating a Level Up System
Kirjoitti: Arantor - elokuu 31, 2013, 02:19:34 IP
Oh, it probably should but there are *far* bigger messes in there, like the need to create a new database connection to access what should be in the same database, the use of fetch_array (fetch_assoc is less wasteful and gives you everything you'd actually need) and the use of member_name as the key of the table when using the member id is faster, smaller and absolutely consistent (because member_name is the user's username which can be changed whereas the member id can't)
Otsikko: Re: Creating a Level Up System
Kirjoitti: onepiece - elokuu 31, 2013, 02:55:24 IP
If I started with things going wrong in that code there would no end to it. So I'll just tell what the user wants to know only.
Otsikko: Re: Creating a Level Up System
Kirjoitti: Arantor - elokuu 31, 2013, 02:56:28 IP
Yeah, trouble is I'm prevented by a sense of ethics from doing that. It's not up to me whether a user fixes such matters, but I feel I should raise them as matters that should be fixed.
Otsikko: Re: Creating a Level Up System
Kirjoitti: Jamora1124 - elokuu 31, 2013, 11:35:10 IP
member_name isn't the key for the table {db_prefix}rpg_characters. character_id is. I changed member_name to member_id though because I literally just realized that user's can change their names on their own. I'm new to simple machines and I thought that name change was an Admin thing. I guess not.

Also, I used $_REQUEST['character'] before I used $_REQUEST['message']['character'] and still no beans.
Otsikko: Re: Creating a Level Up System
Kirjoitti: Jamora1124 - syyskuu 06, 2013, 11:30:06 AP
*BUMP

Anyone? Or perhaps I'm going about coding this the wrong way and someone may have a better way.