Advertisement
Fwaky

Untitled

Dec 25th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <?php
  2. ini_set('max_execution_time', 800);
  3. // This file updates EXP highscores only.
  4. // Other highscores are saved separatly, at diffrent times to even server load.
  5. include("../config.php");
  6. include("../library/tibiaparser.class.php");
  7. include("../library/mysql.db.class.php");
  8. function microtime_float()
  9. {
  10.     list($usec, $sec) = explode(" ", microtime());
  11.     return ((float)$usec + (float)$sec);
  12. }
  13.  
  14. $time_start = microtime_float();
  15. // Pending
  16. // Last update: 2013-06-19 16:57 CEST
  17. // Set database object
  18. $updated_worlds = array();
  19. global $db;
  20. $db = new database($db_host, $db_name, $db_user, $db_pass);
  21. // Updates Tibia Worlds based on Tibia.com
  22. $tc = new TibiaDotCom();
  23. // First we get all worlds
  24. $date = date('Y-m-d');
  25. /*
  26. $db->query("SELECT name, id FROM worlds WHERE name = :name LIMIT 0,1");
  27. $db->bind(":name", "Olympa");*/
  28. $db->query("SELECT name, id FROM worlds WHERE experience_updated != :updated LIMIT 0, 8");
  29. $db->bind(":updated", $date);
  30.  
  31. $worlds = $db->resultSet();
  32.     // Update each worlds experience.
  33. foreach($worlds as $w){
  34.     $worldid = $w["id"];
  35.     $world   = $w["name"];
  36.     $updated_worlds[] = $world;
  37.     for($i = 0; $i < 12; $i++){ // Loop through all pages for each world
  38.         $highscore = $tc->getHighscores($world, "Experience", $i);
  39.         foreach($highscore as $h){
  40.             $db->query("SELECT id FROM characters WHERE name = :name");
  41.                 // Bind
  42.                 $db->bind(":name", $h["name"]);
  43.             $check = $db->single();
  44.             $date = date("Y-m-d");
  45.             if(empty($check)){
  46.                 // Char doesn't exist
  47.                 $db->query("INSERT INTO characters (name, world, level, exp_updated) VALUES(:name, :world, :level, :date)");
  48.                     // Bind
  49.                     $db->bind(":name", $h["name"]);
  50.                     $db->bind(":world", $worldid);
  51.                     $db->bind(":level", $h["level"]);
  52.                     $db->bind(":date", $date);
  53.                 // Execute and get ID
  54.                 $db->execute();
  55.                 $charid = $db->lastInsertId();
  56.             } else {
  57.                 // Char exists
  58.                 $charid = $check["id"];
  59.                     $db->query("UPDATE characters SET level = :level, exp_updated = :date WHERE name = :name");
  60.                     $db->bind(":level", $h["level"]);
  61.                     $db->bind(":name", $h["name"]);
  62.                     $db->bind(":date", $date);
  63.                     $db->execute();
  64.             }
  65.             // Check if character has been updated today
  66.                 $db->query("SELECT * FROM exp_history WHERE date = :date AND charid = :charid");
  67.                     $db->bind(":date", $date);
  68.                     $db->bind(":charid", $charid);
  69.                 $updated_check = $db->resultset();
  70.             // No record for today, save the current record.
  71.             if(count($updated_check) == 0){
  72.                 // Insert EXP record into history.
  73.                 $db->query("INSERT INTO exp_history (charid, experience, date, level) VALUES(:charid, :experience, :date, :level)");
  74.                     // Bind
  75.                     $db->bind(":charid", $charid);
  76.                     $db->bind(":level", $h["level"]);
  77.                     $db->bind(":experience", $h["value"]);
  78.                    
  79.                     $db->bind(":date", $date);
  80.                 // execute
  81.                 $db->execute();
  82.             }
  83.         }
  84.     }
  85.     $db->query("UPDATE worlds SET experience_updated = :date WHERE id = :id");
  86.     $db->bind(":date", $date);
  87.     $db->bind(":id", $worldid);
  88.     $db->execute();
  89. }
  90. $time_end = microtime_float();
  91. $time = $time_end - $time_start;
  92. echo 'It took: '.$time.'s to update '.count($updated_worlds).' worlds.';
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement