Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #28 from rankster/staging
Browse files Browse the repository at this point in the history
upcoming v1.0
  • Loading branch information
vearutop authored Aug 17, 2016
2 parents d388401 + d347ded commit 197886c
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 58 deletions.
6 changes: 6 additions & 0 deletions public/css/rankster.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
color: #fff;
padding: 2px 8px;
font-size: 10px;
}


.reject-button, .confirm-button {
width: 80px;
margin: 1px;
}
6 changes: 6 additions & 0 deletions public/js/rankster.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
$("#game-replay").modal();
}

function newGameDialog() {
$("#game-req").modal();
}


function eventHandler () {
$('#game').select2({
ajax: {
Expand Down Expand Up @@ -141,6 +146,7 @@
formatParticipant: formatParticipant,
formatParticipantSelection: formatParticipantSelection,
gameReplayDialog: gameReplayDialog,
newGameDialog: newGameDialog,
eventHandler: eventHandler,
setUserGameInfo: function setUserGameInfo(u, g) {
users = u;
Expand Down
2 changes: 2 additions & 0 deletions src/Api/V1.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rankster\Api\V1\DeleteUser;
use Rankster\Api\V1\Games;
use Rankster\Api\V1\Login;
use Rankster\Api\V1\Oauth\Oauth;
use Rankster\Api\V1\RecalculateRank;
use Rankster\Api\V1\ReloadFbAvatars;
use Rankster\Api\V1\SeedMatches;
Expand Down Expand Up @@ -47,6 +48,7 @@ static function setUpDefinition(Definition $definition, $options)
->addToEnum(SeedMatches::definition(), 'seed-matches')
->addToEnum(DeleteUser::definition(), 'delete-user')
->addToEnum(ReloadFbAvatars::definition())
->addToEnum(Oauth::definition())
->setIsUnnamed();

}
Expand Down
61 changes: 61 additions & 0 deletions src/Api/V1/Oauth/Google.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Rankster\Api\V1\Oauth;

use Rankster\Entity\User;
use Rankster\Service\AuthSession;
use Yaoi\Command;
use Yaoi\Command\Definition;

class Google extends Command
{
public $state;
public $code;

/**
* @param Definition $definition
* @param \stdClass|static $options
*/
static function setUpDefinition(Definition $definition, $options)
{
$options->state = Command\Option::create()->setType()
->setDescription('State information (contains initial url)');
$options->code = Command\Option::create()->setType()->setIsRequired()
->setDescription('Auth security code');
}

public function performAction()
{
$google = \Rankster\Service\Google::getInstance();
$token = $google->getToken($this->code);
$info = $google->getUserInfo();

if (empty($info['id'])) {
throw new \Exception('Bad response');
}

$user = new User();
$user->email = $info['email'];
if ($saved = $user->findSaved()) {
$user = $saved;
//$user->downloadImage($info['picture'], $user->googleId);
//$user->save();
if (empty($user->googleId)) {
$user->googleId = $info['id'];
$user->save();
}
} else {
$user->name = $info['name'];
$user->googleId = $info['id'];
if (!empty($info['picture'])) {
$user->downloadImage($info['picture'], $user->googleId);
}
$user->save();
}
AuthSession::set($user->id);
header("Location: $this->state");
exit();
}


}
33 changes: 33 additions & 0 deletions src/Api/V1/Oauth/Oauth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rankster\Api\V1\Oauth;


use Yaoi\Command;

class Oauth extends Command
{
/**
* @var Command
*/
public $action;

/**
* @param Command\Definition $definition
* @param \stdClass|static $options
*/
static function setUpDefinition(Command\Definition $definition, $options)
{
$options->action = Command\Option::create()
->addToEnum(Google::definition())
->setIsUnnamed()
->setIsRequired();
}


public function performAction()
{
$this->action->performAction();
}

}
12 changes: 12 additions & 0 deletions src/Entity/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ public function countPlayers()
return $r['c'];
}


public static function findByPrimaryKey($id)
{
static $cache = array();
if (isset($cache[$id])) {
return $cache[$id];
}

$game = parent::findByPrimaryKey($id);
$cache[$id] = $game;
return $game;
}
}
66 changes: 66 additions & 0 deletions src/Entity/MatchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class MatchRequest extends Entity
public $user2Id;
public $winnerId;
public $status;
/** @var \DateTime */
public $eventTime;
/** @var \DateTime */
public $actionTime;

/**
Expand Down Expand Up @@ -84,8 +86,37 @@ public static function getCountNew($userId)
return $cnt['cnt'];
}

public static function getCountOpponentNew($userId)
{
$query = MatchRequest::statement()->select('count(*) as cnt')
->where('? = ?', MatchRequest::columns()->user1Id, $userId)
->where('? = ?', MatchRequest::columns()->status, MatchRequest::STATUS_NEW)
->query();
$query->bindResultClass();
$cnt = $query->fetchRow();

return $cnt['cnt'];
}

public static function getCountRejected($userId)
{
$query = MatchRequest::statement()->select('count(*) as cnt')
->where('? = ?', MatchRequest::columns()->user1Id, $userId)
->where('? = ?', MatchRequest::columns()->status, MatchRequest::STATUS_REJECTED)
->query();
$query->bindResultClass();
$cnt = $query->fetchRow();

return $cnt['cnt'];
}


public static function getMatchRequestNewList($userId, $perPage = 20, $page = 1)
{
if (empty($page)) {
$page = 1;
}

return MatchRequest::statement()
->where('? = ?', MatchRequest::columns()->user2Id, $userId)
->where('? = ?', MatchRequest::columns()->status, MatchRequest::STATUS_NEW)
Expand All @@ -95,8 +126,43 @@ public static function getMatchRequestNewList($userId, $perPage = 20, $page = 1)
->fetchAll();
}

public static function getMatchRequestNewOpponentList($userId, $perPage = 20, $page = 1)
{
if (empty($page)) {
$page = 1;
}

return MatchRequest::statement()
->where('? = ?', MatchRequest::columns()->user1Id, $userId)
->where('? = ?', MatchRequest::columns()->status, MatchRequest::STATUS_NEW)
->order('? DESC', MatchRequest::columns()->eventTime)
->limit($perPage, $perPage * ($page - 1))
->query()
->fetchAll();
}

public static function getMatchRequestRejectedList($userId, $perPage = 20, $page = 1)
{
if (empty($page)) {
$page = 1;
}

return MatchRequest::statement()
->where('? = ?', MatchRequest::columns()->user1Id, $userId)
->where('? = ?', MatchRequest::columns()->status, MatchRequest::STATUS_REJECTED)
->order('? DESC', MatchRequest::columns()->eventTime)
->limit($perPage, $perPage * ($page - 1))
->query()
->fetchAll();
}


public static function getMatchRequestNewListForUserAndGame($userId, $userOpponent, $gameId, $perPage = 20, $page = 1)
{
if (empty($page)) {
$page = 1;
}

return MatchRequest::statement()
->where('? = ?', MatchRequest::columns()->user1Id, $userOpponent)
->where('? = ?', MatchRequest::columns()->user2Id, $userId)
Expand Down
27 changes: 22 additions & 5 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class User extends Entity

public $id;
public $facebookId;
public $googleId;
public $name;
public $login;
public $email;
Expand All @@ -23,12 +24,13 @@ class User extends Entity
static function setUpColumns($columns)
{
$columns->id = Column::AUTO_ID;
$columns->facebookId = Column::create(Column::STRING + Column::NOT_NULL)
$columns->facebookId = Column::create(Column::STRING)
->setStringLength(50)
->setUnique();
$columns->googleId = Column::create(Column::STRING)->setUnique();
$columns->login = Column::create(Column::STRING)->setUnique();
$columns->name = Column::create(Column::STRING + Column::NOT_NULL)->setDefault("");
$columns->email = Column::create(Column::STRING + Column::NOT_NULL)->setDefault("");
$columns->name = Column::create(Column::STRING + Column::NOT_NULL)->setDefault('');
$columns->email = Column::create(Column::STRING + Column::NOT_NULL)->setDefault('')->setUnique();
$columns->picturePath = Column::STRING;
}

Expand All @@ -37,14 +39,17 @@ static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
$table->setSchemaName('user');
}

public function downloadImage($url)
public function downloadImage($url, $id = null)
{
$response = file_get_contents($url);
if (!$response) {
return;
}

$md5 = md5($this->facebookId . 'FacebookHackathon2016');
if (null === $id) {
$id = $this->facebookId;
}
$md5 = md5($id . 'FacebookHackathon2016');
$dir = substr($md5, 0, 3);

$imagesDirectory = DOC_ROOT . '/' . self::IMAGE_FOLDER . '/';
Expand Down Expand Up @@ -143,4 +148,16 @@ public function getMatchRequestNewCount()
{
return MatchRequest::getCountNew($this->id);
}

public static function findByPrimaryKey($id)
{
static $cache = array();
if (isset($cache[$id])) {
return $cache[$id];
}

$entity = parent::findByPrimaryKey($id);
$cache[$id] = $entity;
return $entity;
}
}
62 changes: 62 additions & 0 deletions src/Service/Google.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Rankster\Service;


use Rankster\Service\Google\GoogleSettings;
use Yaoi\Command\Web\RequestMapper;
use Yaoi\Http\Client;
use Yaoi\Service;

/**
* @method GoogleSettings getSettings()
*/
class Google extends Service
{
protected static function getSettingsClassName()
{
return GoogleSettings::className();
}


public function createAuthUrl()
{
$params = array(
'response_type' => 'code',
'client_id' => $this->getSettings()->clientId,
'redirect_uri' => $this->getSettings()->redirectUrl,
'scope' => 'email profile',
'state' => $_SERVER['REQUEST_URI'],
'access_type' => 'online',
'prompt' => 'select_account',
);

return 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query($params);
}


private $accessToken;

public function getToken($code) {
$params = array(
'code' => $code,
'client_id' => $this->getSettings()->clientId,
'client_secret' => $this->getSettings()->secret,
'redirect_uri' => $this->getSettings()->redirectUrl,
'grant_type' => 'authorization_code',
);

$client = new Client();
$client->post = $params;
$tokenResponse = json_decode($client->fetch('https://www.googleapis.com/oauth2/v4/token'),true);
$this->accessToken = $tokenResponse['access_token'];
return $this->accessToken;
}

public function getUserInfo() {
$url = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' . $this->accessToken;
$response = file_get_contents($url);
return json_decode($response, true);
}

}
12 changes: 12 additions & 0 deletions src/Service/Google/GoogleSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rankster\Service\Google;

use Yaoi\Service\Settings;

class GoogleSettings extends Settings
{
public $clientId;
public $secret;
public $redirectUrl;
}
Loading

0 comments on commit 197886c

Please sign in to comment.