<?php
if (defined("IF_GAME")) return;
define("IF_GAME", "Loaded");

include_once("classes/license.h");
include_once("classes/title.h");
include_once("classes/link.h");
include_once("classes/content.h");
include_once("classes/ifiction.h");
include_once("classes/role.h");
include_once("classes/person.h");
include_once("classes/company.h");
include_once("classes/series.h");
include_once("classes/attribute.h");
include_once("classes/review.h");
include_once("classes/genre.h");

class game {
  var $id;
  var $title;
  var $release;
  var $license;

  var $series;
  var $seriesindex;

  var $links;
  var $ifictions;
  var $alttitles;
  var $seealso;
  var $contents;
  var $persons;
  var $companies;
  var $genres;
  var $attributes;
  var $reviews;
  var $awards;

  var $gamegenre;  
  var $platform;
  var $language;

  function game() {
    $this->title = new title;
    $this->license = new license;
  }

  function load($id = 0) {
    if ($id != 0) $this->id = $id;
    else $id = $this->id;
    $conn = &dbconnect();
    list($this->release, $this->title->id, $this->title->title,
	 $this->license->id, $this->license->name, $this->attributes) =
      $conn->GetRow("select release, titleid, title, licenseid, licensename, atts from game_view where gameId = $id");
    $this->title->gameid = $id;

    $series = &$conn->GetRow("select series.seriesid, series.name, gameseries.index from series, gameseries where gameseries.gameid = $this->id and gameseries.seriesid = series.seriesid");
    if ($series) {
      $this->series = new series;
      list($this->series->id, $this->series->name, $this->seriesindex) =
	$series;
    } else {
      $this->series = 0;
      $this->seriesindex = "";
    }
  }

  function store() {
    $conn = &dbconnect();

    if (!$this->license->id)
      $this->license->store;
    $licenseid = $this->license->id;
    $release = escape($this->release);

    if (empty($this->id)) {
      $result = &$conn->execute("insert into games (release, licenseid) values ($release, $licenseid)");
      // Postgres-specific
      $oid = $conn->Insert_ID();
      $this->id = $conn->GetOne("select gameid from games where oid = $oid");
    }

    $this->title->gameid = $this->id;
    $this->title->store();

    $titleid = $this->title->id;
    $conn->execute("update games set release=$release, licenseid=$licenseid, titleid=$titleid where gameid = $this->id");

    $conn->execute("delete from gameseries where gameid = $this->id");
    if ($this->series) {
      $seriesid = $this->series->id;
      $seriesindex = (empty($this->seriesindex))? "null" : $this->seriesindex;
      $conn->execute("insert into gameseries (gameid, seriesid, index) values ($this->id, $seriesid, $seriesindex)");
    }
  }

  function linkto() {
    $this->title->linkto();
  }

