#!/usr/bin/perl # # This script provides search and display functions for maps # It should probably be split into three seperate files. # BEGIN { require "files.pl"; # import path info require "fields.pl"; # load form field associations } # # loadDatabase # Args: $comparefunc comparision function reference (takes entry hash returns bit vector) # $outputfunc output function reference (takes entry hash returns nothing) # # Read database file specified in $database # Parse entries and call &comparefunc # if comparefunc returns 1-bit add hash to return array # if comparefunc returns 2-bit call output function # sub loadDatabase { my ($databasefile, $comparefunc, $outputfunc) = @_; # Load args my @database; @database = (); # Return value (array of hash refs) # open database file or exit open( DATABASEFILE, "<$databasefile" ) or die( "Unable to open database '$databasefile'." ); # First line of database contains the field names, database uses | as field seperator my $fields; $fields = readline( *DATABASEFILE ); # First database line defines fields chomp( $fields ); # remove eol characters if( $fields =~ m/^\#/ ) # Line starts the # { $fields = substr( $fields, 1 ); # remove first character } # print $fields, "\n" if( $debug ); my @fields; @fields = split( /\|/, $fields ); # parse line into array of fields # print @fields, "\n" if( $debug ); my $line; while( $line = readline( *DATABASEFILE ) ) # Read every other line from database { chomp( $line ); # strip EOL my @line; @line = split( /\|/, $line ); # parse into array my $entry; $entry = {}; # Construct a new empty hash my $cnt; $cnt = 0; # Array index foreach $fn (@fields) # for each index field { $$entry{$fn} = $line[$cnt++]; # Add hash entry for field name -> database entry value # print "$fn -> $$entry{$fn} \n" if( $debug ); } # print $comparefunc if( $debug ); $cmp = &{$comparefunc}($entry); # Perform the compare function if( $cmp & 1 ) # Compare returns 1-bit { push( @database, $entry ); # Add entry to return array } if( $cmp & 2 ) # compare returns 2-bit { &{$outputfunc}($entry); # Perform output function } # print @line, "\n" if( $debug ); } return @database; # return array of hash refs that compare identified } # # Arg: Hash table for database entry # Prints the hash name value pairs. # sub showEntry(\%) { my ($entry) = @_; # Get args foreach $key (keys %$entry) # For each hash key { print "$key -> $$entry{$key} \n" if( $debug ); # Print name value pair } } # # Arg: Hash table for database entry # Prints the name. # sub showName(\%) { my ($entry) = @_; # Get args if( $$entry{$MAP_NAME_FIELD} ) { print "$$entry{$MAP_NAME_FIELD} \n" if( $debug ); # Print name } } # # Arg: Hash Table for database entry # Returns 2-bit | 1-bit # Indicates entry should be output and returned # sub compareTrue(\%) { my ($entry) = @_; # load args return 1 | 2; # return 1-bit | 2-bit } # # Creates the table for database entries. # sub showMapInfoHeader { print <<__END_HTML__; __END_HTML__ } # # Arg: Hash Table reference for database entry # Shows the table record for this database entry. # sub showMapInfo { my ($entry) = @_; # load args print "\n"; print "\n"; print "\n"; } # # Args: Current values of $start, $count, $search # Show Prev Next and More buttons # sub showButtons { my ($start, $count, $end, $search, $match) = @_; print '\n"; } # # Arg: Hash Table reference for database entry # Reads referenced datafile for database entry and adds fields to hash. # sub loadDataFile { my ($entry) = @_; # load args $datafile = MAPS::getDatafilename( $$entry{$MAP_FILENAME_FIELD} ); # Get datafile name # print "Loading $datafile...\n" if( $debug ); # return MAPS::readDatafile( $datafile ); my $data = MAPS::readDatafile( "$INFO_DIRECTORY/$datafile" ); # print "Loaded $data\n" if($debug); return $data; } # # End definitions started in header. # sub showMapInfoFooter { print <<__END_HTML__;
\n"; print '
', "\n"; print '', "\n"; if( $$entry{$MAP_MINIMAP_FILENAME_FIELD} ) { print 'Minimap Image', "\n"; } if( $$entry{$MAP_FILENAME_FIELD} ) { print ''; print $$entry{$MAP_NAME_FIELD} || "Unknown Name"; print '', "\n"; } else { print '' . ($$entry{$MAP_NAME_FIELD} || "Unknown Name") . '', "\n"; } print "
Author: $$entry{$AUTHOR_LINK_FIELD}\n"; if( $$entry{$EDITOR_LINK_FIELD} ) { print '
Editor: '; print "$$entry{$EDITOR_LINK_FIELD}\n"; } print '
Game Type: ' . $$entry{$GAME_TYPE_FIELD} . '', "\n" if( $$entry{$GAME_TYPE_FIELD} ); my @temp = (); push @temp, $$entry{$MAP_TYPE_FIELD} if( $$entry{$MAP_TYPE_FIELD} ); push @temp, $$entry{$TERRAIN_TYPE_FIELD} if( $$entry{$TERRAIN_TYPE_FIELD} ); push @temp, $$entry{$MAP_SIZE_FIELD} if( $$entry{$MAP_SIZE_FIELD} ); if( @temp ) { print '
Map Data: ' . join( " - ", @temp ) . "\n"; } if( $$entry{$MAX_PLAYERS_FIELD} ) { print "
Players: "; if( $$entry{$MIN_PLAYERS_FIELD} && $$entry{$MIN_PLAYERS_FIELD} != $$entry{$MAX_PLAYERS_FIELD} ) { print "$$entry{$MIN_PLAYERS_FIELD} - $$entry{$MAX_PLAYERS_FIELD}"; if( $$entry{$RECOMMENDED_PLAYERS_FIELD} ) { print "($$entry{$RECOMMENDED_PLAYERS_FIELD} recommended)"; } } else { print "$$entry{$MAX_PLAYERS_FIELD}"; } print "\n"; } print '
Description: ' . $$entry{$MAP_DESCRIPTION_FIELD} . "\n" if( $$entry{$MAP_DESCRIPTION_FIELD} ); # print ""; print "
Staff Rating: $$entry{$STAFF_RATING_FIELD} - $$entry{$STAFF_REVIEW_FIELD}\n" if( $$entry{$STAFF_RATING_FIELD} ); if( $$entry{$USER_RATING_FIELD} ) { print "
User Rating: $$entry{$USER_RATING_FIELD}"; print " - $$entry{$USER_REVIEW_FIELD}" if( $$entry{$USER_REVIEW_FIELD} ); print "\n"; } else { print "
User Rating: Not yet rated.\n"; } # print "
"; print '', "\n"; print '', "\n"; print "
Downloads: $$entry{$DOWNLOAD_COUNT_FIELD}\n" if( $$entry{$DOWNLOAD_COUNT_FIELD} ); print "
\n"; print "
', "\n"; print '
', "\n"; print '', "\n"; print '', "\n"; print '', "\n"; print '', "\n"; print "", "\n"; print ' '), "\n"; print ' '), "\n"; print ' '), "\n"; print "
'. ($start<=0?'':''. (($end || $count>=50)?'':''. ($end?'':'
\n"; print "
\n"; print "
__END_HTML__ } BEGIN { %searches = ( 'all' => sub { return 1 | 2; }, 'players' => sub { my ($entry) = @_; return ($match <= $$entry{$MAX_PLAYERS_FIELD} && $match >= $$entry{$MIN_PLAYERS_FIELD}) ? 1|2 : 0; }, 'user' => sub { my ($entry) = @_; return 1|2 if( index( lc($$entry{$AUTHOR_FIELD}), lc($match) ) != -1 ); return 1|2 if( index( lc($$entry{$EDITORS_FIELD}), lc($match) ) != -1 ); return 1|2 if( index( lc($$entry{$SUBMITOR_FIELD}), lc($match) ) != -1 ); return 0; }, 'name' => sub { my ($entry) = @_; print "Compare $$entry{$MAP_NAME_FIELD} vs $match...\n" if( $debug ); return (index( lc($$entry{$MAP_NAME_FIELD}), lc($match) ) != -1) ? 1|2 : 0; }, 'size_min' => sub { my ($entry) = @_; if( $$entry{$MAP_SIZE_FIELD} =~ m/^([0-9]+)x([0-9]+)$/ ) { if( $1 >= $match && $2 >= $match ) { return 1 | 2; } } return 0; }, 'size_max' => sub { my ($entry) = @_; if( $$entry{$MAP_SIZE_FIELD} =~ m/^([0-9]+)x([0-9]+)$/ ) { if( $1 <= $match && $2 <= $match ) { return 1 | 2; } } return 0; } ); } # # listmaps.cgi main module (sample database access) # Args: beginindex value to display # Displays beginindex then Reads database with compareTrue and showEntry functions # { use CGI; # Load CGI.pm use CGI::Carp qw(fatalsToBrowser); # Set to display fatal errors in web page use MAPS; # Map Database module use HTML; # HTML output module $cgi = new CGI; print # $cgi->header(-pragma=>'no-cache'), # don't cache $cgi->header(), # allow cache $cgi->start_html( # begin page -title=>'Starcraft Infoceptor - Maps', -author=>'GoldenUrg@iName.com' ); HTML::includeScriptFile( "main-head.shtml", 0 ); # start header print "
" if( $debug );						# preformat for debug information
	print "CGIs = ", join( ",", $cgi->param() ), "\n" if( $debug );
	
	$search = $cgi->param('search');				# Get form search info (if any)
	$match = $cgi->param('match');					# Get form search info (if any)
	
