php一起来捉妖配猫

32F  2019/04/29 php 游戏


php一起来捉妖简单配猫程序,

运行代码前需要数据入库
pdo连接,参数自行配置


/**
 * 配猫
 * @param string $name  拥有人
 * @param boolean $match  true输出存在匹配的结果,false输出所有结果(包括是否无匹配的)
 * @return array
*/
function checkCat($name='',$match=true){
    $cat = [
        'A'=>[//cv
            [//dv
                'need'=>['小丑假面', '美人尖'],
                'get'=>'一叶障目',
                'part'=>'额纹'
            ],
            [
                'need'=>['螺旋封印','凯撒眼圈'],
                'get'=>'爱的魔法',
                'part'=>'额纹'
            ],
            [
                'need'=>['雨花台','力量印记'],
                'get'=>'爱的魔法',
                'part'=>'额纹'
            ],
            [
                'need'=>['钻石甜心','小猫妙盗'],
                'get'=>'超能蒙面',
                'part'=>'肚皮'
            ],
            [
                'need'=>['太空钻石','肌肉战士'],
                'get'=>'煤堆探索',
                'part'=>'肚皮'
            ]
            ,
            [
                'need'=>['小好奇','鲨鱼牙'],
                'get'=>'咬牙切齿',
                'part'=>'嘴巴'
            ],
            [
                'need'=>['忍耐','愤怒'],
                'get'=>'开心派',
                'part'=>'嘴巴'
            ],
            [
                'need'=>['怒发冲冠','呆萌宝宝'],
                'get'=>'好怕怕',
                'part'=>'眼睛'
            ],
            [
                'need'=>['专心致志','俏皮甜心'],
                'get'=>'圆点点',
                'part'=>'眼睛'
            ],
            [
                'need'=>['夜猫眼','眯眯眼'],
                'get'=>'海蓝宝石',
                'part'=>'眼睛'
            ],
            [
                'need'=>['死鱼眼','街头霸猫'],
                'get'=>'童话萌猫',
                'part'=>'眼睛'
            ]
        ],
        'B'=>[
            [
                'need'=>['爱的魔法','确认眼神','小软糖'],
                'get'=>'以爱之名'
            ],
            [
                'need'=>['煤堆探索','圆点点','一本正经','小狮尾'],
                'get'=>'侏罗纪'
            ]
        ],
       'C'=>[
            [
                'need'=>['草莓饼干','告白气球'],
                'get'=>'生如夏花'
            ],
            [
                'need'=>['香草','软糖'],
                'get'=>'折耳'
            ],
            [
                'need'=>['竖耳','小尖尖'],
                'get'=>'小毛毛'
            ],
            [
                'need'=>['折耳','小毛毛'],
                'get'=>'奶酪'
            ]
        ],
    ];
    $dsn = 'mysql:dbname=dbname;host=127.0.0.1';
    $user = 'username';
    $password = 'password';
    try {
        $pdo = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    $pdo->exec("set names 'utf8'");
    if($name)
        $sql = " select * from cat where own = '{$name}' and is_deleted = 0 ";
    else
        $sql = " select * from cat where is_deleted = 0 ";
    $stmt = $pdo->prepare($sql);
    $rs = $stmt->execute();
    $mine = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $ret = [];
    if($mine){
        foreach ($mine as $v){
            $ret[$v['id']]['name'] = $v['name'];//名称
            $ret[$v['id']]['level'] = $v['level'];//代数
            $ret[$v['id']]['own'] = $v['own']; //拥有者
            $has = $has_str = $need = $need_str = [];
            foreach ($cat as $ck=>$cv){
                foreach ($cv as $dk=>$dv){
                    foreach ($dv['need'] as $ek=>$ev){
                        if(mb_strpos($v['content'],$ev) !== false){
                            $has[] = $ck.($dk+1).($ek+1);
                            $has_str[] = $ev;
                            $d = $dv['need'];
                            array_splice($d,$ek,1);
                            $need = array_merge($need,$d);
                        }
                    }
                    if(mb_strpos($v['content'],$dv['get']) !== false){
                        $has[] = $ck.($dk+1);
                        $has_str[] = $ev;
                    }
                }
            }
            $ret[$v['id']]['has'] = !empty($has) ? implode(',',$has) : '';
            $ret[$v['id']]['has_str'] = !empty($has_str) ? implode(',',$has_str) : '';
            $ret[$v['id']]['need'] = !empty($need) ? implode(',',$need) : '';
            $ret[$v['id']]['match'] = [];
        }
    }

    //echo json_encode($ret);
    //其他人
    if($name)
        $sql = " select * from cat where own != '{$name}' and is_deleted = 0 ";
    else
        $sql = " select * from cat where is_deleted = 0 ";
    $stmt = $pdo->prepare($sql);
    $rs = $stmt->execute();
    $other = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if($ret){
        foreach ($ret as $k=>&$v){
            $need = explode(',',$v['need']);
            foreach ($other as $ov){
                if($v['own'] == $ov['own'])
                    continue;
                $other_has = [];
                foreach ($need as $nv){
                    if(mb_strpos($ov['content'],$nv) !== false){
                        $other_has[] = $nv;
                    }
                }
                if(!empty($other_has)){
                    $v['match'][] = [
                        'id'=>$ov['id'],
                        'own'=>$ov['own'],
                        'name'=>$ov['name'],
                        'level'=>$ov['level'],
                        'has'=>implode(',',$other_has)
                    ];
                }
            }
            //只输出匹配项
            if($match && empty($v['match'])){
                unset($ret[$k]);
            }
        }
        unset($v);
    }
    return $ret;
}

$ret = checkCat('小明',true);
echo json_encode($ret);

表结构

CREATE TABLE `cat` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `content` varchar(255) DEFAULT NULL COMMENT '属性',
  `name` varchar(20) DEFAULT NULL COMMENT '名称',
  `level` tinyint(2) DEFAULT NULL COMMENT '等级',
  `own` varchar(10) DEFAULT NULL COMMENT '拥有人',
  `is_deleted` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;

结果json格式输出


添加评论
全部回复
1f 32F  回复 32F 2019-07-13 23:20:38

123回复


2f 32F  2019-07-13 23:20:30

111回复


3f 32F  2019-06-03 16:05:10

公式有误,且猫品种基因较为容易辨认,这种方式有点冗余,脑子记住就行回复