  function describe() {
    global $tables;
    if ($tables) {
      $left = "<TR BGCOLOR=#EEEEEE><TD VALIGN=top WIDTH=25%><B>";
      $mid = "</B></TD><TD VALIGN=top WIDTH=75%>";
      $right = "</TD></TR>";
    } else {
      $left = "<br><b>";
      $mid = "</b> ";
      $right = "\n";
    }

    // The parts
    // s series
    // t alternate titles
    // p persons
    // c companies
    // g genres
    // d release date
    // l license
    // a attributes
    // P platforms
    // R reviews
    // A awards
    // i ifiction.org links
    // L links
    // D downloads
    // S see also

    $parts = getparts();
    
    showhead($this->title->getName());
  
    if ($this->series && $parts['s']) {
      echo "<p class=center align=center>Part of ";
      $this->series->linkto();
      echo "</p>\n";
    }

    findRoles();
    global $roles;

    if ($tables)
      echo "<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2 WIDTH=100%>";

    if ($parts['t']) {
      $this->getAltTitles();
      if (!empty($this->alttitles)) {
        echo $left, "Alternate Titles:", $mid;
        for ($alt = reset($this->alttitles); $alt;
	     $alt = current($this->alttitles)) {
          echo "<i>";
          $alt->describe();
          echo "</i>";
	  if (next($this->alttitles)) echo ", ";
        }
        if ($tables) echo "<br>\n";
        echo $right;
      }
    }

    if ($parts['p']) {
      $this->getPersons();    
      if (empty($this->persons)) {
        if ($tables) echo $left, "Author:", $mid;
        echo "Anonymous", $right;
      } else {
        reset($this->persons);
        while (list($roleid, $stuff) = each($this->persons)) {
          echo $left;
	  echo ucwords($roles[$roleid]->name);
          if (count($stuff)>1) echo "s";
          echo ":", $mid;
          listnames($stuff);
          echo $right;
        }
      }
    }

    if ($parts['c']) {
      $this->getCompanies();
      if (!empty($this->companies)) {
        reset($this->companies);
        while (list($roleid, $stuff) = each($this->companies)) {
          echo $left;
          echo ucwords($roles[$roleid]->name);
          if (count($stuff)>1) echo "s";
          echo ":", $mid;
          for (reset($stuff); $company = current($stuff); ) {
            $company->linkto();
            if (next($stuff)) echo ", ";
          }
         echo $right;
        }
      }
    }

    if ($parts['g']) {
      $gens = getGenres();
      $this->getGenres();
      if (!empty($this->genres)) {
        echo $left, "Genre";
        if (count($this->genres)>1) echo "s";
        echo ":", $mid;
        for ($gen = reset($this->genres); $gen; ) {
          $gens[$gen]->fullname(1);
          $gen = next($this->genres);
          if ($gen) {
            if ($tables) echo "<BR>\n";
            else echo ", ";
          }
        }
        echo $right;
      }
    }

    if (!empty($this->release) && $parts['d']) {
      if ($tables)
        echo $left, "Released:", $mid;
      else echo "<br>";
      echo $this->release;
      echo $right;
    }

    if ($this->license->id != 1 && $parts['l']) {
      if ($tables)
        echo $left, "License:", $mid;
      echo $this->license->name;
      echo $right;
    }

    if ($parts['P']) {
      $this->getPlatforms();
      if (!empty($this->platform)) {
        echo $left, "Platform";
        if (count($this->platform)>1) echo "s";
        echo ":", $mid;
        listPlatforms($this->platform,1);
        echo $right;
      }
    }

    if ($parts['a']) {
      echo $left, "Attributes:", $mid;
      listatts($this->attributes);
      echo $right;
    }

    if ($parts['R']) {
      $this->getReviews();
      for ($r = reset($this->reviews); $r; $r = next($this->reviews)) {
        echo $left, "Review:", $mid;
        $r->describe();
        echo $right;
      }
    }

    if ($parts['A']) {
      $this->getAwards();
      if (!empty($this->awards)) {
        global $up;
        if ($tables) echo $left, "Competitions/<BR>Awards:", $mid;
        else echo $left, "Competition/Awards:", $mid, "<br>\n";
        foreach ($this->awards as $a) {
          if ($a->category) echo htmlentities($a->category), ", ";
          else if ($a->place) echo ordinal($a->place), " place, ";
          else echo "Entered in ";
          if ($a->division) echo $a->division, ", ";
          echo "<a href=", $up, "award/$a->compgroupid#$a->compid>";
          if ($a->compname) echo $a->compname;
          else {
            echo $a->name;
            if ($a->number) echo " ", $a->number;
          }
          echo "</a><br>\n";
        }
        echo $right;
      }
    }

    if ($parts['i']) {
      $this->getIfictions();
      if (!empty($this->ifictions)) {
        if ($tables) echo $left, "Play Online:", $mid;
        for ($ific = reset($this->ifictions); $ific; $ific = next($this->ifictions)) {
          $ific->describe();
          echo "<br>\n";
        }
        echo $right;
      }
    }

    if ($parts['L']) {
      $this->getLinks();
      if (!empty($this->links)) {
        echo $left, "Related Links:", $mid;
        if (!$tables) echo "<br>";
        for ($link = reset($this->links); $link; $link = next($this->links)) {
          $link->describe();
          echo "<br>\n";
        }
        echo $right;
      }
    }

    if ($parts['D']) {
      global $showdates;
      $this->getContents();
      echo $left, "Downloads:", $mid, "<dl>";
      while (list($fileid, $stuff) = each($this->contents)) {
	$file = new iffile;
	$file->load($fileid);
	echo "<dt><b>";
	if ($showdates)
	  echo $file->archived, " ";
	$file->linkto();
	echo " (";
	echo $file->sizestr(), ")";
	echo "</b>\n<dd>";
	while ($content = current($stuff)) {
	  $content->describe();
	  if (next($stuff)) echo ", ";
	}
      }
      echo "</dl>\n", $right;
    }

    if ($parts['S']) {
      $this->getSeeAlso();
      if (count($this->seealso) > 0) {
        echo $left, "See also:", $mid;
        for ($see = reset($this->seealso); $see; $see = current($this->seealso)) {
	  $see->linkto();
	  if (next($this->seealso)) echo ", ";
        }
        echo $right;
      }
    }
    
    if ($tables)
      echo "</table>";
    
  }

