Skip to content

Commit

Permalink
Add game to database when not found #57
Browse files Browse the repository at this point in the history
  • Loading branch information
Clidus committed Apr 3, 2016
1 parent 3653756 commit f7e0bce
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ignition_application/models/game.php
Expand Up @@ -35,6 +35,44 @@ function __construct()
// get game by GBID
public function getGame($GBID, $userID)
{
// check if game is in database
if($this->isGameInDB($GBID))
{
// get game from database
if($this->getGameFromDatabase($GBID, $userID))
// game found
return true;
}

// game was not found, get from Giant Bomb
$this->load->model('GiantBomb');
$result = $this->GiantBomb->getGame($GBID, $userID);

// if game was returned
if($result != null && $result->error == "OK" && $result->number_of_total_results > 0)
{
// add game to database
$this->addGame($result->results);

// get game from database
return $this->getGameFromDatabase($GBID, $userID);
} else {
// game was not found
return false;
}
}

// is game in database?
function isGameInDB($GBID)
{
$query = $this->db->get_where('games', array('GBID' => $GBID));

return $query->num_rows() > 0 ? true : false;
}

// get game from database
public function getGameFromDatabase($GBID, $userID)
{
// get game from db
$this->db->select('games.GameID, games.GBID, games.Name, games.Image, games.ImageSmall, games.Deck, lists.ListID, lists.ListName, lists.ListStyle');
$this->db->select('gameStatuses.StatusID, gameStatuses.StatusName, gameStatuses.StatusStyle, collections.CurrentlyPlaying, collections.DateComplete, collections.HoursPlayed');
Expand Down Expand Up @@ -116,4 +154,23 @@ function getPlatforms($gameID, $userID)

return null;
}

// add game to database
function addGame($game)
{
$this->load->model('GiantBomb');
$releaseDate = $this->GiantBomb->convertReleaseDate($game);

$data = array(
'GBID' => $game->id,
'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')
);

return $this->db->insert('games', $data);
}
}
78 changes: 78 additions & 0 deletions ignition_application/models/giantbomb.php
@@ -0,0 +1,78 @@
<?php

class GiantBomb extends CI_Model {

function __construct()
{
// Call the Model constructor
parent::__construct();
}

// get game from Giant Bomb API
public function getGame($GBID, $userID)
{
// build API request
$url = $this->config->item('gb_api_root') . "/game/" . $GBID . "?api_key=" . $this->config->item('gb_api_key') . "&format=json";

// make API request
$result = $this->Utility->getData($url, "Game");

if(is_object($result))
{
return $result;
} else {
return null;
}
}

function convertReleaseDate($game)
{
// original release date
if($game->original_release_date != null)
{
return $game->original_release_date;
} else {
// expected release date
if($game->expected_release_year != null)
{
// year
$releaseYear = $game->expected_release_year;

// month (assume end of year if unknown)
$releaseMonth = ($game->expected_release_month != null ? $game->expected_release_month : "12");

// day (assume end of month if unknown)
if($game->expected_release_day != null) {
$releaseDay = $game->expected_release_day;
} else {
switch($releaseMonth) {
case "01":
case "03":
case "05":
case "07":
case "08":
case "10":
case "12":
$releaseDay = "31";
break;
case "02":
$releaseDay = "28";
break;
case "04":
case "06":
case "09":
case "11":
$releaseDay = "30";
break;
}
}

return $releaseYear . "-" . $releaseMonth . "-" . $releaseDay;
// unknown release date
} else {
// if completly unknwon, set to five years in the future
return (date("Y")+5) . "-12-31";
}
}
}
}

0 comments on commit f7e0bce

Please sign in to comment.