0)
{
$lock_sql = "LOCK TABLES $table1 $lockType, $table2 $lockType";
} else {
$lock_sql = "LOCK TABLES $table1 $lockType";
}
//------------------
//Connect to the DB
//------------------
$conn = dbConnect();
//------------------
//Lock mysql tables
//------------------
$result = mysqli_query($conn, $lock_sql);
if($result === FALSE)
{
$err = mysqli_error($conn);
$strLog = "\nMyLockTables: Cannot lock tables - $err";
$strLog .= "\nsql=$lock_sql";
WriteToErrorLog($strLog, 'data');
return "Cannot lock tables - Please try again";
}
return $conn;
}
//=================================================================================
function MyUnlockTables($conn)
//=================================================================================
{
$unlock_sql = "UNLOCK TABLES";
//------------------
//Unlock mysql tables
//------------------
$result = mysqli_query($conn, $unlock_sql);
if($result === FALSE)
{
echo "
MyUnlockTables: Cannot unlock tables";
}
mysqli_close($conn);
}
//=================================================================================
function MyGetLoginInfo($username)
//=================================================================================
{
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tablePlayers, "", 'READ');
//-----------------
//Get players data
//-----------------
$sql = "SELECT * FROM $tablePlayers WHERE username = \"$username\"";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot select username=$username: $err";
//WriteToErrorLog($strLog, 'data');
return 1;
}
$row = $result->fetch_assoc();
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
return $row;
}
//=================================================================================
function MyGetPlayerInfo()
//=================================================================================
{
global $Players;
global $Status;
global $Systemdata;
global $ndxPlayers;
global $NumDBPlayers;
global $SUSNoStatus;
global $SUSNoDays;
//$SUSNoStatus = $_SESSION['susnostatus']; //Number of days of status in DB
//$SUSNoDays = $_SESSION['susnodays']; //Number of days displayed
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$Zero = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
$issetPlayers = (isset($Players)) ? 1 : 0;
$issetPlayers = 0; //always read entire database
$dbConn = MyLockTables($tablePlayers, $tableSystemdata, 'READ');
//-----------------------
//Get system data
//-----------------------
$sql = "SELECT * FROM $tableSystemdata";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows != 1)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot select $tableSystemdata: $err";
WriteToErrorLog($strLog, 'data');
return "Database busy - Please try again";
}
//$Systemdata[] = array();
$row = $result->fetch_assoc();
$Sjd = explode(',', $row['jdstart']);
$Sdate = explode(',', $row['date']);
$Sday = explode(',', $row['day']);
$Shigh = explode(',', $row['highseq']);
$Sblk = explode(',', $row['signupblock']);
for($d=0; $d<$SUSNoDays; $d++) //for each day of play
{
$Systemdata['jdstart'][$d] = $Sjd[$d];
$Systemdata['date'][$d] = $Sdate[$d];
$Systemdata['day'][$d] = $Sday[$d];
$Systemdata['highseq'][$d] = $Shigh[$d];
$Systemdata['signupblock'][$d] = $Sblk[$d];
}
// free result set
mysqli_free_result($result);
//-----------------------
//Get player information
//-----------------------
if($issetPlayers)
{
$lastMod = $_SESSION['lastread'];
$sql = "SELECT * FROM $tablePlayers WHERE last_modified > $lastMod";
} else {
$sql = "SELECT * FROM $tablePlayers";
}
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot get player info ($err)";
$strLog .= "\n$sql";
WriteToErrorLog($strLog, 'data');
return "Database busy - Please try again";
}
if($numrows <= 0)
{
mysqli_free_result($result);
MyUnlockTables($dbConn);
$_SESSION['lastread'] = time();
$NumDBPlayers = $_SESSION['susnoplayers'] = count($Players);
return $NumDBPlayers;
}
//-----------------------
//Generate players array
//-----------------------
//$Players[] = array(); //clear any current array
//$Status[][] = array(); //clear any current array
$i = -1;
$s = -1;
while($row = $result->fetch_assoc())
{
if($issetPlayers == 1)
{
$i = PlayerIndex($row['pid']);
$s = StatusIndex($row['pid']);
} else {
$i++;
$s++;
}
$Players[$i]['pid'] = $row['pid'];
$Players[$i]['super'] = $row['super'];
$Players[$i]['name'] = $row['name'];
$Players[$i]['username'] = $row['username'];
$Players[$i]['login'] = $row['login'];
$Players[$i]['phone'] = $row['phone'];
$Players[$i]['mobile'] = $row['mobile'];
$Players[$i]['ptype'] = $row['ptype'];
$Players[$i]['email'] = $row['email'];
$Players[$i]['notify'] = $row['notify'];
$Players[$i]['contact'] = $row['contact'];
$Players[$i]['playerid'] = $row['playerid'];
$Players[$i]['masterpts'] = $row['masterpts'];
//----------------------
//Generate status array
//----------------------
$Spart = array();
$Sposn = array();
$Sstat = array();
$Sprev = array();
$Sseq = array();
$Stblnum = array();
$Stblposn = array();
$Spart = explode(',', $row['partner']);
$Sposn = explode(',', $row['posn']);
$Sstat = explode(',', $row['status']);
$Sprev = explode(',', $row['prevstatus']);
$Sseq = explode(',', $row['seq']);
$Stblnum = explode(',', $row['tablenum']);
$Stblposn = explode(',', $row['tableposn']);
$cnt = count($Spart);
//for($d=0; $d<$cnt; $d++) //for each day
for($d=0; $d<$SUSNoStatus; $d++) //for each day
{
$Status[$s][$d]['pid'] = $row['pid'];
$Status[$s][$d]['partner'] = $Spart[$d];
$Status[$s][$d]['posn'] = $Sposn[$d];
$Status[$s][$d]['status'] = $Sstat[$d];
$Status[$s][$d]['prevstatus'] = $Sprev[$d];
$Status[$s][$d]['seq'] = $Sseq[$d];
$Status[$s][$d]['table'] = $Stblnum[$d];
$Status[$s][$d]['tableposn'] = $Stblposn[$d];
}
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
$NumDBPlayers = count($Players);
$_SESSION['susnoplayers'] = $NumDBPlayers;
// -------------------------------------
// Sort the player info by player name
// -------------------------------------
$rowName = array();
// create array of player names in column form
for($i=0; $i<$NumDBPlayers; $i++)
{
$rowName[$i] = $Players[$i]['name'];
}
// sort the names and reorder players array accordingly
array_multisort($rowName, SORT_ASC, $Players);
// --------------------------
// Generate pid lookup array
// --------------------------
$ndxPlayers = array();
for($i=0; $i<$NumDBPlayers; $i++)
{
$row[$i] = $Players[$i]['pid'];
$ndxPlayers[$i][0] = $Players[$i]['pid'];
$ndxPlayers[$i][1] = $i;
}
array_multisort($row, SORT_ASC, $ndxPlayers);
$_SESSION['lastread'] = time();
return $NumDBPlayers;
}
//=================================================================================
function MyUpdateSystemdata()
//=================================================================================
{
global $SUSNoStatus;
global $Systemdata;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tableSystemdata);
//------------------------------
//Generate the systemdata table
//------------------------------
$v1 = implode(',', $Systemdata['jdstart']);
$v2 = implode(',', $Systemdata['date']);
$v3 = implode(',', $Systemdata['day']);
$v4 = implode(',', $Systemdata['highseq']);
$v5 = implode(',', $Systemdata['signupblock']);
$sql = "UPDATE $tableSystemdata SET jdstart='$v1', date='$v2',
day='$v3', highseq='$v4',
signupblock='$v5'
WHERE row = 1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot update $tableSystemdata ($err)";
WriteToErrorLog($strLog, 'data');
return "Database busy - Please try again";
}
MyUnlockTables($dbConn);
return intval(0);
}
//=================================================================================
function MyUpdatePType($pid, $ptype)
//=================================================================================
{
global $SUSNoStatus;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tablePlayers);
//--------------------------
//Set player information
//--------------------------
$v1 = $pid;
$v8 = $ptype;
//-------------------------------------
//Check that player is in the database
//-------------------------------------
$sql = "SELECT * FROM $tablePlayers Where pid = $v1";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
MyUnlockTables($dbConn);
return "pid $v1 is not in the database";
}
//--------------------
//update the database
//--------------------
$lastmod = time();
$sql = "UPDATE $tablePlayers SET ptype=$v8, last_modified=$lastmod
where pid = $v1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot update player pid $v1: $err";
WriteToErrorLog($strLog, 'data');
return "Database busy - Please try again";
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
MyGetPlayerInfo();
return 0;
}
//=================================================================================
function MyUpdateStatus()
//=================================================================================
{
global $SUSNoStatus;
global $Status;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
if(!isset($_SESSION['changed_pids'])) return 0;
//--------------------------------------
//prune the list of pids for duplicates
//--------------------------------------
$chgpids = array();
$temp = $_SESSION['changed_pids'];
$_SESSION['changed_pids'] = array();
if(!is_array($temp))
{
$cnt = 1;
$chgpids[] = $temp;
} else {
$cnt = count($temp);
$j = 0;
sort($temp);
$chgpids[$j++] = $temp[0];
for($i=1; $i<$cnt; $i++)
{
if($temp[$i-1] == $temp[$i])
{
continue;
} else {
$chgpids[$j++] = $temp[$i];
}
}
$cnt = count($chgpids);
}
$dbConn = MyLockTables($tablePlayers);
for($p=0; $p<$cnt; $p++)
{
$v1 = $chgpids[$p];
$n = StatusIndex($v1);
if($n < 0) continue;
//-------------------------------------
//Check that player is in the database
//-------------------------------------
$sql = "SELECT * FROM $tablePlayers Where pid = $v1";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
// free result set
mysqli_free_result($result);
echo "
Player $v1 is not in database";
continue;
}
// free result set
mysqli_free_result($result);
//--------------------------------
//Format status info for database
//--------------------------------
$t2 = array();
$t3 = array();
$t4 = array();
$t5 = array();
$t6 = array();
$t7 = array();
$t8 = array();
for($d=0; $d<$SUSNoStatus; $d++)
{
$t2[] = $Status[$n][$d]['partner'];
$t3[] = $Status[$n][$d]['posn'];
$t4[] = $Status[$n][$d]['status'];
$t5[] = $Status[$n][$d]['prevstatus'];
$t6[] = $Status[$n][$d]['seq'];
$t7[] = $Status[$n][$d]['table'];
$t8[] = $Status[$n][$d]['tableposn'];
}
$v2 = implode(',', $t2);
$v3 = implode(',', $t3);
$v4 = implode(',', $t4);
$v5 = implode(',', $t5);
$v6 = implode(',', $t6);
$v7 = implode(',', $t7);
$v8 = implode(',', $t8);
$v9 = time();
//--------------------
//update the database
//--------------------
$lastmod = time();
$sql = "UPDATE $tablePlayers SET partner='$v2', posn='$v3',
status='$v4', prevstatus='$v5',
seq='$v6', tablenum='$v7',
tableposn='$v8', last_modified='$v9'
where pid = $v1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
echo "
Cannot update player status for $v1: $err";
continue;
}
}
MyUnlockTables($dbConn);
MyGetPlayerInfo();
return 0;
}
//=================================================================================
function MyAddPlayer($PData)
//=================================================================================
{
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$Zero = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
$Open = "O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O";
//echo "
PData
";
//print_r($PData);
$dbConn = MyLockTables($tablePlayers);
//--------------------------
//Check for duplicate names
//--------------------------
$v3 = $PData['name'];
$sql = "SELECT * FROM $tablePlayers Where name = \"$v3\"";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
// free result set
mysqli_free_result($result);
if($numrows > 0)
{
MyUnlockTables($dbConn);
return "$v3 is already in the database";
}
//--------------------------
//Set player information
//--------------------------
$v2 = $PData['super'];
$v3 = $PData['name'];
$v4 = $PData['username'];
$v5 = $PData['login'];
$v6 = $PData['phone'];
$v7 = $PData['mobile'];
$v8 = $PData['ptype'];
$v9 = $PData['email'];
$v10 = $PData['notify'];
$v11 = $PData['contact'];
$v12 = $PData['playerid'];
$v13 = $PData['masterpts'];
//--------------------------
//Set status information
//--------------------------
$s2 = $Zero;
$s3 = $Zero;
$s4 = $Open;
$s5 = $Open;
$s6 = $Zero;
$s7 = $Zero;
$s8 = $Zero;
//--------------------------
//Insert player information
//--------------------------
$lastmod = time();
$sql = "INSERT INTO $tablePlayers (super, name, username, login, phone, mobile, ptype, email, notify, contact, playerid,
masterpts, partner, posn, status, prevstatus, seq, tablenum, tableposn, last_modified)
VALUES ($v2, \"$v3\", \"$v4\", '$v5', '$v6', '$v7', $v8, '$v9', $v10, $v11, '$v12', $v13,
'$s2', '$s3', '$s4', '$s5', '$s6', '$s7', '$s8', $lastmod)";
$rc = mysqli_query($dbConn, $sql); // or die("Can't insert pid=$v1");
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyAddPlayer: Cannot insert player: $err";
WriteToErrorLog($strLog, 'data');
//echo "
$v2, \"$v3\", '$v4', '$v5', '$v6', '$v7', $v8, '$v9', $v10, $v11, '$v12', $v13";
//echo "
'$s2', '$s3', $s4, '$s5', '$s6', '$s7', '$s8', $lastmod";
return "MyAddPlayer: Cannot insert player: $err";
}
//------------------
//Sort table by pid
//------------------
$sql = "ALTER TABLE `$tablePlayers` ORDER BY `pid`" ;
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyAddPlayer: Cannot sort sort players: $err";
WriteToErrorLog($strLog, 'data');
}
MyUnlockTables($dbConn);
MyGetPlayerInfo();
$strLog = "\nMyAddPlayer: $v3 has been added to the database";
WriteToErrorLog($strLog, 'data');
return 0;
}
//=================================================================================
function MyDeletePlayer($pname)
//=================================================================================
{
global $SUSNoStatus;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tablePlayers);
//get player information
$sql = "SELECT * FROM $tablePlayers WHERE name = \"$pname\"";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($result === FALSE || $numrows <= 0)
{
MyUnlockTables($dbConn);
return "Player \"$pname\" not in DB";
}
$player = $result->fetch_assoc();
$pid = $player['pid'];
$playpart = explode(',', $player['partner']);
//find partners and remove player from their status
for($i=0; $i<$SUSNoStatus; $i++) //for each day
{
if($playpart[$i] != 0) //if player has partner that day
{
//get partner's status
$partpid = $playpart[$i];
$sql = "SELECT * FROM $tablePlayers WHERE pid = '$partpid'";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($result === FALSE || $numrows <= 0)
{
MyUnlockTables($dbConn);
return "Cannot get status for pid $partpid";
}
$partstat = $result->fetch_assoc();
$Spart = explode(',', $partstat['partner']);
$Sposn = explode(',', $partstat['posn']);
$Sstat = explode(',', $partstat['status']);
$Sprev = explode(',', $partstat['prevstatus']);
$Sseq = explode(',', $partstat['seq']);
$Stblnum = explode(',', $partstat['tablenum']);
$Stblposn = explode(',', $partstat['tableposn']);
//reset player's partnership in partner's status
$chgcnt = 0;
for($j=0; $j<$SUSNoStatus; $j++)
{
if($Spart[$j] == $pid) //if partner is player
{
$Spart[$j] = 0; //reset status
$Sposn[$j] = 0;
$Sstat[$j] = 'O';
$Sprev[$j] = 'O';
$Sseq[$j] = 0;
$Stblnum[$j] = 0;
$Stblposn[$j] = 0;
$chgcnt++;
}
}
if($chgcnt > 0) //if any changes in status
{
$Spart = implode(',', $Spart);
$Sposn = implode(',', $Sposn);
$Sstat = implode(',', $Sstat);
$Sprev = implode(',', $Sprev);
$Sseq = implode(',', $Sseq);
$Stblnum = implode(',', $Stblnum);
$Stblposn = implode(',', $Stblposn);
$sql = "UPDATE $tablePlayers SET partner='$Spart', posn='$Sposn',
status='$Sstat', status='$Sprev',
seq='$Sseq', tablenum='$Stblnum',
tableposn='$Stblposn'
where pid = $partpid";
$result = mysqli_query($dbConn, $sql);
if($result === FALSE)
{
echo "
MyDeletePlayer: Cannot update status for pid $partpid";
exit;
}
}
}
}
//delete the player's info and status
$sql = "DELETE FROM $tablePlayers WHERE pid = $pid";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyDeletePlayer: Cannot delete $pname($pid) $err";
WriteToErrorLog($strLog, 'data');
return "MyDeletePlayer: Cannot delete $pname($pid) $err";
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
MyGetPlayerInfo();
$strLog = "\nMyDeletePlayer: $pname($pid) has been deleted";
WriteToErrorLog($strLog, 'data');
return 0;
}
//=================================================================================
function MyUpdatePlayer($PData)
//=================================================================================
{
global $SUSNoStatus;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tablePlayers);
//--------------------------
//Set player information
//--------------------------
$v1 = $PData['pid'];
$v2 = $PData['super'];
$v3 = $PData['name'];
$v4 = $PData['username'];
$v5 = $PData['login'];
$v6 = $PData['phone'];
$v7 = $PData['mobile'];
$v8 = $PData['ptype'];
$v9 = $PData['email'];
$v10 = $PData['notify'];
$v11 = $PData['contact'];
$v12 = $PData['playerid'];
$v13 = $PData['masterpts'];
//echo "
$v2, '$v3', $v4, '$v5', '$v6', '$v7', $v8, '$v9', $v10, $v11, '$v12', $v13";
//exit;
//-------------------------------------
//Check that player is in the database
//-------------------------------------
$sql = "SELECT * FROM $tablePlayers Where pid = $v1";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
MyUnlockTables($dbConn);
$strLog = "\n$v3 is not in the database";
WriteToErrorLog($strLog, 'data');
return 0;
}
//--------------------
//update the database
//--------------------
$lastmod = time();
$sql = "UPDATE $tablePlayers SET super=$v2, name=\"$v3\",
username=\"$v4\", login='$v5',
phone='$v6', mobile='$v7',
ptype=$v8, email='$v9',
notify=$v10, contact=$v11,
playerid='$v12',masterpts=$v13,
last_modified=$lastmod
where pid = $v1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot update player $v3: $err";
WriteToErrorLog($strLog, 'data');
return "Database busy - Please try again";
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
MyGetPlayerInfo();
return 0;
}
//=================================================================================
function MyUpdateMasterpts($pid, $ACBL, $mpts )
//=================================================================================
{
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tablePlayers);
//-------------------------------------
//Check that player is in the database
//-------------------------------------
$sql = "SELECT * FROM $tablePlayers Where pid = $pid";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
MyUnlockTables($dbConn);
return 0;
}
//--------------------
//update the database
//--------------------
$lastmod = time();
$fpMPts = floatval($mpts);
$sql = "UPDATE $tablePlayers SET playerid='$ACBL', masterpts=$fpMPts,
last_modified=$lastmod
where pid = $pid";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nCannot update master pts - $err";
WriteToErrorLog($strLog, 'data');
return 1;
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
//MyGetPlayerInfo();
return 0;
}
//=================================================================================
function MyRollover($fullro = 0)
//=================================================================================
{
global $Players;
global $Status;
global $Systemdata;
global $NumDBPlayers;
global $SUSNoStatus;
global $SUSNoDays;
global $SUSDaysUsed;
$TablesFileName = $_SESSION['path_tables'];
$TablesCsvFileName = $_SESSION['path_tabcsv'];
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$DayName = array("Su ","M ","Tu ","W ","Th ","F ","Sa ");
$Zero = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
if($fullro > 0)
{
$jd = $Systemdata['jdstart'][0];
$rolloverDOW = (jddayofweek($jd,0));
PruneLogFile();
//----------------------------------------
//backup the database before rolling over
//----------------------------------------
$suffix = '_R' . $rolloverDOW;
MyBackupDB($suffix);
$line = "\nMyRollover: Manual backup & rollover for DOW=$rolloverDOW";
WriteToLogFile($line, "data");
//---------------------------
//reset player ptype entries
//---------------------------
$rc = MyResetPType();
WriteToLogFile("\nResetPType(): $rc resets", "data");
}
$dbConn = MyLockTables($tablePlayers, $tableSystemdata);
//-----------------------
//Get system data
//-----------------------
$sql = "SELECT * FROM $tableSystemdata";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows != 1)
{
MyUnlockTables($dbConn);
$strLog = "\nMyRollover: Cannot get $tableSystemdata info from the database";
WriteToErrorLog($strLog, 'data');
return 0;
}
$row = $result->fetch_assoc();
$Sjd = explode(',', $row['jdstart']);
$flag = 0;
$jd = $Sjd[$SUSNoDays-1]; //last jd in current table
$jd++;
while($flag == 0) { //for each day of play
$dow = (jddayofweek($jd,0));
if($SUSDaysUsed[$dow] == 0) { //day not day of play
$jd++;
continue;
}
$flag = 1;
$md = jdtogregorian($jd); // returns month/day/year
$md = substr($md,0,strrpos($md,"/")); //get month/day
$dn = $DayName[$dow];
}
array_shift($Sjd);
$Sjd[] = $jd;
$str_jd = implode(',', $Sjd);
$Sdate = explode(',', $row['date']);
array_shift($Sdate);
$Sdate[] = $md;
$str_date = implode(',', $Sdate);
$Sday = explode(',', $row['day']);
array_shift($Sday);
$Sday[] = $dn;
$str_day = implode(',', $Sday);
$Shigh = explode(',', $row['highseq']);
array_shift($Shigh);
$Shigh[] = 0;
$str_high = implode(',', $Shigh);
$Sblk = explode(',', $row['signupblock']);
array_shift($Sblk);
$Sblk[] = 0;
$str_blk = implode(',', $Sblk);
$jdStart = $Sjd[0];
/*
echo "
jdStart=$jdStart";
echo "
jd=$str_jd";
echo "
date=$str_date";
echo "
day=$str_day";
echo "
high=$str_high";
echo "
blk=$str_blk";
exit;
*/
//--------------------
//update the database
//--------------------
$sql = "UPDATE $tableSystemdata SET jdstart='$str_jd', date='$str_date',
day='$str_day', highseq='$str_high',
signupblock='$str_blk'
WHERE row = 1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRollover: Cannot rollover $tableSystemdata ($err)";
WriteToErrorLog($strLog, 'data');
return 1;
}
// free result set
mysqli_free_result($result);
//-----------------------
//Get player information
//-----------------------
$sql = "SELECT * FROM $tablePlayers";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRollover:Cannot select $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return 1;
}
//-----------------------
//Generate players array
//-----------------------
while($row = $result->fetch_assoc())
{
$pid = $row['pid'];
//-------------------------
//Rollover the status info
//-------------------------
$Spart = array();
$Spart = explode(',', $row['partner']);
array_shift($Spart);
$Spart[] = 0;
$str_part = implode(',', $Spart);
$Sposn = array();
$Sposn = explode(',', $row['posn']);
array_shift($Sposn);
$Sposn[] = 0;
$str_posn = implode(',', $Sposn);
$Sstat = array();
$Sstat = explode(',', $row['status']);
array_shift($Sstat);
$Sstat[] = 'O';
$str_stat = implode(',', $Sstat);
$Sprev = array();
$Sprev = explode(',', $row['prevstatus']);
array_shift($Sprev);
$Sprev[] = 'O';
$Sprev[0] = $Sstat[0]; //Avoid sending email for change of status
$str_prev = implode(',', $Sprev);
$Sseq = array();
$Sseq = explode(',', $row['seq']);
array_shift($Sseq);
$Sseq[] = 0;
$str_seq = implode(',', $Sseq);
$Stblnum = array();
$Stblnum = explode(',', $row['tablenum']);
array_shift($Stblnum);
$Stblnum[] = 0;
$str_tblnum = implode(',', $Stblnum);
$Stblposn = array();
$Stblposn = explode(',', $row['tableposn']);
array_shift($Stblposn);
$Stblposn[] = 0;
$str_tblposn = implode(',', $Stblposn);
//--------------------
//update the database
//--------------------
$sql = "UPDATE $tablePlayers SET partner='$str_part', posn='$str_posn', status='$str_stat',
prevstatus='$str_prev', seq='$str_seq', tablenum='$str_tblnum',
tableposn='$str_tblposn'
WHERE pid = $pid";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRollover: Cannot rollover $tablePlayers $err";
WriteToErrorLog($strLog, 'data');
return 1;
}
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
if($fullro > 0)
{
//---------------------------------
//pop the Tables file (tables.ser)
//---------------------------------
if(file_exists($TablesFileName))
{
$Tables = unserialize(file_get_contents($TablesFileName));
if($Tables)
{
ksort($Tables);
array_shift($Tables);
$Tables[2][0]['table'] = -1;
file_put_contents($TablesFileName, serialize($Tables));
}
}
//---------------------------------------
//remove the tables csv file (tables.csv)
//---------------------------------------
if(file_exists($TablesCsvFileName))
{
//fclose($TablesCsvFileName);
unlink($TablesCsvFileName);
}
}
return intval($jdStart);
}
//======================================================================================
function MyChgDaysUsed($newDaysUsed, $daysDisplayed) { //add and remove days of a week
//======================================================================================
global $Players;
global $Status;
global $Systemdata;
global $SUSNoDays; //number of days displayed
global $SUSNoStatus; //number of days of data in an entry
global $SUSDaysUsed; //array of days used in a week (smtwtfs) 0=>not used 1=>used
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$DayName = array("Su ","M ","Tu ","W ","Th ","F ","Sa ");
$NumDB= MyGetPlayerInfo();
$dbConn = MyLockTables($tablePlayers, $tableSystemdata);
$jdstart = $Systemdata['jdstart'];
$date = $Systemdata['date'];
$day = $Systemdata['day'];
//------------------------------------------------------------------------
//------------------------------
//Generate the systemdata table
//------------------------------
$Zero = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
//jdstart, date and dayname
$j = explode($Zero);
$d = explode($Zero);
$n = explode($Zero);
$jd = $jdstart[0];
$days = 0;
$dow = (jddayofweek($jd,0));
// echo "
-------------------";
while($days < $daysDisplayed) { //for each day of play
$dow = (jddayofweek($jd,0));
if($newDaysUsed[$dow] == 0) { //day not day of play
$jd++;
continue;
}
$md = jdtogregorian($jd); // returns month/day/year
$md = substr($md,0,strrpos($md,"/")); //get month/day
$j[$days] = $jd;
$d[$days] = $md;
$dn = $DayName[$dow];
$n[$days] = $dn;
$days++;
$jd++;
}
$v1 = implode(',', $j);
$v2 = implode(',', $d);
$v3 = implode(',', $n);
$highseq = $Systemdata['highseq'];
$signupblock = $Systemdata['signupblock'];
$JulDay = $jdstart[0];
$oldDU = $SUSDaysUsed;
$newDU = $newDaysUsed;
//----------------------------
//align the days used to today
//----------------------------
$ndxDOW = jddayofweek($JulDay); //smtwtfs (0=sun, 1=mon, ...)
//-------------------------------------------------
//init new $Status arrays
//-------------------------------------------------
$newStatus[][] = array();
$newHS = array();
$newSB = array();
//---------------------------------------
//build new $Status arrays
//---------------------------------------
for($p=0; $p<$NumDB; $p++) //for all status entries
{
$pid = $Status[$p][0]['pid']; //pid for current player
$dow = $ndxDOW; //day of the week index
$src = 0;
$d = 0;
while($d<$SUSNoStatus) //for each day in a status entry
{
if($d >= $daysDisplayed)
{
$highseq[$d] = 0;
$signupblock[$d] = 0;
$newStatus[$p][$d]['pid'] = $pid;
$newStatus[$p][$d]['partner'] = 0;
$newStatus[$p][$d]['posn'] = 0;
$newStatus[$p][$d]['status'] = 'O';
$newStatus[$p][$d]['prevstatus'] = 'O';
$newStatus[$p][$d]['seq'] = 0;
$newStatus[$p][$d]['table'] = 0;
$newStatus[$p][$d]['tableposn'] = 0;
$d++;
continue;
}
if($src >= $SUSNoStatus) $src = $SUSNoStatus - 1;
//-----------------------------------------------
//cycle thru the days used arrays to determine
//the action required
//-----------------------------------------------
// $action == 0 =>skip the day (day not used)
// == 1 =>add as new day
// == 2 =>keep as retained day
// == 3 =>remove
//-----------------------------------------------
$action = 0;
while($action == 0) //proceed thru arrays while nothing to do
{
if($oldDU[$dow] == 0)
{
if($newDU[$dow] == 0)
{
$action = 0; //old==0 new==0 (skip)
$dow++;
if($dow > 6) $dow = 0;
} else {
$action = 1; //old==0 new==1 (add)
}
} else {
if($newDU[$dow] == 0)
{
$action = 3; //old==1 new==0 (remove)
} else {
$action = 2; //old==1 new==1 (keep)
}
}
}
switch ($action)
{
case 1: //add
if($p == 0)
{
$newHS[$d] = 0;
$newSB[$d] = 0;
}
//if($p == 0) { echo "$d,";}
$newStatus[$p][$d]['pid'] = $pid;
$newStatus[$p][$d]['partner'] = 0;
$newStatus[$p][$d]['posn'] = 0;
$newStatus[$p][$d]['status'] = 'O';
$newStatus[$p][$d]['prevstatus'] = 'O';
$newStatus[$p][$d]['seq'] = 0;
$newStatus[$p][$d]['table'] = 0;
$newStatus[$p][$d]['tableposn'] = 0;
$d++;
break;
case 2: //keep
if($p == 0)
{
$newHS[$d] = $highseq[$src];
$newSB[$d] = $signupblock[$src];
}
$newStatus[$p][$d]['pid'] = $Status[$p][$src]['pid'];
$newStatus[$p][$d]['partner'] = $Status[$p][$src]['partner'];
$newStatus[$p][$d]['posn'] = $Status[$p][$src]['posn'];
$newStatus[$p][$d]['status'] = $Status[$p][$src]['status'];
$newStatus[$p][$d]['prevstatus'] = $Status[$p][$src]['prevstatus'];
$newStatus[$p][$d]['seq'] = $Status[$p][$src]['seq'];
$newStatus[$p][$d]['table'] = $Status[$p][$src]['table'];
$newStatus[$p][$d]['tableposn'] = $Status[$p][$src]['tableposn'];
$src++;
$d++;
break;
case 3: //remove
$src++;
break;
}
$dow++;
if($dow > 6) $dow = 0;
}
}
//-------------------------------------------------
//update $Status array
//-------------------------------------------------
$Status[][] = array();
$SUSNoDays = $daysDisplayed;
$SUSDaysUsed = $newDU;
for($p=0; $p<$NumDB; $p++) //for all status entries
{
for($d=0; $d<$SUSNoStatus; $d++) //for all status entries
{
$Status[$p][$d]['pid'] = $newStatus[$p][$d]['pid'];
$Status[$p][$d]['partner'] = $newStatus[$p][$d]['partner'];
$Status[$p][$d]['posn'] = $newStatus[$p][$d]['posn'];
$Status[$p][$d]['status'] = $newStatus[$p][$d]['status'];
$Status[$p][$d]['prevstatus'] = $newStatus[$p][$d]['prevstatus'];
$Status[$p][$d]['seq'] = $newStatus[$p][$d]['seq'];
$Status[$p][$d]['table'] = $newStatus[$p][$d]['table'];
$Status[$p][$d]['tableposn'] = $newStatus[$p][$d]['tableposn'];
}
}
//----------------------
//update the systemdata
//----------------------
$str_high = implode(',', $newHS);
$str_blk = implode(',', $newSB);
$sql = "UPDATE $tableSystemdata SET jdstart='$v1', date='$v2',
day='$v3', highseq='$str_high',
signupblock='$str_blk'
WHERE row = 1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyChgDaysUsed: Cannot update $tableSystemdata ($err)";
WriteToErrorLog($strLog, 'data');
return 1;
}
// free result set
mysqli_free_result($result);
for($p=0; $p<$NumDB; $p++) //for all status entries
{
$v1 = $Status[$p][0]['pid'];
//--------------------------------
//Format status info for database
//--------------------------------
$t2 = array();
$t3 = array();
$t4 = array();
$t5 = array();
$t6 = array();
$t7 = array();
$t8 = array();
for($d=0; $d<$SUSNoStatus; $d++)
{
$t2[] = $Status[$p][$d]['partner'];
$t3[] = $Status[$p][$d]['posn'];
$t4[] = $Status[$p][$d]['status'];
$t5[] = $Status[$p][$d]['prevstatus'];
$t6[] = $Status[$p][$d]['seq'];
$t7[] = $Status[$p][$d]['table'];
$t8[] = $Status[$p][$d]['tableposn'];
}
$v2 = implode(',', $t2);
$v3 = implode(',', $t3);
$v4 = implode(',', $t4);
$v5 = implode(',', $t5);
$v6 = implode(',', $t6);
$v7 = implode(',', $t7);
$v8 = implode(',', $t8);
$v9 = time();
//--------------------
//update the database
//--------------------
$lastmod = time();
$sql = "UPDATE $tablePlayers SET partner='$v2', posn='$v3',
status='$v4', prevstatus='$v5',
seq='$v6', tablenum='$v7',
tableposn='$v8', last_modified='$v9'
where pid = $v1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
echo "
Cannot update player status for $v1: $err";
continue;
}
}
MyUnlockTables($dbConn);
//WriteCfgFile();
MyGetPlayerInfo();
return 0;
}
//======================================================================================
function MyZeroJDStart() {
//======================================================================================
global $SUSNoDays;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$j = array();
$d = array();
$n = array();
$dbConn = MyLockTables($tableSystemdata);
for($i=0; $i<$SUSNoDays; $i++)
{
$j[$i] = '0';
$d[$i] = '0';
$n[$i] = '0';
$Systemdata['jdstart'][$i] = 0;
$Systemdata['date'][$i] = '0';
$Systemdata['day'][$i] = '0';
}
$v1 = implode(',', $j);
$v2 = implode(',', $d);
$v3 = implode(',', $n);
//--------------------
//update the database
//--------------------
$sql = "UPDATE $tableSystemdata SET jdstart='$v1', date='$v2', day='$v3'
WHERE row = 1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyZeroJDStart: Cannot fill jdstart in $tableSystemdata ($err)";
WriteToErrorLog($strLog, 'data');
return "Cannot fill jdstart in $tableSystemdata ($err)";
}
MyUnlockTables($dbConn);
return 'Zero jdStart completed';
}
//======================================================================================
function MyFillJDStart() {
//======================================================================================
global $SUSNoDays;
global $SUSDaysUsed;
global $Systemdata;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$DayName = array("Su ","M ","Tu ","W ","Th ","F ","Sa ");
$j = array();
$d = array();
$n = array();
$dbConn = MyLockTables($tableSystemdata);
$jdToday = cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
$jd = $jdToday;
$days = 0;
// echo "
-------------------";
while($days < $SUSNoDays) { //for each day of play
$dow = (jddayofweek($jd,0)); //day of week (0=Sunday)
if($SUSDaysUsed[$dow] == 0) { //day not day of play
$jd++;
continue;
}
$md = jdtogregorian($jd); // returns month/day/year
$md = substr($md,0,strrpos($md,"/")); //get month/day
$j[$days] = $jd;
$d[$days] = $md;
$dn = $DayName[$dow];
$n[$days] = $dn;
$days++;
$jd++;
}
$jdStart = $j[0]; //jd number of next day of play
for($i=0; $i<$SUSNoDays; $i++)
{
$Systemdata['jdstart'][$i] = $j[$i];
$Systemdata['date'][$i] = $d[$i];
$Systemdata['day'][$i] = $n[$i];
}
$v1 = implode(',', $j);
$v2 = implode(',', $d);
$v3 = implode(',', $n);
//--------------------
//update the database
//--------------------
$sql = "UPDATE $tableSystemdata SET jdstart='$v1', date='$v2', day='$v3'
WHERE row = 1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyFillJDStart: Cannot fill jdstart in $tableSystemdata ($err)";
WriteToErrorLog($strLog, 'data');
return "Cannot fill jdstart in $tableSystemdata ($err)";
}
MyUnlockTables($dbConn);
return $jdStart;
}
//======================================================================================
function MyClearStatusDB() {
//======================================================================================
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$Zero = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
$Open = "O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O,O";
$dbConn = MyLockTables($tablePlayers);
//-----------------------
//Get player information
//-----------------------
$sql = "SELECT * FROM $tablePlayers";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyClearStatusDB: Cannot select $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return "MyClearStatusDB: Cannot select $tablePlayers - $err";
}
//-----------------------
//Generate players array
//-----------------------
while($row = $result->fetch_assoc())
{
$pid = $row['pid'];
//-------------------------
//Reset the status info
//-------------------------
$str_part = $Zero;
$str_posn = $Zero;
$str_stat = $Open;
$str_prev = $Open;
$str_seq = $Zero;
$str_tblnum = $Zero;
$str_tblposn = $Zero;
//--------------------
//update the database
//--------------------
$sql = "UPDATE $tablePlayers SET partner='$str_part', posn='$str_posn', status='$str_stat',
prevstatus='$str_prev', seq='$str_seq', tablenum='$str_tblnum',
tableposn='$str_tblposn'
WHERE pid = $pid";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyClearStatusDB: Cannot clear status: $err";
WriteToErrorLog($strLog, 'data');
return "MyClearStatusDB: Cannot clear status: $err";
}
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
Return "Clear status completed";
}
//=================================================================================
function MyRepairHighseqDB() {
//=================================================================================
global $SUSNoStatus;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$HS = array();
for($i=0; $i<$SUSNoStatus; $i++)
$HS[$i] = 0;
$dbConn = MyLockTables($tablePlayers, $tableSystemdata);
//-----------------------
//Get player information
//-----------------------
$sql = "SELECT * FROM $tablePlayers";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRepairHighseqDB: Cannot select $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return "MyRepairHighseqDB: Cannot select $tablePlayers - $err";
}
//-----------------------
//Generate high seq array
//-----------------------
while($row = $result->fetch_assoc())
{
$high = explode(',', $row['seq']);
for($i=0; $i<$SUSNoStatus; $i++)
{
if($high[$i] > $HS[$i])
$HS[$i] = $high[$i];
}
}
// free result set
mysqli_free_result($result);
//-----------------------
//Get system data
//-----------------------
$sql = "SELECT * FROM $tableSystemdata";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows != 1)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRepairHighseqDB: Cannot get $tableSystemdata - $err";
WriteToErrorLog($strLog, 'data');
return "MyRepairHighseqDB: Cannot get $tableSystemdata - $err";
}
//--------------------
//update the database
//--------------------
$high = implode(',', $HS);
$sql = "UPDATE $tableSystemdata SET highseq='$high' WHERE row = 1";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRepairHighseqDB: Cannot update $tableSystemdata - $err";
WriteToErrorLog($strLog, 'data');
return "MyRepairHighseqDB: Cannot update $tableSystemdata - $err";
}
// free result set
mysqli_free_result($result);
return "Highseq DB repaired";
}
//=================================================================================
function MyResetPType() {
//=================================================================================
global $SUSNoStatus;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$dbConn = MyLockTables($tablePlayers);
//-----------------------
//Get player information
//-----------------------
$sql = "SELECT * FROM $tablePlayers";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyResetPType: Cannot select $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return "MyResetPType: Cannot select $tablePlayers - $err";
}
while($row = $result->fetch_assoc())
{
$pid = $row['pid'];
$ptype = $row['ptype'];
//------------------------
//Change ptype to defaults
//------------------------
if($ptype == 10)
{
$ptype = 0;
}
if($ptype == 11)
{
$ptype = 1;
}
//--------------------
//update the database
//--------------------
$sql = "UPDATE $tablePlayers SET ptype=$ptype WHERE pid = $pid";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyResetPType: Cannot reset ptype: $err";
WriteToErrorLog($strLog, 'data');
return "MyResetPType: Cannot reset ptype: $err";
}
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
return "PType reset completed";
}
//=================================================================================
function MyBackupDB($daynumber)
//=================================================================================
{
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
//backup files location = ../dbasefiles/sheetid_bkup/sheetid#/*
//where # is the day of the week (0 <= # <= 6) 0=Sunday
//susname = "tcbc"
$buDir = $_SESSION['susname'] . $daynumber;
//path_buloc = "../dbasefiles/tcbc_bkup"
$buLoc = $_SESSION['path_buloc'];
//buDir = "../dbasefiles/tcbc_bkup/tcbc#"
$buPath = $buLoc . '/' . $buDir;
//path_dbloc = "../dbasefiles/tcbc"
$workingDir = $_SESSION['path_dbloc'];
if(!file_exists($buLoc))
{
mkdir($buLoc); //make the backup directory
}
if(file_exists($buPath))
{
RemoveDir($buPath); //remove previous backup
}
mkdir($buPath); //make directory for new backup
$rc = LockPlayerFiles(1);
CopyDir($workingDir, $buPath); //copy files into backup directory
$rc = LockPlayerFiles(0);
//---------------------
//Create backup object
//---------------------
$BackupObj = new DBTables;
//add current julian date
$jdToday = cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
$BackupObj->jddate = $jdToday;
$dbConn = MyLockTables($tablePlayers, $tableSystemdata, 'READ');
//-----------------------
//Get system data
//-----------------------
$sql = "SELECT * FROM $tableSystemdata";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows != 1)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyBackupDB: Cannot select $tableSystemdata: $err";
WriteToErrorLog($strLog, 'data');
return "MyBackupDB: Cannot select $tableSystemdata: $err";
}
//add systemdata to backup object
$row = $result->fetch_assoc();
$BackupObj->systemdata = $row;
//add field names
$cnt = mysqli_field_count($dbConn);
$foo = array();
for($i=0; $i<$cnt; $i++)
{
$prop = mysqli_fetch_field_direct($result, $i);
$foo[] = $prop->name;
}
$BackupObj->sdatafields = implode(',', $foo);
$BackupObj->sdatacount = $cnt;
// free result set
mysqli_free_result($result);
//-----------------------
//Get player information
//-----------------------
$sql = "SELECT * FROM $tablePlayers";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows <= 0)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyBackupDB: Cannot select $tablePlayers: $err";
WriteToErrorLog($strLog, 'data');
return "MyBackupDB: Cannot select $tablePlayers: $err";
}
//add number of players
$BackupObj->num_players[] = $numrows;
//add field names
$cnt = mysqli_field_count($dbConn);
$foo = array();
for($i=0; $i<$cnt; $i++)
{
$prop = mysqli_fetch_field_direct($result, $i);
$foo[] = $prop->name;
}
$BackupObj->playfields = implode(',', $foo);
$BackupObj->playcount = $cnt;
//add data rows
while($row = $result->fetch_assoc())
{
$BackupObj->players[] = $row;
}
// free result set
mysqli_free_result($result);
// -------------------------------------
// Serialize and write the backup object
// -------------------------------------
$path = $buPath . "/dbtables";
$rc = file_put_contents($path, serialize($BackupObj));
if($rc === FALSE)
{
return "MyBackupDB: Cannot write $path";
}
unset($BackupObj); //free memory to GC
return "MyBackupDB: Backed up to $path";
}
//=================================================================================
function MyRestoreDB($filename)
//=================================================================================
{
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
//backup files location = ../dbasefiles/sheetid_bkup/sheetid#/*
//where # is the day of the week (0 <= # <= 6) 0=Sunday
//susname = sheetid = "tcbc"
//path_buloc = "../dbasefiles/tcbc_bkup"
$buDir = $_SESSION['path_buloc'];
//buPath = "../dbasefiles/tcbc_bkup/tcbc#"
$buPath = $buDir . '/' . $filename;
//path_dbloc = "../dbasefiles/tcbc"
$workingDir = $_SESSION['path_dbloc'];
$rc = LockPlayerFiles(1);
if(file_exists($workingDir))
{
RemoveDir($workingDir); //remove previous working directory
}
mkdir($workingDir); //make new working directory
CopyDir($buPath, $workingDir); //copy files into working directory
$path = $workingDir . '/dbtables'; //remove dbtables file from working dir
unlink($path);
// -------------------------------------
// Unserialize and create backup object
// -------------------------------------
$path = $buPath . '/dbtables';
$RestObj = unserialize(file_get_contents($path));
if($RestObj === FALSE)
{
return "MyRestoreDB: Cannot get $path";
}
//-----------------------
//clear systemdata table
//-----------------------
$dbConn = dbConnect();
$sql = "TRUNCATE TABLE $tableSystemdata";
$result = mysqli_query($dbConn, $sql);
if($result === FALSE)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyRestoreDB: Cannot truncate $tableSystemdata - $err";
WriteToErrorLog($strLog, 'data');
return "MyRestoreDB: Cannot truncate $tableSystemdata - $err";
}
//-------------------------
//Restore systemdata table
//-------------------------
$vars = $RestObj->sdatafields;
$f = explode(",", $vars);
$numFlds = $RestObj->sdatacount;
for($i=0; $i<$numFlds; $i++)
{
if($i == 0)
{
$values = '"';
} else {
$values .= '","';
}
$values .= $RestObj->systemdata[$f[$i]];
}
$values .= '"';
$sql = "INSERT INTO $tableSystemdata ($vars) VALUES ($values)";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRestoreDB: Cannot insert $tableSystemdata - $err";
WriteToErrorLog($strLog, 'data');
return "MyRestoreDB: Cannot insert $tableSystemdata - $err";
}
//-----------------------
//clear players table
//-----------------------
$dbConn = dbConnect();
$sql = "TRUNCATE TABLE $tablePlayers";
$result = mysqli_query($dbConn, $sql);
if($result === FALSE)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyRestoreDB: Cannot truncate $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return "MyRestoreDB: Cannot truncate $tablePlayers - $err";
}
mysqli_close($dbConn);
$dbConn = MyLockTables($tablePlayers, $tableSystemdata);
//-------------------------
//Restore players table
//-------------------------
$vars = $RestObj->playfields;
$f = explode(",", $vars);
$numFlds = $RestObj->playcount;
foreach($RestObj->players as $row)
{
for($i=0; $i<$numFlds; $i++)
{
if($i == 0)
{
$values = '"';
} else {
$values .= '","';
}
$values .= $row[$f[$i]];
}
$values .= '"';
$sql = "INSERT INTO $tablePlayers ($vars) VALUES ($values)";
$rc = mysqli_query($dbConn, $sql);
if($rc === FALSE)
{
$err = mysqli_error($dbConn);
MyUnlockTables($dbConn);
$strLog = "\nMyRestoreDB: Cannot insert $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return "MyRestoreDB: Cannot insert $tablePlayers - $err";
}
}
MyUnlockTables($dbConn);
MyGetPlayerInfo();
return "Restored from $buPath";
}
//=================================================================================
function MyImportV1DB() {
//=================================================================================
global $Players;
global $Status;
global $SUSDBName;
global $SUSNoDays;
global $SUSNoStatus;
global $SUSDaysUsed;
global $DBFilesLocation;
global $dbConn;
$tablePlayers = $_SESSION['mysqlPlayers'];
$tableSystemdata = $_SESSION['mysqlSystemdata'];
$DayName = array("Su ","M ","Tu ","W ","Th ","F ","Sa ");
$NumDBPlayers = ReadPlayerFiles(-1);
//---------------------------------------------------------------
//Special Action ...
//generate MySQL database tables from old form database
//---------------------------------------------------------------
// -------------------------------------
// Sort the player info by pid
// -------------------------------------
$rowName = array();
// create array of player pids in column form
for($i=0; $i<$NumDBPlayers; $i++)
{
$rowName[$i] = $Players[$i]['pid'];
}
// sort the names and reorder players array accordingly
array_multisort($rowName, SORT_ASC, $Players);
// -------------------------------------
// Sort the status info by pid
// -------------------------------------
$rowName = array();
// create array of player pids in column form
for($i=0; $i<$NumDBPlayers+1; $i++)
{
$rowName[$i] = $Status[$i][0]['pid'];
}
// sort the names and reorder players array accordingly
array_multisort($rowName, SORT_ASC, $Status);
//-----------------------
//clear players table
//-----------------------
$dbConn = dbConnect();
$sql = "TRUNCATE TABLE $tablePlayers";
$result = mysqli_query($dbConn, $sql);
if($result === FALSE)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyImportV1DB: Cannot truncate $tablePlayers - $err";
WriteToErrorLog($strLog, 'data');
return "MyImportV1DB: Cannot truncate $tablePlayers - $err";
}
//-----------------------
//clear systemdata table
//-----------------------
$sql = "TRUNCATE TABLE $tableSystemdata";
$result = mysqli_query($dbConn, $sql);
if($result === FALSE)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyImportV1DB: Cannot truncate $tableSystemdata - $err";
WriteToErrorLog($strLog, 'data');
return "MyImportV1DB: Cannot truncate $tableSystemdata - $err";
}
mysqli_close($dbConn);
$dbConn = MyLockTables($tablePlayers, $tableSystemdata);
// ------------------------------
// Generate pid conversion array
// ------------------------------
$ppid = array(); //pid conversion array
$ppid[0] = 0;
$jds = 0;
for($i=0; $i<$NumDBPlayers; $i++)
{
//$ppid[$i+1] = $Players[$i]['pid'];
$ppid[] = $Players[$i]['pid'];
if($jds == 0 && $Players[$i]['jdstart'] > 0)
$jds = $Players[$i]['jdstart'];
}
$cnt = count($ppid);
// -------------------------------------
// Generate the players table
// -------------------------------------
$lastmod = time();
for($i=0; $i<$NumDBPlayers; $i++)
{
$v1 = $i+1;
$v2 = $Players[$i]['super'];
$v3 = $Players[$i]['name'];
//$v4 = $Players[$i]['username'];
$v4 = "";
$v5 = $Players[$i]['login'];
$v6 = $Players[$i]['phone'];
$v7 = $Players[$i]['mobile'];
$v8 = $Players[$i]['ptype'];
$v9 = $Players[$i]['email'];
$v10 = $Players[$i]['notify'];
$v11 = $Players[$i]['contact'];
$v12 = $Players[$i]['playerid'];
$v13 = $Players[$i]['masterpts'];
if(strlen($v13) == 0) $v13 = 0;
$pid = $Players[$i]['pid'];
$s = StatusIndex($pid);
if($s < 0)
{
echo "
Cannot get status index for pid = $pid";
exit;
}
//$s = $i + 1;
//convert pids
$part = $Status[$s][0]['partner'];
if($part != 0)
{
for($j=1; $j<$cnt; $j++)
{
if($ppid[$j] == $part) { $part = $j; }
}
}
$s2 = $part;
$s3 = $Status[$s][0]['posn'];
$s4 = $Status[$s][0]['status'];
$s5 = $Status[$s][0]['prevstatus'];
$s6 = $Status[$s][0]['seq'];
$s7 = $Status[$s][0]['table'];
$s8 = $Status[$s][0]['tableposn'];
for($d=1; $d<$SUSNoStatus; $d++)
{
//convert pids
$part = $Status[$s][$d]['partner'];
if($part != 0)
{
for($j=1; $j<$cnt; $j++)
{
if($ppid[$j] == $part) { $part = $j; }
}
}
$s2 .= ',' . $part;
$s3 .= ',' . $Status[$s][$d]['posn'];
$s4 .= ',' . $Status[$s][$d]['status'];
$s5 .= ',' . $Status[$s][$d]['prevstatus'];
$s6 .= ',' . $Status[$s][$d]['seq'];
$s7 .= ',' . $Status[$s][$d]['table'];
$s8 .= ',' . $Status[$s][$d]['tableposn'];
}
//Generate player's username
if(strlen(strstr($v3, "Guest")) > 0)
{
$v4 = ""; //Guests don't have usernames
} else {
$foo = explode(',', $v3);
$foocnt = count($foo);
$u = 0;
foreach($foo as $str)
{
trim($foo[$u]);
$foo[$u++] = preg_replace("/[^-\w]/", "", $str);
}
if($foocnt > 1)
{
$v4 = $foo[1] . $foo[0]; //$v4 = FirstLast
} else {
$v4 = $foo[0]; //$v4 = FirstLast
}
if(strlen($v4) > 20) //make username lenght 20 or less
{
$v4 = substr($v4, 0, 20);
}
$u = 0;
$foo = $v4;
while(TRUE) //check if username already used
{
$sql = "SELECT * FROM $tablePlayers WHERE username = \"$foo\"";
$result = mysqli_query($dbConn, $sql);
$numrows = $result->num_rows;
if($numrows > 0)
{
if(strlen($v4) > 18) { $v4 = substr($v4, 0, 18); }
$foo = $v4 . "_$u"; //if used add _#
$u++;
} else {
// free result set
mysqli_free_result($result);
break;
}
// free result set
mysqli_free_result($result);
}
$v4 = $foo;
}
//insert player info into players table
$sql = "INSERT INTO $tablePlayers (pid, super, name, username, login, phone, mobile, ptype, email, notify, contact, playerid,
masterpts, partner, posn, status, prevstatus, seq, tablenum, tableposn, last_modified)
VALUES ($v1, $v2, \"$v3\", \"$v4\", '$v5', '$v6', '$v7', $v8, '$v9', $v10, $v11, '$v12', $v13,
'$s2', '$s3', '$s4', '$s5', '$s6', '$s7', '$s8', $lastmod)";
$result = mysqli_query($dbConn, $sql);
if($result === false)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyImportV1DB: Cannot insert player $v3 ($v1) error: $err";
WriteToErrorLog($strLog, 'data');
return "MyImportV1DB: Cannot insert player $v3 ($v1) error: $err";
}
// free result set
mysqli_free_result($result);
}
//------------------------------
//Generate the systemdata table
//------------------------------
$Zero = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
//jdstart, date and dayname
$j = explode(',', $Zero);
$d = explode(',', $Zero);
$n = explode(',', $Zero);
$jd = $jds;
$days = 0;
$jdStart = -1; //jd number of next day of play
$dow = (jddayofweek($jd,0));
// echo "
-------------------";
while($days < $SUSNoDays) { //for each day of play
$dow = (jddayofweek($jd,0));
if($SUSDaysUsed[$dow] == 0) { //day not day of play
$jd++;
continue;
}
$md = jdtogregorian($jd); // returns month/day/year
$md = substr($md,0,strrpos($md,"/")); //get month/day
$j[$days] = $jd;
$d[$days] = $md;
$dn = $DayName[$dow];
$n[$days] = $dn;
$days++;
$jd++;
}
$v1 = implode(',', $j);
$v2 = implode(',', $d);
$v3 = implode(',', $n);
//highseq and blocks
$v4 = $Status[0][0]['seq'];
$v5 = $Status[0][0]['partner'];
for($i=1; $i<$SUSNoStatus; $i++)
{
$v4 .= ',' . $Status[0][$i]['seq'];
$v5 .= ',' . $Status[0][$i]['partner'];
}
$sql = "INSERT INTO $tableSystemdata (row, jdstart, date, day, highseq, signupblock)
VALUES (1, '$v1', '$v2', '$v3', '$v4', '$v5')";
$result = mysqli_query($dbConn, $sql);
if($result === false)
{
$err = mysqli_error($dbConn);
$strLog = "\nMyImportV1DB: Cannot insert $tableSystemdata: error: $err";
WriteToErrorLog($strLog, 'data');
return "MyImportV1DB: Cannot insert $tableSystemdata: error: $err";
}
// free result set
mysqli_free_result($result);
MyUnlockTables($dbConn);
return "V1 DB import completed";
}
//==================PHP End ======================================================== ?>