  function form() {
    $this->title->form();
    echo "Year of initial release: <input type=text width=4 name=release value=\"$this->release\"><br>\n";
    echo "License: ";
    licenseChooser($this->license->id);
    echo "<br>\nSeries: ";
    seriesChooser($this->series? $this->series->id : 0);
    echo " Index in series: <input type=text name=seriesindex value=\"$this->seriesindex\"><br>\n";
  }

  function readForm($vars) {
    $this->id = $vars["gameid"];
    $this->release = $vars["release"];
    $this->title->readForm($vars);
    $this->license->id = $vars["licenseid"];
    $seriesid = $vars["seriesid"];
    if ($seriesid) {
      $this->series = new series;
      $this->series->id = $seriesid;
      $this->seriesindex = $vars["seriesindex"];
    }
  }

  function getLinks() {
    if (!is_array($this->links))
      $this->links = findLinks(", game_links where game_links.gameid = $this->id and game_links.linkid = links.linkid");
    return $this->links;
  }

  function getIfictions() {
    if (!is_array($this->ifictions))
      $this->ifictions = findIfictions($this->id);
    return $this->ifictions;
  }

  function getAltTitles() {
    if (!is_array($this->alttitles)) {
      $titleid = $this->title->id;
      $this->alttitles = findTitles("where gameid = $this->id and not titleid = $titleid");
    }
    return $this->alttitles;
  }

  function getSeeAlso() {
    if (!is_array($this->seealso)) {
      $conn = &dbconnect();
      $result = &$conn->execute("select titles.title, games.gameid from see_also, games, titles where see_also.game1 = $this->id and see_also.game2 = games.gameid and games.titleid = titles.titleid");
      $this->seealso = array();
      while (!$result->EOF) {
	$title = new title;
        list($title->title, $title->gameid) = $result->FetchRow();
	$this->seealso[] = $title;
      }
    }
    return $this->seealso;
  }

  function getContents() {
    if (!is_array($this->contents))
      $this->contents = findContents($this->id);
    return $this->contents;
  }

  function getPersons() {
    if (!is_array($this->persons)) {
      $this->persons = array();
      $conn = &dbconnect();
      $result = &$conn->execute("select personnames.nameid, personnames.personid, personnames.name, gamepersons.roleid from gamepersons, persons, personnames where gamepersons.gameid = $this->id and persons.personid = gamepersons.personid and persons.nameid = personnames.nameid order by gamepersons.roleid, personnames.sortkey");
      while (!$result->EOF) {
	$name = new personname;
	list($name->id, $name->personid, $name->name, $roleid) =
          $result->FetchRow();
	$this->persons[$roleid][] = $name;
      }
    }
    return $this->persons;
  }

