<?php
/*****************************************************************************
 *   Gambling (/inc/plugins/myplaza/gamble.php)
 *     - MyPlaza for MyBB 1.2
 *    By ZiNgA BuRgA, 2007
 * 
 * Adds some items which allow users to gamble their money.
 *****************************************************************************/

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

function gamble_lang()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->gamble_rndmoney_range = 'Get a random amount of money between %1$s and %2$s.';
			$lang->quickbet_info = 'You have a %1$s%% chance to win %2$s.';
			
			$lang->gamble_rndmoney_got = 'You got %1$s!';
			$lang->gamble_rndmoney_got_log = 'Got %1$s from Random Money.';
			
			$lang->gamble_guessno_info = 'Guess a %1$s digit number:';
			
			$lang->gamble_guessno_correct = 'You guessed correctly!!';
			$lang->gamble_guessno_correct_log = 'Won pot of %1$s from Guess the Number.';
			
			$lang->gamble_guessno_pot = 'There is currently %1$s in the pot.';
			
			$lang->gamble_guessno_incorrect = 'Sorry, that\'s not the number.';
			$lang->gamble_guessno_incorrect_log = 'Incorrect guess for Guess the Number.';
			
			$lang->gamble_quickbet_win = 'You won!';
			$lang->gamble_quickbet_win_log = 'Won a Quick Bet.';
			
			$lang->gamble_quickbet_loose = 'You lost.';
			$lang->gamble_quickbet_loose_log = 'Lost a Quick Bet.';
	}
}

function gamble_lang_admin()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->gamble_name = 'Gambling';
			$lang->gamble_description = 'Adds items, allowing users to gamble their money.';
			
			$lang->rnd_money = 'Random Money';
			$lang->rnd_money_desc = 'Gives you a random amount of money.';

			$lang->guess_no = 'Guess the number';
			$lang->guess_no_desc = 'Adds money to the pot.  If you guess the number correctly, you get all the money in the pot!  The number will stay the same until someone guesses it correctly.';
			
			$lang->quick_bet = 'Quick Bet';
			$lang->quick_bet_desc = 'You either win or loose.  Simple :)';
			
			$lang->gamble_rndmoney_min = 'Minimum Random Money';
			$lang->gamble_rndmoney_min_desc = 'This is the minimum amount of money given out by the Random Money item.';
			$lang->gamble_rndmoney_max = 'Maximum Random Money';
			$lang->gamble_rndmoney_max_desc = 'This is the maximum amount of money given out by the Random Money item.';
			
			$lang->gamble_guessno_digits = 'Number of digits to guess for';
			$lang->gamble_guessno_digits_desc = 'This is the number of digits a user has to guess.';
			$lang->gamble_guessno_init = 'Initial Pot';
			$lang->gamble_guessno_init_desc = 'The initial amount of money in the pot.';
			
			$lang->gamble_quickbet_payoff = 'Quick Bet Payoff';
			$lang->gamble_quickbet_payoff_desc = 'Amount of money paid out for a &quot;win&quot;.';
			$lang->gamble_quickbet_chance = 'Chance of Winning';
			$lang->gamble_quickbet_chance_desc = 'The % chance of winning a Quick Bet.';
			
			$lang->gamble_options = 'Gamble Options';
	}
}

function gamble_info()
{
	return array(
		"website"		=> "http://myplaza.zingaburga.com",
		"author"		=> "ZiNgA BuRgA",
		"authorsite"	=> "http://zingaburga.com/",
		"version"		=> "1.0",
		"compatibility"	=> array(0.5)
	);
}

