Skip to content

Commit e539641

Browse files
committed
:tada
1 parent 306fe71 commit e539641

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
composer.phar
22
/vendor/
3+
.idea
4+
composer.lock
35

46
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
57
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "vbot/guess-number",
3+
"license": "MIT",
4+
"authors": [
5+
{
6+
"name": "HanSon",
7+
"email": "[email protected]"
8+
}
9+
],
10+
"require-dev": {
11+
"hanson/vbot": "^2.0"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"Vbot\\GuessNumber\\": "src/"
16+
}
17+
}
18+
}

src/GuessNumber.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Vbot\GuessNumber;
4+
5+
use Hanson\Vbot\Extension\AbstractMessageHandler;
6+
use Hanson\Vbot\Message\Text;
7+
use Illuminate\Support\Collection;
8+
9+
class GuessNumber extends AbstractMessageHandler
10+
{
11+
12+
public $author = 'HanSon';
13+
14+
public $version = '1.0';
15+
16+
public $name = 'guess_number';
17+
18+
public $zhName = '猜数字';
19+
20+
private static $array = [];
21+
22+
public function handler(Collection $message)
23+
{
24+
if ($message['type'] === 'text' && $message['fromType'] === 'Group') {
25+
$username = $message['from']['UserName'];
26+
27+
$isBegin = isset(static::$array[$username]);
28+
29+
if ($message['pure'] === '猜数字') {
30+
if ($isBegin) {
31+
Text::send($username, '猜数字已经开始,还没结束呢');
32+
Text::send($username, '当前区间为:'.static::$array[$username]['begin'].''.static::$array[$username]['end']);
33+
} else {
34+
Text::send($username, '猜数字开始,请猜一个 1 ~ 99 的数字,中了就赢了哦');
35+
static::$array[$username] = [
36+
'begin' => 0,
37+
'end' => 100,
38+
'target' => random_int(1, 99),
39+
];
40+
}
41+
} elseif (is_numeric($message['content']) && $isBegin) {
42+
$message['content'] = intval($message['content']);
43+
$target = static::$array[$username]['target'];
44+
if ($message['content'] > static::$array[$username]['begin'] && $message['content'] < static::$array[$username]['end']) {
45+
if ($message['content'] == $target) {
46+
Text::send($username, $message['sender']['NickName'].'你赢了!数字就为:'.static::$array[$username]['target']);
47+
unset(static::$array[$username]);
48+
} elseif ($message['content'] > $target) {
49+
Text::send($username, '当前区间为:'.static::$array[$username]['begin'].''.$message['content']);
50+
static::$array[$username]['end'] = $message['content'];
51+
} else {
52+
Text::send($username, '当前区间为:'.$message['content'].''.static::$array[$username]['end']);
53+
static::$array[$username]['begin'] = $message['content'];
54+
}
55+
}
56+
}
57+
}
58+
}
59+
60+
/**
61+
* 注册拓展时的操作.
62+
*/
63+
public function register()
64+
{
65+
66+
}
67+
}

0 commit comments

Comments
 (0)