238 lines
7.1 KiB
PHP
238 lines
7.1 KiB
PHP
<?php
|
|
|
|
|
|
namespace models;
|
|
require_once('models/Chip.php');
|
|
require_once('mt/ChipAttr.php');
|
|
require_once('services/ChipPageService.php');
|
|
|
|
use mt\ChipAttr;
|
|
use phpcommon\SqlHelper;
|
|
use services\ChipPageService;
|
|
|
|
class ChipPage extends BaseModel
|
|
{
|
|
|
|
public static function find($page){
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_chip_page',
|
|
array(
|
|
'account_id'=> myself()->_getAccountId(),
|
|
'page_id' => $page
|
|
)
|
|
);
|
|
if (!$row){
|
|
return null;
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
public static function getList($cb){
|
|
SqlHelper::ormSelect(
|
|
myself()->_getSelfMysql(),
|
|
't_chip_page',
|
|
array(
|
|
'account_id'=> myself()->_getAccountId(),
|
|
),
|
|
function ($row) use($cb) {
|
|
$cb($row);
|
|
}
|
|
);
|
|
}
|
|
|
|
public static function toDto($row){
|
|
return array(
|
|
'idx' => $row['idx'],
|
|
'account_id' => $row['account_id'],
|
|
'page_name' => $row['page_name'],
|
|
'page_id' => $row['page_id'],
|
|
'createtime' => $row['createtime'],
|
|
);
|
|
}
|
|
|
|
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 ) {
|
|
$value['chip_id'] = 0;
|
|
}
|
|
}
|
|
self::update($row['page_id'],array(
|
|
'data' => json_encode($data)
|
|
));
|
|
$newRow = self::find($row['page_id']);
|
|
$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'];
|
|
$chipAttrMeta = ChipAttr::getAttrByItemId($chipDb['item_id']);
|
|
$rand_attr = emptyReplace(json_decode($chipDb['rand_attr'], true), array());
|
|
array_push($attrs,array(
|
|
'attr_id'=>$chipAttrMeta['attr_id'],
|
|
'val'=>$chipAttrMeta['lv'.$chipDb['chip_grade']],
|
|
));
|
|
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'],
|
|
'page_id' => $newRow['page_id'],
|
|
'page_name' => $newRow['page_name'],
|
|
'data' => $newData,
|
|
'attr' => $item
|
|
);
|
|
return $info;
|
|
}
|
|
|
|
public static function toDtoBattle($row){
|
|
$data = emptyReplace(json_decode($row['data'], true), array());
|
|
foreach ($data as &$value){
|
|
$chipDb = Chip::find($value['chip_id']);
|
|
if ( !$chipDb ) {
|
|
$value['chip_id'] = 0;
|
|
}
|
|
}
|
|
self::update($row['page_id'],array(
|
|
'data' => json_encode($data)
|
|
));
|
|
$newRow = self::find($row['page_id']);
|
|
$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'];
|
|
$chipAttrMeta = ChipAttr::getAttrByItemId($chipDb['item_id']);
|
|
$rand_attr = emptyReplace(json_decode($chipDb['rand_attr'], true), array());
|
|
array_push($attrs,array(
|
|
'attr_id'=>$chipAttrMeta['attr_id'],
|
|
'val'=>$chipAttrMeta['lv'.$chipDb['chip_grade']],
|
|
));
|
|
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'];
|
|
}
|
|
}
|
|
return $item;
|
|
}
|
|
|
|
public static function updatePage($page,$slot_id,$chip_id){
|
|
$row = self::find($page);
|
|
$data = emptyReplace(json_decode($row['data'], true), array());
|
|
foreach ($data as &$value){
|
|
if ($value['slot_id'] == $slot_id){
|
|
$value['chip_id'] = $chip_id;
|
|
}
|
|
}
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_chip_page',
|
|
array(
|
|
'account_id'=> myself()->_getAccountId(),
|
|
'page_id' => $page
|
|
),
|
|
array(
|
|
'data'=> json_encode($data)
|
|
)
|
|
);
|
|
}
|
|
|
|
public static function update($page,$fieldsKv){
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_chip_page',
|
|
array(
|
|
'account_id'=> myself()->_getAccountId(),
|
|
'page_id' => $page
|
|
),
|
|
$fieldsKv
|
|
);
|
|
}
|
|
|
|
public static function addChipPage(){
|
|
$data = array();
|
|
for ($i=1;$i <= ChipPageService::MAX_CHIP_SLOT_NUM;$i++){
|
|
array_push(
|
|
$data,
|
|
array(
|
|
'slot_id'=>$i,
|
|
'state'=>0,
|
|
'chip_id'=>0,
|
|
)
|
|
);
|
|
}
|
|
$rows = SqlHelper::ormSelect(
|
|
myself()->_getSelfMysql(),
|
|
't_chip_page',
|
|
array(
|
|
'account_id'=> myself()->_getAccountId()
|
|
)
|
|
);
|
|
$page_id = 1;
|
|
if ($rows){
|
|
$page_id = count($rows)+1;
|
|
}
|
|
$info = array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'page_name' => 'page'.$page_id,
|
|
'page_id' => $page_id,
|
|
'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(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'page_id' => $page_id,
|
|
),
|
|
array(),
|
|
$info
|
|
);
|
|
}
|
|
|
|
public static function getCount(){
|
|
$rows = SqlHelper::ormSelect(
|
|
myself()->_getSelfMysql(),
|
|
't_chip_page',
|
|
array(
|
|
'account_id'=> myself()->_getAccountId()
|
|
)
|
|
);
|
|
$count = 0;
|
|
if ($rows){
|
|
$count = count($rows);
|
|
}
|
|
return $count;
|
|
}
|
|
} |