<?php

namespace tw\lib\robot;

use think\facade\Log;
use tw\command\console;
use tw\redis\RobotsInfoRds;

/**
 * 手动指定 robot 用户资料
 * 
 * robot 用户昵称头像要求固定，level, vip 可以不定期升级
 * 
 */
class Robots
{

    // 昵称
    static public $nicknames = [
        "萌傻卿",
        "勇敢的小萝卜",
        "且听风铃",
        "我去叫彭于晏",
        "晚点相遇",
        "トキメク",
        "Coisini",

        "给你一口甜",
        "是小宝阿",
        "星河几重",
        "养了一个月亮",
        "白日梦我",
        "CD女王",
        "百事可爱",

        "一觉睡到小时候",
        "Pumpkin",
        "咋又饿了呢",
        "Kilig",
        "幸识",
        "433",
        "搬砖小土妞",

        "初遇",
        "睡了",
        "可口可爱",
        "咕噜叽叽",
        "whaoe",
        "超级消烦员",
        "萝莉啰嗦",

        "骑着蜗牛追导弹",
        "月亮邮递员",
        "小公举",
        "智慧女孩不要秃头",
        "清蒸肉肉",
        "联合国认证小可爱",
        "给糖就不闹",

        "激萌美少女李逵",
        "文件转输助手",
        "沙雕少女",
        "来了老弟",
        "我妈妈告诉我要欺负你们",
        "看瓜少年和猹",
        "超级凶傲呜",

        "拉风的鼻涕",
        "作业不多吗",
        "我是你爸爸",
        "很秀但没必要",
        "有关部门",
        "二三",
        "孙尚香",

        "芝士就是力量",
        "颈上鲜草莓",
        "你家小祖宗",
        "蟑螂恶霸",
        "胖大海",
        "蔡文姬腿堡",
        "美少女壮士",

        "萝卜蹲",
        "祖国老花朵",
        "脑子里有个泡",
        "萌够就回家",
        "怪力少女",
        "new bee",
        "你爸爸我",

        "铁锤妹妹",
    ];  // 64 N

    // 头像 base url
    static protected $HEADER_BASE_URL = 'http://twongstatic.shotshock.shop/headers/wechat/';
    /**
     * 头像，七牛保存，名称按 1-64 的 png 图片，所以可以生成 url 
     */
    static public $headers = [];

    // robot 起始 ID
    static protected $ROBOT_BASE_ID = 44040001;

    /**
     *  "uid" => 123,
     *  "avatar" => "http://2.png",
     *  "nickname" => "xxx",
     *  "level" => 1,
     *  "value" => 23.3,
     *  "border" => 1,
     *  "vip" => 2,
     */
    static public $robots = []; // N x 3 機器人

    /**
     * 构建机器人信息。
     * 
     * 可重现：多次构建结果应一致
     * 
     * TODO:结果存入 redis
     * 
     * @return boolean
     */
    public static function build(): bool
    {
        self::_build_headers();

        if (
            count(self::$nicknames) != count(self::$headers) ||
            count(self::$nicknames) != 64
        ) {
            Log::error('nickname or headers has wrong numbers');
            return false;
        }

        // clear
        self::$robots = [];

        for ($i = 0; $i < 64; $i++) {
            $uid = self::$ROBOT_BASE_ID + $i;
            self::$robots[$uid] = [
                'uid' => $uid,
                'avatar' => self::$headers[$i],
                'nickname' => self::$nicknames[$i],
                'level' => tw_rand(1, 4),
                'value' => 0,
                'border' => 0,
                'vip' => 0,
            ];
        } // for

        return true;
    }

    /**
     * 如果没有操作七牛中文件，不要修改本函数。
     * 
     * 本函数不是用于生成，而是对已有资源的描述/信息提取。
     */
    protected static function _build_headers()
    {
        // clear
        self::$headers = [];

        for ($i = 1; $i <= 64; $i++) {
            self::$headers[] = self::$HEADER_BASE_URL . $i . '.png';
        }
    }

    /**
     * 把成員 robots 寫入 redis
     */
    public static function to_redis()
    {
        if (count(self::$robots) <= 0) {
            self::build();
        }

        if (count(self::$robots) <= 0) {
            errlog('build robots failed?');
            return;
        }

        foreach (self::$robots as $robot) {
            (new RobotsInfoRds)->hset(false, $robot['uid'], $robot);
        }
    }

    /**
     * 加载 redis 数据到 self::$robots
     */
    public static function from_redis()
    {
        $robots = (new RobotsInfoRds)->hget_all(false);
        foreach ($robots as $k => $v) {
            $robots[$k] = json_decode($v, true);
        }
        self::$robots = $robots;
    }

    /**
     * 下單，隨機選擇若幹機器人，模擬下單(增加 value 值)
     * 
     * 排行時，簡單取出所有機器人數據，內存排序即可
     */
    public static function order()
    {
        if (count(self::$robots) <= 0) {
            self::from_redis();
        }

        if (count(self::$robots) <= 0) {
            errlog("load robots from redis failed.");
            return;
        }
        // 下单人数
        $robot_num = tw_rand(0, count(self::$robots) / 2);

        $min = sys_config_int('leader_robot_earn_min');
        $max = sys_config_int('leader_robot_earn_max');
        if ($min > $max) { // swap
            $t = $min;
            $min = $max;
            $max = $t;
        }
        debuglog("there will be $robot_num robot making order. will earn: min=$min,max=$max");
        
        for ($i = 0; $i < $robot_num; $i++) {
            // 下单人UID
            $index = tw_rand(0, count(self::$robots));
            $uid = self::$ROBOT_BASE_ID + $index;
            // 获利金额
            $value = tw_divf(tw_rand($min * 100, $max * 100), 100.0, 2);

            // get - update - set
            $srobot = (new RobotsInfoRds)->hget(false, $uid);
            if (!$srobot) {
                console::log('not found: uid=' . $uid);
            }
            $robot = json_decode($srobot, true);
            $robot['value'] = tw_addf($robot['value'], $value, 2);

            (new RobotsInfoRds)->hset(false, $uid, $robot);

            debuglog("robot $uid order win $value");
        }
    }

    /**
     * 重置所有機器人 value 字段
     */
    public static function reset()
    {
        return self::build();
    }

    /**
     * 根據 value 字段的前 n 個
     */
    public static function first_n_by_value(int $n): array
    {
        $robots = (new RobotsInfoRds)->hget_all(false);
        foreach ($robots as $uid => $robot) {
            $robots[$uid] = json_decode($robot, true);
        }
        usort($robots, function ($l, $r) {
            return $r['value'] <=> $l['value'];
        });
        return array_slice($robots, 0, $n);
    }
}
