#!/usr/local/bin/perl use CGI qw/:standard :html3 *table *center escapeHTML/; use LWP::UserAgent; use HTTP::Request; use File::Copy; use Fcntl; use POSIX; $head = "Content-Type: text/html\n\n"; print $head; $datadir ="/home/rbistaff/erps/WWW/zugtournament/"; $authfile ="/home/rbistaff/erps/WWW/zugtournament/.htauth"; $blackfile ="/home/rbistaff/erps/WWW/zugtournament/blacklist"; $guestfile ="/home/rbistaff/erps/WWW/zugtournament/guestlist"; $url ="http://www.informatik.uni-frankfurt.de/~erps/zugtournament/"; $url_ttr = "http://www.daysofwonder.com/index.php?t=usrinfo&id="; $url_xml = "http://www.daysofwonder.com/en/reststat/ranking/?idlist=#idlist#&game=#map#&key=IsLIBKZeSxyDrUbj2gMPcg"; $my_email = "erps\@informatik.uni-frankfurt.de"; $elodatadir ="/home/rbistaff/erps/WWW/zugstatistik/data/"; @schluessel=param; $command = cleanStr(param("command")); $login = cleanStr(param("login")); $passwd = cleanStr(param("passwd")); $tour = cleanStr(param("tour")); $savetour = cleanStr(param("savetour")); $type = cleanStr(param("type")); $self = cleanStr(param("self")); $invite = cleanStr(param("invite")); $team = cleanStr(param("team")); $id = cleanStrNumber(param("id")); $win_id = cleanStrNumber(param("win_id")); $rnd_id = cleanStrNumber(param("rnd_id")); $captainid = cleanStrNumber(param("captainid")); $name = cleanStr(param("name")); $sched1 = cleanStrNumber(param("sched")); $sched = ($sched1 < 0 || $sched > 2)?0:$sched1; $date = cleanStr(param("date")); $minpoints = cleanStrNumber(param("minpoints")); $maxpoints = cleanStrNumber(param("maxpoints")); $maxplayer = cleanStrNumber(param("maxplayer")); $subwolimit = cleanStrNumber(param("subwolimit")); $deletepl = cleanStr(param("deletepl")); $renamepl = cleanStr(param("renamepl")); $deletetour = cleanStr(param("deletetour")); $renameall = cleanStr(param("renameall")); $editfield = cleanStr(param("editfield")); $edit = cleanStr(param("edit")); $newedit = cleanStr(param("newedit")); $ranklists = cleanStrMap(param("ranklists")); $rl = ($ranklists eq "")?"O":$ranklists; $title = "Ticket To Ride Tournament System"; $message =""; $playername =""; $touradmin =""; $tourfinished = 0; $authadmin = 0; $playerpoints = 0; %id2name; %id2active; %admins = ("admin","","130877","","90443","","37613",""); @scheduling = ("Random Pairing","Dividing in Half regarding to ranking. Random Pairing","Pairing regarding to ranking","Whole Board regarding to ranking","Multiplayer Tournament with Random Pairing / Round"); %rankmatch = ("O","TT","U","TT_us","E","TT_eu","C","TT_ch","N","TT_u1","M","TT_u2","B","TT_u3","2","TT_2p","3","TT_mp"); $isshow = 0; # Authcheck $auth = checkAuth($login,$passwd); $authcheck = "Auth: " . (($auth == 1)?"Yes":"No"); $superadmin = (exists $admins{$login} && $auth == 1)?1:0; $authcheck .= (($superadmin ==1)?" Superuser":""); $options = ($auth == 1)?'login=' .$login. '&passwd=' .$passwd:""; if ($command eq "login") { if ($auth == 0) {$message = " -- Authentication failed"}; } if ($command eq "logout") { $message = " -- Authenticated Session ended."; } if ($command eq "create") { if ($auth == 0) { $message = " -- Not Allowed to create Tournaments -- Demo Mode."; } $isshow = 1; $title .= ": Create Tournament"; printHead(); $createform = '

Tournament Title:
Date and time of Tournament:
Type of Tournament:
Scheduling System: Help
Seeding based on (OUECNMB23, Default O=Overall):
Maximum players (4-256, Default 32):
Minimum points (1000-1600, Default no limit):
Maximum points (1200-1800, Default no limit):
Myself participation:
Invitation only:
Team Tournament:

