1
|
|
|
<?php |
2
|
|
|
namespace Posibrain; |
3
|
|
|
|
4
|
|
|
use Seld\JsonLint\JsonParser; |
5
|
|
|
use Seld\JsonLint\ParsingException; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
include_once (__DIR__ . '/../tools.php'); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @author Fylhan (http://fylhan.la-bnbox.fr) |
12
|
|
|
* @license LGPL-2.1+ |
13
|
|
|
*/ |
14
|
|
|
class Positroner implements IPositroner |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private static $logger = NULL; |
18
|
|
|
|
19
|
|
|
public $config; |
20
|
|
|
|
21
|
|
|
public $params; |
22
|
|
|
|
23
|
|
|
public $positrons; |
24
|
|
|
|
25
|
|
|
public $selectedPositrons; |
26
|
|
|
|
27
|
|
|
public function __construct($config = array(), $params = array()) |
28
|
|
|
{ |
29
|
|
|
// Logger |
30
|
|
View Code Duplication |
if (NULL == self::$logger) { |
|
|
|
|
31
|
|
|
self::$logger = new Logger(__CLASS__); |
32
|
|
|
if (! empty($params) && isset($params['loggerHandler'])) { |
33
|
|
|
self::$logger->pushHandler($params['loggerHandler']); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Init seed for random values |
38
|
|
|
mt_srand((double) microtime() * 1000000); |
39
|
|
|
$this->params = $params; |
40
|
|
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function listPositrons($ids = null) |
44
|
|
|
{ |
45
|
|
|
$files = glob(dirname(__FILE__) . '/Positron/*/*Positron.php'); |
46
|
|
|
if (empty($files)) |
47
|
|
|
$files = array(); |
48
|
|
|
|
49
|
|
|
$positrons = array(); |
50
|
|
|
foreach ($files as $file) { |
51
|
|
|
$path = preg_replace('!^.*(Posibrain/Positron/.*/.*Positron).*$!ui', '$1', $file); |
52
|
|
|
$name = preg_replace('!^.*Posibrain/Positron/(.*)/.*Positron.*$!ui', '$1', $file); |
53
|
|
|
if (empty($ids) || $ids === $name || (is_array($ids) && in_array($name, $ids))) |
54
|
|
|
{ |
55
|
|
|
$positrons[$name] = $path; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $positrons; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function loadPositrons($ids, $config, $params = array()) |
62
|
|
|
{ |
63
|
|
|
$positrons = $this->listPositrons($ids); |
64
|
|
|
foreach ($positrons as $positronName => $positronPath) { |
65
|
|
|
require_once (dirname(__FILE__) . '/../' . $positronPath . '.php'); |
66
|
|
|
$className = '\\' . str_replace('/', '\\', $positronPath); |
67
|
|
|
$positron = new $className($config, $params); |
68
|
|
|
$this->positrons[$positronName] = $positron; |
69
|
|
|
} |
70
|
|
|
$this->selectedPositrons = array(); |
71
|
|
|
return $this->positrons; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getPostitrons() |
75
|
|
|
{ |
76
|
|
|
if (empty($this->positrons)) |
77
|
|
|
$this->loadPositrons($this->config, $this->params); |
|
|
|
|
78
|
|
|
return $this->positrons; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function findPositron($id) |
82
|
|
|
{ |
83
|
|
|
if (! isset($this->positrons[$id])) { |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
return $this->positrons[$id]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function updatePositron($id, $state) |
90
|
|
|
{} |
91
|
|
|
|
92
|
|
|
public function isBotTriggered(TchatMessage $request, $currentValue = true) |
93
|
|
|
{ |
94
|
|
|
if (empty($this->positrons)) { |
95
|
|
|
return $currentValue; |
96
|
|
|
} |
97
|
|
|
// Positrons trigerred? |
98
|
|
|
$this->selectedPositrons = array(); |
99
|
|
|
foreach ($this->positrons as $positron) { |
100
|
|
|
if ($positron->isPositronTriggered($request)) { |
101
|
|
|
$this->selectedPositrons[] = $positron; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
// Bot trigerred? |
105
|
|
|
foreach ($this->selectedPositrons as $positron) { |
106
|
|
|
$currentValue = $positron->isBotTriggered($request, $currentValue); |
107
|
|
|
} |
108
|
|
|
return $currentValue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function analyseRequest(TchatMessage $request, AnalysedRequest $currentAnalysedRequest = null) |
112
|
|
|
{ |
113
|
|
|
if (empty($this->selectedPositrons)) { |
114
|
|
|
return new AnalysedRequest($request->getMessage(), $request->getName(), $request->getDate(), $request); |
115
|
|
|
} |
116
|
|
|
foreach ($this->selectedPositrons as $positron) { |
117
|
|
|
$currentAnalysedRequest = $positron->analyseRequest($request, $currentAnalysedRequest); |
118
|
|
|
} |
119
|
|
|
if (null == $currentAnalysedRequest) { |
120
|
|
|
$currentAnalysedRequest = new AnalysedRequest($request->getMessage(), $request->getName(), $request->getDate(), $request); |
121
|
|
|
} |
122
|
|
|
return $currentAnalysedRequest; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function loadMemory(AnalysedRequest $request, $currentMemory = null) |
126
|
|
|
{ |
127
|
|
|
if (empty($this->selectedPositrons)) { |
128
|
|
|
return $currentMemory; |
129
|
|
|
} |
130
|
|
|
foreach ($this->selectedPositrons as $positron) { |
131
|
|
|
$currentMemory = $positron->loadMemory($request, $currentMemory); |
132
|
|
|
} |
133
|
|
|
return $currentMemory; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function generateSymbolicAnswer(AnalysedRequest $request, $memory = null, TchatMessage $currentAnswer = null) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
if (empty($this->selectedPositrons)) { |
139
|
|
|
return new TchatMessage('Goxjjdp !?'); |
140
|
|
|
} |
141
|
|
|
foreach ($this->selectedPositrons as $positron) { |
142
|
|
|
$currentAnswser = $positron->generateSymbolicAnswer($request, $memory, $currentAnswer); |
143
|
|
|
} |
144
|
|
|
if (null == $currentAnswser) { |
|
|
|
|
145
|
|
|
$currentAnswser = new TchatMessage('HJJoc ?'); |
146
|
|
|
} |
147
|
|
|
return $currentAnswser; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function provideMeaning(AnalysedRequest $request, $memory, TchatMessage $answer, TchatMessage $currentAnswer = null) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
if (empty($this->selectedPositrons)) { |
153
|
|
|
return new TchatMessage('Ndfdslp !?'); |
154
|
|
|
} |
155
|
|
|
foreach ($this->selectedPositrons as $positron) { |
156
|
|
|
$currentAnswser = $positron->provideMeaning($request, $memory, $answer, $currentAnswer); |
157
|
|
|
} |
158
|
|
|
if (null == $currentAnswser) { |
|
|
|
|
159
|
|
|
$currentAnswser = new TchatMessage('Ndfdslp ?'); |
160
|
|
|
} |
161
|
|
|
return $currentAnswser; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function beautifyAnswer(AnalysedRequest $request, $memory, TchatMessage $answer, TchatMessage $currentAnswer = null) |
165
|
|
|
{ |
166
|
|
|
if (empty($this->selectedPositrons)) { |
167
|
|
|
return $currentAnswer; |
168
|
|
|
} |
169
|
|
|
foreach ($this->selectedPositrons as $positron) { |
170
|
|
|
$currentAnswser = $positron->beautifyAnswer($request, $memory, $answer, $currentAnswer); |
171
|
|
|
} |
172
|
|
|
return $currentAnswser; |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.