public function load()
 {
     $mode;
     if (!isset($_GET['mode']) || empty($_GET['mode'])) {
         $mode = 'joinable';
     } else {
         $mode = $_GET['mode'];
     }
     //No cache for Game List
     header("Cache-Control: no-cache, must-revalidate");
     // HTTP/1.1
     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
     // Date in the past
     $max_game = $this->config->item('max_game');
     if ($mode == 'friends') {
         $config = array('appId' => $this->config->item('fb_appid'), 'secret' => $this->config->item('fb_appsecret'), 'cookie' => $this->config->item('fb_cookie'), 'domain' => $this->config->item('fb_domain'));
         $this->facebook->init($config);
         $query = getGames($this->db, $max_game, $this->config->item('max_player'), $mode, $this->session->userdata('fid'), $this->facebook);
     } else {
         $query = getGames($this->db, $max_game, $this->config->item('max_player'), $mode);
     }
     $games = array();
     foreach ($query->result_array() as $row) {
         $game = array();
         $game['id'] = $row['gid'];
         $game['state'] = $row['state'];
         $game['players'] = getPlayersFromGid($this->db, $row['gid']);
         $games[] = $game;
     }
     $rows = array();
     if ($query->num_rows() < $max_game && $mode == 'joinable') {
         $rows = createGames($this->db, $max_game - $query->num_rows());
         foreach ($rows as $row) {
             $game = array();
             $game['id'] = $row['gid'];
             $game['state'] = 0;
             $game['players'] = array();
             $games[] = $game;
         }
     }
     $this->load->view('game_list_ajax', array("games" => $games, "appId" => $this->config->item('fb_appid'), "mode" => $mode));
 }
function getGamesByPlayer($db, $fid)
{
    $sql = "SELECT DISTINCT `gameid`,`outcome` FROM plays WHERE `playerid`=? AND `outcome`>? ORDER BY gameid DESC LIMIT 5";
    $result = array('lost', 'won', 'tied');
    $query = $db->query($sql, array($fid, -1));
    $games = array();
    foreach ($query->result_array() as $row) {
        $game = array();
        $game['gid'] = $row['gameid'];
        $game['result'] = $result[$row['outcome']];
        $game['players'] = getPlayersFromGid($db, $row['gameid']);
        $games[] = $game;
    }
    return $games;
}