/
dmChessCrafty.php
87 lines (70 loc) · 1.99 KB
/
dmChessCrafty.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
class dmChessCrafty extends dmConfigurable
{
protected
$serviceContainer;
public function __construct(dmBaseServiceContainer $serviceContainer, array $options)
{
$this->serviceContainer = $serviceContainer;
$this->initialize($options);
}
protected function initialize(array $options)
{
$this->configure($options);
$this->setOption('result_file', dmOs::join(sfConfig::get('sf_cache_dir'), '/crafty_'.dmString::random()));
}
public function execute($forsythe)
{
return $this->runPlayCommand($forsythe);
}
protected function runPlayCommand($forsytheNotation)
{
$fs = $this->serviceContainer->get('filesystem');
$file = $this->getOption('result_file');
$fs->mkdir(dirname($file));
touch($file);
$fs->chmod($file, 0777);
file_put_contents($file, '');
$command = $this->getPlayCommand($forsytheNotation);
ob_start();
passthru($command, $code);
$return = ob_get_clean();
if($code !== 0)
{
throw new dmChessCraftyException(sprintf('Can not run crafty: '.$command.' '.$return));
}
$forsythe = $this->extractForsythe(file($file, FILE_IGNORE_NEW_LINES));
if(!$forsythe)
{
throw new dmChessCraftyException(sprintf('Can not run crafty: '.$command.' '.$return));
}
$fs->remove($file);
return $forsythe;
}
protected function extractForsythe($results)
{
return str_replace('setboard ', '', $results[0]);
}
protected function getPlayCommand($forsytheNotation)
{
return sprintf("cd %s && %s log=off ponder=off %s <<EOF
setboard %s
move
savepos %s
quit
EOF",
dirname($this->getOption('result_file')),
$this->getOption('path'),
$this->getCraftyLevel(),
$forsytheNotation,
basename($this->getOption('result_file'))
);
}
protected function getCraftyLevel()
{
/*
* st is the time in seconds crafty can think about the situation
*/
return "st=".(round($this->getOption('level')/10, 2));
}
}