Exemplo n.º 1
0
<?php

require '../vendor/autoload.php';
// Setup custom Twig view
$twigView = new \Slim\Extras\Views\Twig();
$app = new \Slim\Slim(array('debug' => true, 'view' => $twigView, 'templates.path' => '../templates/'));
// Make a new connection
$app->db = Capsule\Database\Connection::make('default', array('driver' => 'mysql', 'host' => 'localhost', 'port' => 3306, 'database' => 'bowling', 'username' => 'root', 'password' => '', 'prefix' => '', 'charset' => "utf8", 'collation' => 'utf8_general_ci'), true);
$app->get('/', function () use($app) {
    $users = User::all();
    foreach ($users as $user) {
        $user->score = Score::where('user_id', '=', $user->id)->sum('points');
    }
    $app->render('players.html', array('users' => $users));
});
$app->get('/users', function () {
    $users = User::all();
    echo $users->toJson();
});
$app->get('/team/:team', function ($team) {
    $users = User::where('team', '=', $team)->get();
    echo $users->toJson();
});
$app->get('/user/:id', function ($id) {
    $user = User::with('scores')->where('id', '=', $id)->get();
    echo $user->toJson();
    echo Score::where('user_id', '=', $id)->sum('points');
});
$app->run();
Exemplo n.º 2
0
            $score->you = true;
        }
        $score->rank = ++$i;
    }
    $app->render('index.php', array('scores' => $scores, 'local' => $local));
});
$app->post('/score', function () use($app) {
    header("Content-Type: application/json");
    if (in_array($_SERVER['REMOTE_ADDR'], array('localhost', '127.0.0.1', '::1'))) {
        echo json_encode(array('success' => false, 'error' => 'local.'));
        exit;
    }
    $score = intval($app->request()->params('score'));
    $nickname = $app->request()->params('nickname');
    $user = $app->request->cookies;
    if (isset($user['unique']) && mb_strlen($user['unique']) === 16 && count($records = Score::where('user', '=', $user['unique'])->get()) && ($record = $records[0])) {
        if ($record->score <= $score) {
            echo json_encode(array('success' => false, 'error' => 'Ton score est trop pété, désolé.'));
            exit;
        }
        $record->updated_at = new DateTime();
        $record->score = $score;
        if ($nickname) {
            $record->nickname = $nickname;
        }
        if ($record->save()) {
            echo json_encode(array('success' => true, 'score' => $score, 'id' => $record->id));
            exit;
        } else {
            echo json_encode(array('success' => false));
            exit;