Create a new leaderboard
This commit is contained in:
parent
c1817a24be
commit
beea5e57c1
7 changed files with 104 additions and 5 deletions
41
modules/leaderboard/create.php
Normal file
41
modules/leaderboard/create.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
class leaderboard_create extends leaderboard_new
|
||||
{
|
||||
protected $ajax = true;
|
||||
protected $method = 'POST';
|
||||
protected $validate = array(
|
||||
'name' => array(
|
||||
'length:>:100' => 'Leaderboard name may not be more than 100 characters.',
|
||||
),
|
||||
);
|
||||
|
||||
public function __default()
|
||||
{
|
||||
// Checks the current UID value
|
||||
$uid_key = 'leaderboard:uid';
|
||||
|
||||
if ($this->redis->get($uid_key) === false)
|
||||
{
|
||||
$uid = 1000000;
|
||||
$this->redis->set($uid_key, $uid);
|
||||
}
|
||||
else
|
||||
{
|
||||
$uid = $this->redis->incr($uid_key);
|
||||
}
|
||||
|
||||
$timestamp = Time::timestamp();
|
||||
|
||||
// Creates the rest of the data for the leaderboard
|
||||
$this->redis->multi()
|
||||
->hmset('leaderboard:' . $uid, array('name' => $_POST['name'], 'uid' => $this->uid))
|
||||
->zadd('user:' . $this->uid . ':leaderboards:updated', $timestamp, $uid)
|
||||
->zadd('leaderboards:updated', $timestamp, $uid)
|
||||
->exec();
|
||||
|
||||
return array('status' => 'success', 'url' => '/leaderboards');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
8
modules/leaderboard/new.php
Normal file
8
modules/leaderboard/new.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
class leaderboard_new extends UserModule
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -5,7 +5,7 @@ class leaderboards extends UserModule
|
|||
public function __default()
|
||||
{
|
||||
// Grabs the user's leaderboards
|
||||
$leaders = '';
|
||||
$leaderboards = array();
|
||||
|
||||
return array('leaderboards' => $leaderboards);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ class user_authenticate extends AnonymousModule
|
|||
setcookie('__auth', base64_encode($uid . '|' . $user['auth']), time() + Time::YEAR, '/');
|
||||
}
|
||||
|
||||
return array('status' => 'success', 'url' => '/');
|
||||
return array('status' => 'success', 'url' => '/leaderboards');
|
||||
}
|
||||
|
||||
return array('error' => 'Invalid email address or password.');
|
||||
|
|
|
@ -58,13 +58,30 @@ class user_create extends AnonymousModule
|
|||
'auth' => $auth_token,
|
||||
));
|
||||
|
||||
// Creates an API key for the user
|
||||
$api_key = false;
|
||||
|
||||
while (!$api_key)
|
||||
{
|
||||
$new_key = sha1(microtime() . mt_rand());
|
||||
$redis_key = 'user:api:' . $new_key;
|
||||
|
||||
if ($this->redis->get($redis_key) === false)
|
||||
{
|
||||
$api_key = $new_key;
|
||||
$this->redis->set($redis_key, $api_key);
|
||||
}
|
||||
}
|
||||
|
||||
$mapping_fields[] = 'user:api:' . $api_key;
|
||||
|
||||
// Sets the UID mappings
|
||||
$this->redis->mset(array_combine($mapping_fields, array($uid, $uid)));
|
||||
$this->redis->mset(array_combine($mapping_fields, array($uid, $uid, $uid)));
|
||||
|
||||
// Sets a cookie with the UID and auth token
|
||||
setcookie('__auth', base64_encode($uid . '|' . $auth_token), time() + Time::YEAR, '/');
|
||||
|
||||
return array('status' => 'success', 'url' => '/');
|
||||
return array('status' => 'success', 'url' => '/leaderboards');
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
|
17
templates/leaderboard/new.phtml
Normal file
17
templates/leaderboard/new.phtml
Normal file
|
@ -0,0 +1,17 @@
|
|||
<form role="form" class="ajax row col-xs-12 col-sm-6 col-sm-offset-3" method="post" action="/leaderboard/create" onsubmit="return false">
|
||||
<h1>New Leaderboard</h1>
|
||||
<?php
|
||||
echo $this->html->div(
|
||||
array('class' => 'form-group'),
|
||||
$this->html->input(array(
|
||||
'label' => 'Name',
|
||||
'name' => 'name',
|
||||
'class' => 'form-control input-lg required',
|
||||
'placeholder' => 'What shall we call this leaderboard?',
|
||||
'autocomplete' => 'off',
|
||||
'autofocus' => 'autofocus',
|
||||
))
|
||||
);
|
||||
?>
|
||||
<button type="submit" class="btn btn-success btn-lg">Make it so!</button>
|
||||
</form>
|
|
@ -1,3 +1,19 @@
|
|||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-6">
|
||||
<h1>My Leaderboards</h1>
|
||||
</div>
|
||||
<div class="col-xs-12 col-sm-6 text-right">
|
||||
<a class="btn btn-success" href="/leaderboard/new">New Leaderboard</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
if ($this->module['leaderboards'])
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
echo '<div class="lead">You have no leaderboards, <a href="/leaderboard/new">want to create one?</a></div>';
|
||||
}
|
||||
?>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue