-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
579 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
abstract class items_mapper { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters