Skip to content

Commit

Permalink
Introduce the dm_chess_ajax_cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 19, 2010
1 parent ce4d65f commit 95d33e9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
7 changes: 7 additions & 0 deletions config/dm/services.yml
Expand Up @@ -5,6 +5,8 @@ parameters:
square_class: dmChessSquare

dm_chess_event_log.class: dmChessEventLog

dm_chess_ajax_cache.class: dmChessAjaxCache

dm_chess_asset_loader.class: dmChessAssetLoader

Expand Down Expand Up @@ -51,6 +53,11 @@ services:
shared: true
arguments: [ @dispatcher ]

dm_chess_ajax_cache:
class: %dm_chess_ajax_cache.class%
shared: true
arguments: [ @dispatcher ]

dm_chess_asset_loader:
class: %dm_chess_asset_loader.class%
shared: false
Expand Down
6 changes: 3 additions & 3 deletions config/dmChessPluginConfiguration.class.php
Expand Up @@ -15,12 +15,12 @@ public function initialize()

public function listenToContextLoadedEvent(sfEvent $e)
{
$this->eventLog = $e->getSubject()->get('event_log');

$this->eventLog->setOption('ignore_models', array_merge($this->eventLog->getOption('ignore_models'), array(
$e->getSubject()->get('event_log')->setOption('ignore_models', array_merge($this->eventLog->getOption('ignore_models'), array(
'DmChessGame',
'DmChessPlayer'
)));

$e->getSubject()->get('dm_chess_ajax_cache')->connect();

$this->dispatcher->connect('dm.chess.piece_move', array($this, 'listenToPieceMoveEvent'));

Expand Down
52 changes: 52 additions & 0 deletions lib/cache/dmChessAjaxCache.php
@@ -0,0 +1,52 @@
<?php

class dmChessAjaxCache
{
protected $dispatcher;
protected $dir;

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

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'));
}

public function listenToPlayerSetEventsEvent(dmChessEvent $event)
{
if(!$event->getSubject()->isAi)
{
touch($this->getPlayerFile($event->getSubject()));
}
}

public function listenToPlayerClearEventsEvent(dmChessEvent $event)
{
if(!$event->getSubject()->isAi)
{
unlink($this->getPlayerFile($event->getSubject()));
}
}

public function getPlayerFile(DmChessPlayer $player)
{
return $this->getDir().'/'.$player->get('code');
}

public function getDir()
{
$dir = sfConfig::get('sf_web_dir').'/cache/chess';

if(!is_dir($dir))
{
mkdir($dir);
}

return $dir;
}
}
3 changes: 3 additions & 0 deletions lib/model/doctrine/PluginDmChessPlayer.class.php
Expand Up @@ -191,6 +191,9 @@ public function is(DmChessPlayer $player)
public function setEvents($events)
{
$this->_set('events', json_encode($events), false);

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

return $this;
}

Expand Down

0 comments on commit 95d33e9

Please sign in to comment.