'; print $createform; } if ($command eq "createnow") { $message .= createNow(); } if ($command eq "editplayer") { $message .= editPlayer(); adminTourfile(); } if ($command eq "showwiki") { showWiki(); } if ($command eq "nextround") { $message .= nextRound(); adminTourfile(); } if ($command eq "savetour") { $message .= saveTour(); adminTourfile(); } if ($command eq "loadtour") { $message .= loadTour(); adminTourfile(); } if ($command eq "deactivate") { $message .= deactivatePlayer(0); adminTourfile(); } if ($command eq "deactivate2") { $message .= deactivatePlayer(1); adminTourfile(); } sub deactivatePlayer($) { $set = shift; if (getTourfile() == 1) { if ($authadmin == 0) { return " -- Command not allowed."; } if ($edit ne "on") { return " -- Command not executed. Mark the checkbox."; } $loserank = 0; $vicrank = 0; foreach $elem (@origfilelines) { $elem =~ s/Player(.*): \#{0,1}(\d*?)--$id--(.*)--/Player: --$id--$3--/; $loserank = $2; } foreach $elem (@origfilelines) { $elem =~ /Player(.*): \#{0,1}(\d*?)--$win_id--(.*)--/; $vicrank = $2; } $msg_fin =""; if ($set == 1) { $rnd_id++; if ($vicrank > $loserank) { foreach $elem (@origfilelines) { $elem =~ s/(Player.*: \#{0,1})$vicrank(--$win_id--.*--)/$1$loserank$2/ or next; } $vicrank = $loserank; } #getPlayerInfo($win_id); #if ($playerpoints < 100) {$playerpoints = 0;} $winner = ""; foreach $elem (@origfilelines) { $elem =~ s/(--Round--$rnd_id)(--|-Winner--)(\d*--)\#$vicrank(--\d*)(--\#{0,1}\d*--\d*)/$1$2$3$win_id$4$5/; $winner = $2; $elem =~ s/(--Round--$rnd_id)(--|-Winner--)(\d*--\#{0,1}\d*--\d*--)\#$vicrank(--\d*)/$1$2$3$win_id$4/; } if ($winner eq "-Winner--") { $tour =~ s/.tour/.fintour/; $newtourfile = $tour; rename $tourfile, $newtourfile or return " -- File System Error."; $msg_fin = " -- Tournament moved to finished tournaments."; } } $tourfile = $tour; open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); foreach $elem (@origfilelines) { print TOUR $elem . "\n"; } close(TOUR); return " -- Player deactivated." . $msg_fin; } else { return " -- File System Error."; } return ""; } sub nextRound() { if (getTourfile() == 1) { if ($authadmin == 0) { return " -- Command not allowed."; } if ($edit ne "on") { return " -- Command not executed. Mark the checkbox."; } if ($tourfinished == 1) { return " -- Tournament finished."; } $filelines[0] =~ /(.*)Round: (\d{1,2})/; $final = $1; $round = $2; $filelines[6] =~ /Scheduling: (\d{1,2})/; $schedultype = $1; %id2name = (); %id2active = (); $id2name {"0"} = "bye"; foreach $elem (@filelines) { $elem =~ /Player(.*?): --(\d*?)--(.*?)--/ or next; $is_active = $1; $captain_id = $2; $captain_name = $3; $id2name {$captain_id} = $captain_name ; if ($is_active eq " Active") { $t_numbers = 0; $t_rank = 0; while ($elem =~ s/Player.*?: --(\d*?)--(.*?)--//o) { getPlayerInfo($1); $t_numbers++; if ($playerpoints < 100) {$playerpoints = 0;} $t_rank += $playerpoints; }; $id2active {$captain_id} = int($t_rank / $t_numbers) ; } } @activeids = (); foreach $key (reverse sort { $id2active{$b} <=> $id2active{$a} } keys %id2active) { unshift(@activeids, $key); } $numberactive = @activeids; if ($numberactive < 2) { $tour =~ s/.tour/.fintour/; $newtourfile = $tour; rename $tourfile, $newtourfile or return " -- File System Error."; return " -- Tournament moved to finished tournaments."; } # Big Routine if ($schedultype > 2) { if ($round > 0) { return ""; } $round++; $origfilelines[0] = "Round: $round"; ($rest,$expo) = modf(log($numberactive)/log(2)); $game = 1; if ($rest == 0) { $expo--; } $topround = $expo + 2; @numbers = (); while ($topround > 0) { if ($topround == ($expo + 2)) { $text = "--Round--$topround-Winner--0--#1--1--0--0"; push (@origfilelines, $text); push (@numbers,1); } else { @newnumbers = (); foreach $elem (@numbers) { $oppnumber = $game + 1 - $elem; push (@newnumbers, $elem); push (@newnumbers, $oppnumber); if ($topround == 1) { $firstid = $activeids[$elem-1]; $firstrank = $id2active{$firstid}; $secondid = 0; $secondrank = 0; if ($oppnumber <= $numberactive) { $secondid = $activeids[$oppnumber-1]; $secondrank = $id2active{$secondid}; } $text = "--Round--$topround--0--$firstid--$elem--$secondid--$oppnumber"; } elsif ($topround == 2) { $bye = 2**($expo+1) - $numberactive; $firstid = "#" . $elem; $firstrank = 0; if ($elem <= $bye) { $firstid = $activeids[$elem-1]; $firstrank = $elem; } $secondid = "#" . $oppnumber; $secondrank = 0; if ($oppnumber <= $bye) { $secondid = $activeids[$oppnumber-1]; $secondrank = $id2active{$secondid}; } $text = "--Round--$topround--0--$firstid--$elem--$secondid--$oppnumber"; } else { $text = "--Round--$topround--0--#$elem--$elem--#$oppnumber--$oppnumber"; } push (@origfilelines, $text); } @numbers = @newnumbers; } $topround--; $game = $game * 2; } $number = 0; foreach $key (@activeids) { $number ++; foreach $elem (@origfilelines) { $elem =~ s/(Player.*?: )(--$key--.*?--)/$1#$number$2/ or next; } } $tourfile = $tour; open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); foreach $elem (@origfilelines) { print TOUR $elem . "\n"; } close(TOUR); # Backup the current round $backupfile = $tourfile; $backupfile =~ s/.tour/.backup$round/; copy ($tourfile, $backupfile) or return " -- Next Round activated. Backup failed!"; return " -- Table completed."; } # End Big Routine $round++; if ($numberactive == 2) { if ($final ne "") { return " -- Round not finished.";} $origfilelines[0] = "Final Round: $round"; } else { $origfilelines[0] = "Round: $round"; } ($rest,$expo) = modf(log($numberactive)/log(2)); $game = 1; if ($rest > 0) { if ($round > 1) { return " -- Round not finished.";} $bye = 2**($expo+1) - $numberactive; for ($i = 0; $i < $bye; $i++) { $top = shift @activeids; $toprank = $id2active{$top}; $byetext = "--Round--$round--$game--$top--$toprank--0--0"; push (@origfilelines, $byetext); $game++ } } while (@activeids > 0) { $players = @activeids; $half = $players / 2; $top = shift @activeids; $toprank = $id2active{$top}; $players--; if ($schedultype == 2) { $bottom = pop(@activeids); } elsif ($schedultype == 1) { $n = int( rand($half)) + $half - 1; $bottom = splice(@activeids, $n, 1); } else { $n = int( rand($players)); $bottom = splice(@activeids, $n, 1); } $bottomrank = $id2active{$bottom}; $byetext = "--Round--$round--$game--$top--$toprank--$bottom--$bottomrank"; push (@origfilelines, $byetext); $game++ } $tourfile = $tour; open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); foreach $elem (@origfilelines) { print TOUR $elem . "\n"; } close(TOUR); # Backup the current round $backupfile = $tourfile; $backupfile =~ s/.tour/.backup$round/; copy ($tourfile, $backupfile) or return " -- Next Round activated. Backup failed!"; return " -- Next Round activated."; } else { return " -- File System Error."; } return ""; } # # LOAD TOUR SUB # sub loadTour() { if (getTourfile() == 1) { if ($authadmin == 0) { return " -- only the admin is allowed to load a tournament."; } if ($edit ne "on") { return " -- Please check the checkbox for loading a file."; } if (not -e $savetour) { return " -- No file to load."; } $newtourfile = $savetour; $newtourfile =~ s/tour\.(.*?)\.tbackup/tour/; unlink $tourfile or return " -- File System Error."; copy ($savetour, $newtourfile) or return " -- File System Error: Copy of $savetour to $newtourfile"; $tour = $newtourfile; return " -- Retrieving backup of tourfile completed."; } } # # SAVE TOUR SUB # sub saveTour() { if (getTourfile() == 1) { if ($authadmin == 0) { return " -- Only the admin is allowed to backup a tournament."; } $backupfile = $tourfile . '.' . $savetour . '.tbackup'; copy ($tourfile, $backupfile) or return " -- Backup failed!"; return " -- Backup of tourfile completed."; } } if ($command eq "edittour") { $message .= editTour(); if ($deletetour ne "on") {adminTourfile();} } # # EDIT TOUR SUB # sub editTour() { if (getTourfile() == 1) { if ($authadmin == 0) { return " -- Tournament not edited."; } if ($deletetour eq "on") { $tourfile = $tour; if ($deletetour eq "on") { unlink $tourfile or return " -- File System Error."; return " -- Tournament deleted."; } } if ($renameall eq "on") { # Rename all players open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); foreach $elem (@origfilelines) { if ($elem =~ /^Player.*: .*--(\d*)--.*--/) { $pid = $1; getPlayerInfo($pid); $elem =~ s/(^Player.*: .*--\d*)--.*--/$1--$playername--/; }; print TOUR $elem . "\n"; } close(TOUR); return " -- Player names updated."; } if ($edit eq "on") { if ($round > 0) { return " -- Tournament in progress. Edit not allowed.";} $tourfile = $tour; open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); foreach $elem (@origfilelines) { if ($elem =~ /^$editfield: .*/) { if ($editfield eq "Maxplayer") {$newedit = ($newedit > 256 || $newedit < 4)?32:$newedit; } if ($editfield eq "Maxpoints") {$newedit = ($newedit > 4000 || $newedit < 1200)?10000:$newedit; } if ($editfield eq "Minpoints") {$newedit = ($newedit > 3800 || $newedit < 1000)?100:$newedit; } $elem =~ s/(^$editfield: ).*/$1$newedit/; } if ($elem =~ /^TTR Tournament File: .*/ && ($editfield eq "Invitation")) { $newinvite = ($newedit eq "on")?"invite ":""; $newteam = ($team==1)?"team ":""; $elem =~ s/(^TTR Tournament File: ).*/$1$newinvite$newteam/; } print TOUR $elem . "\n"; } close(TOUR); return " -- Field edited."; } return " -- Field not edited. Mark the checkbox."; } else { return " -- File System Error."; } return ""; } # # FILE VALID SUB # sub isFileValid() { if ($tour eq "") {return 0;} @tourfiles = sort(<*.tour>); foreach (@tourfiles) { if ($tourfile eq $_) {return 0;} } return 1; } # # POINTS VALID SUB # sub pointsValid() { $correct = 1; if ($maxpoints > 4000 || $maxpoints < 1200) { $maxpoints = 10000; $correct = 0; } if ($minpoints < 1000 || $minpoints > 3800 || $minpoints > ($maxpoints - 99)) { $minpoints = 100; $correct = 0; } return $correct; } # # PLAYER VALID SUB # sub isPlayerValid($$) { $pid1 = shift; $first = shift; if ($first == 0 && checkFile($pid1) == 0) {return 0;} if (checkBlackList($pid1) == 0) {return 0;}; return getPlayerInfo($pid1); } # # CHECK FILE SUB # sub checkFile($) { $pid4 = shift; if (getTourfile() == 1) { if ($invite == 1 && $auth == 0) {return 0;} $number = 0; foreach $elem (@filelines) { if ($elem =~ /Round: (\d{1,2})/) {$round = $1;} if ($round > 0) {return 0;} if ($elem =~ /Maxpoints: (\d{1,5})/) {$maxpoints = $1;} if ($elem =~ /Minpoints: (\d{1,4})/){$minpoints = $1;} if ($elem =~ /Maxplayer: (\d{1,3})/) {$maxplayer = $1;} if ($elem =~ /^Player.*: --(\d*)--(.*)--/) { $number++; if ($1 == $pid4) {return 0;} } } if ($number >= $maxplayer) {return 0;} return 1; } return 0; } # # CHECK BLACKLIST SUB # sub checkBlackList($) { $pid2 = shift; open(BLACK, "<$blackfile") or return 1; while () { chomp; if ( $pid2 eq $_ ) { return 0; } } close(BLACK); return 1; } # # CHECK GUESTLIST SUB # sub checkGuest($) { $pid2 = shift; open(GUEST, "<$guestfile") or return 0; while () { chomp; if ( /^$pid2;(\d*)/ ) { return $1; } } close(GUEST); return 0; } # # GET ALL PLAYER INFO SUB # sub getAllPlayerInfo() { $rllen = length($rl); $arrayc = 0; @idlistalla; $countplayers = 0; $tempkeys = ""; foreach $key (keys %id2active) { $tempkeys .= $key . ","; $countplayers++; if ($countplayers == 100) { $countplayers = 0; $idlistalla[$arrayc] = $tempkeys; $tempkeys = ""; $arrayc++; } } if ($countplayers > 0) { $idlistalla[$arrayc] = $tempkeys; } for ($tem = 0; $tem < @idlistalla; $tem++) { $idlistall = $idlistalla[$tem]; $idlistall = substr($idlistall,0,length($idlistall)-1); #print $tem . " ---- " . $idlistall . " ---- " . $rl; for ($l = 0;$l < $rllen; $l++) { #$url_xml = "http://www.daysofwonder.com/en/reststat/ranking/?idlist=#idlist#&game=#map#&key=IsLIBKZeSxyDrUbj2gMPcg"; $ua = LWP::UserAgent->new; $m = $rankmatch{substr($rl,$l,1)};#substr($rl,$l,1) $url_xml_temp = $url_xml; $url_xml_temp =~ s/#idlist#/$idlistall/; $url_xml_temp =~ s/#map#/$m/; $r = new HTTP::Request 'GET', $url_xml_temp; $resp = $ua->request($r); $rs = $resp->code(); #print $url_xml_temp; #print $rs; if ($rs != 200) { return 0; } $source = $resp->content; #print $source; while ($source =~ s///o) { $id2name{$1} = $2; $id2active{$1} += $3; } } } return 1; } # # GET PLAYER INFO SUB # sub getPlayerInfo($) { $pid3 = shift; $rllen = length($rl); $playerpoints = 0; for ($l = 0;$l < $rllen; $l++) { #$url_xml = "http://www.daysofwonder.com/en/reststat/ranking/?idlist=#idlist#&game=#map#&key=IsLIBKZeSxyDrUbj2gMPcg"; $ua = LWP::UserAgent->new; $m = $rankmatch{substr($rl,$l,1)};#substr($rl,$l,1) $url_xml_temp = $url_xml; $url_xml_temp =~ s/#idlist#/$pid3/; $url_xml_temp =~ s/#map#/$m/; $r = new HTTP::Request 'GET', $url_xml_temp; $resp = $ua->request($r); $rs = $resp->code(); #print $url_xml; #print $rs; if ($rs != 200) { return 0; } $source = $resp->content; #print $source; if ($source =~ /name="(.*?)"/) { $playername = $1; } if ($source =~ /score="(\d{3,4})"/) { $playerpoints += $1; } } # Save data to database = textfiles so far #$nowtime = time(); #open (FILE, ">$elodatadir$pid3"); #print FILE "$nowtime\n$playerpoints"; #close (FILE); # Check for limits? if ($playerpoints < 1) {$playerpoints = checkGuest($pid3);} if (($authadmin == 1) && ($subwolimit == 1)) {return 1;} if (($playerpoints > $maxpoints) || ($playerpoints < $minpoints)) {return 0;} return 1; } # # GET PLAYER INFO SUB # sub getTempPlayerInfo($) { $pid3 = shift; if (-e "$elodatadir$pid3") { open (FILE, "<$elodatadir$pid3"); @elofilelines = ; chomp @filelines; close (FILE); $oldtime = $elofilelines[0]; $playerpoints = $elofilelines[1]; $nowtime = time(); if ($playerpoints < 1) {$playerpoints = checkGuest($pid3); return 1;} if (($oldtime + 3600 + int(rand(1000)))>$nowtime) {return 1;} } getPlayerInfo($pid3); return 1; } # # CREATE TOURNAMENT FILE SUB # sub createNow() { if ($auth == 1) { $t_options = ""; $tourfile = $tour . ".tour"; if (isFileValid() == 0) {return " -- Tournament Name not allowed.";} if ($type eq "") {return " -- No Description of Tournament.";} if (pointsValid() == 0) {$message = " -- Wrong point limits corrected to unlimited.";} if ($maxplayer < 4 || $maxplayer > 256) {$maxplayer = 32; $message .= " -- Player limit corrected to 32.";} if ($invite eq "on") {$t_options = "invite ";} if ($team eq "on") {$t_options .= "team ";} $playervalid = isPlayerValid($login, 1); open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); print TOUR "Round: 0"; print TOUR "\nTTR Tournament File: " . $t_options; print TOUR "\nName: " . $tour; print TOUR "\nAdmin: " . $login . "--" . $playername; print TOUR "\nDate: " . $date; print TOUR "\nType: " . $type; print TOUR "\nScheduling: " . $sched; print TOUR "\nBased on: " . $rl; print TOUR "\nMaxplayer: " . $maxplayer; print TOUR "\nMaxpoints: " . $maxpoints; print TOUR "\nMinpoints: " . $minpoints; print TOUR "\n--Playerlist--\n"; #print $id; if (($self eq "on") && ($playervalid == 1)) {print TOUR "Player Active: --" . $login . "--" . $playername ."--\n";} close(TOUR); # Backup the initial round $backupfile = $tour . ".backup0"; copy ($tourfile, $backupfile) or return " -- Tournament successfull created. Backup failed!"; return " -- Tournament successfull created."; } else { return " -- Not Allowed to create Tournaments."; } } # # EDIT PLAYER SUB # sub editPlayer() { if (getTourfile() == 1) { if ($authadmin == 1 && ($deletepl eq "on" || $renamepl eq "on" || ($command eq "addteamplayer" & $team == 1))) { $tourfile = $tour; open(TOUR, ">$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); foreach $elem (@origfilelines) { if ($elem =~ /^Player.*?: --$id--.*?--/) { if ($deletepl eq "on") { next;} # Rename Player getPlayerInfo($id); if ($renamepl eq "on") { $elem =~ s/(^Player.*: --$id)--.*--/$1--$playername--/; } } if ($command eq "addteamplayer" && $elem =~ /^Player.*: --$captainid--.*--/) { # Add Team Player $elem =~ s/(^Player.*: --$captainid--.*--.*)/$1Player: --$id--$playername--/; }; print TOUR $elem . "\n"; } close(TOUR); #print $id; return " -- Tournament successfull edited."; } else { return " -- Tournament not edited."; } } else { return " -- File System Error."; } } # # GET TOUR FILE SUB # sub getTourfile() { $tourfile = $tour; if ($tour =~ /.fintour/) {$tourfinished = 1;} open(TOUR, "<$tourfile") or return 0; flock(TOUR, LOCK_SH); @filelines = ; chomp @filelines; @origfilelines = @filelines; close(TOUR); # Get options of tournament $invite = ($filelines[1] =~ /invite/)?1:0; $team = ($filelines[1] =~ /team/)?1:0; $filelines[7] =~ /Based on: (.*)/; $rl = cleanStrMap($1); $rl = ($rl eq "")?"O":$rl; # Get Admin of Tourfile $filelines[3] =~ s/Admin: (\d*)--(.*)/Admin: $2/; $touradmin = $1; # Check if Admin is allowed to edit the Tourfile if (($superadmin == 1) || ($touradmin eq $login)) { $authadmin = 1;} return 1; } if ($command eq "admin") { adminTourfile(); } # # FORM DELETE PLAYER SUB # sub getDeletePlayerForm($$) { $pid = shift; $pname = shift; $deleteform = ' Player: '.$pname.'   Delete Check Name '; return $deleteform; } # # FORM ADD TEAM PLAYER SUB # sub addTeamMemberForm($) { $pid = shift; #$pname = shift; $addteamform = '

