game2006api/webapp/models/ChipPage.php
aozhiwei ba867c7697 1
2024-09-12 13:45:30 +08:00

212 lines
6.2 KiB
PHP

<?php
namespace models;
require_once('models/Chip.php');
require_once('services/ChipPageService.php');
use phpcommon\SqlHelper;
use services\ChipPageService;
class ChipPage extends BaseModel
{
const MAX_CHIP_SLOT_NUM = 9;
public static function find($hero_unnid){
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_chip_page',
array(
'hero_uniid' => $hero_unnid
)
);
return $row;
}
public static function dataToHash($row){
$dataHash = array();
$data = emptyReplace(json_decode($row['data'], true), array());
array_walk($data, function ($row) use(&$dataHash) {
$dataHash[$row['slot_id']] = $row;
});
return $dataHash;
}
public static function getUnlockCount($row , $slotType = 0){
$data = emptyReplace(json_decode($row['data'], true), array());
$count = 0;
if ($slotType == 0){
foreach ($data as $value){
if($value['state'] == 1){
$count += 1;
}
}
}else{
foreach ($data as $value){
if($slotType == $value['slot_type'] && $value['state'] == 1){
$count += 1;
}
}
}
return $count;
}
public static function unlockSlot($row,$slot){
$data = emptyReplace(json_decode($row['data'], true), array());
foreach ($data as $key=>$value){
if ($value['slot_id'] == $slot){
$data[$key]['state'] = 1;
}
}
self::update($row['hero_uniid'],array(
'data' => json_encode($data)
));
}
public static function toDtoInfo($row){
$data = emptyReplace(json_decode($row['data'], true), array());
foreach ($data as &$value){
$chipDb = Chip::find($value['chip_id']);
if ( !$chipDb ) {
Chip::updateInlayState($value['chip_id'],0);
$value['chip_id'] = 0;
}
}
self::update($row['hero_uniid'],array(
'data' => json_encode($data)
));
$newRow = self::find($row['hero_uniid']);
$newData = emptyReplace(json_decode($newRow['data'], true), array());
$attrs = array();
foreach ($newData as $key=>$value){
$newData[$key]['item_id'] = 0;
if ($value['chip_id']){
$chipDb = Chip::find($value['chip_id']);
$newData[$key]['item_id'] = $chipDb['item_id'];
$newData[$key]['quality'] = $chipDb['quality'];
$rand_attr = emptyReplace(json_decode($chipDb['rand_attr'], true), array());
foreach ($rand_attr as $val){
array_push($attrs,$val);
}
}
}
$item = [];
foreach ($attrs as $k=>$v){
if (!isset($item[$v['attr_id']])){
$item[$v['attr_id']] = $v;
}else{
$item[$v['attr_id']]['val']+= $v['val'];
}
}
$info = array(
'idx' => $newRow['idx'],
'hero_uniid' => $newRow['hero_uniid'],
'data' => $newData,
'attr' => $item
);
return $info;
}
public static function toDtoBattle($row){
if (!$row){
return $row;
}
$data = emptyReplace(json_decode($row['data'], true), array());
if ($data){
foreach ($data as &$value){
$chipDb = Chip::find($value['chip_id']);
if ( !$chipDb ) {
Chip::updateInlayState($value['chip_id'],0);
$value['chip_id'] = 0;
}
}
self::update($row['hero_uniid'],array(
'data' => json_encode($data)
));
}
$newRow = self::find($row['hero_uniid']);
$newData = emptyReplace(json_decode($newRow['data'], true), array());
$attrs = array();
foreach ($newData as $key=>$value){
if ($value['chip_id']){
$chipDb = Chip::find($value['chip_id']);
$rand_attr = emptyReplace(json_decode($chipDb['rand_attr'], true), array());
foreach ($rand_attr as $val){
array_push($attrs,$val);
}
}
}
$item = [];
$i = 0;
foreach ($attrs as $k=>$v){
$item['attr_' . $i] = $v;
++$i;
/*
if (!isset($item[$v['attr_id']])){
$item[$v['attr_id']] = $v;
}else{
$item[$v['attr_id']]['val']+= $v['val'];
}*/
}
return $item;
}
public static function update($hero_uniid,$fieldsKv){
SqlHelper::update
(myself()->_getSelfMysql(),
't_chip_page',
array(
'hero_uniid'=> $hero_uniid,
),
$fieldsKv
);
}
public static function addChipPage($hero_uniid){
$data = array();
for ($i=1;$i <= self::MAX_CHIP_SLOT_NUM;$i++){
if ( $i<=3 ){
$slotType = 1;
}elseif($i>3 && $i <= 6){
$slotType = 2;
}else{
$slotType = 3;
}
array_push(
$data,
array(
'slot_id'=>$i,
'slot_type'=>$slotType,
'state'=>0,
'chip_id'=>0,
)
);
}
$info = array(
'account_id' => myself()->_getAccountId(),
'hero_uniid' => $hero_uniid,
'data' => json_encode($data),
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
);
// SqlHelper::insert(
// myself()->_getSelfMysql(),
// 't_chip_page',
// $info
// );
SqlHelper::upsert
(myself()->_getSelfMysql(),
't_chip_page',
array(
'hero_uniid' => $hero_uniid,
),
array(),
$info
);
}
}