<?php
/**
 * 客户端热更新相关
 * Date: 2017/9/13 0013
 * Time: 12:00
 */

//请求参数

//print_r($_FILES);
//print_r($_POST);


$subversion = trim($_POST['subversion']);//热更新名称
$actList = $_POST['act']; //act名称
$device = $_POST['device'] == 1 ? 'android' : 'ios'; //设备名称
$stagefile = $_FILES['stagefile']; //上传文件名称名称

//加了_old表示之前的
$act_old = $_POST['act_old'];
$size_old = $_POST['size_old'];
$name_old = $_POST['name_old'];
$code_old = $_POST['code_old'];
$silent_old = $_POST['silent_old'];

$old_stageStrList = '';
if ($act_old) {
    foreach ($act_old as $i => $actname) {
        $old_stageStrList .= '{size="'.$size_old[$i].'",act="'.$actname.'",name="'.$name_old[$i].'",code="'.$code_old[$i].'",silent="'.$silent_old[$i].'"},';                
    }
}

if( empty($subversion) ){
    return -1;
}
$title = getTitle($subversion);
//循环处理
$stageStrList = '{';
if ($old_stageStrList) {
    $stageStrList .= $old_stageStrList;
}
$luaFile = '';//生成的lua文件
$time = time();
$hotuploadPath = '../hotupdatefile/'.$device.'/';
foreach ($stagefile['name'] as $k => $fileName){
    if (!$fileName) {
        continue;
    }
    $uploadName = $fileName;
    $filePath = $hotuploadPath.$uploadName;
    //echo $filePath."<br />";
    $tmpName = $stagefile['tmp_name'][$k];
    move_uploaded_file($tmpName, $filePath);
    $code = md5_file($filePath);
    rename($filePath, $hotuploadPath.$code);
    $size = round($stagefile['size'][$k]/1024);
    $act = trim($actList[$k]);
    $silent = 0;

    $stageStrList .= '{size="'.$size.'",act="'.$act.'",name="'.$uploadName.'",code="'.$code.'",silent="'.$silent.'"},';
}
$stageStrList = rtrim($stageStrList, ',');
$stageStrList .= '}';


$luaFile = 'local list = {ver = "'.$subversion.'", stage = '.$stageStrList.'} return list';
file_put_contents($hotuploadPath.'flist'.$title, $luaFile);

echo "生成热更新文件成功, 点击查看生成配置文件: <a href='https://crazycoin.superant.vip/hotupdatefile/{$device}/flist{$title}'>flist{$title}</a>";


function getTitle($version){
    $arr = explode('.', $version);
    array_pop($arr);
    $new = implode('.', $arr);
    return $new;
}