New Team PlayerID:
'; return $addteamform; } # # FORM LOAD SUB # sub getLoadTournamentForm() { $tourload = ' Tournament Load: ' . $tourload . ' '; if (@tourfiles == 0) {return "";} return $loadtourform; } # # FORM SAVE SUB # sub getSaveTournamentForm() { $savetourform = '
Tournament Save: ' . $tour . '. .tbackup
'; return $savetourform; } # # FORM DELETE TOURNAMENT SUB # sub getDeleteTournamentForm() { $deletetourform = '
Tournament Edit: Delete Check All Names
'; return $deletetourform; } # # FORM NEXT ROUND SUB # sub getNextRoundForm() { $nextroundform = '
Tournament Admin: Next Round
'; return $nextroundform; } # # FORM CHANGE VALUE SUB # sub getChangeForm($$$) { $editfield = shift; $temptext = shift; $size = shift; $changeform = '
'.$editfield.': '.$temptext.'    Edit
'; return $changeform; } # # FORM CHANGE VALUE OF CB SUB # sub getChangeCBForm($$) { $editfield = shift; $checked = shift; $checkstring = $checked == 1?" checked ":""; $changeform = '
'.$editfield.':    Edit
'; return $changeform; } # # FORM CHANGE SCHEDULING SUB # sub getSchedForm($$) { $editfield = shift; $temptext = shift; $schedform = '
'.$editfield.': '.$temptext.'    Edit Help
'; return $schedform; } # # FORM SHOW WIKI TABLE INSERT # sub getShowWikiForm() { $showWikiForm = '
Create Tickipedia View for this table
'; return $showWikiForm; } # # ADMIN TOUR SUB # sub adminTourfile() { if (getTourfile() == 1) { $filelines[0] =~ /Round: (\d{1,2})/; $round = $1; # Which Admin? if ($auth == 0) { $message = " -- Not Allowed to admin Tournaments -- Demo Mode."; } else { if ($authadmin == 0) { $message = " -- Wrong Admin for this Tournament -- Demo Mode.";} } $isshow = 1; $title .= ": Admin Tournament"; printHead(); if ($round == 0) { foreach $elem (@filelines) { if ($elem =~ /TTR Tournament File/) {next;} $elem =~ s/Round: 0/Registration Phase<\/b>/; $elem =~ s/--Playerlist--/List of Players<\/b>/; if ($elem =~ /^Name:|^Admin/) { print "
$elem"; next;}; if ($elem =~ /^(Date): (.*)/) { $elem = getChangeForm($1,$2,30); print "$elem"; next;}; if ($elem =~ /^(Type): (.*)/) { $elem = getChangeForm($1,$2,30); print "$elem"; next;}; if ($elem =~ /^(Scheduling): (.*)/) { $elem = getSchedForm($1,$scheduling[$2]); print "$elem"; next;}; if ($elem =~ /^(Based on): (.*)/) { $elem = getChangeForm($1,$2,10); print "$elem"; next;}; if ($elem =~ /^(Maxplayer): (.*)/) { $elem = getChangeForm($1,$2,3); print "$elem"; next;}; if ($elem =~ /^(Maxpoints): (.*)/) { $elem = getChangeForm($1,$2,4); print "$elem"; next;}; if ($elem =~ /^(Minpoints): (.*)/) { $elem = getChangeForm($1,$2,4); print "$elem"; next;}; if ($elem =~ /^Player.*?: --(\d*?)--(.*?)--/o) { $captainid = $1; $printplayer = ""; while ($elem =~ s/Player.*?: --(\d*?)--(.*?)--//o) { $printplayer .= ""; } if ($team==1) {$printplayer .= "";} print $printplayer . "
" . getDeletePlayerForm($1, $2) . "" . addTeamMemberForm($captainid) . "
"; next; }; print "$elem"; } if ($team==1) {print "Team Tournament
";} $cbinvite = getChangeCBForm("Invitation",$invite); print "$cbinvite"; if ($invite==1) {print "Invitation Only
";} $submitform = '

