Pro/extend/Waybill/WaybillDice.php
2026-02-25 01:50:31 +08:00

177 lines
6.2 KiB
PHP

<?php
namespace Waybill;
class WaybillDice{
/**
* 露珠生成
* @param $ns
* @return array
*/
public static function createWaybill($ns){
/**************************** 计算 showRoad start ***************************/
$showRoadLocation = array();
foreach($ns as $v){
$strArray = explode(",", $v['dice_result']);
$intArray = array();
foreach ($strArray as $value){
$intArray[] = intval($value);
}
$showRoadLocation[] = $intArray;
}
/**************************** 计算 showRoad end ***************************/
/**************************** 计算 bigRoadBS start ***************************/
//列
$yKey = 0;
//行
$xKey = 0;
$bigRoadBS = array();
foreach($ns AS $key => $value) {
$value['result'] = self::resultParseBS($value['dice_result']);
$ns[$key] = $value;
}
foreach($ns AS $key => $value){
$bigRoadBS[$yKey][$xKey] = array('result' => $value['result']);
if(isset($ns[$key+1]) && $ns[$key+1]['result'] != $bigRoadBS[$yKey][$xKey]['result']){
$yKey++;
$xKey = 0;
}else{
$xKey++;
}
}
//重新计算坐标
$bigRoadBSLocation = array();
$occupy = array();
foreach($bigRoadBS AS $key => $value){
$swerve = false;
$swerveY = $key;
foreach($value AS $k => $v){
$show_y = $key;
$show_x = $k;
if($show_x > 5 && $swerve === false){
$swerveY = $swerveY + 1;
$show_y = $swerveY;
$show_x = 5;
$occupy[] = $show_y . '-' . $show_x;
}elseif(in_array($show_y.'-'.$show_x,$occupy)){
if($swerve === false){
$swerve = $show_x - 1;
}
$swerveY = $swerveY + 1;
$show_y = $swerveY;
$show_x = $swerve;
$occupy[] = $show_y . '-' . $show_x;
}elseif($swerve !== false){
$swerveY = $swerveY + 1;
$show_y = $swerveY;
$show_x = $swerve;
$occupy[] = $show_y . '-' . $show_x;
}
$pushArray = array('show_x' => $show_y+1, 'show_y' => $show_x+1, 'result' => $v['result']);
$bigRoadBSLocation[] = $pushArray;
}
}
/**************************** 计算 bigRoadBS end ***************************/
/**************************** 计算 bigRoadSP start ***************************/
//列
$yKey = 0;
//行
$xKey = 0;
$bigRoadSP = array();
foreach($ns AS $key => $value) {
$value['result'] = self::resultParseSP($value['dice_result']);
$ns[$key] = $value;
}
foreach($ns AS $key => $value){
$bigRoadSP[$yKey][$xKey] = array('result' => $value['result']);
if(isset($ns[$key+1]) && $ns[$key+1]['result'] != $bigRoadSP[$yKey][$xKey]['result']){
$yKey++;
$xKey = 0;
}else{
$xKey++;
}
}
//重新计算坐标
$bigRoadSPLocation = array();
$occupy = array();
foreach($bigRoadSP AS $key => $value){
$swerve = false;
$swerveY = $key;
foreach($value AS $k => $v){
$show_y = $key;
$show_x = $k;
if($show_x > 5 && $swerve === false){
$swerveY = $swerveY + 1;
$show_y = $swerveY;
$show_x = 5;
$occupy[] = $show_y . '-' . $show_x;
}elseif(in_array($show_y.'-'.$show_x,$occupy)){
if($swerve === false){
$swerve = $show_x - 1;
}
$swerveY = $swerveY + 1;
$show_y = $swerveY;
$show_x = $swerve;
$occupy[] = $show_y . '-' . $show_x;
}elseif($swerve !== false){
$swerveY = $swerveY + 1;
$show_y = $swerveY;
$show_x = $swerve;
$occupy[] = $show_y . '-' . $show_x;
}
$pushArray = array('show_x' => $show_y+1, 'show_y' => $show_x+1, 'result' => $v['result']);
$bigRoadSPLocation[] = $pushArray;
}
}
/**************************** 计算 bigRoadSP end ***************************/
$data = array();
$data['showRoad'] = $showRoadLocation;
$data['bigRoadBS'] = $bigRoadBSLocation;
$data['bigRoadSP'] = $bigRoadSPLocation;
return (['status'=>true, 'msg'=>'数据存在', 'waybill'=>$data]);
}
// 结果分析(大小)
public static function resultParseBS($resultString){
$resultArray = explode(",", $resultString);
$intResultArray = [];
foreach ($resultArray as $v){
$intResultArray[] = intval($v);
}
$total = array_sum($intResultArray);
// 判断是不是全部值都是一样的
$uniqueArray = array_unique($intResultArray);
if (count($uniqueArray) == 1) {
return 3;
} else {
if ($total >= 4 && $total <= 10) {
return 2;
} else {
return 1;
}
}
}
// 结果分析(单双)
public static function resultParseSP($resultString){
$resultArray = explode(",", $resultString);
$intResultArray = [];
foreach ($resultArray as $v){
$intResultArray[] = intval($v);
}
$total = array_sum($intResultArray);
// 判断是不是全部值都是一样的
$uniqueArray = array_unique($intResultArray);
if (count($uniqueArray) == 1) {
return 3;
} else {
if ($total % 2 == 0) {
return 1;
} else {
return 2;
}
}
}
}