|
| 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