PlayerID: '; print $submitform; print 'Help'; } else { %id2name = (); %id2active = (); $id2name {"0"} = "bye"; print "
$filelines[0]"; print "
$filelines[2]"; print "
$filelines[3]"; print "
$filelines[4]"; print "
$filelines[5]"; if ($team==1) {print "
Options: Team Tournament";} $filelines[6] =~ s/Scheduling: (\d{1,2})/Scheduling: $scheduling[$1]/; $sched = $1; print "
$filelines[6]

"; foreach $elem (@filelines) { $elem =~ /Player(.*?): \#{0,1}(\d*?)--(\d*?)--(.*?)--/ or next; $is_active = $1; $rank_number = $2; $captain_id = $3; $team_name = ""; while ($elem =~ s/Player.*?: \#{0,1}\d*?--(\d*?)--(.*?)--//o) { $team_name .= "-" . $2 . "-"; } $id2name {$captain_id} = $team_name ; if ($is_active eq " Active") { $id2active {$captain_id} = $team_name ; } } print '

'; if ($sched != 3 ) { $lastround = 1; @nextround = (); for ($i = $round; $i > 0; $i--) { print ''; $a = 0; $span = 2 ** ($i - 1); @thisround = @nextround; @nextround = (); do { foreach $elem (@filelines) { $elem =~ /--Round--$i--(\d*)--(\d*)--(\d*)--(\d*)--(\d*)/ or next; if ($lastround == 0) { if ($2 != $thisround[0] && $4 != $thisround[0]) { next; } shift @thisround; } $activepair = 0; $player1text = getPlayerRank($2,$3); $player2text = getPlayerRank($4,$5); if ($activepair == 2) { $player1text = getPairingForm($4,$player1text); $player2text = getPairingForm($2,$player2text); } else { $player1text .= "
"; } print ""; push (@nextround, $2, $4); } } while (@thisround > 0); print ''; $lastround = 0; } } else { $lastround = 0; $maxround = 0; foreach $elem (@filelines) { if ($elem =~ /--Round--(\d*)-Winner--(\d*)--(\#{0,1}\d*)--(\d*)--(\#{0,1}\d*)--(\d*)/) { $maxround = $1; $lastround = $1; print ''; } $elem =~ /--Round--(\d*)(--|-Winner--)(\d*)--(\#{0,1}\d*)--(\d*)--(\#{0,1}\d*)--(\d*)/ or next; $thisrnd = $1; $firstid = $4; $firstrank = $5; $secondid = $6; $secondrank = $7; if ($thisrnd < $lastround) { $lastround = $thisrnd; print ''; } $span = 2 ** ($thisrnd - 1); $activepair = 0; print ""; } print ''; } print '
Round ' . $i . '"; print $player1text; print $player2text; print "
Winner
Round ' . $thisrnd . '"; $player1text = getPlayerRank($firstid,$firstrank); $player2text = getPlayerRank($secondid,$secondrank); if ($activepair == 2) { $player1text = getPairingForm2($secondid,$firstid,$thisrnd,$player1text); $player2text = getPairingForm2($firstid,$secondid,$thisrnd,$player2text); } print $player1text; if ($thisrnd < $maxround) { print "
"; print $player2text; } print "
'; } print "
" . getShowWikiForm(); if ($sched != 3 | $round == 0) { print "
" . getNextRoundForm(); } print "
" . getSaveTournamentForm() . "
" . getLoadTournamentForm(); print "
" . getDeleteTournamentForm(); } else { $message = " -- File System Error."; } } sub getPairingForm2($$$$) { $pid = shift; $win_id = shift; $rnd_id = shift; $temptext = shift; $pairform = '
'.$temptext.' 
'; return $pairform; } sub getPairingForm($$) { $pid = shift; $temptext = shift; $pairform = '
'.$temptext.' 
'; return $pairform; } if ($command eq "show" || $command eq "finished") { showTourfile(); } if ($command eq "submit" || $command eq "addteamplayer") { if (isPlayerValid($id,0) == 1) { if ($command eq "addteamplayer") { $message .= editPlayer(); } else { $message .= addPlayer(); } } else { $message .= " -- Player participation not allowed."; } if ($subwolimit == 1) {adminTourfile();} else { showTourfile(); } } sub addPlayer() { open(TOUR, ">>$tourfile") or return " -- File System Error."; flock(TOUR, LOCK_EX); #print $id; print TOUR "Player Active: --" . $id . "--" . $playername . "--\n"; close(TOUR); return " -- Player added to Tournament."; } sub showWiki() { if (getTourfile() == 1) { $isshow = 1; $title .= ": Show Wiki Table"; printHead(); $filelines[0] =~ /.*Round: (\d{1,2})/; $round = $1; if ($round > 0) { %id2name = (); %id2active = (); $id2name {"0"} = "bye"; print "
$filelines[0]"; print "
$filelines[2]"; print "
$filelines[3]"; print "
$filelines[4]"; print "
$filelines[5]"; if ($team==1) {print "
Options: Team Tournament";} $filelines[6] =~ s/Scheduling: (\d{1,2})/Scheduling: $scheduling[$1]/; $sched = $1; print "
$filelines[6]

