Page 1 of 2

Heads Up Tournament with Levels

Posted: Wed Sep 23, 2009 12:17 pm
by CanadaWest
If any php gurus out there want to give me a hand, I'm creating a script to handle a heads up tournament by api registering the winners of level 1 into level 2, etc.

I have the structure mapped out and can handle all the api stuff, but creating and managing the arrays and loops is about to cause my head to explode.

For instance,
//How to parse the results of api TournamentsResults and get the winners of 16 tournaments,
//then array them in random pairs (playersets) for use in a loop
$playerset=array("playerset1","playerset2","playerset3"...);

//How to array up to 16 tournaments with one 2-player table for use in the same loop
//eg. HeadsUp Lv 1 T1, HeadsUp Lv 1 T2, HeadsUp Lv 1 T3, HeadsUp Lv 1 T4, HeadsUp Lv 1 T5...
$table=array("table1","table2","table3"...);

//Then, can there be a loop with 2 different values?
foreach ($playerset as $value1 && $table as $value2)
{
$params = "Password=$pw&Command=TournmentsRegister" .
"&Name=" . urlencode($value2) .
"&Player=" urlencode($value1);
$api = Poker_API($url,$params,true);
}

Share the work, share the file?

PM me if you want to collaborate.

Re: Heads Up Tournament with Levels

Posted: Mon Sep 28, 2009 5:00 am
by deisss
Canada share plz...

Re: Heads Up Tournament with Levels

Posted: Mon Sep 28, 2009 7:59 pm
by CanadaWest
I would love to share, deisss .

but I can't quite get it to work.

i am sooooo close.

I can array the winners of any round, but can't get it to register them in the next one.

Here is just the code for the troublesome loop:

Lets say $win is the array "aces123, canada, friend, player1, extreem, sootedaces, dummy, callme"
then $t is 4 (the number of tables)
and $n is 8 (the number of players in $win)


}
$table = 1;
$i = 1;
while ($i <= $n)
{
while ($table <= $t)
{
foreach($win as $Player)
{
$params = "Password=$pw&Command=TournamentsRegister" .
"&Player=" . urlencode($Player) .
"&Name=" . urlencode("Heads Up " . $Rnd . $table++);
$api = Poker_API($url,$params,true);
}
}
$i++;
}


Trouble is, the foreach loop doesn't continue through the entire $win array (the list of players) but stops when it has been once through the tables.
The result is that the first 4 players in the array are each registered in one of the 4 tables, then the loop breaks or ends.

Re: Heads Up Tournament with Levels

Posted: Mon Sep 28, 2009 9:21 pm
by CanadaWest
OK, found one solution.

Not elegant but it works.

I used array_slice to divide the array into 2 arrays and then ran the loop once with each of the new arrays.

Re: Heads Up Tournament with Levels

Posted: Mon Sep 28, 2009 9:35 pm
by Kent Briggs
CanadaWest wrote: Trouble is, the foreach loop doesn't continue through the entire $win array (the list of players) but stops when it has been once through the tables.
The result is that the first 4 players in the array are each registered in one of the 4 tables, then the loop breaks or ends.
Your looping doesn't make any sense to me. I've copied it here inside Code tags to line up the brackets:

Code: Select all

}
  $table = 1;
  $i = 1;
  while ($i <= $n)
  {
    while ($table <= $t)  
    {
      foreach($win as $Player)
      {
        $params = "Password=$pw&Command=TournamentsRegister" .
        "&Player=" . urlencode($Player) .
        "&Name=" . urlencode("Heads Up " . $Rnd . $table++);
        $api = Poker_API($url,$params,true);
      }
    }
  $i++;
}
It looks to me that $table is going to go from 2 to 9 inside the foreach loop with the $table++ command. And thus the while($table <=$t) loop is only going to run once and the outer while ($i <= $n) loop doesn't do anything after the first loop because $table will still not be less that or equal to $t. In the example you gave with the 8 players, what are the tournament names where each player should be registered? If I knew that, it would be more clear. I know you wouldn't need a loop inside a loop inside a loop to register 8 players.

Re: Heads Up Tournament with Levels

Posted: Tue Sep 29, 2009 12:06 am
by CanadaWest
Sorry I had posted he second-last version of that code.

naturally, $i and $table were set to 0, not 1.

But I see what you mean,
the while ($table <= $t) loop wasn't necessary at all. I took it out and it changed nothing.
what I finally did was split the Player array into two arrays: $Set1 and $Set2.
So each array contains the same number of players as there are tables.
Then I just ran the loop twice. Once with each array.

Code: Select all

  
$table = 0;
$i = 0;
      while ($i <= $n)
     {
                foreach($set1 as &$Player)
               {
	                 $params = "Password=$pw&Command=TournamentsRegister" .
		"&Player=" . urlencode($Player) .
		"&Name=" . urlencode("Hansen HU " .  $Rnd . $table++);
		$api = Poker_API($url,$params,true);
               }
                $i++;
       }
$table = 0;
$i = 0;
      while ($i <= $n)
      {
                 foreach($set2 as &$Player)
                {
	                 $params = "Password=$pw&Command=TournamentsRegister" .
		"&Player=" . urlencode($Player) .
		"&Name=" . urlencode("Hansen HU " .  $Rnd . $table++);
		$api = Poker_API($url,$params,true);
                }
               $i++;
       } 
 

Re: Heads Up Tournament with Levels

Posted: Tue Sep 29, 2009 1:27 pm
by CanadaWest
Is there a way to express a variable that takes several possibilities into account?

There are 31 separate tournaments:

16 called "Hansen HU Rnd 1 Ti" with "i" being 1 - 16
8 called "Hansen HU Rnd 2 Ti" with "i" being 1 - 8
4 called "Hansen HU Rnd 3 Ti" with "i" being 1 - 4
2 called "Hansen HU Rnd 4 Ti" with "i" being 1 - 2
and 1 called "Hansen HU Final T1"

is there a way to isolate just Rnd 1 tournaments from api TournamentsResults?
Something like:

Code: Select all

  
if ($Tourney == ("Hansen HU Rnd 1 T" + "i") && $Date == date("Y-m-d"))
$HU1 [] = ($Tourney);
For some reason, this returns all the tournaments that ran today.

Re: Heads Up Tournament with Levels

Posted: Tue Sep 29, 2009 1:47 pm
by Kent Briggs
CanadaWest wrote:For some reason, this returns all the tournaments that ran today.
In PHP, you use a period (not a plus) to join two strings together. Use the strpos() function to see if a substring is contained in a larger string:

if ($tname,"Rnd 1") then echo("Rnd 1 found in " . $tname); else echo("Rnd 1 not found in " . $tname);

http://us2.php.net/manual/en/function.strpos.php

Re: Heads Up Tournament with Levels

Posted: Tue Sep 29, 2009 6:27 pm
by CanadaWest
That worked nicely, thank you.

Re: Heads Up Tournament with Levels

Posted: Tue Oct 06, 2009 11:27 am
by CanadaWest
Its all done.

Not the most elegant piece of code I've ever seen but it works!

At least it works under test conditions... lol

I am testing it in gameplay on Thursday. Wish me luck.