#!/usr/bin/perl # NOTE: the above line must be changed to show the path to the # Perl interpreter on your system! Leave the #! as is, but # the path may have to be changed if yours is different. ######################################################################### # SFEPoker v1.0.6 # # Simple online video poker game for a website. # # Copyright 2003, Kristina L. Pfaff-Harris # # All rights reserved. # # # ######################################################################### ##### License for SFEPoker 1.0.6 ##### ##### This program may be used free of charge under the following ##### conditions: ##### ##### 1. All instructions and Copyright lines must remain unchanged. ##### ##### 2. All pages generated by the program must contain one of the ##### following pieces of HTML code: ##### ##### Powered by SFEPoker: ##### ##### http://www.tesol.net/scripts ##### OR: ##### ##### ##### ##### You may not remove the information described above from the script ##### without express written permission from the author. ##### ##### 3. You may not sell or distribute this program. You may charge ##### a reasonable fee for installing it for a client as long as ##### you make it clear that you are not the author, and you are ##### not selling the program to them: only charging for installing ##### it. ##### ##### 4. You agree that this program is offered without warranty of ##### any kind, including warranty of fitness for a particular ##### purpose. You further agree that the author and all sites ##### associated in any way with this program are not liable for ##### any damage or loss incurred as a result of using this program. ##### ##### 5. You may modify the program for your own use but you may not ##### distribute modified copies under any circumstances without ##### express written permission from the author. ##### ##### 6. Use of this program requires agreement to all the terms and ##### conditions of this license. If you do not agree to one or ##### more of these terms, you may not use the program. ##### ######################################################################### ##### # ##### IMPORTANT INSTRUCTIONS: # ##### # ##### In this program, I have put **CHANGE** in all the places where # ##### you will need to modify the program to run on your server, so # ##### that you can easily find all the places where changes are # ##### necessary. This program must be chmod 755 or 775 in order to # ##### work, and the directory where you want SFEPoker to put its # ##### files must be chmod 777. # ##### # ######################################################################### # # # IMPORTANT: If you FTPed this program to your server in "binary" mode, # # it will NOT work! If you're not sure, please go back and FTP it # # to your server again and make sure you use "ascii" mode. If you get # # "500 Server Error", this is almost always the cause. :) # # # # Using the program: # # This program requires the file "sfepoker.cgi" at a minimum in # # order to work. Please read the README.sfepoker file for more # # information. # # # # Please check the Scripts for Educators FAQ first # # (http://tesol.net/scripts/FAQ/) if you have problems. If none of that # # helps, email me and I'll at least try to help. :-) # # # # I hope this program proves useful to you! Please contact me # # (http://tesol.net/scriptmail.html) for any bugs or feedback. # ######################################################################### ##### # ##### BEGIN SECTION WHERE YOU WILL NEED TO CHANGE THINGS: # ##### # ##### In this section, there are several places where you'll need # ##### to make changes. Please read all instructions carefully # ##### before you make the changes. # ##### # ######################################################################### # $basedir: This is the base directory on your server where you # want the program to store the files that keep track of # the players' credits and their cards for their games. # *CHANGE* this to the full base system path # to where you want these files to be created and stored. # Please see the README.sfepoker file for more # information about $basedir, and how to set it up. # Please note that this is NOT a URL, and should NOT # contain "http://" # # If your web server is a Unix-type system, then this # probably MUST begin a /, and the folder must be writeable # by the web server. For example: # $basedir = "/path/to/your/files"; # # If your web server is a Windows-type system, then this # probably MUST begin a "c:" or "d:" or "e:". For example: # $basedir = "c:/path/to/your/files"; # # One slight exception to the above is this: if you make a directory # called "poker" in the same directory where this script is, then # what I have below may work. If nothing else, it's worth a shot. :-) $basedir = "./poker_files/"; # $keep_cookies_time: Information about each player and their # "credits," cards, and so forth, is stored in a cookie in their browser, # but also in files on the server. (This is because it's easy to get # around anything in a browser, but not so easy to get around something # on the server.) $keep_cookies_time is how long, in seconds, you want # to keep these files lying around if the person isn't playing. Note # that if a person closes their browser, they'll probably lose the cookie # and have to create new files anyway. The default timeout is 15 minutes, # meaning that if someone doesn't play for 15 minutes, their session will # time out and they'll start over. If you do not want to ever delete these # files, set $keep_cookies_time = "forever"; (This is not advisable because # eventually this could take up all your disk space.) One hour is # 3600. 24 hours is 86400. One week is 604800. One month is 2592000. $keep_cookies_time = 900; # $cgi_url is the web address of where you've set up this script. # You'll need to change this to the URL of where you've set it up on # your site. If you get a "File not found" error, it usually means # that this isn't set up correctly. **CHANGE** this to the web # address of this script on your web server. $cgi_url = "http://yourdomain.com/sfepoker.cgi"; # $image_url is the web address of where you've put the gif images # that came with this program. These images are needed in order to draw the # cards. If the corners of the cards look strange, or show broken images, # it probably means that this setting is not correct. **CHANGE** this # to the URL of where you've put the gif image files that came with this # program. This MUST begin with "http://" and it MUST end with a "/" as # in the example below. $image_url = "http://www.yourdomain.com/poker_images/"; # As you'll notice from the demo, there's a title across the top # of the game page that shows a link. # $game_title_text is the text of that link, or, in other words, # the part of the link you see. # $game_title_url is the site you want this to link to. You can have # it say "Back to My Site" and have the link go to http://yoursite.com, # or **CHANGE** it to whatever you like. # IMPORTANT: DO NOT USE any " or @ signs or the script will break. # ONLY change in between the "" that are already there. $game_title_text = "SFE Presents: Online Video Poker"; $game_link_url = "http://tesol.net/scripts/SFEPoker/"; # $page_title_text is just the text for the title of the pages # generated by the program. # IMPORTANT: DO NOT USE any " or @ signs or the script will break. # ONLY change in between the "" that are already there. $page_title_text = "Welcome to Online Video Poker!"; # Now, the rest of this is just to make it easier to change the titles # and words that are shown for various things. If you want to translate # this game into another language, just translate the words below. # IMPORTANT: DO NOT USE any " or @ signs or the script will break. # ONLY change in between the "" that are already there. # Names for winning hands $royal_flush_text = "Royal Flush"; $straight_flush_text = "Straight Flush"; $four_of_a_kind_text = "Four of a Kind"; $straight_text = "Straight"; $flush_text = "Flush"; $full_house_text = "Full House"; $three_of_a_kind_text = "Three of a Kind"; $two_pair_text = "Two Pair"; $jacks_or_better_text = "Jacks or Better"; # What to say to the loser. $loser_text = "LOSER!!!!"; $loser_amount_text = "ZIP!!!"; # Miscellaneous text $new_game_text = "New Game."; $reload_detected_text = "Reload Detected."; $hold_text = "HOLD"; $held_text = "HELD"; $your_cards_text = "Your Cards"; $click_on_a_card_text = "Click on a card to select or unselect it."; $draw_text = "Draw"; $you_have_won_text = "You have won %WINNINGS%"; $you_have_text = "You have %WINNINGS% in the bank."; $betting_text = "Betting 5."; $payouts_text = "Payouts"; $balance_text = "Balance: "; $credits_text = "Credits"; $rules_text = "Rules"; $play_again_text = "Play Again?"; $entertainment_only_text = " This game is for ENTERTAINMENT ONLY.
Credits are NOT-NEGOTIABLE.
No actual money or currency of any kind is involved in this game. "; ######################################################################### ##### # ##### END SECTION WHERE YOU WILL NEED TO CHANGE THINGS: # ##### # ##### You should not need to change anything beyond this point. In # ##### fact, anything you change beyond this point may completely # ##### break the program and cause it not to function. However, feel # ##### free to browse through the code if you like that sort of thing. # ##### :-) # ##### # ######################################################################### my %data = &get_data(); # If $basedir doesn't exist, or we can't create files there, then the script # won't run. So, we'll run a check or two to make sure we can, and if not, # we'll print an informative error message. &no_basedir_error(); # First, let's clear out old cookie files. Otherwise, they build up # until there are millions of them. if($keep_cookies_time && $keep_cookies_time !~ /^forever$/ && $keep_cookies_time =~ /^\d+$/){ opendir(DIR, "$basedir"); @cookiefiles = readdir(DIR); closedir(DIR); foreach $ckf (@cookiefiles){ if(-f "$basedir/$ckf" && $ckf =~ /^\d\d\d\d\d\d\d\d\d/){ if(time - (stat("$basedir/$ckf"))[9] > $keep_cookies_time){ unlink("$basedir/$ckf"); } } } } # We keep track of the user's cards and money and so forth by setting # a cookie which we use to find the file where we saved their stuff # on the server. $cookie = $ENV{'HTTP_COOKIE'}; # I was kind of sloppy in setting the cookie when I was writing this and I # set two different cookies, which ruined my whole day. This is just for # those beta testers who were using it. $cookie =~ s/Student=;//; if($ENV{'SERVER_NAME'} =~ /linguistic-funland.com$/i || $ENV{'SERVER_NAME'} =~ /tesol.net$/i){ $entertainment_only_text .= "
**DEMO VERSION** All credits will be reset to zero periodically in this demo.
"; } ($stuff,$cookie) = split(/=/, $cookie); if($cookie !~ /^\d+/){ $cookieyear = (CORE::localtime(time))[5] + 1900 + 25; $cookie = time . $$ . $ENV{'REMOTE_ADDR'}; print "Content-type: text/html\n"; print "Set-Cookie: RCV=$cookie; expires=Fri, 26-Dec-$cookieyear 15:45:40 GMT;\n\n"; } else { print "Content-type: text/html\n\n"; } $cookie =~ s/;//; $moneyfile = "$basedir/$cookie"; $cardfile = "$basedir/$cookie.cards"; $screenfile = "$basedir/$cookie.screentracker"; # No longer needed if(-f $screenfile){ unlink($screenfile); } # If the money file based on their cookie doesn't exist, then we'll # create it and give them 100 to start off with. unless(-f $moneyfile){ open(F, ">>$moneyfile"); print F "100"; close(F); } # This is just a shortcut so I didn't have to keep typing it. :-) $bg = " bgcolor=\"#FFFFFF\""; $pagetop = <$page_title_text EOF $topmsg = <
$game_title_text
%MSG%
$credits_text: %CREDITS%
EOF $cardrow_html = <
$your_cards_text
$click_on_a_card_text
%CARDROW%%HOLDROW%
EOF $pagebottom = <
$royal_flush_text500 $straight_flush_text200 $four_of_a_kind_text100$full_house_text50 $flush_text40 $straight_text20$three_of_a_kind_text15 $two_pair_text10 $jacks_or_better_text5
$rules_text
$entertainment_only_text
$payouts_text
Powered by SFEPoker Online Video Poker program
Put this video poker game on your website free!
 
