Using Percentages

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
JrTroopa
Registered User
Posts: 142
Joined: Fri Aug 31, 2007 12:10 am

Using Percentages

Post by JrTroopa » Sat Mar 01, 2008 11:51 pm

Hello All,

It’s been awhile since I have been coding using phpbb2, and I’ve been using phpbb3. I’ve had a forum, that I haven’t given a new, large feature in awhile. I am currently coding a thieving mod, which will enable users to rob from the bank.

I believe I have parts right, but I am trying to make it, that a user has a certain “percentage” of robbing the bank, but I am unsure on how to do this. Any tips would be appreciated.

Thanks!

User avatar
battye
Extension Customisations
Extension Customisations
Posts: 10940
Joined: Wed Feb 11, 2004 11:02 am
Location: Australia
Contact:

Re: Using Percentages

Post by battye » Sun Mar 02, 2008 3:03 am

Your post is quite vague, but you could try this:

Code: Select all

$bank = $row['total_money']; // The total money in the bank
$percentage = (1/10); // Amount user can steal (ie 1/10 is 10% of $bank)

$steal = $HTTP_POST_VARS['steal']; // In dollars the amount the user wanted to steal, set as POST vars

if( ($bank * $percentage) < $steal )
{
// Allow the user to rob the bank
}

else
{
message_die(GENERAL_MESSAGE, 'You cannot steal that much');
}
Let me re-iterate, your post was very vague. Don't expect to just use that code above and it works, you'll certainly need to modify it for your needs.
Customisations Team Member

https://github.com/battye/php-array-parser - Give it a Star! :D

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Using Percentages

Post by Brf » Sun Mar 02, 2008 4:03 am

If you are looking for a "percentage chance" that a user can do something, you can use the rand() function.

Use something like:

if (rand(1,100) <= 5)
{
// for a 5% chance of success.
}

JrTroopa
Registered User
Posts: 142
Joined: Fri Aug 31, 2007 12:10 am

Re: Using Percentages

Post by JrTroopa » Sun Mar 02, 2008 10:14 pm

Thank You both for your advice, I have merged/edited both of them, and it seems to work. Sorry about the vagueness.

I have another question, how will I be able to make it, so that a user can only rob the bank, in a certain amount of hours?

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Using Percentages

Post by Brf » Mon Mar 03, 2008 11:54 am

Store off the time when they attempt to rob it, using time(). Then the next time they try it, check the current time and do not allow it until a certain number of seconds has gone by.

JrTroopa
Registered User
Posts: 142
Joined: Fri Aug 31, 2007 12:10 am

Re: Using Percentages

Post by JrTroopa » Mon Mar 03, 2008 8:55 pm

Thanks, I'm having a bit of trouble understanding the store part, does that mean to the database? Also, could you please show me an example of this whole process.

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Using Percentages

Post by RMcGirr83 » Mon Mar 03, 2008 10:01 pm

Code: Select all

if( ($bank * $percentage) < $steal )
{
// Allow the user to rob the bank
   $steal_time = time();
   // store the time the user stole in the database
   		$sql = "UPDATE " . USERS_TABLE . "
			SET user_steal_time = " . $steal_time . "
			WHERE user_id =" . $user_data['user_id'];
}
eg....but you have to make a new column in the phpbb_user table in the database called "user_steal_time" in the case above
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

JrTroopa
Registered User
Posts: 142
Joined: Fri Aug 31, 2007 12:10 am

Re: Using Percentages

Post by JrTroopa » Tue Mar 04, 2008 1:36 am

Thank You, would this be the correct SQL?

Code: Select all

ALTER TABLE `phpbb_user` ADD `user_steal_time` TINYINT( 1 ) DEFAULT '0' ,

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Using Percentages

Post by RMcGirr83 » Tue Mar 04, 2008 2:12 am

The time function is a unix time stamp that is calculated (seconds from January 1, 1970 IIRC)...int(11) probably would be better.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

JrTroopa
Registered User
Posts: 142
Joined: Fri Aug 31, 2007 12:10 am

Re: Using Percentages

Post by JrTroopa » Fri Mar 21, 2008 11:33 pm

Hello All,

After awhile trying to get my servers back up, I've been working on the mod again, and I was wondering if anyone could please check the script? This is supposed to be compatible with the Bank Mod.

Code: Select all

