Thread: Blackjack
View Single Post
  #7  
Old 01-19-2017, 04:50 PM
ghanja's Avatar
ghanja
Dragon
 
Join Date: Aug 2012
Location: Hershey, PA
Posts: 499
Default

Code:
#############
#Quest Name: Black Jack's Blackjack
#Author: Cisyouc
#################

sub DealCard
{
	return quest::ChooseRandom(1,2,3,4,5,6,7,8,9,10,11);
}

sub EVENT_SAY
{
	my $permissions = $qglobals{"permissions"};
	my $serverTotal = $qglobals{"serverTotal"};
	my $clientTotal = $qglobals{"clientTotal"};

	if($text=~/hail/i)
	{
		quest::say("Greetings! Want to play balackjack? Hand me your bet to begin! All games return double on win! $permissions");
		return; #return on each say
	}

	if($permissions < 1)
	{ #easier to escape than than heavily nested if/else conditions
		quest::say("Have you bet?");
		return;
	}

	if($text=~/deal/i)
	{
		my $card1 = DealCard();
		my $card2 = DealCard();
		$clientTotal = $card1 + $card2;
		$serverTotal = DealCard() + DealCard();
		if($serverTotal < 16)
		{
			$serverTotal = $serverTotal + DealCard();
		}
		quest::say("The dealer passes you a $card1 and a $card2 giving you a total of $clientTotal. The dealer is showing a $serverTotal. Say [ ". quest::saylink("yes")." ] for another card or [ ". quest::saylink("no")." ] to stay.");
		quest::setglobal("permissions", 2, 0, "F"); #set permissions
		quest::setglobal("clientTotal", $clientTotal, 0, "F"); #set permissions
		quest::setglobal("serverTotal", $serverTotal, 0, "F"); #set permissions
		return; 
	}

	if($text=~/yes/i)
	{
		if($permissions != 2)
		{
			quest::say("You must [ ". quest::saylink("deal")." ] first!");
			return;
		}
		my $card1 = DealCard();
		$clientTotal = $clientTotal + $card1;
		quest::setglobal("clientTotal", $clientTotal, 0, "F"); #set permissions
		if ($clientTotal > 21) {
			quest::say("The dealer has handed you a $card1, giving you a total of $clientTotal. You bust, and lose the game.");
			quest::setglobal("bet", 0, 0, "F"); #reset
			quest::setglobal("permissions", 0, 0, "F"); #reset
			return;
		}
		quest::say("The dealer has handed you a $card1, giving you a total of $clientTotal. The deal is showing a $serverTotal. Say [ ". quest::saylink("yes")." ] for another card or [ ". quest::saylink("no")." ] to stay.");
		return;
	}

	if($text=~/no/i)
	{
		if($permissions != 2)
		{
			quest::say("You must [ ". quest::saylink("deal")." ] first!");
			return;
		}

		my $iWon = 0;
		if ($clientTotal > 21 && $serverTotal < 21) 
		{ #client went over, server didn't

		} elsif ($serverTotal > 21 && $clientTotal < 21) 
		{ #server went over, client didn't
			$iWon = 1;
		} elsif ($clientTotal < $serverTotal && $serverTotal <= 21)
		{ #server won fair

		} elsif ($serverTotal < $clientTotal && $clientTotal <= 21)
		{ #client won fair			
			$iWon = 1;
		}

		if (!$iWon) 
		{ 
			quest::say("Dealer wins with $serverTotal. Thanks for playing.");
		} else
		{
			quest::say("You win with $clientTotal. Thanks for playing.");
			my $cash = $qglobals{"bet"};
			$copper	= ($cash % 10);
			$silver	= (int ($cash / 10) % 10);
			$gold = (int ($cash / 100) % 10);
			$platinum = int ($cash / 1000);
			quest::givecash($copper, $silver, $gold, $platinum); #Give money
		}
		quest::setglobal("bet", 0, 0, "F"); #reset
		quest::setglobal("permissions", 0, 0, "F"); #reset 
		return;
	}
}

sub EVENT_ITEM
{	
	my $cash = $copper + ($silver * 10) + ($gold * 100) + ($platinum * 1000);

	if ($cash < 1) {
		quest::say("You must give me money to bet");
		return;
	}

	if ($qglobals{"permissions"} > 0) {
		quest::say("You still have a deal going on.");
		return;
	}
	$cash = $cash * 2; #double the bet
	quest::say("Your bet of $platinum platinum, $gold gold, $silver silver, $copper copper has been saved. You may [ ". quest::saylink("deal")." ] the cards now.");
	quest::setglobal("bet", $cash, 0, "F"); #easier to just set all money.
	quest::setglobal("permissions", 1, 0, "F"); #set permissions
}
I'd probably use entity variables to reduce the read/writes to DB, but try the above.
Reply With Quote