"; foreach $elem (@filelines) { $elem =~ /Player(.*?): \#{0,1}(\d*?)--(\d*?)--(.*?)--/ or next; $is_active = $1; $rank_number = $2; $captain_id = $3; $team_name = ""; while ($elem =~ s/Player.*?: \#{0,1}\d*?--(\d*?)--(.*?)--//o) { $team_name .= " " . $2 . " "; } $id2name {$captain_id} = $team_name ; if ($is_active eq " Active") { $id2active {$captain_id} = $team_name ; } } print 'Copy and paste text between the lines
----------'; if ($sched != 3) { $counter = 1; $lastround = 1; @nextround = (); for ($i = $round; $i > 0; $i--) { $counter*=2; $a = 0; $span = 2 ** ($i - 1); @thisround = @nextround; @nextround = (); $inner = 0; do { foreach $elem (@filelines) { $elem =~ /--Round--$i--(\d*)--(\d*)--(\d*)--(\d*)--(\d*)/ or next; if ($lastround == 0) { if ($2 != $thisround[0] && $4 != $thisround[0]) { next; } shift @thisround; } print "
"; $inner++; print getWikiPlayer($2,$counter,$inner); $inner++; print getWikiPlayer($4,$counter,$inner); push (@nextround, $2, $4); } } while (@thisround > 0); $lastround = 0; } } else { $counter = 1; $lastround = 0; $maxround = 0; foreach $elem (@filelines) { if ($elem =~ /--Round--(\d*)-Winner--(\d*)--(\#{0,1}\d*)--(\d*)--(\#{0,1}\d*)--(\d*)/) { $maxround = $1; $lastround = $1; print "
|winner=''' $id2name{$3} '''"; next; } $elem =~ /--Round--(\d*)(--|-Winner--)(\d*)--(\#{0,1}\d*)--(\d*)--(\#{0,1}\d*)--(\d*)/ or next; $thisrnd = $1; $firstid = $4; $firstrank = $5; $secondid = $6; $secondrank = $7; if ($thisrnd < $lastround) { $lastround = $thisrnd; $counter*=2; $inner = 0; } $span = 2 ** ($thisrnd - 1); print "
"; $inner++; print getWikiPlayer($firstid,$counter,$inner); if ($thisrnd < $maxround) { $inner++; print getWikiPlayer($secondid,$counter,$inner); } } } print '
----------'; } else { print "
$filelines[0]"; print "
$filelines[2]"; print "
$filelines[3]"; print "
$filelines[4]"; print "
$filelines[5]"; if ($team==1) {print "
Options: Team Tournament";} $filelines[6] =~ s/Scheduling: (\d{1,2})/Scheduling: $scheduling[$1]/; $sched = $1; print "
$filelines[6]