function gamble_activate()
{
	// stop people trying to activate this module as a MyBB plugin
	if(!defined("IN_MYPLAZA_ADMIN"))
		cperror('This is not a normal MyBB plugin!  Please upload this file to your /inc/plugins/myplaza directory.');
	
	global $lang;
	// add our items
	myplaza_add_item(array(
		"name" => $lang->rnd_money,
		"idname" => 'gamble_rndmoney',
		"description" => $lang->rnd_money_desc,
		"cost" => 100,
		"htmlextra" => '{$lang->gamble_rndmoney_range}'
	));
	myplaza_add_item(array(
		"name" => $lang->guess_no,
		"idname" => 'gamble_guessno',
		"description" => $lang->guess_no_desc,
		"cost" => 10,
		"htmlextra" => '{$lang->gamble_guessno_info} <input type="text" name="guess" maxlength="{$mybb->settings[\'gamble_guessno_digits\']}" /><br />{$lang->gamble_guessno_pot}'
	));
	myplaza_add_item(array(
		"name" => $lang->quick_bet,
		"idname" => 'gamble_quickbet',
		"description" => $lang->quick_bet_desc,
		"cost" => 50,
		"htmlextra" => '{$lang->quickbet_info}'
	));
	
	// add settings
	add_settings(array(
		array(
			"name"			=> 'gamble_rndmoney_min',
			"title"			=> $lang->gamble_rndmoney_min,
			"description"	=> $lang->gamble_rndmoney_min_desc,
			"optionscode"	=> 'text',
			"value"			=> '0',
		),
		array(
			"name"			=> 'gamble_rndmoney_max',
			"title"			=> $lang->gamble_rndmoney_max,
			"description"	=> $lang->gamble_rndmoney_max_desc,
			"optionscode"	=> 'text',
			"value"			=> '190',
		),
		
		array(
			"name"			=> 'gamble_guessno_digits',
			"title"			=> $lang->gamble_guessno_digits,
			"description"	=> $lang->gamble_guessno_digits_desc,
			"optionscode"	=> 'text',
			"value"			=> '2',
		),
		array(
			"name"			=> 'gamble_guessno_init',
			"title"			=> $lang->gamble_guessno_init,
			"description"	=> $lang->gamble_guessno_init_desc,
			"optionscode"	=> 'text',
			"value"			=> '50',
		),
		
		array(
			"name"			=> 'gamble_quickbet_payoff',
			"title"			=> $lang->gamble_quickbet_payoff,
			"description"	=> $lang->gamble_quickbet_payoff_desc,
			"optionscode"	=> 'text',
			"value"			=> '95',
		),
		array(
			"name"			=> 'gamble_quickbet_chance',
			"title"			=> $lang->gamble_quickbet_chance,
			"description"	=> $lang->gamble_quickbet_chance_desc,
			"optionscode"	=> 'text',
			"value"			=> '50',
		)
	));
}

function gamble_deactivate()
{
	global $db, $cache;
	myplaza_remove_module_items();
	$db->delete_query(MY_TABLE_PREFIX.'settings', "name IN ('gamble_rndmoney_min', 'gamble_rndmoney_max', 'gamble_guessno_digits', 'gamble_guessno_init', 'gamble_quickbet_payoff', 'gamble_quickbet_chance')");
	my_rebuild_settings();
	
	delete_cache_items('gamble_guessno');
}

