Beispiel #1
0
<?php

/**
 * Game Controller
 */
require_once 'includes/common.inc.php';
// check if user is logged in
if (!Auth::isLoggedin()) {
    buildView('auth/login');
    exit;
}
// if a game is not already running get a random word from db, and the logged in user
if (!isset($_SESSION['game'])) {
    $word = Word::selectRandom($db);
    $player = Auth::user();
    $game = new Game($player, $word);
    // init game
    $_SESSION['game'] = serialize($game);
    // save to session
} else {
    $game = unserialize($_SESSION['game']);
}
// if 'Games' is clicked, show all games saved in db
if (isset($_GET['games'])) {
    $games = $db->raw('
        SELECT games.id, username, word, start_datetime, score FROM games
        INNER JOIN users ON user_id=users.id
        INNER JOIN words ON word_id=words.id;
    ');
    buildView('user/games', compact('games'));
    exit;