1
|
|
|
<?php |
2
|
|
|
namespace Posibrain\Positron\Haddock; |
3
|
|
|
|
4
|
|
|
use Monolog\Logger; |
5
|
|
|
use Posibrain\Positron\Positron; |
6
|
|
|
use Posibrain\TchatBotConfig; |
7
|
|
|
use Posibrain\AnalysedRequest; |
8
|
|
|
use Posibrain\TchatMessage; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @author Fylhan (http://fylhan.la-bnbox.fr) |
13
|
|
|
* @license LGPL-2.1+ |
14
|
|
|
*/ |
15
|
|
|
class HaddockPositron extends Positron |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private static $logger = NULL; |
19
|
|
|
|
20
|
|
|
private $config; |
21
|
|
|
|
22
|
|
|
private $insults; |
23
|
|
|
|
24
|
|
|
private $nbOfInsults; |
25
|
|
|
|
26
|
|
|
private $ponctuations; |
27
|
|
|
|
28
|
|
|
private $nbOfPonctuations; |
29
|
|
|
|
30
|
|
|
private $internalPonctuations; |
31
|
|
|
|
32
|
|
|
private $nbOfInternalPonctuations; |
33
|
|
|
|
34
|
|
|
public function __construct($config, $params = array()) |
35
|
|
|
{ |
36
|
|
|
// Logger |
37
|
|
View Code Duplication |
if (NULL == self::$logger) { |
|
|
|
|
38
|
|
|
self::$logger = new Logger(__CLASS__); |
39
|
|
|
if (! empty($params) && isset($params['loggerHandler'])) { |
40
|
|
|
self::$logger->pushHandler($params['loggerHandler']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Brain Manager |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->insults = array( |
47
|
|
|
'Bachi-bouzouk', |
48
|
|
|
'Tonnerre de Brest', |
49
|
|
|
'Mille tonnerres de Brest', |
50
|
|
|
'Misérable ectoplasme', |
51
|
|
|
'Papou des Carpathes', |
52
|
|
|
'Que le grand cric me croque et me fasse avaler ma barbe', |
53
|
|
|
'Ectoplasme à roulettes', |
54
|
|
|
'Espèce de zouave interplanétaire', |
55
|
|
|
'Sale vilaine bête de tonnerre de Brest', |
56
|
|
|
'Espèce de porc-épic mal embouché', |
57
|
|
|
'Patagon de zoulou', |
58
|
|
|
'Loup-garou à la graisse de renoncule', |
59
|
|
|
'Bougres d’extrait de crétins des Alpes', |
60
|
|
|
'Macchabée d\'eau de vaisselle', |
61
|
|
|
'Astronaute d\'eau douce', |
62
|
|
|
'Moules à gaufres', |
63
|
|
|
'Ornithorynque', |
64
|
|
|
'Macrocéphale', |
65
|
|
|
'Iconoclaste', |
66
|
|
|
'Mille millions de mille sabords' |
67
|
|
|
); |
68
|
|
|
$this->nbOfInsults = count($this->insults); |
69
|
|
|
$this->ponctuations = array( |
70
|
|
|
'!' |
71
|
|
|
); |
72
|
|
|
$this->nbOfPonctuations = count($this->ponctuations); |
73
|
|
|
$this->internalPonctuations = array( |
74
|
|
|
'?', |
75
|
|
|
'&', |
76
|
|
|
'#', |
77
|
|
|
'ټ', |
78
|
|
|
'☠', |
79
|
|
|
'@' |
80
|
|
|
); |
81
|
|
|
$this->nbOfInternalPonctuations = count($this->internalPonctuations); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isPositronTriggered(TchatMessage $request) |
85
|
|
|
{ |
86
|
|
|
return mt_rand(0, 1); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function beautifyAnswer(AnalysedRequest $request, $memory, TchatMessage $answer, TchatMessage $currentAnswer = null) |
90
|
|
|
{ |
91
|
|
|
if (null == $currentAnswer) { |
92
|
|
|
$currentAnswer = $answer; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$ponctuationSize = mt_rand(1, 3); |
96
|
|
|
$ponctuation = ''; |
97
|
|
|
for ($i = 0; $i < $ponctuationSize; $i ++) { |
98
|
|
|
if ($i == ($ponctuationSize - 1) && $ponctuationSize >= 3) { |
99
|
|
|
$internalPonctuationSize = mt_rand(1, 5); |
100
|
|
|
for ($j = 0; $j < $internalPonctuationSize; $j ++) { |
101
|
|
|
$ponctuation .= $this->internalPonctuations[mt_rand(0, $this->nbOfInternalPonctuations - 1)]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$ponctuation .= $this->ponctuations[mt_rand(0, $this->nbOfPonctuations - 1)]; |
105
|
|
|
} |
106
|
|
|
$insult = $this->insults[mt_rand(0, $this->nbOfInsults - 1)] . ' ' . $ponctuation; |
107
|
|
|
// Start with insulte |
108
|
|
|
if (mt_rand(0, 1)) { |
109
|
|
|
$message = $insult . ' ' . ucfirst($currentAnswer->getMessage()); |
110
|
|
|
} |
111
|
|
|
// End with insult |
112
|
|
|
else { |
113
|
|
|
// Add final punctuation if not yet there |
114
|
|
|
if (! preg_match('![\.\!\?;]$!', $currentAnswer->getMessage())) { |
115
|
|
|
$currentAnswer->setMessage($currentAnswer->getMessage() . ' !'); |
116
|
|
|
} |
117
|
|
|
$message = ucfirst($currentAnswer->getMessage()) . ' ' . $insult; |
118
|
|
|
} |
119
|
|
|
$currentAnswer->setMessage($message); |
120
|
|
|
return $currentAnswer; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getConfig() |
124
|
|
|
{ |
125
|
|
|
return $this->config; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function setConfig($config) |
129
|
|
|
{ |
130
|
|
|
$this->config = $config; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.