Tyler_Elric

Adoptables API 0.1

Sep 15th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.41 KB | None | 0 0
  1. <?php
  2. /*
  3. Adoptables Database Layout:
  4.  
  5. -Languages
  6. --ID
  7. --Abbreviation
  8. --Name
  9.  
  10. -Species
  11. --ID
  12. --Base Value
  13.  
  14. -EvolutionNodes
  15. --ID
  16. --Species.ID #<- old species
  17. --Species.ID #<- new species
  18. --Level
  19. --Items.ID
  20. --Gender
  21.  
  22. -Evolutions #Keep track of evolutions?
  23. --Adoptables.ID
  24. --EvolutionNodes.ID
  25. --Time/Date of evolution.
  26.  
  27. -Items
  28. --ID
  29. --Base Value
  30. --Item_Types.ID# Potion, click packages, etc.
  31. --Strength #Potion = 200, Hyperpotion=1000 or whatever.
  32.  
  33. -Species Names
  34. --Species.ID
  35. --Languages.ID
  36. --Name
  37.  
  38. -Users
  39. --ID
  40. #Do some fancy-schmancy stuff that links to the vBulletin database.
  41.  
  42. -Adoptables
  43. --ID
  44. --Users.ID
  45. --Species.ID
  46. --Date Adopted
  47. --Name
  48. --EXP Points #number of clicks.
  49. --Level
  50. --Gender
  51.  
  52. ** BTW, all of this belongs in a folder /api
  53. ** or have a sub-domain. "api.adopt.thepokemonboard.net/<REST_PATH>"
  54. */
  55. ?>
  56.  
  57. ========================================================
  58. -------------Rest.php-----------------------------------
  59. ========================================================
  60. <?php
  61.     function RestAPI($mapped_functions){
  62.         if(array_key_exists($mapped_functions,$_SERVER['REQUEST_METHOD']))
  63.             $mapped_functions[$_SERVER['REQUEST_METHOD']]();
  64.         throw new Exception("Unsupported REQUEST.Method encountered.");
  65.     }
  66. ?>
  67. ========================================================
  68.  
  69. ========================================================
  70. --------------DB_Connect.php----------------------------
  71. ========================================================
  72. <?php
  73.     function Connect($db_name){
  74.         $con = mysql_connect("localhost","user","password");
  75.         if(!$con)
  76.             throw new Exception('Could not connect: ' . mysql_error())
  77.         mysql_select_db($db_name,$con);
  78.         return $con;
  79.     }
  80. ?>
  81. ========================================================
  82.  
  83. ========================================================
  84. --------------user.php----------------------------------
  85. ========================================================
  86. <?php
  87.     include("rest.php");
  88.     include("db_connect.php");
  89.     function GetUser()
  90.     {
  91.         $con = Connect("vBulletin");
  92.         //Obtain user information here.
  93.         $user = array('id'=>5);
  94.         if($user==null)
  95.             throw new Exception("No user logged in.");
  96.         mysql_close($con);
  97.         return json_encode($user);
  98.     }
  99.     function Users()
  100.     {
  101.         try {
  102.             echo RestAPI(array("GET"=>display_adoptable,"POST"=>new_adoptable));
  103.         } catch(Exception $e) {
  104.             die($e->getMessage());
  105.         }
  106.     }
  107. ?>
  108. ========================================================
  109.  
  110. ========================================================
  111. --------------Adoptables.php----------------------------
  112. ========================================================
  113. <?php
  114.     include("rest.php");
  115.     include("db_connect.php");
  116.     include("user.php");.
  117.     $adopt_db_name="Adoptables";
  118.     function NewAdoptable(){
  119.         $user=json_decode(GetUser())['id'];
  120.         $con = Connect($adopt_db_name);
  121.         $adopt_name=isset($_POST['name'])?$_POST['name']:null;
  122.         $adopt_species=$_POST['species'];
  123.         if($adopt_species=="")
  124.             throw new Exception("A species is required.");
  125.     }
  126.     function GetAdoptable(){
  127.         $con = Connect($adopt_db_name);
  128.         $lang = isset($_GET['lang'])?$_GET['lang']:'0';
  129.         $query =
  130.         'Select
  131.             adopt.id as \'ID\',
  132.             adopt.user_id as \'userID\',
  133.             (select species_names.name from species_names where species_names.species_id==adopt.id AND species_names.language_id=='.$lang.') as \'species\'
  134.             coalesce(
  135.              adopt.name,
  136.              (select species_names.name from species_names where species_names.species_id==adopt.id AND species_names.language_id=='.$lang.')
  137.             ) as \'name\',
  138.             adopt.gender as \'gender\',
  139.             adopt.level as \'level\',
  140.             adopt.exp*20 as \'clicks\',
  141.             adopt.created as \'creation\'
  142.          from adoptables as adopt
  143.         ';
  144.         if(isset($_GET['id'])){
  145.             $query = $query . "\n where adopt.id==$_GET['id'] limit 0,1";
  146.         }
  147.         $num_results = 0;
  148.         $query_result = mysql_query($query);
  149.         $return = array();
  150.         while($row = mysql_fetch_array($query_result))
  151.         {
  152.             $num_results+=1;
  153.             array_push($return,array(
  154.                 'id' => $row['ID'],
  155.                 'user' => $row['userID'],
  156.                 'species' => $row['species'],
  157.                 'name' => $row['name'],
  158.                 'gender' => $row['gender'],
  159.                 'exp' => $row['clicks'],
  160.                 'created' => $row['created']
  161.             ));
  162.         }
  163.         if($num_results>0)
  164.             return json_encode($return);
  165.         else
  166.             throw new Exception("Couldn't find any adoptables".((isset($_GET['id']))?('by the ID of '.$_GET['id']):"").".");
  167.     }
  168.     function Adoptable(){
  169.         try {
  170.             echo RestAPI(array("GET"=>"GetAdoptable","POST"=>"NewAdoptable"));
  171.         } catch(Exception $e) {
  172.             die($e->getMessage());
  173.         }
  174.     }
  175. ?>
  176. ========================================================
Advertisement
Add Comment
Please, Sign In to add comment