Skip to content

Commit

Permalink
Record platforms when updating database cache from crawler #57
Browse files Browse the repository at this point in the history
  • Loading branch information
Clidus committed Oct 25, 2015
1 parent 3dba702 commit e1e7e15
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 11 deletions.
94 changes: 83 additions & 11 deletions ignition_application/models/game.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,22 +680,94 @@ function getGameToUpdate()
return null;
}

// get platforms for a game
function getPlatforms($gameID)
{
$this->db->select('platforms.GBID, platforms.Name, platforms.Abbreviation');
$this->db->from('games');
$this->db->join('gamePlatforms', 'games.GameID = gamePlatforms.GameID');
$this->db->join('platforms', 'gamePlatforms.PlatformID = platforms.PlatformID');
$this->db->where('games.GameID', $gameID);
$query = $this->db->get();

if($query->num_rows() > 0)
{
return $query->result();
}

return null;
}

// update game cache
function updateGame($game)
{
$releaseDate = $this->getReleaseDate($game);
// get GameID
$gameID = $this->getGameID($game->id);

$data = array(
'Name' => $game->name,
'Image' => is_object($game->image) ? $game->image->small_url : null,
'ImageSmall' => is_object($game->image) ? $game->image->icon_url : null,
'Deck' => $game->deck,
'ReleaseDate' => $releaseDate,
'LastUpdated' => date('Y-m-d')
);
// if game exists
if($gameID != null) {
// get release date
$releaseDate = $this->getReleaseDate($game);

$this->db->where('GBID', $game->id);
$this->db->update('games', $data);
$data = array(
'Name' => $game->name,
'Image' => is_object($game->image) ? $game->image->small_url : null,
'ImageSmall' => is_object($game->image) ? $game->image->icon_url : null,
'Deck' => $game->deck,
'ReleaseDate' => $releaseDate,
'LastUpdated' => date('Y-m-d')
);

// update game data
$this->db->where('GameID', $gameID);
$this->db->update('games', $data);

// add platforms to game
if(property_exists($game, "platforms") && $game->platforms != null)
{
// load platforms model
$this->load->model('Platform');

// get platforms game already has
$platforms = $this->getPlatforms($gameID);

// loop over platforms returned by GB
$platformsToAdd = [];
foreach($game->platforms as $gbPlatform)
{
// loop over platforms for game already in db
$gameHasPlatform = false;
if($platforms != null) {
foreach ($platforms as $platform)
{
// if game has platform
if($platform->GBID == $gbPlatform->id)
{
$gameHasPlatform = true;
break;
}
}
}

// if game doesnt have platform
if(!$gameHasPlatform) {
// get or add platform to db
$platform = $this->Platform->getOrAddPlatform($gbPlatform);

// add to list of platforms to add to game
array_push($platformsToAdd, array(
'GameID' => $gameID,
'PlatformID' => $platform->PlatformID
));
}
}

// if there are platforms to add to game
if(count($platformsToAdd) > 0)
// add to game in db
$this->db->insert_batch('gamePlatforms', $platformsToAdd);
}
}
}

// failed to get response from GB API, save error in db
Expand Down
32 changes: 32 additions & 0 deletions ignition_application/models/platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ function isPlatformInDB($GBID)
return $query->num_rows() > 0 ? true : false;
}

// get PlatformID from GBID
function getPlatformByGBID($GBID)
{
$query = $this->db->get_where('platforms', array('GBID' => $GBID));

if($query->num_rows() == 1)
{
return $query->first_row();
}

return null;
}

// returns platform if in db, or adds and returns it if it isn't
function getOrAddPlatform($gbPlatform)
{
// get platform from db
$platform = $this->getPlatformByGBID($gbPlatform->id);

// if platform isn't in db
if($platform == null)
{
// add platform to db
$this->Platform->addPlatform($gbPlatform);

// get platform from db
$platform = $this->getPlatformByGBID($gbPlatform->id);
}

return $platform;
}

// add platform to database
function addPlatform($platform)
{
Expand Down

0 comments on commit e1e7e15

Please sign in to comment.