EOF # This is a basic deck of cards, all in order from 2 - A, clubs through # spades. @init_suits = ('C','H','D','S'); @init_nums = (2,3,4,5,6,7,8,9,10,'J','Q','K','A'); foreach $suit (@init_suits){ foreach $num (@init_nums){ $init_cards[$i] = "$num.$suit"; $i++; } } # @init_cards should have all the cards in it now. # shuffle the deck srand; while (@init_cards){ push(@new, splice(@init_cards, rand @init_cards, 1)); } # @new now has the shuffled cards, so we stick it back into the array # @init_cards. @init_cards = @new; if($data{'FA'} eq "$draw_text"){ &draw(); } else { &deal(); } exit(); sub deal { # Find out how much money they have open(F, "<$moneyfile"); chomp($money = ); close(F); # If they don't have any, give 'em 100. if($money <= 0){ # print "SETTING UP"; $money = 100; open(F, ">$moneyfile"); print F $money; close(F); } $money -= 5; $credits = $money; open(M, ">$moneyfile"); print M $money; close(M); print $pagetop; $javascript_setit = ""; $you_have_text =~ s/%WINNINGS%/$money/s; $topmsg =~ s/%MSG%/$new_game_text $betting_text/s; $topmsg =~ s/%CREDITS%/$money/s; print $topmsg; close(F); # Actually, gameid is not used at the moment. if($data{'gameid'}){ $gameid = $data{'gameid'}; } else { $gameid = time.$$; } foreach $num (1,2,3,4,5){ $i = $num - 1; $card = shift(@init_cards); $bg = " bgcolor=\"#FFFFFF\""; $jscard = $card; $jscard =~ s/\./_/g; $holdrow .= "
$hold_text
\n"; $pcard = &draw_card($card,$i); #KPH $cardrow .= "$pcard\n"; # ♠ ♠ # ♣ ♣ # ♥ ♥ # ♯ ♦ } # save the rest of the deck for drawing later while(@init_cards){ $deck .= shift(@init_cards) . " "; } open(F, ">$cardfile"); print F $deck; close(F); $cardrow_html =~ s/%CARDROW%/$cardrow/s; $cardrow_html =~ s/%HOLDROW%/$holdrow/s; $cardrow_html =~ s/%BUTTON%/$draw_text/s; print $cardrow_html; print $javascript_setit; print $pagebottom; exit(); } sub draw { unless(-f $cardfile){ deal(); } #print "\n"; #chomp($which = ); foreach $held (@held){ $which .= "$held "; } $which =~ s/\s+$//; $which =~ s/^\s+//; @keep = split(/ /, $which); open(F, "<$cardfile"); @init_cards = split(/ /, ); close(F); # Go through each card that is left in the deck, # and compare it with the cards the user says they # held. If one of the cards they say they held is # also in the deck, they're lying, since we remove # the cards from the deck when we deal them. In that # case, they probably hit reload or are trying to # manipulate the system by telling the script that # they have a better hand than they do, so we'll # just deal them a new game. foreach $left_in_deck (@init_cards){ foreach $held_card (@held){ if($held_card eq $left_in_deck){ $new_game_text = $reload_detected_text; unlink($cardfile); &deal(); } } } #print "keep is $#keep
"; foreach $card (@keep){ $jscard = $card; $jscard =~ s/\./_/g; $dc = "card$jscard"; # print "$data{$dc}
"; if($data{$dc} ne ""){ $keep2[$data{$dc}] = $card; } } @keep = @keep2; foreach $num (0,1,2,3,4){ # print "\$keep[\$num] $keep[$num]
"; if($keep[$num] ne ""){ $card = $keep[$num]; $heldrow .= " $held_text 
 "; } else { $card = shift(@init_cards); $heldrow .= " 
 "; } ($n,$s) = split(/\./, $card); $suits{$s}++; $cards{$n}++; # print "$card "; ($face,$suit) = split(/\./, $card); if($card =~ /H|D/){$fontcolor = "#FF0000"; } if($card =~ /C|S/){$fontcolor = "#000000"; } $pcard = &draw_card($card); $cardrow .= "$pcard\n"; } foreach $suit ('H','S','D','C'){ if($suits{$suit} == 5){ $flush = 1; } } $i = 0; # Yes, this could be done more easily, and in a much more elegant # fashion. Sometimes brute force is good too, though. :-) foreach $num (@init_nums){ if($cards{$num} == 2 && $num =~ /^J|Q|K|A$/){ $jacksorbetter = 1; } if($cards{$num} == 2){ $pair++; } if($cards{$num} == 3){ $threeofakind = 1; } if($cards{$num} == 4){ $fourofakind = 1; } if($i > 12){ $i -= 12; } $i1 = $i + 1; if($i1 > 12){ $i1 -= 13;} $i2 = $i + 2; if($i2 > 12){ $i2 -= 13;} $i3 = $i + 3; if($i3 > 12){ $i3 -= 13;} $i4 = $i + 4; if($i4 > 12){ $i4 -= 13;} if($cards{$init_nums[$i]} && $cards{$init_nums[$i1]} && $init_nums[$i1] ne 'A' && $cards{$init_nums[$i2]} && $init_nums[$i2] ne 'A' && $cards{$init_nums[$i3]} && $init_nums[$i3] ne 'A' && $cards{$init_nums[$i4]}){ $straight = 1; } if($cards{$init_nums[$i]} && $init_nums[$i] eq "10" && $cards{$init_nums[$i1]} && $init_nums[$i+1] eq "J" && $cards{$init_nums[$i2]} && $init_nums[$i+2] eq "Q" && $cards{$init_nums[$i3]} && $init_nums[$i+3] eq "K" && $cards{$init_nums[$i4]} && $init_nums[$i+4] eq "A"){ $royal = 1; } $i++; } print $pagetop; if($straight == 1 && $flush == 1 && $royal == 1){ $winmsg = "$royal_flush_text!\n"; $win = (100 * 5); } elsif($straight == 1 && $flush == 1){ $winmsg = "$straight_flush_text!\n"; $win = (40 * 5); } elsif($fourofakind){ $winmsg = "$four_of_a_kind_text!\n"; $win = (20 * 5); } elsif($threeofakind && $pair == 1){ $winmsg = "$full_house_text!\n"; $win = (10 * 5); } elsif($flush == 1){ $winmsg = "$flush_text!\n"; $win = (8 * 5); } elsif($straight == 1){ $winmsg = "$straight_text!\n"; $win = (4 * 5); } elsif($threeofakind){ $winmsg = "$three_of_a_kind_text!\n"; $win = (3 * 5); } elsif($pair == 2){ $winmsg = "$two_pair_text!\n"; $win = (2 * 5); } elsif($jacksorbetter == 1){ $winmsg = "$jacks_or_better_text!\n"; $win = (1 * 5); } else { $winmsg = "$loser_text\n"; $win = "$loser_amount_text"; } open(F, "<$moneyfile"); chomp($_ = ); $money = $_; close(F); $money += $win; open(F, ">$moneyfile"); print F $money; close(F); $you_have_won_text =~ s/%WINNINGS%/$win/sg; $you_have_text =~ s/%WINNINGS%/$money/sg; $topmsg =~ s/%MSG%/$winmsg $you_have_won_text/s; $topmsg =~ s/%CREDITS%/$money/s; print $topmsg; $cardrow_html =~ s/%CARDROW%/$cardrow/s; $cardrow_html =~ s/%HOLDROW%/$heldrow/s; $cardrow_html =~ s/%BUTTON%/$play_again_text/s; print $cardrow_html; print $pagebottom; unlink($cardfile); exit(); } sub get_data { use CGI; use CGI::Carp qw/fatalsToBrowser/; $CGI::POST_MAX=4096; $CGI::DISABLE_UPLOADS=1; my $q = new CGI; my ($par, %data); @held = $q->param('hold'); foreach $par ($q->param()){ if($data{$par}){ $data{$par} .= "\0" . $q->param($par); } else { $data{$par} = $q->param($par); } } return %data; } # This draws the cards in the sense of constructing a card # to display given something like A.H or 2.C. sub draw_card { my($card,$i) = @_; my($face,$suit,$pcard); ($face,$suit) = split(/\./, $card); if($card =~ /H|D/){$fontcolor = "#FF0000"; } if($card =~ /C|S/){$fontcolor = "#000000"; } $pcard = $card; $pcard =~ s/S/♠/; $pcard =~ s/C/♣/; $pcard =~ s/H/♥/; $pcard =~ s/D/♦/; $suit =~ s/S/♠/; $suit =~ s/C/♣/; $suit =~ s/H/♥/; $suit =~ s/D/♦/; if($i ne ""){ $js0 = "onClick=\"void(setit($i,document.sfepoker.card$jscard));\" onMouseOver=\"this.style.cursor='pointer';\""; # $js = ""; # $jsa = ""; } else { $js0 = ""; $js = ""; $jsa = ""; } $pcard = " $js$pcard
$suit $jsa
"; return $pcard; } sub no_basedir_error { unless(-d "$basedir"){ $err = "Your base directory \$basedir does not appear to be a directory, or does not exist. You have set:

\$basedir = \"$basedir\";

in the script, but it doesn't appear to be correct."; if($basedir =~ /^http:/){ $err .= "\$basedir cannot begin with \"http://\". You may need to ask your web hosting provider, \"What is the full system path to files on my website?\".

"; } } else { open(F, ">$basedir/sfe_poker_testfile.$$"); print F ""; close(F); unless(-e "$basedir/sfe_poker_testfile.$$"){ $err .= "I cannot create files in your base directory \$basedir ($basedir). When I tried to create a file there, the server said \"$!\". \$basedir should be chmod 777 (on Unix/Linux-type systems) or otherwise set up so that the server can create files in that directory. Sometimes, you cannot chmod something to 777 if it is in your cgi-bin. If this is the case, try creating this directory outside your cgi-bin, change the path to \$basedir in the script, and try again.

"; } else { unlink("$basedir/sfe_poker_testfile.$$"); } close(F); } if($err){ $err .= "The following information may help you find the correct path. If not, please feel free to email me this information and tell me what was going wrong, and I'll try to help.

"; if($ENV{'PATH_TRANSLATED'}){ $err .= "Path Translated: $ENV{'PATH_TRANSLATED'}
"; } if($ENV{'DOCUMENT_ROOT'}){ $err .= "Document Root: $ENV{'DOCUMENT_ROOT'}
"; } if($ENV{'SCRIPT_FILENAME'}){ $err .= "Script Filename: $ENV{'SCRIPT_FILENAME'}
"; } if($ENV{'SERVER_SOFTWARE'}){ $err .= "Server Software: $ENV{'SERVER_SOFTWARE'}
"; } print "Content-type: text/html\n\n"; print "Ooooops! CONFIGURATION ERROR!

$err"; exit(); } }