"; print 'Copy and paste text between the lines
----------'; $nu = 0; foreach $elem (@filelines) { $elem =~ /Player(.*?): \#{0,1}(\d*?)--(\d*?)--(.*?)--/ or next; $is_active = $1; $rank_number = $2; $captain_id = $3; $team_name = ""; while ($elem =~ s/Player.*?: \#{0,1}\d*?--(\d*?)--(.*?)--//o) { $team_name .= " " . $2 . " "; } if ($is_active eq " Active") { $nu ++; print '
'; print "
$nu. $team_name"; } } print '
----------'; } } else { $message = " -- File System Error."; } } sub getWikiPlayer($$$) { $pid = shift; $counter = shift; $inner = shift; if ($pid =~ /\#.*/) {return "";} if (exists $id2active{$pid}) { $playertext = '|r' . "$counter" . '_' . "$inner='''$id2name{$pid}'''"; $activepair++ } else { $playertext = '|r' . "$counter" . '_' . "$inner=$id2name{$pid}"; if ($pid > 0) { $id2active{$pid} = $id2name{$pid}; } } return $playertext } sub showTourfile() { if (getTourfile() == 1) { $isshow = 1; $title .= ": Show Tournament"; printHead(); $filelines[0] =~ /.*Round: (\d{1,2})/; $round = $1; if ($round == 0) { %id2name = (); %id2active = (); @copyfilelines = @filelines; if ($team == 1) { foreach $elem (@copyfilelines) { $elem =~ /Player(.*?): --(\d*?)--(.*?)--/ or next; $is_active = $1; $captain_id = $2; $captain_name = $3; $id2name {$captain_id} = $captain_name ; if ($is_active eq " Active") { $t_numbers = 0; $t_rank = 0; while ($elem =~ s/Player.*?: --(\d*?)--(.*?)--//o) { getPlayerInfo($1); $t_numbers++; if ($playerpoints < 100) {$playerpoints = 0;} $t_rank += $playerpoints; }; $id2active {$captain_id} = int($t_rank / $t_numbers) ; } } } else { foreach $elem (@copyfilelines) { $elem =~ /Player(.*?): --(\d*?)--(.*?)--/ or next; $is_active = $1; $captain_id = $2; $captain_name = $3; $id2name {$captain_id} = $captain_name ; if ($is_active eq " Active") { $id2active {$captain_id} = 0; } } getAllPlayerInfo(); } @activeids = (); foreach $key (reverse sort { $id2active{$b} <=> $id2active{$a} } keys %id2active) { unshift(@activeids, $key); } $numberactive = @activeids; ($rest,$expo) = modf(log($numberactive>1?$numberactive:1)/log(2)); if ($rest != 0) {$expo++;} $maxnumberactive = 2**$expo+1; @playertext = (); $n_pl = 1; foreach $elem (@filelines) { if ($elem =~ /TTR Tournament File/) {next;} $elem =~ s/Round: 0/Registration Phase<\/b>/; $elem =~ s/--Playerlist--/
List of Players<\/b>/; $printplayer = ""; if ($elem =~ /^Player.*?: --(\d*?)--(.*?)--/o) { $teamid = $1; while ($elem =~ s/Player.*?: --(\d*?)--(.*?)--//o) { $printplayer .= ": " . $2 . " "; } $printplayer .= " "; last; } $loop++ } $playertext[$loop] = $printplayer; #print $printplayer; $n_pl++; next; }; $elem =~ s/^(Scheduling: )(.*)/$1$scheduling[$2]/; print "
$elem"; if ($team==1 & $elem =~ /^Type:/) {print "
Team Tournament";} } foreach $textelem (@playertext) { print $textelem; } print "
(" . $id2active{$teamid} . ") "; $loop = 1; foreach $elemid (@activeids) { if ($elemid == $teamid) { $printplayer = "Player #" . $loop . "" . $printplayer; $opploop = $maxnumberactive - $loop; if ($opploop > $numberactive) {$opploop = "bye";} $printplayer .= " plays vs. #" . $opploop . "
"; if ($invite==1) {print "

Invitation Only";} else { $submitform = '

Your PlayerID:

'; print $submitform; print 'Help'; } } else { %id2name = (); %id2active = (); $id2name {"0"} = "bye"; print "
$filelines[0]"; print "
$filelines[2]"; print "
$filelines[3]"; print "
$filelines[4]"; print "
$filelines[5]"; if ($team==1) {print "
Options: Team Tournament";} $filelines[6] =~ s/Scheduling: (\d{1,2})/Scheduling: $scheduling[$1]/; $sched = $1; print "
$filelines[6]

"; foreach $elem (@filelines) { $elem =~ /Player(.*?): \#{0,1}(\d*?)--(\d*?)--(.*?)--/ or next; $is_active = $1; $rank_number = $2; $captain_id = $3; $team_name = ""; while ($elem =~ s/Player.*?: \#{0,1}\d*?--(\d*?)--(.*?)--//o) { $team_name .= "-" . $2 . "-"; } $id2name {$captain_id} = $team_name ; if ($is_active eq " Active") { $id2active {$captain_id} = $team_name ; } } print ''; if ($sched != 3) { $lastround = 1; @nextround = (); for ($i = $round; $i > 0; $i--) { print ''; $a = 0; $span = 2 ** ($i - 1); @thisround = @nextround; @nextround = (); do { foreach $elem (@filelines) { $elem =~ /--Round--$i--(\d*)--(\d*)--(\d*)--(\d*)--(\d*)/ or next; if ($lastround == 0) { if ($2 != $thisround[0] && $4 != $thisround[0]) { next; } shift @thisround; } print ""; push (@nextround, $2, $4); } } while (@thisround > 0); print ''; $lastround = 0; } } else { $lastround = 0; $maxround = 0; foreach $elem (@filelines) { if ($elem =~ /--Round--(\d*)-Winner--(\d*)--(\#{0,1}\d*)--(\d*)--(\#{0,1}\d*)--(\d*)/) { $maxround = $1; $lastround = $1; print ''; } $elem =~ /--Round--(\d*)(--|-Winner--)(\d*)--(\#{0,1}\d*)--(\d*)--(\#{0,1}\d*)--(\d*)/ or next; $thisrnd = $1; $firstid = $4; $firstrank = $5; $secondid = $6; $secondrank = $7; if ($thisrnd < $lastround) { $lastround = $thisrnd; print ''; } $span = 2 ** ($thisrnd - 1); print ""; } print ''; } print '
Round ' . $i . '"; print getPlayerRank($2,$3); print "
"; print getPlayerRank($4,$5); print "
Winner
Round ' . $thisrnd . '"; print getPlayerRank($firstid,$firstrank); if ($thisrnd < $maxround) { print "
"; print getPlayerRank($secondid,$secondrank); } print "
'; } } else { $message = " -- File System Error."; } } sub getPlayerRank($$) { $pid = shift; $pidrank = shift; if ($pidrank > 19999 || $pidrank < 1) { $pidrank = ""; } else { $pidrank = "(#" . $pidrank . ")"; } if ($pid =~ /\#.*/) {return $pid;} if (exists $id2active{$pid}) { $playertext = "$id2name{$pid} $pidrank"; $activepair++ } else { $playertext = "$id2name{$pid} $pidrank"; } return $playertext } if ($isshow == 0) { $tourform0 = getTour(0); $tourform1 = getTour(1); $tourform2 = getTour(2); printHead(); print "

Administration Part

"; $loginform = '

Login: Password:

Don\'t use your DOW-account! Help'; $logintext = ($auth == 1)?(''):$loginform; print $logintext; $logoutform = '

Help'; $logouttext = ($auth == 0)?(''):$logoutform; print $logouttext; print getTourForm("Create"); print getTourForm("Admin"); print "

Player and Visitor Part

"; print getTourForm("Show"); print getTourForm("Finished"); } printFoot(); exit(0); sub getTour($) { $finished = shift; $tour = ''; return $tour; } sub printHead() { $title = '' .$title. ''.$authcheck.$message.'

' . $title. '

'; print $title ; } sub getTourForm($) { $comm = shift; $form = '

Tournament'; %order2txt = ( "Create", " (Login required)

", "Admin", ": $tourform2 (Login required) ", "Show", ": $tourform0 (Registration available)", "Finished", ": $tourform1" ); $form .= $order2txt{$comm}; return $form; } sub printFoot() { $foot = '
Back to Menu'; $foot .= '
Contact me
'; print $foot; } sub checkAuth($$) { $li = shift; $pw = shift; $check = $li . $pw; open(DATA, "<$authfile") or return 0; while () { chomp; if ( $check eq $_ ) { return 1; } } close(DATA); return 0; } sub cleanStr($) { $leer = ""; local ($_) = @_; /^([-A-Za-z0-9. ÄÖÜäöüß_]*)$/ or return $leer; return $1; } sub cleanStrMap($) { $leer = ""; local ($_) = @_; /^([OUECNMB23]*)$/ or return $leer; return $1; } sub cleanStrNumber($) { $leer = 0; local ($_) = @_; /^([0-9]*)$/ or return $leer; return $1; }