Skip to content

Commit

Permalink
Complete dm_chess_ajax_cache service. Amazing performance improvement :)
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 19, 2010
1 parent e0e922f commit eaa67cc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
3 changes: 2 additions & 1 deletion lib/asset/dmChessJavascriptConfig.php
Expand Up @@ -35,7 +35,8 @@ protected function getJavascriptConfig()
'targets' => ($this->player->isMyTurn() && $this->player->Game->isStarted) ? $this->player->getTargetKeysByPieces() : null,
'beat' => array(
'url' => $this->helper->link('@dm_chess_whatsup')->param('player', $this->player->code)->getHref(),
'delay' => 2500
'cache_url' => $this->helper->link('/cache/chess/'.$this->player->code.'.txt')->getHref(),
'delay' => 2000
),
'game' => array(
'code' => $this->player->Game->code,
Expand Down
51 changes: 37 additions & 14 deletions lib/cache/dmChessAjaxCache.php
Expand Up @@ -7,46 +7,69 @@ class dmChessAjaxCache

public function __construct(sfEventDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
$this->dispatcher = $dispatcher;

$this->dir = sfConfig::get('sf_web_dir').'/cache/chess';

if(!is_dir($this->dir))
{
mkdir($this->dir);
}
}

public function connect()
{
$this->dispatcher->connect('dm.chess.player_set_events', array($this, 'listenToPlayerSetEventsEvent'));

$this->dispatcher->connect('dm.chess.player_clear_events', array($this, 'listenToPlayerClearEventsEvent'));

$this->dispatcher->connect('dm.chess.game_start', array($this, 'listenToGameStartEvent'));
}

public function listenToPlayerSetEventsEvent(dmChessEvent $event)
public function listenToGameStartEvent(dmChessEvent $event)
{
if(!$event->getSubject()->isAi)
foreach($event->getSubject()->Players as $player)
{
touch($this->getPlayerFile($event->getSubject()));
$this->setPlayerEventCache($player, false);
}
}

public function listenToPlayerClearEventsEvent(dmChessEvent $event)
public function setPlayerEventCache(DmChessPlayer $player, $value)
{
if(!$event->getSubject()->isAi)
if(!$player->isAi)
{
unlink($this->getPlayerFile($event->getSubject()));
$this->setPlayerCodeEventCache($player->get('code'), $value);
}
}

public function getPlayerFile(DmChessPlayer $player)
public function setPlayerCodeEventCache($playerCode, $value)
{
return $this->getDir().'/'.$player->get('code');
file_put_contents($this->getPlayerCodeFile($playerCode), $value ? '1' : '0');
}

public function getDir()
public function listenToPlayerSetEventsEvent(dmChessEvent $event)
{
$dir = sfConfig::get('sf_web_dir').'/cache/chess';
if($player = $event->getSubject()->getOpponent())
{
$this->setPlayerEventCache($player, true);
}
}

if(!is_dir($dir))
public function listenToPlayerClearEventsEvent(dmChessEvent $event)
{
if($player = $event->getSubject()->getOpponent())
{
mkdir($dir);
$this->setPlayerEventCache($player, false);
}
}

return $dir;
public function getPlayerFile(DmChessPlayer $player)
{
return $this->getPlayerCodeFile($player->get('code'));
}

public function getPlayerCodeFile($playerCode)
{
return $this->dir.'/'.$playerCode.'.txt';
}
}
2 changes: 2 additions & 0 deletions lib/model/doctrine/PluginDmChessGame.class.php
Expand Up @@ -154,6 +154,8 @@ public function preInsert($event)
public function start()
{
$this->isStarted = true;

$this->getEventDispatcher()->notify(new dmChessEvent($this, 'dm.chess.game_start'));

return $this;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/model/doctrine/PluginDmChessPlayer.class.php
Expand Up @@ -216,6 +216,8 @@ public function getStringEvents()
public function clearEvents()
{
$this->_set('events', null, false);

$this->getEventDispatcher()->notify(new dmChessEvent($this, 'dm.chess.player_clear_events'));

return $this;
}
Expand Down Expand Up @@ -336,7 +338,7 @@ public function preInsert($event)

protected function getDefaultAiLevel()
{
return 3;
return 1;
}

protected function createPiece($type, $x)
Expand Down
1 change: 1 addition & 0 deletions modules/dmChessGame/actions/actions.class.php
Expand Up @@ -117,6 +117,7 @@ public function executeWhatsUp(dmWebRequest $request)

if(empty($opponentEvents))
{
$this->getService('dm_chess_ajax_cache')->setPlayerCodeEventCache($playerCode, false);
return $this->renderJson(null);
}

Expand Down
38 changes: 31 additions & 7 deletions web/js/game.js
Expand Up @@ -162,24 +162,48 @@
beat: function()
{
var self = this;

if (self.options.game.finished)
{
return;
}

function refresh()
{
$.ajax({
url: self.options.beat.url,
dataType: "json",
error: function()
{
self.restartBeat();
},
success: function(data)
{
if (data)
{
self.updateFromJson(data);
}
self.restartBeat();
}
});
}

$.ajax({
url: self.options.beat.url,
dataType: "json",
url: self.options.beat.cache_url,
error: function()
{
self.restartBeat();
refresh();
},
success: function(data)
success: function(mustRefresh)
{
if (data)
if(0 != mustRefresh)
{
refresh();
}
else
{
self.updateFromJson(data);
self.restartBeat();
}
self.restartBeat();
}
});
},
Expand Down

0 comments on commit eaa67cc

Please sign in to comment.