#	print "Search=$search to match $match\n";

	if( $search )									# if search specified but not function found
	{
		$comparefunc = $searches{$search};			# lookup search function
		if( !$comparefunc )
		{
	#		print "Creating compare $search=$match...\n" if( $debug );
			$comparefunc = sub 							# Create exact match search for $search db field
			{
				my ($entry) = @_;
				return (lc($$entry{$search}) eq lc($match)) ? 1|2 : 0;
			};
		}
	}
	elsif( !($search) )								# if no search specified
	{
#		print "No Search. All show.\n" if( $debug );
		$comparefunc = \&compareTrue;				# Display all records
		$search = 'all';
	}
	else
	{
#		print "Search function specified. $comparefunc\n";
	}
	
	@database = loadDatabase						# Perform search and list
	(
		"$DATA_DIRECTORY/$database",
		$comparefunc,
		\&showName
	);
	print "
" if( $debug ); # show count of records found my $rec_count = $#database + 1; print "

There are $rec_count entries matching your search.

"; # get browse data my $startindex = $cgi->param('start'); # start index $startindex = 0 unless( $startindex ); my $count = $cgi->param('count'); # number of entries to display $count = 10 if( !($count) ); # reset count if not specified $count *= 2 if( $cgi->param('more') ); # increase size if more clicked $count = 50 if( $count > 50 ); # Limit count to 50 $startindex += $count if( $cgi->param('next') ); # Set to next page if next $startindex -= $count if( $cgi->param('prev') ); # set to prev page if prev $startindex = 0 if( $startindex < 0 ); # set start to 0 if negative my $endindex = $count + $startindex; # compute last scm index $endindex = $rec_count if( $endindex > $rec_count ); # set end to last if too many # $endindex = 50 if( $endindex > 50 ); # limit number of records in one search $startindex = $endindex if( $endindex < $startindex ); #clip start to end if( $startindex < $endindex ) { print "Showing ($startindex - $endindex)...
\n"; } else { print "No more records
\n"; } # Show map list &showMapInfoHeader(); &showButtons( $startindex, $count, ($endindex>=$rec_count), $search, $match ); for( my $index = $startindex; $index < $endindex; $index++ ) { my $entry = $database[$index]; $entry = loadDataFile( $entry ); &showMapInfo( $entry ); } &showButtons( $startindex, $count, ($endindex>=$rec_count), $search, $match ); &showMapInfoFooter(); HTML::includeScriptFile( "main-foot.shtml", 0 ); # end header print $cgi->end_html; print "\n" if( $debug ); exit; } # EOF