  function getCompanies() {
    if (!is_array($this->companies)) {
      $this->companies = array();
      $conn = &dbconnect();
      $result = &$conn->execute("select companies.companyid, companies.name, gamecompanies.roleid from gamecompanies, companies where gamecompanies.gameid = $this->id and gamecompanies.companyid = companies.companyid order by gamecompanies.roleid, companies.name");
      while (!$result->EOF) {
	$company = new company;
	list($company->id, $company->name, $roleid) =
          $result->FetchRow();
	$this->companies[$roleid][] = $company;
      }
    }
    return $this->companies;
  }

  function getGenres() {
    if (!is_array($this->genres)) {
      $this->genres = array();
      $conn = &dbconnect();
      $result = &$conn->execute("select genreid from gamegenres where gameid = $this->id");
      while (!$result->EOF) {
	list($genreid) = $result->FetchRow();
	$this->genres[$genreid] = $genreid;
      }
    }
    return $this->genres;
  }

  function getGameGenre() {
    if (!is_array($this->gamegenre)) {
      $conn = &dbconnect();
      $result = &$conn->execute("select name from genres where gamegenres.gameid = $this->id and gamegenres.genreid = genres.genreid");
      while (!$result->EOF) {
        list($gamegenre) = $result->FetchRow();
        $this->gamegenre[] = $gamegenre;
      }
    }
    return $this->gamegenre;
  }

  function hasAttribute($i) {
    if (!is_string($this->attributes)) {
      $conn = &dbconnect();
      $this->attributes = $conn->GetOne("select atts from games where gameId = $this->id");
    }
    return substr($this->attributes, $i-1, 1);
  }

  function getReviews() {
    if (!is_array($this->reviews)) {
      $this->reviews = array();
      $conn = &dbconnect();
      $result = &$conn->execute("select reviews.entered, reviews.rating, reviews.text, reviews.reviewer, personnames.name from reviews, persons, personnames where reviews.gameid = $this->id and persons.personid = reviews.reviewer and persons.nameid = personnames.nameid order by personnames.sortkey");
      while (!$result->EOF) {
	$r = new review;
	$r->gameid = $this->id;
	list($r->entered, $r->rating, $r->text, $r->reviewer->personid, $r->reviewer->name) = $result->FetchRow();
	$this->reviews[$r->reviewer->personid] = $r;
      }
    }
    return $this->reviews;
  }

  function getAwards() {
    if (!is_array($this->awards)) {
      $this->awards = array();
      $conn = &dbconnect();
      $result = &$conn->execute("select compgroups.name, compgroups.compgroupid, comps.name as compname, comps.number, comps.compid, compgames.division, compgames.name as category, compgames.place from compgroups, comps, compgames where compgames.gameid = $this->id and compgames.compid = comps.compid and comps.compgroupid = compgroups.compgroupid");
      while (!$result->EOF) {
	$o = $result->FetchNextObject(false);
	$this->awards[] = $o;
      }
    }
    return $this->awards;
  }

  function getPlatforms() {
    if (!is_array($this->platform)) {
      $this->platform = array();
      $conn = &dbconnect();
      $result = &$conn->execute("select distinct on (platforms.platformid) platforms.name where
 		((contents.gameid = $this->id) and
		(contents.contentid = imps.contentid) and (imps.platformid = platforms.platformid))");
      while (!$result->EOF) {
        list($platform) = $result->FetchRow();
        $this->platform[] = $platform;
      }
      return $this->platform;
    }
  }
}

function listPlatforms($platforms, $long = 0) {
  $rowcount = count($platforms);
  if (($rowcount < 5)||($long != 0)) {
    for ($row = 0; $row < $rowcount; $row++) {
      if ($row>0) {
        if ($long == 0) echo "<br>";
        else {
          if ($row+1<$rowcount)echo ", ";
          else echo " and ";
        }
      }
      if ($long==0) echo "<b>", $platforms[$row], "</b>";
      else echo $platforms[$row];
    }
  }
  else echo "<b><i>Various</i></b>";
}

?>