function gamble_run($item)
{
	global $mybb, $lang, $db, $buyRtnMsg, $buyLogMsg;
	
	switch($item['idname'])
	{
		case 'gamble_rndmoney':
			$min = floatval($mybb->settings['gamble_rndmoney_min']);
			$max = floatval($mybb->settings['gamble_rndmoney_max']);
			$decimals = intval($mybb->settings['myplaza_money_decimals']);
			
			// do we have a silly admin?
			if($max < $min) return false;
			
			$factor = pow(10, $decimals);
			$rndmoney = mt_rand($min * $factor, $max * $factor) / $factor;
			
			user_change_money($mybb->user, $rndmoney);
			$buyRtnMsg = sprintf($lang->gamble_rndmoney_got, my_format_money($rndmoney));
			$buyLogMsg = sprintf($lang->gamble_rndmoney_got_log, my_format_money($rndmoney));
			
			return true;
			
		case 'gamble_guessno':
			global $cache;
			
			$gcache = $cache->read('gamble_guessno');
			// if there's no valid number...
			if(!is_array($gcache))
			{
				// regenerate it
				gen_next_guessno();
				$gcache = $cache->read('gamble_guessno');
			}
			
			$gcache['pot'] += calcCost($item['cost']);
			
			if($mybb->input['guess'] == $gcache['curnum'])
			{
				user_change_money($mybb->user, $gcache['pot']);
				gen_next_guessno();
				
				$buyRtnMsg = $lang->gamble_guessno_correct;
				$buyLogMsg = sprintf($lang->gamble_guessno_correct_log, $gcache['pot']);
			}
			else
			{
				$cache->update('gamble_guessno', $gcache);
				
				$buyRtnMsg = $lang->gamble_guessno_incorrect;
				$buyLogMsg = $lang->gamble_guessno_incorrect_log;
			}
			
			return true;
			
		case 'gamble_quickbet':
			if(mt_rand(0, 999999999) < (floatval($mybb->settings['gamble_quickbet_chance'])*10000000))
			{
				user_change_money($mybb->user, floatval($mybb->settings['gamble_quickbet_payoff']));
				$buyRtnMsg = $lang->gamble_quickbet_win;
				$buyLogMsg = $lang->gamble_quickbet_win_log;
			}
			else
			{
				$buyRtnMsg = $lang->gamble_quickbet_loose;
				$buyLogMsg = $lang->gamble_quickbet_loose_log;
			}
			
			return true;
			
		default:
			return false;
	}
}

function gen_next_guessno()
{
	global $cache, $mybb;
	$cache->update('gamble_guessno', array(
		'curnum' => mt_rand(0, pow(10, intval($mybb->settings['gamble_guessno_digits'])) -1),
		'pot' => floatval($mybb->settings['gamble_guessno_init'])
	));
}

function gamble_htmlextra()
{
	global $lang, $mybb, $cache;
	$lang->gamble_rndmoney_range = sprintf($lang->gamble_rndmoney_range,
		my_format_money(floatval($mybb->settings['gamble_rndmoney_min'])),
		my_format_money(floatval($mybb->settings['gamble_rndmoney_max']))
	);
	$lang->gamble_guessno_info = sprintf($lang->gamble_guessno_info, intval($mybb->settings['gamble_guessno_digits']));
	
	
	$gcache = $cache->read('gamble_guessno');
	// if there's no valid number...
	if(!is_array($gcache))
	{
		// regenerate it
		gen_next_guessno();
		$gcache = $cache->read('gamble_guessno');
	}
	$lang->gamble_guessno_pot = sprintf($lang->gamble_guessno_pot, my_format_money($gcache['pot']));
	$lang->quickbet_info = sprintf($lang->quickbet_info,
		floatval($mybb->settings['gamble_quickbet_chance']),
		my_format_money(floatval($mybb->settings['gamble_quickbet_payoff']))
	);
}

function gamble_admin($process)
{
	if($process)
	{
		myplaza_process_settings(array('gamble_rndmoney_min', 'gamble_rndmoney_max', 'gamble_guessno_digits', 'gamble_guessno_init', 'gamble_quickbet_payoff', 'gamble_quickbet_chance'));
	}
	else
	{
		global $lang;
		myplaza_langload('gamble');
		
		cpheader_myplaza_module();
		starttable();
		tableheader($lang->gamble_options, "", 2);
		tablesubheader($lang->rnd_money, '', 2);
		myplaza_generate_settings_code(array('gamble_rndmoney_min', 'gamble_rndmoney_max'));
		tablesubheader($lang->guess_no, '', 2);
		myplaza_generate_settings_code(array('gamble_guessno_digits', 'gamble_guessno_init'));
		tablesubheader($lang->quick_bet, '', 2);
		myplaza_generate_settings_code(array('gamble_quickbet_payoff', 'gamble_quickbet_chance'));
		endtable();
		
		cpfooter_myplaza_module();
	}
}

?>