From eaa67cc122e3c963ad75a99aafad054705a1c0f2 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 20 Apr 2010 01:15:53 +0200 Subject: [PATCH] Complete dm_chess_ajax_cache service. Amazing performance improvement :) --- lib/asset/dmChessJavascriptConfig.php | 3 +- lib/cache/dmChessAjaxCache.php | 51 ++++++++++++++----- .../doctrine/PluginDmChessGame.class.php | 2 + .../doctrine/PluginDmChessPlayer.class.php | 4 +- modules/dmChessGame/actions/actions.class.php | 1 + web/js/game.js | 38 +++++++++++--- 6 files changed, 76 insertions(+), 23 deletions(-) diff --git a/lib/asset/dmChessJavascriptConfig.php b/lib/asset/dmChessJavascriptConfig.php index 75024dc..19f568e 100644 --- a/lib/asset/dmChessJavascriptConfig.php +++ b/lib/asset/dmChessJavascriptConfig.php @@ -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, diff --git a/lib/cache/dmChessAjaxCache.php b/lib/cache/dmChessAjaxCache.php index ecb32eb..9fc2097 100644 --- a/lib/cache/dmChessAjaxCache.php +++ b/lib/cache/dmChessAjaxCache.php @@ -7,7 +7,14 @@ 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() @@ -15,38 +22,54 @@ 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'; } } \ No newline at end of file diff --git a/lib/model/doctrine/PluginDmChessGame.class.php b/lib/model/doctrine/PluginDmChessGame.class.php index d34e5b9..1131587 100644 --- a/lib/model/doctrine/PluginDmChessGame.class.php +++ b/lib/model/doctrine/PluginDmChessGame.class.php @@ -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; } diff --git a/lib/model/doctrine/PluginDmChessPlayer.class.php b/lib/model/doctrine/PluginDmChessPlayer.class.php index e42c791..37ffae2 100644 --- a/lib/model/doctrine/PluginDmChessPlayer.class.php +++ b/lib/model/doctrine/PluginDmChessPlayer.class.php @@ -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; } @@ -336,7 +338,7 @@ public function preInsert($event) protected function getDefaultAiLevel() { - return 3; + return 1; } protected function createPiece($type, $x) diff --git a/modules/dmChessGame/actions/actions.class.php b/modules/dmChessGame/actions/actions.class.php index eb693e1..3d205a6 100755 --- a/modules/dmChessGame/actions/actions.class.php +++ b/modules/dmChessGame/actions/actions.class.php @@ -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); } diff --git a/web/js/game.js b/web/js/game.js index e73af4c..bbbe0c2 100644 --- a/web/js/game.js +++ b/web/js/game.js @@ -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(); } }); },