/** * Make a service call to the remote BinaryBeast API * * @param string $svc Service to call (ie Tourney.TourneyCreate.Create) * @param array $args Arguments to send * @param int $ttl If you configured the BBCache class, use this to define how many minutes this result should be cached * @param int $object_type For caching objects: see BBCache::type_ constants * @param mixed $object_id For caching objects: The id of this the object, like tourney_id or tourney_team_id * * @return object */ public function call($svc, $args = null, $ttl = null, $object_type = null, $object_id = null) { //Use the new library to make the actual call, pass the request directly $result = $this->bb->call($svc, $args, $ttl, $object_type, $object_id); /** * Append an api_version_warning in hopes that developers * will update their code to use the new object oriented functionalty */ $result->api_version_warning = 'You are using the old legacy wrapper method "' . $svc . '",' . 'please consider updating your code to take advantage of the new object-oriented functionality of the new API library. ' . 'Check out our github pages for examples / tutorials: https://github.com/BinaryBeast/BinaryBeast_API_PHP'; return $result; }
/** * Clears ALL cache associated with this object_type * For example calling this against a BBTournament, will delete * ALL cache for EVERY tournament in your database */ public function clear_object_cache() { $object_type = $this->get_cache_setting('object_type'); if (!is_null($object_type)) { $this->bb->clear_cache(null, $object_type); } }
/** */ public function test_error_history() { $this->object->set_error('Custom Error'); $key = sizeof($this->object->error_history) - 1; $this->assertTrue(is_object($this->object->error_history[$key])); $this->assertEquals('Custom Error', $this->object->error_history[$key]->error_message); }
/** * Attempt to extract data sent from a BinaryBeast callback * * <br /> * This method will try to extract the data from $_POST and $_GET, and then * process the data further based on the event_id * * <br /><br /> * For example if a tourney_info array is provided, you'll get a full {@link BBTournament} instance * * * @return BBCallbackHandlerObject|boolean * <b>FALSE</b> returned if unable to find any relevent data in $_POST and $_GET * * <br /><br /> * Refer to the properties documented in {@link BBCallbackObject} for values that you can expect * * <br /><br /> * In additon to the standard values in {@link BBCallbackObject}, each event may return data that this method will convert into <b>model objects</b><br/ > * For example, if the callback sends a tourney_info array, you can expect <var>$tournament</var> to be a {@link BBTournament} model object * * <br /><br /> * Events that expect these values are documented in each event_id constant in this class */ public function handle_callback() { //Prepare the object we'll be returning $data = new stdClass(); //Figure out if we should use $_POST or $_GET, then save a refernece to $request $request = null; if (isset($_POST['callback_id'])) { $request =& $_POST; } else { if (isset($_GET['callback_id'])) { $request =& $_GET; } else { return $this->bb->set_error('Unable to locate a "callback_id" value in either $_POST or $_GET'); } } //All standard callback values must be present $keys = array('callback_id', 'event_id', 'event_description', 'event_utc_date'); foreach ($keys as $key) { if (!isset($request[$key])) { return $this->bb->set_error('Unable to locate a "' . $key . '" value, unable to process this as a valid callback', 'BBCallback'); } $data->{$key} = $request[$key]; } //Try to extract a tournament if (isset($request['tourney_info'])) { $data->tournament = $this->bb->tournament($request); } else { if (strpos($request['trigger_id'], 'x') === 0) { $data->tournament = $this->bb->tournament($request['trigger_id']); } else { if (isset($request['tourney_id'])) { $data->tournament = $this->bb->tournament($request['tourney_id']); } } } //Try to extract a match if (isset($request['match_info'])) { if (isset($request['games'])) { $data->match = $this->bb->match($request); } } else { if (isset($request['tourney_match_id'])) { $data->match = $this->bb->match($request['tourney_match_id']); } } //Try to extract a team if (isset($request['team_info'])) { $data->team = $this->bb->team($request); } else { if (isset($request['tourney_team_id'])) { $data->team = $this->bb->team($request['tourney_team_id']); } } //Success! return $data; }
/** * @group clear_expired_cache */ public function test_clear_expired() { //Delete all tournament cache to avoid conflicts with previous tests $this->assertTrue($this->bb->clear_cache(null, BBCache::TYPE_TOURNAMENT)); //Cache a tournament, specify negative ttl to auto-expire $result = $this->object->call('Tourney.TourneyLoad.Info', array('tourney_id' => 'xQL1302101'), -1, BBCache::TYPE_TOURNAMENT, 'xQL1302101'); $this->assertServiceSuccessful($result); //Cache a second tour, with normal cache ttl $result = $this->object->call('Tourney.TourneyLoad.Info', array('tourney_id' => 'xSC213021613'), 2, BBCache::TYPE_TOURNAMENT, 'xSC213021613'); $this->assertServiceSuccessful($result); //clear expired cache $this->assertTrue($this->object->clear_expired_cache()); //Reload tour 1 - shouldn't be cached $result = $this->object->call('Tourney.TourneyLoad.Info', array('tourney_id' => 'xQL1302101'), -1, BBCache::TYPE_TOURNAMENT, 'xQL1302101'); $this->assertServiceNotLoadedFromCache($result); //Reload tour 2 - should be cached $result = $this->object->call('Tourney.TourneyLoad.Info', array('tourney_id' => 'xSC213021613'), 2, BBCache::TYPE_TOURNAMENT, 'xSC213021613'); $this->assertServiceLoadedFromCache($result); }
/** * Can be used in place of $bb->call, this method will check the local * cache table for any results from previous identical calls * * It does not match arguments, but it matches tourney_id or tourney_team_id with the service * * @param string $svc * @param array $args * @param int $ttl In minutes, how long to keep the result as valid * @param int $object_type Tournament, game, etc - use BBCache::TYPE_ constants for values * @param int|string $object_id * * @return boolean */ public function call($svc, $args = null, $ttl = null, $object_type = null, $object_id = null) { //Build the WHERE clause to try to find a cacheed response in the local database $where = $this->build_where($svc, $object_type, $object_id); //First step - try to find an already cached response - if expired, remember the ID and we'll update later $id = null; $result = $this->db->query("\n SELECT id, result, TIMESTAMPDIFF(MINUTE, UTC_TIMESTAMP(), expires) AS minutes_remaining\n FROM {$this->config->cache_db_table}\n {$where}\n "); //Found it! is ist still valid?? if ($result->rowCount() > 0) { $row = $result->fetchObject(); //Success! if (intval($row->minutes_remaining) > 0) { //Add a value "from_cache" just FYI $result = $this->decompress($row->result); $result->from_cache = true; return $result; } else { $id = $row->id; } } //We don't have a valid cached response, call the API now $api_result = $this->bb->call($svc, $args); //Compress the result into a string we can save in the database $result_compressed = $this->compress($api_result); //If null, convert to string 'NULL' for database, otherwise surround with quores $object_type = is_null($object_type) ? 'NULL' : $object_type; $object_id = is_null($object_id) ? 'NULL' : "'{$object_id}'"; //If we have an id, update it now if (!is_null($id)) { $this->update($id, $result_compressed, $ttl); } else { $this->insert($svc, $object_type, $object_id, $ttl, $result_compressed); } //Return the direct result from $bb return $api_result; }
<?php /** * Example demonstrating how to load a list of maps available for StarCraft 2 * * @package BinaryBeast * @subpackage Examples */ require '../../BinaryBeast.php'; $bb = new BinaryBeast(); $bb->disable_ssl_verification(); $maps = $bb->map->game_list('SC2'); foreach ($maps as $map) { echo $map->map . ' (' . $map->map_id . ') <br />'; }
/** * Pseudo-static constructor, this method only once, * only the first time this class is instantiated * * It's used to pre-load some of the core library classes that * we are most likely to use - most other libraries (for example BBLegacy, BBTournament), * are only loaded as they are used * * @param BinaryBeast $bb Since it's a static method, we need a reference to the instance * * @return void */ private static function init(&$bb) { $bb->load_library('BBConfiguration'); $bb->load_library('BBHelper'); $bb->load_library('BBSimpleModel'); $bb->load_library('BBModel'); $bb->load_library('BBCache'); $bb->load_library('BBCallback'); $bb->load_library('BBDev'); //Next instantiation will know not to call this method self::$first = false; }
/** * Creates an active double elimination bracket to keeps other examples DRY * * @filesource * * @package BinaryBeast * @subpackage Examples * * @version 1.0.0 * @date 2013-04-13 * @since 2013-04-13 * @author Brandon Simmons <*****@*****.**> */ $path = str_replace('\\', '/', dirname(__FILE__)) . '/../../BinaryBeast.php'; require_once $path; $bb = new BinaryBeast(); $bb->disable_ssl_verification(); /* * First - create a tournament with brackets */ $tournament = $bb->tournament(); $tournament->title = 'API Demo - Brackets'; $tournament->description = 'Simple API PHP Library demonstration - elimination only'; $tournament->elimination = BinaryBeast::ELIMINATION_DOUBLE; // for ($x = 0; $x < 16; $x++) { $team = $tournament->team(); $team->confirm(); $team->display_name = 'Demo Player ' . ($x + 1); } //
* * @filesource * * @global BBTournament $tournament * * @package BinaryBeast * @subpackage Examples * * @version 1.0.1 * @date 2013-04-13 * @author Brandon Simmons <*****@*****.**> */ //May be set from another example if (!isset($bb)) { require '../../../BinaryBeast.php'; $bb = new BinaryBeast(); $bb->disable_ssl_verification(); } /** * Process the request */ if (isset($_POST['id'])) { $tournament = $bb->tournament($_POST['id']); if (!$tournament->delete()) { var_dump(array('Error deleting tournament', 'errors' => $bb->error_history)); die; } else { die('<h1 style="color:red">Tournament (' . $_POST['id'] . ') deleted successfully!</h1>'); } } /**
<?php /** * Example script for loading a list of popular tournaments * * @filesource * * @package BinaryBeast * @subpackage Examples * * @version 1.0.0 * @date 2013-05-14 * @since 2013-05-14 * @author Brandon Simmons <*****@*****.**> */ $path = str_replace('\\', '/', dirname(__FILE__)) . '/../../../BinaryBeast.php'; require_once $path; $bb = new BinaryBeast(); $bb->enable_dev_mode(); $bb->disable_ssl_verification(); $tournaments = $bb->tournament->list_popular(); foreach ($tournaments as $tournament) { echo '<a href="' . $tournament->url . '">' . $tournament->title . '</a><br />'; }
/** * Returns a brand new unsaved tournament * @return BBTournament */ protected function get_tournament_new() { $this->tournament = $this->bb->tournament(); self::$tournaments[] =& $this->tournament; return $this->tournament; }