<?php

require_once realpath(dirname(__FILE__)) . '/../common.php';

ini_set("display_errors", "On");
error_reporting(E_ALL & ~E_NOTICE);

$horseNum = 8;       // 马数量
$stageNum = 5;       // 舞台数量
$stageLength = 120;    // 舞台长度

$horseList = initHorse($horseNum);

$map = [];
for ($n = 0; $n < 10000; $n++) {
    $eventList = initEvent($stageNum);

    $winnerIndex = 0;
    $minTime = 2000;
    foreach ($horseList as $i => $horse) {
        // 第一阶段
        $total_time = 0;
        for ($j = 0; $j < $stageNum; $j++) {
            $weatherNum = $eventList[$j][0]; // 第一个数字是天气
            $eventNum = $eventList[$j][$i];    // 当前马匹的事件

            $bufferSpeed = getBufferSpeed($weatherNum, $eventNum, $horse);
            $time = $stageLength / $bufferSpeed;
            $total_time += $time;
            $horse['time' . $j] = $time;

            $weatherStr = getWeatherStr($weatherNum);
            $eventStr = getEventStr($eventNum);
//            EosBase::log("NO.{$n}, {$i}th horse round {$j}, {$weatherStr} {$eventStr}, bufferSpeed:{$bufferSpeed}, speed total:{$total_time}, time: {$time}");

        }

        $horse['total_time'] = round($total_time);
        if ($minTime > $total_time) {
            $minTime = $total_time;
            $winnerIndex = $i;
        }

        $horseList[$i] = $horse;
    }

    $total_time_list = array_column($horseList, 'total_time');
    $map[$winnerIndex]++;
    $minTime = round($minTime);
//    EosBase::log("++++++++++++++++ NO.{$n} game, winner: {$winnerIndex}, time:{$minTime} ++++++++++++++++++, list:" . join(", ", $total_time_list));
}

ksort($map);
foreach ($map as $i => $times) {
    $horse = $horseList[$i];
    EosBase::log("第{$i}只马，获胜{$times}次, 总能力:{$horse['ability']}, 速度:{$horse['speed']}, 跳跃:{$horse['jump']}, 敏捷:{$horse['agility']}, 幸运:{$horse['luck']}, 意志:{$horse['volition']}");
}

function getWeatherStr($weatherNum) {
    if ($weatherNum == 0) {
        return 'weather:wind, ';
    } else if ($weatherNum == 1) {
        return 'weather:snow, ';
    } else {
        return '';
    }
}

function getEventStr($eventNum) {
    switch ($eventNum) {
        case 0:
        case 1:
        case 2:
            return "event:石头, ";
        case 4:
        case 5:
        case 6:
            return "event:泥坑, ";
        case 7:
        case 8:
            return "event:加速, ";
    }

    return 'event:正常';
}


function getBufferSpeed($weatherNum, $eventNum, $horse) {
    $speed = $horse['speed'];

    // 0 属于正常跑
    // 1 ~ 3 属于 石头，靠跳跃 0.3
    // 4 ~ 6 属于 泥坑，靠敏捷 0.3
    // 7 ~ 9 属于 Buffer，靠幸运 0.3
    switch ($eventNum) {
        case 0:
        case 1:
        case 2:
            $speed *= $horse['jump'] / 100;
            break;
        case 4:
        case 5:
        case 6:
            $speed *= $horse['agility'] / 100;
            break;
        case 7:
        case 8:
            $speed += $horse['luck'] + 10;
            break;
    }

    // 0、1 属于 逆风、下雪，靠意志 0.2
    if ($weatherNum == 0 || $weatherNum == 1) {
        $speed *= $horse['volition'] / 100;
    }

    $speed = max($speed, 10);
    return $speed;
}

function initHorse($horseNum) {
    $horseList = [];
    for ($i = 0; $i < $horseNum; $i++) {
        $horse = [
            "speed" => rand(20, 90),    // 速度
            "jump" => rand(1, 90),      // 跳跃
            "agility" => rand(1, 90),   // 敏捷
            "luck" => rand(20, 90),     // 幸运
            "volition" => rand(10, 90), // 意志
        ];

        foreach ($horse as $value) {
            $horse['ability'] += $value;
        }

        $horseList[] = $horse;

        EosBase::log("第{$i}只马: 速度:{$horse['speed']}, 跳跃:{$horse['jump']}, 敏捷:{$horse['agility']}, 幸运:{$horse['luck']}, 意志:{$horse['volition']}");
    }

    return $horseList;
}

function initEvent($stageNum) {
    $list = [];
    for ($num = $stageNum; $num >= 1; $num--) {
        $numList = [];
        for ($i = 0; $i < 9; $i++) {
//            $numList[] = rand(0, 9);
            $numList[] = rand(0, 9);
        }

        $list[] = $numList;
    }

    return $list;
}







