Advertisement
1amw31rd

ComputerCraft - Serverside sorting system

Jul 7th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.27 KB | None | 0 0
  1. <?php
  2.  
  3. //$GLOBALS['savepoint'] = "test";
  4.  
  5. // Setup error reporting
  6. // All errors are sent to C:/wamp/www/storage.log
  7. ini_set('error_reporting', E_ALL);
  8. error_reporting(E_ALL);
  9. ini_set('log_errors',TRUE);
  10. ini_set('html_errors',FALSE);
  11. ini_set('error_log','storage.log');
  12. ini_set('display_errors',FALSE);
  13.  
  14. // Utility functions
  15. function fail($closeDB=false, $rollbackAndCommit=false)
  16. {
  17.     $mysqlerror = mysql_error();
  18.     if($mysqlerror == "") $mysqlerror="No MySQL error";
  19.    
  20.     ob_start();
  21.     debug_print_backtrace();
  22.     error_log($mysqlerror."\n".ob_get_clean()."-----------");
  23.    
  24.     if($rollbackAndCommit)
  25.     {
  26.         mysql_query("ROLLBACK;");
  27.         mysql_query("COMMIT;");
  28.     }
  29.    
  30.     if($closeDB) mysql_close();
  31.     header(' ', true, 400);
  32.     exit();
  33. }
  34. function mExit($message, $closeDB)
  35. {
  36.     echo $message;
  37.     if($closeDB) mysql_close();
  38.     exit();
  39. }
  40. function openDB()
  41. {
  42.     mysql_connect('localhost','root','','sorting_system') or fail(false);
  43.     mysql_select_db('sorting_system') or fail(false);
  44. }
  45.  
  46. // Storage query functions
  47. function checkSorterIsInited($sorterID)
  48. {
  49.     $result = mysql_query("SELECT sorterID FROM sorters2 WHERE sorterID=$sorterID;");
  50.     if(!$result) return -1;
  51.    
  52.     if(mysql_num_rows($result) > 0) return 1;
  53.     else return 0;
  54. }
  55. function checkIDExists($itemID)
  56. {
  57.     $result = mysql_query("SELECT id FROM items WHERE id=$itemID;");
  58.     if(!$result) return -1;
  59.    
  60.     if(mysql_num_rows($result) > 0) return 1;
  61.     else return 0;
  62. }
  63. function checkNameExists($itemName)
  64. {
  65.     $result = mysql_query("SELECT name FROM items WHERE name='$itemName';");
  66.     if(!$result) return -1;
  67.    
  68.     if(mysql_num_rows($result) > 0) return 1;
  69.     else return 0;
  70. }
  71. function getItemStackSize($itemID)
  72. {
  73.     $result = mysql_query("SELECT stacksize FROM items WHERE id=$itemID;");
  74.     if(!$result) return -1;
  75.    
  76.     $row = mysql_fetch_row($result);
  77.     return $row[0];
  78. }
  79. function checkBankForItem($sorterID, $itemID)
  80. {
  81.     $result = mysql_query("SELECT * FROM sorters2 WHERE sorterID=$sorterID;");
  82.     if(!$result) return -1;
  83.     $row = mysql_fetch_row($result);
  84.    
  85.     for($i=2; $i<4; ++$i)
  86.         if($row[$i] == $itemID)
  87.             if($row[$i+2] < $row[$i+4])
  88.                 return $i-1;
  89.    
  90.     return -2;
  91. }
  92. function otherBanksHaveItem($sorterID, $itemID)
  93. {
  94.     $result = mysql_query("SELECT * FROM sorters2 WHERE sorterID!=$sorterID;");
  95.     if(!$result) return -1;
  96.    
  97.     while ($row = mysql_fetch_row($result))
  98.     {
  99.         for($i=2; $i<4; ++$i)
  100.             if($row[$i] == $itemID)
  101.                 if($row[$i+2] < $row[$i+4])
  102.                     return 1;
  103.     }
  104.    
  105.     return 0;
  106. }
  107. function updateChestCount($sorterID, $chestNum, $amount)
  108. {
  109.     if(!mysql_query("UPDATE sorters2 SET chest".$chestNum."Count=chest".$chestNum."Count+$amount WHERE sorterID=$sorterID;")) return -1;
  110.     return 1;
  111. }
  112. function dropAllIntoChest($sorterID, $chestNum, &$amount)
  113. {
  114.     $result = mysql_query("SELECT * FROM sorters2 WHERE sorterID=$sorterID;");
  115.     if(!$result) return -1;
  116.     $row = mysql_fetch_row($result);
  117.    
  118.     $availableSpace = $row[$chestNum+5] - $row[$chestNum+3];
  119.    
  120.     if($amount > $availableSpace)
  121.     {
  122.         updateChestCount($sorterID, $chestNum, $availableSpace);
  123.         $amount -= $availableSpace;
  124.         return "$availableSpace>$chestNum,";
  125.     }
  126.     else
  127.     {
  128.         updateChestCount($sorterID, $chestNum, $amount);
  129.         $command = "$amount>$chestNum,";
  130.         $amount = 0;
  131.         return $command;
  132.     }
  133. }
  134. function getFirstUnusedChest($sorterID)
  135. {
  136.     $result = mysql_query("SELECT chest1ID, chest2ID FROM sorters2 WHERE sorterID=$sorterID;");
  137.     if(!$result) return -1;
  138.     $row = mysql_fetch_row($result);
  139.    
  140.     for($i=0; $i<2; ++$i)
  141.         if($row[$i] <= 0)
  142.             return $i+1;
  143.    
  144.     return -2;
  145. }
  146. function initChest($sorterID, $chestNum, $itemID)
  147. {
  148.     $result = mysql_query("SELECT chest".$chestNum."Slots FROM sorters2 WHERE sorterID=$sorterID;");
  149.     if(!$result) return -1;
  150.     $row = mysql_fetch_row($result);
  151.  
  152.     if(!mysql_query("UPDATE sorters2 SET chest".$chestNum."ID=$itemID,chest".$chestNum."Max=".($row[0]*getItemStackSize($itemID)).",chest".$chestNum."Count=0 WHERE sorterID=$sorterID;")) return -1;
  153. }
  154. function clearChest($sorterID, $chestNum)
  155. {
  156.     if(!mysql_query("UPDATE sorters2 SET chest".$chestNum."Count=0,chest".$chestNum."ID=0,chest".$chestNum."Max=0 WHERE sorterID=".$sorterID.";")) return -1; return 1;
  157. }
  158. function checkAllOtherChestsForItem($sorterID, $chestNum, $itemID) // Checks every chest in the system other than the selected one for an item
  159. {
  160.     $query = "SELECT * FROM sorters2;";
  161.     $result = mysql_query($query);
  162.     if(!$result) return -1;
  163.    
  164.     while ($row = mysql_fetch_row($result))
  165.     {
  166.         for($i=2; $i<4; ++$i)
  167.         {
  168.             if($sorterID == $row[0] and $i-1==$chestNum)
  169.                 continue;
  170.                
  171.             if($row[$i] == $itemID)
  172.                 if($row[$i+2] < $row[$i+6])
  173.                     return 1;
  174.         }          
  175.     }
  176.    
  177.     return 0;
  178. }
  179.  
  180. // GET functions
  181. function getUserComputer()
  182. {
  183.     openDB();
  184.     $result = mysql_query("SELECT userComp FROM info WHERE row=1;");
  185.     if(!$result) fail(true);
  186.     echo mysql_fetch_row($result)[0];
  187.     mysql_close();
  188. }
  189. function getSorterDirection()
  190. {
  191.     isset($_GET['sID']) or fail();
  192.     openDB();
  193.    
  194.     switch(checkSorterIsInited($_GET['sID']))
  195.     {
  196.         case -1: case 0: fail(true);
  197.     }
  198.    
  199.     $result = mysql_query("SELECT sorterDirection FROM sorters2 WHERE sorterID=".$_GET['sID'].";");
  200.     if(!$result) fail(true);
  201.     $row = mysql_fetch_row($result);
  202.    
  203.     echo $row[0];
  204.     mysql_close();
  205. }
  206. function getStackSizeAndCatigoryByID()
  207. {
  208.     isset($_GET['iID']) or fail();
  209.     openDB();
  210.    
  211.     $size = getItemStackSize($_GET['iID']);
  212.     if($size == -1) fail(true);
  213.     else echo $size;
  214.     echo ",";
  215.     $result = mysql_query("SELECT catigory FROM items WHERE id=".$_GET['iID']);
  216.     if(!$result) fail(true);
  217.     $row = mysql_fetch_row($result);
  218.     if(empty($row[0])) fail(true);
  219.     echo $row[0];
  220.    
  221.     mysql_close();
  222. }
  223. /*maybe dont need*/function getStackSizeByID()
  224. {
  225.     isset($_GET['iID']) or fail();
  226.     openDB();
  227.    
  228.     $size = getItemStackSize($_GET['iID']);
  229.     if($size == -1) fail(true);
  230.     else echo $size;
  231.        
  232.     mysql_close();
  233. }
  234. /*maybe dont need*/ function getCatigoryByID()
  235. {
  236.     isset($_GET['iID']) or fail();
  237.     openDB();
  238.    
  239.     $result = mysql_query("SELECT catigory FROM items WHERE id=".$_GET['iID']);
  240.     if(!$result) fail(true);
  241.     $row = mysql_fetch_row($result);
  242.     if(empty($row[0])) fail(true);
  243.     echo $row[0];
  244.     mysql_close();
  245. }
  246. function getCatigories()
  247. {
  248.     openDB();
  249.    
  250.     $result = mysql_query("SELECT DISTINCT catigory FROM items ORDER BY catigory;");
  251.     if(!$result) fail();
  252.    
  253.     while ($row = mysql_fetch_row($result))
  254.         echo $row[0] . ",";
  255.    
  256.     mysql_close();
  257. }
  258. function listItemsByCatigory()
  259. {
  260.     isset($_GET['catigory']) or fail();
  261.     openDB();
  262.    
  263.     $result = mysql_query("SELECT name FROM items WHERE ((id IN (SELECT chest1ID FROM sorters2 WHERE chest1Count>0)) OR (id IN (SELECT chest2ID FROM sorters2 WHERE chest2Count>0))) AND catigory='".$_GET['catigory']."' ORDER BY name;");
  264.     if(!$result) fail(true);
  265.    
  266.     while ($row = mysql_fetch_row($result))
  267.         echo $row[0] . ",";
  268.    
  269.     mysql_close();
  270. }
  271. function getStacksizeByName()
  272. {
  273.     isset($_GET['name']) or fail();
  274.     openDB();
  275.    
  276.     $result = mysql_query("SELECT stacksize FROM items WHERE name='".$_GET['name']."';");
  277.     if(!$result) fail(true);
  278.    
  279.     $row = mysql_fetch_row($result);
  280.     echo $row[0];
  281.    
  282.     mysql_close();
  283. }
  284. function getItemAmountByName()
  285. {
  286.     isset($_GET['name']) or fail();
  287.     openDB();
  288.    
  289.     $result = mysql_query("SELECT id FROM items WHERE name='".$_GET['name']."';");
  290.     if(!$result) fail(true);
  291.     $row = mysql_fetch_row($result);
  292.     $iID = $row[0];
  293.    
  294.     if(!$iID) fail(true);
  295.    
  296.     $result = mysql_query("SELECT chest1ID,chest2ID,chest1Count,chest2Count FROM sorters2 WHERE chest1ID=$iID OR chest2ID=$iID;");
  297.     if(!$result) fail(true);
  298.    
  299.     $total = 0;
  300.     while ($row = mysql_fetch_row($result))
  301.         for($i=0;$i<2;++$i)
  302.             if($row[$i] == $iID)
  303.                 $total += $row[$i+2];
  304.    
  305.     echo $total;
  306.     mysql_close();
  307. }
  308.  
  309. // POST functions
  310. function addItem()
  311. {
  312.     isset($_POST['iID']) or fail();
  313.     isset($_POST['stacksize']) or fail();
  314.     isset($_POST['name']) or fail();
  315.     isset($_POST['catigory']) or fail();
  316.    
  317.     $meta = 0;
  318.     if(isset($_POST['meta'])) $meta = $_POST['meta'];
  319.     $id = $_POST['iID'] + ($meta * 32768);
  320.  
  321.     openDB();
  322.    
  323.     mysql_query("INSERT INTO items (id, stacksize, name, catigory) VALUES ($id, " . $_POST['stacksize'] . ", '" . $_POST['name'] . "', '".$_POST['catigory']."');") or fail(true);
  324.     echo "Success.";
  325.     mysql_close();
  326. }
  327. function setUserComputer()
  328. {
  329.     openDB();
  330.     if(!mysql_query("UPDATE info SET userComp=".$_POST['cID']." WHERE row=1;"))
  331.         fail(true);
  332.     mysql_close();
  333. }
  334. function initSorter()
  335. {
  336.     $sorterID = $_POST['sID'];
  337.     isset($sorterID) or fail();
  338.     openDB();
  339.    
  340.     switch(checkSorterIsInited($sorterID))
  341.     {
  342.         case -1: fail(true);
  343.         case 1: mExit("Already initialized.", true);
  344.     }
  345.    
  346.     mysql_query("INSERT INTO sorters2 (sorterID,sorterDirection) VALUES ($sorterID,-1);") or fail(true);
  347.     mysql_close();
  348. }
  349. function setChestSizes()
  350. {
  351.     $sorterID = $_POST['sID'];
  352.     isset($sorterID) or fail();
  353.     isset($_POST['chest1']) or fail();
  354.     isset($_POST['chest2']) or fail();
  355.    
  356.     if($_POST['chest1'] == -1 or $_POST['chest2'] == -1) fail();
  357.    
  358.     openDB();
  359.     mysql_query("BEGIN;") or fail(true);
  360.     mysql_query("UPDATE sorters2 SET chest1Slots=".$_POST['chest1'].",chest2Slots=".$_POST['chest2']." WHERE sorterID=$sorterID;") or fail(true, true);
  361.    
  362.     $result = mysql_query("SELECT * FROM sorters2 WHERE sorterID=$sorterID;") or fail(true, true);
  363.     $row = mysql_fetch_row($result);
  364.    
  365.     for($i=2; $i<4; ++$i)
  366.     {
  367.         if($row[$i] > 0 and checkIDExists($row[$i]) == 1)
  368.         {
  369.             $max = $row[$i+6]*getItemStackSize($row[$i]);
  370.             if($max < 0 or !mysql_query("UPDATE sorters2 SET chest".($i-1)."Max=$max WHERE sorterID=$sorterID;"))
  371.                 fail(true, true);
  372.         }  
  373.     }
  374.    
  375.     mysql_query("COMMIT;") or fail(true, true);
  376.     mysql_close();
  377. }
  378. function setSorterDirection()
  379. {
  380.     $dir = $_POST['dir'];
  381.  
  382.     isset($_POST['sID']) or fail();
  383.     isset($dir) or fail();
  384.     openDB();
  385.    
  386.     switch(checkSorterIsInited($_POST['sID']))
  387.     {
  388.         case -1: case 0: fail(true);
  389.     }
  390.    
  391.     if($dir > 3 or $dir < 0)
  392.         fail(true);
  393.    
  394.     mysql_query("UPDATE sorters2 SET sorterDirection=\"$dir\" WHERE sorterID=".$_POST['sID'].";") or fail(true);
  395.     mysql_close();
  396. }
  397. function recieveItem()
  398. {
  399.     $sorterID = $_POST['sID'];
  400.     $itemID = $_POST['iID'];
  401.     $amount = $_POST['amt'];
  402.     isset($sorterID) or fail();
  403.     isset($itemID) or fail();
  404.     isset($amount) or fail();
  405.    
  406.     openDB();
  407.     mysql_query("BEGIN;") or fail(true);
  408.    
  409.     if(checkIDExists($itemID) != 1) fail(true, true);
  410.     if(checkSorterIsInited($sorterID) != 1) fail(true, true);
  411.    
  412.     $i=0;
  413.     for(;$i<10;++$i)
  414.     {
  415.         $chest = checkBankForItem($sorterID, $itemID);
  416.         if($chest == -1)
  417.         {
  418.             fail(true, true);
  419.         }
  420.         elseif($chest > 0)
  421.         {
  422.             $command = dropAllIntoChest($sorterID, $chest, $amount);
  423.            
  424.             if($command == -1)
  425.                 fail(true, true);
  426.             else
  427.                 echo $command;
  428.         }
  429.         else
  430.         {
  431.             $otherBanks = otherBanksHaveItem($sorterID, $itemID);
  432.             if($otherBanks == -1)
  433.             {
  434.                 fail(true, true);
  435.             }
  436.             else if($otherBanks == 1)
  437.             {
  438.                 echo "SENDNEXT,";
  439.                 break;
  440.             }  
  441.             else
  442.             {
  443.                 $firstEmpty = getFirstUnusedChest($sorterID);
  444.                 if($firstEmpty == -1)
  445.                 {
  446.                     fail(true, true);
  447.                 }
  448.                 else if($firstEmpty == -2)
  449.                 {
  450.                     echo "SENDNEXT,";
  451.                     break;
  452.                 }  
  453.                 else
  454.                 {
  455.                     if(initChest($sorterID, $firstEmpty, $itemID) == -1)
  456.                         fail(true, true);
  457.                    
  458.                     $command = dropAllIntoChest($sorterID, $firstEmpty, $amount);
  459.                    
  460.                     if($command == -1)
  461.                         fail(true, true);
  462.                     else
  463.                         echo $command;
  464.                 }
  465.             }
  466.         }
  467.        
  468.         if($amount <= 0)
  469.             break;
  470.     }
  471.    
  472.     if($i>=9) fail(true, true);
  473.    
  474.     mysql_query("COMMIT;") or fail(true, true);
  475.     mysql_close();
  476. }
  477. function pullItem()
  478. {
  479.     isset($_POST['name']) or fail();
  480.     isset($_POST['amt']) or fail();
  481.    
  482.     openDB();
  483.     mysql_query("BEGIN;") or fail(true);
  484.    
  485.     $nameExists = checkNameExists($_POST['name']);
  486.     if($nameExists == -1) fail(true, true);
  487.     elseif($nameExists == 0) fail(true, true);
  488.    
  489.     $result = mysql_query("SELECT id FROM items WHERE name='".$_POST['name']."';");
  490.     if(!$result) fail(true, true);
  491.     $row = mysql_fetch_row($result);
  492.     $iID = $row[0];
  493.    
  494.     $result = mysql_query("SELECT * FROM sorters2 WHERE chest1ID=$iID OR chest2ID=$iID;");
  495.     if(!$result) fail(true, true);
  496.    
  497.     $amount_left = $_POST['amt'];
  498.    
  499.     while ($row = mysql_fetch_row($result))
  500.     {
  501.         for($i=2;$i<4;++$i)
  502.         {
  503.             $chestNum = $i-1;
  504.            
  505.             if($row[$i] == $iID)
  506.             {
  507.                 $count = $row[$i+2];
  508.                
  509.                 if($count <= 0)
  510.                 {
  511.                     continue;
  512.                 }
  513.                 elseif($count <= $amount_left)
  514.                 {
  515.                     if(updateChestCount($row[0], $chestNum, -$count) == -1)
  516.                     {
  517.                         fail(true, true);
  518.                     }
  519.                     else
  520.                     {
  521.                         echo "PULL ".$row[0]." $chestNum $iID $count,";
  522.                        
  523.                         if(checkAllOtherChestsForItem($row[0], $chestNum, $iID)==1)
  524.                             //setChestCountToZero($row[0], $chestNum);
  525.                         //else
  526.                             clearChest($row[0], $chestNum);
  527.                     }
  528.                    
  529.                     $amount_left -= $count;
  530.                 }
  531.                 else
  532.                 {
  533.                     if(updateChestCount($row[0], $chestNum, -$amount_left) == -1)
  534.                         fail(true, true);
  535.                     else
  536.                         echo "PULL ".$row[0]." $chestNum $iID $amount_left,";
  537.                    
  538.                     $amount_left = 0;
  539.                 }
  540.             }
  541.            
  542.             if($amount_left <= 0) break;
  543.         }
  544.        
  545.         if($amount_left <= 0) break;
  546.     }
  547.  
  548.     //if($amount_left > 0)
  549.     //  $lua_command .= "NOTIFY not_enough";
  550.    
  551.     mysql_query("COMMIT;") or fail(true, true);
  552.     mysql_close();
  553. }
  554.  
  555. if(isset($_GET['func']))
  556.     $_GET['func']();
  557. else
  558.     fail();
  559.    
  560. //mysql_connect('localhost','root','','sorting_system') or fail(false);
  561. //mysql_select_db('sorting_system') or fail(false);
  562. //echo updateChestCount(1, 1, 3); echo mysql_error();
  563. //mysql_close();
  564.  
  565. //mysql_query("BEGIN;"); echo mysql_error()."<br>";
  566. //mysql_query("SAVEPOINT aidan"); echo mysql_error()."<br>";
  567. //updateChestCount(1, 1, 3);
  568. //if(isset($_GET['rollback']))
  569. //  mysql_query("ROLLBACK;"); echo mysql_error()."<br>";
  570. //mysql_query("COMMIT;"); echo mysql_error()."<br>";
  571. //mysql_close();
  572. //mysql_query("ROLLBACK TO SAVEPOINT aidan"); echo mysql_error()."<br>";
  573. //mysql_query("COMMIT"); echo mysql_error()."<br>";
  574.  
  575. //
  576.  
  577. //$id = mysql_connect('localhost','root','','sorting_system') or fail(false);
  578. //mysql_select_db('sorting_system') or fail(false);
  579.  
  580. //openDB();
  581.  
  582. //updateChestCount(1, 1, 3);
  583. //echo $id;
  584. //mysqli_rollback($id);
  585.  
  586. //echo mysql_query("SAVEPOINT aidansave");
  587. //echo "<br>";
  588. //echo mysql_error();
  589. //echo "<br>";
  590. //
  591. //sleep(5);
  592. //echo "<br>";
  593. //echo mysql_query("ROLLBACK TO SAVEPOINT aidansave");//("SAVEPOINT start");
  594. //echo "<br>";
  595. //echo mysql_error();
  596.  
  597. // function test()
  598. // {
  599.     // echo $GLOBALS['savepoint'];
  600. // }
  601. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement