Skip to content

Commit

Permalink
#57 Add Items request support
Browse files Browse the repository at this point in the history
  • Loading branch information
kronusme committed Jul 4, 2014
1 parent 4f0269e commit 61be2fc
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 65 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ First of all you need web-server with **PHP 5.3+** ( **PDO** and **cURL** should
| GetLiveLeagueGames | https://api.steampowered.com/IDOTA2Match_570/GetLiveLeagueGames/v0001/ |
| GetTeamInfoByTeamID | https://api.steampowered.com/IDOTA2Match_570/GetTeamInfoByTeamID/v001/ |
| GetHeroes | https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0001/ |
| GetTournamentPrizePool | https://api.steampowered.com/IEconDOTA2_570/GetTournamentPrizePool/v1/ |
| GetTournamentPrizePool | https://api.steampowered.com/IEconDOTA2_570/GetTournamentPrizePool/v1/ |
| GetGameItems | https://api.steampowered.com/IEconDOTA2_570/GetGameItems/v0001/ |
| **Unsupported** | |
| EconomySchema | https://api.steampowered.com/IEconItems_570/GetSchema/v0001/ |
| GetMatchHistoryBySequenceNum | https://api.steampowered.com/IDOTA2Match_570/GetMatchHistoryBySequenceNum/v0001/ |
Expand Down Expand Up @@ -133,6 +134,52 @@ print_r($heroes);
````
$heroes - array with numeric indexes (heroes ids)

#### Get current items list
````php
<?php
require_once ('config.php');
$items_mapper_web = new items_mapper_web();
$items_info = $items_mapper_web->load();
print_r($items_info);
foreach($items_info as $item) {
echo $item->get('id');
echo $item->get('name');
echo $item->get('cost');
echo $item->get('secret_shop');
echo $item->get('side_shop');
echo $item->get('recipe');
echo $item->get('localized_name');
}
````

#### Save received from web items list to db
````php
<?php
require_once ('config.php');
$items_mapper_web = new items_mapper_web();
$items = $items_mapper_web->load();
$items_mapper_db = new items_mapper_db();
$items_mapper_db->save($items);
````

#### Get current items list from db
````php
<?php
require_once ('config.php');
$items_mapper_db = new items_mapper_db();
$items_info = $items_mapper_db->load();
print_r($items_info);
foreach($items_info as $item) {
echo $item->get('id');
echo $item->get('name');
echo $item->get('cost');
echo $item->get('secret_shop');
echo $item->get('side_shop');
echo $item->get('recipe');
echo $item->get('localized_name');
}
````

#### Get leagues list
````php
<?php
Expand Down
411 changes: 351 additions & 60 deletions db_latest.sql

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions includes/mappers/class.items_mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

abstract class items_mapper {

}
48 changes: 48 additions & 0 deletions includes/mappers/class.items_mapper_db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Load info about in game items from db
*
* @example
* <code>
* $items_mapper_db = new items_mapper_db();
* $items_info = $items_mapper_db->load();
* print_r($items_info);
* foreach($items_info as $item) {
* echo $item->get('id');
* echo $item->get('name');
* echo $item->get('cost');
* echo $item->get('secret_shop');
* echo $item->get('side_shop');
* echo $item->get('recipe');
* echo $item->get('localized_name');
* }
* </code>
*/
class items_mapper_db extends items_mapper {

public function load() {
$db = db::obtain();
$items = array();
$items_info = $db->fetch_array_pdo('SELECT * FROM '.db::real_tablename('items'));
foreach($items_info as $item_info) {
$item = new item();
$item->set_array($item_info);
$items[$item_info['id']] = $item;
}
return $items;
}

/**
* @param item[] $data list of items
*/
public function save(array $data) {
$db = db::obtain();
$data_to_insert = array();
foreach($data as $item) {
array_push($data_to_insert, $item->get_data_array());
}
$db->insert_many_pdo(db::real_tablename('items'), array('id', 'name', 'cost', 'secret_shop', 'side_shop', 'recipe', 'localized_name'), $data_to_insert, true);
}

}
58 changes: 58 additions & 0 deletions includes/mappers/class.items_mapper_web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Load info about in game items
*
* @example
* <code>
* $items_mapper_web = new items_mapper_web();
* $items_info = $items_mapper_web->load();
* print_r($items_info);
* foreach($items_info as $item) {
* echo $item->get('id');
* echo $item->get('name');
* echo $item->get('cost');
* echo $item->get('secret_shop');
* echo $item->get('side_shop');
* echo $item->get('recipe');
* echo $item->get('localized_name');
* }
* </code>
*/
class items_mapper_web extends items_mapper {

/**
* Request url
*/
const items_steam_url = 'https://api.steampowered.com/IEconDOTA2_570/GetGameItems/v0001/';

public function __construct(){}

/**
* @return array
*/
public function load() {
$request = new request(
self::items_steam_url,
array()
);
$response = $request->send();
if (is_null($response)) {
return null;
}
$items_info = (array)($response->items);
$items_info = $items_info['item'];
$items = array();
foreach($items_info as $item_info) {
$info = (array)$item_info;
array_walk($info, function (&$v) {
$v = (string)$v;
});
$item = new item();
$item->set_array($info);
$items[$info['id']] = $item;
}
return $items;
}

}
5 changes: 2 additions & 3 deletions includes/mappers/class.player_mapper_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function load($id = null) {
$player->set_array($result);
return $player;
}

/**
* Determines whether the player should be inserted or updated in the db
* @param player object
Expand Down Expand Up @@ -77,5 +77,4 @@ public static function player_exists($id = null) {
}
return false;
}
}
?>
}
56 changes: 56 additions & 0 deletions includes/models/class.item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

class item extends stat_object {

/**
* the id of the item
* @var int
*/
protected $_id;
/**
* the code name of the item (eg "item_blink")
* @var string
*/
protected $_name;
/**
* the gold cost of the item
* @var int
*/
protected $_cost;
/**
* 1 if the item is bought from the secret shop, 0 otherwise
* @var bool
*/
protected $_secret_shop;
/**
* 1 if the item can be bought from the side shops, 0 otherwise
* @var bool
*/
protected $_side_shop;
/**
* 1 if the item is a recipe, 0 otherwise
* @var bool
*/
protected $_recipe;
/**
* the name of the item in the specified language (missing if no language specified)
* @var string
*/
protected $_localized_name;

public function set($name, $value) {
if ($name === 'name') {
$value = 'item_'.str_replace('item_', '', $value);
}
return parent::set($name, $value);
}

public function get($name) {
$value = parent::get($name);
if ($name === 'name') {
$value = str_replace('item_', '', $value);
}
return $value;
}

}
12 changes: 11 additions & 1 deletion includes/utils/class.db.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ public function update_pdo($table, $data, $where=array()){
* @param string $table
* @param array $fields array of fields names
* @param array $data array of array of data
* @param bool $update should data be updated on duplicate
* @return bool
*/
public function insert_many_pdo($table, array $fields, array $data = array()) {
public function insert_many_pdo($table, array $fields, array $data = array(), $update = false) {
if (empty($data)) {
return false;
}
Expand All @@ -297,6 +298,14 @@ public function insert_many_pdo($table, array $fields, array $data = array()) {
}
$v = rtrim($v, ',');
$q .= $n . ' VALUES '.$v;
if ($update) {
$q .= ' ON DUPLICATE KEY UPDATE ';
foreach($fields as $f) {
$q .= $f.' = VALUES(`'.$f.'`),';
}
$q = rtrim($q, ',');
}
echo $q;
try
{
$this->_query_id = self::$_link_id->prepare($q);
Expand All @@ -315,6 +324,7 @@ public function insert_many_pdo($table, array $fields, array $data = array()) {
catch(PDOException $e)
{
$this->_error = $e->getMessage();
echo $this->_error;
return false;
}
}
Expand Down

0 comments on commit 61be2fc

Please sign in to comment.