$template->assign_block_vars('', array());
}
elseif ($_REQUEST['action'] == "rob")
{
	$template->set_filenames(array( 'body' => 'bank_body.tpl'));
	if ( !$userdata['session_logged_in'] )
	{
		$redirect = "bank.$phpEx&action=rob&rob=$rob";
		$redirect .= ( isset($user_id) ) ? '&user_id=' . $user_id : '';
		header('Location: ' . append_sid("login.$phpEx?redirect=$redirect", true));
	}
  $rob = round($_REQUEST['rob']);
  if ((!is_numeric($rob)) || ($rob < 1)) { message_die(GENERAL_MESSAGE, $lang['error_rob']); }
  if ($userdata['user_jailed']) { message_die(GENERAL_MESSAGE, $lang['error_jailed'].'!'); }
  if ($userdata['user_dead']) { message_die(GENERAL_MESSAGE, $lang['error_dead'].'!'); }
 
  $bankholdings = $bhrow['holding']; // The total money in the bank
  $percentage = (rand(1,100) <= 5); // Amount user can steal (ie 5% of $bank)

  $rob = $HTTP_POST_VARS['rob']; // In dollars the amount the user wanted to steal, set as POST vars

  if( ($bank * $percentage) < $rob )
  {
  // Allow the user to rob the bank
       $rob_time = time()=86400;
   // store the time the user stole in the database
         $sql = "UPDATE " . USERS_TABLE . "
         SET user_steal_time = " . $rob_time . "
         WHERE user_id =" . $user_data['user_id'];
  }
  else
  {
  message_die(GENERAL_MESSAGE, 'You cannot steal that much');
  }

  $newbalance = $row['holding'] - $robtotal;
	$newpoints = $userdata['user_points'] + $rob;
	$newtotalrob = $row['totalrob'] + $rob;

	if ( !($db->sql_query("update " . USERS_TABLE . " set user_points='$newpoints' where user_id='{$userdata['user_id']}'")) ) { message_die(GENERAL_MESSAGE, 'Fatal Updating User Points!<br>'.mysql_error()); }

	if ( !($db->sql_query("update phpbb_bank set holding='$newbalance', totalrob='$newtotalrob' where name='{$userdata['user_id']}'")) ) { message_die(GENERAL_MESSAGE, 'Fatal Updating User Points!<br>'.mysql_error()); }

  $newtotalrob = $board_config['banktotalrob'] + $rob;
  $sql = "update " . CONFIG_TABLE . " set config_value='$newtotalrob' where config_name='banktotalrob'";
  if ( !($db->sql_query($sql)) ) { message_die(GENERAL_MESSAGE, 'Fatal Updating Total Robbed!<br>'.mysql_error()); }
  $bankcommands = '<tr><td class="row2"><span class="gensmall">'.$lang['have_rob'].' '.$rob.' '.$board_config['points_name'].' '.$lang['from_account'].'.<br>'.$lang['new_balance'].' '.$newbalance.'.<br>'.$lang['now_have'].' '.$newpoints.' '.$board_config['points_name'].' '.$lang['on_hand'].'.</span></td></tr>';
	$banklocation = ' -> <a href="'.append_sid("bank.".$phpEx).'" class="nav">'.$board_config['bankname'].'</a> -> <a href="'.append_sid("bank.$phpEx?action=deposit&deposit=$deposit").'" class="nav">'.$lang['rob'].' class="nav">'.$lang['rob'].' '.$board_config['points_name'].'</a>';
	$page_title = $board_config['bankname'];
	$title = 'Withdraw '.$board_config['points_name'];
	$template->assign_vars(array(
		'BANKLOCATION' => $banklocation,
		'L_BANK_TITLE' => $title,
		'BANKTABLEROWS' => 1,
		'BANKLIST' => $bankcommands,
	));
That will be an edit, the user will need to make inside the bank.php.

I've already tested it out, but all I receive is a blank page when someone clicks on "bank".

Any advice would be greatly appreciated.

Thanks!

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51874
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Using Percentages

Post by Brf » Sat Mar 22, 2008 11:24 am

YOu would probably get better help posting in the Bank mod's support thread, since the author of that mod would know what you need for that mod.

JrTroopa
Registered User
Posts: 142
Joined: Fri Aug 31, 2007 12:10 am

Re: Using Percentages

Post by JrTroopa » Sat Mar 22, 2008 12:30 pm

There's actually quite a problem with that. The mod author of that mod, doesn't have their web site running, and their topic here is locked. Should I just send the bank mod to someone, and ask them to have a look at my edits?

Post Reply

Return to “[2.0.x] MOD Writers Discussion”