<?php

$pageLink="./";

include("html_functions.php");
include($_SESSION['adminpath']."/dbopen.php");

//Get League ID
if (rand(1,100)<=75) {$lgid=$_SESSION['lgid'];}  //75% chance to stay in current league context
else {
    $query="SELECT league_id FROM `leagues` ORDER BY -LOG(1.0 - RAND()) * league_level LIMIT 1";
    if ($_GET['showq']==1) {echo "$query<br/>\n";}
    $result=mysqli_query($db,$query);
    $row=mysqli_fetch_array($result);
    $lgid=$row['league_id'];
}
//Get SubLeague ID and Year
$query="SELECT sub_league_id,year FROM league_history WHERE league_id=".$lgid." ORDER BY rand() LIMIT 1;";
if ($_GET['showq']==1) {echo "$query<br/>\n";}
$result=mysqli_query($db,$query);
$row=mysqli_fetch_array($result);
$slid=$row['sub_league_id'];
$year=$row['year'];
//Get Team ID
$query="SELECT team_id FROM `team_history` WHERE league_id=".$lgid." AND year=".$year." ORDER BY rand() LIMIT 1;";
if ($_GET['showq']==1) {echo "$query<br/>\n";}
$result=mysqli_query($db,$query);
$row=mysqli_fetch_array($result);
$tid=$row['team_id'];
//Get Batter Or Pitcher
$who=((rand(1,2)==1)?'batt':'pitch');
//Get Player ID
$query="SELECT player_id FROM players_career_".$who."ing_stats WHERE league_id=".$lgid." ORDER BY RAND() LIMIT 1";
if ($_GET['showq']==1) {echo "$query<br/>\n";}
$result=mysqli_query($db,$query);
$row=mysqli_fetch_array($result);
$pid=$row['player_id'];

//Get Page To Display
$modules = Array("season","history","frontOffice","managers");
$seasonPages = Array("boxscores","graphs","simstats","teamStats","statOfTheDay");
$historyPages = Array("leagues","teams","players","graphs","leaderboards");
$frontOfficePages = Array("tradeBlock","injuries","financials","payroll");

$module = rand(1,100);
$pageType = rand(1,100);
$subPageType = rand(1,100);

switch (true) {
    case ($module<5):  //Season Page <25
        switch (true) {
            case ($pageType<20):
                $pageLink="teamStats.php?view=T".$tid."&league_id=".$lgid;
                break;
            case ($pageType<101):  //Stat Of The Day Page
                $pageLink="statOfTheDay.php?fact=".rand(0,365);
                break;
        }
        break;
    case ($module<101):  //History Page <80
        switch (true) {
            case ($pageType<50):  //Player pages
                $pageLink="./player.php?player_id=".$pid."&league_id=".$lgid;
                break;
            case ($pageType<=100):  //Team Pages
                switch (true) {
                    case ($subPageType<20):  //Year Summary
                        $pageLink="./team_summary.php?team_id=".$tid."&year=".$year."&league_id=".$lgid;
                        break;
                    case ($subPageType<30):  //Positional Starters
                        $pageLink="./team_positional_starters.php?team_id=".$tid."&league_id=".$lgid;
                        break;
                    case ($subPageType<40):  //Pitching Starters
                        $pageLink="./team_pitcher_starters.php?team_id=".$tid."&league_id=".$lgid;
                        break;
                    case ($subPageType<50):  //Postseason Grid
                        $pageLink="./team_postseason_table.php?team_id=".$tid."&league_id=".$lgid;
                        break;
                    case ($subPageType<60):  //Park History
                        $pageLink="./team_park_history.php?team_id=".$tid."&league_id=".$lgid;
                        break;
                    case ($subPageType<70):  //Batting Leaders
                        $pageLink="./team_batting_leaders.php?team_id=".$tid."&league_id=".$lgid;
                        break;
                    case ($subPageType<80):  //Picthing Leaders
                        $pageLink="./team_pitching_leaders.php?team_id=".$tid."&league_id=".$lgid;
                        break;
                    case ($subPageType<101):  //Pitching Starters
                        $pageLink="./team_player_tenure.php?team_id=".$tid."&year=".$year."&league_id=".$lgid;
                        break;
                }
                break;
        }
    case ($module<99):  //Front Office Page <99
        break;
    case ($module==100):  //Manager Page =100
        break;
}

mysqli_close($db);

header('Location: '.$pageLink);

?>

