game2006api/webapp/models/BattleHistory.php
2023-05-10 11:37:03 +08:00

128 lines
3.6 KiB
PHP

<?php
namespace models;
require_once('models/Hero.php');
require_once('models/Gun.php');
require_once('mt/Hero.php');
use mt;
use phpcommon\SqlHelper;
class BattleHistory extends BaseModel
{
public static function add($data)
{
return SqlHelper::insert
(myself()->_getSelfMysql(),
't_battle_history',
$data
);
}
public static function getMyBattleHistoryByMode($mode)
{
if ($mode == 1) {
$row1 = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_battle_history',
array(
'account_id' => myself()->_getAccountId(),
'match_mode' => 0,
)
);
$row2 = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_battle_history',
array(
'account_id' => myself()->_getAccountId(),
'match_mode' => 1,
)
);
$row = array_merge($row1,$row2);
return $row;
}elseif($mode == 2){
$row = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_battle_history',
array(
'account_id' => myself()->_getAccountId(),
'match_mode' => 2,
)
);
return $row;
}else{
return array();
}
}
public static function toDto($row){
if ($row['hero_id']){
$heroDb = Hero::findByAccountId($row['account_id'],$row['hero_id']);
$row['hero_id'] =$heroDb ? $heroDb['hero_id'] : 0;
}
if ($row['weapon1']){
$weaponDb1 = Gun::findByAccountId($row['account_id'],$row['weapon1']);
$row['weapon1'] =$weaponDb1 ? $weaponDb1['gun_id'] : 0;
}
if ($row['weapon2']){
$weaponDb2 = Gun::findByAccountId($row['account_id'],$row['weapon2']);
$row['weapon2'] = $weaponDb2 ? $weaponDb2['gun_id'] : 0;
}
return $row;
}
public static function orderBy($data,$order){
$len = count($data);
if ($len<=1){
return $data;
}
if ($order == 'desc'){
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($data[$i]['idx'] < $data[$j]['idx']) {
$tmp = $data[$i];
$data[$i] = $data[$j];
$data[$j] = $tmp;
}
}
}
}else{
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($data[$i]['idx'] > $data[$j]['idx']) {
$tmp = $data[$i];
$data[$i] = $data[$j];
$data[$j] = $tmp;
}
}
}
}
return $data;
}
public static function findByAccount($account,$battleUid){
return SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_battle_history',
array(
'account_id' => $account,
'battle_uniid' => $battleUid,
)
);
}
public static function updateField($account,$battleUid,$field){
return SqlHelper::update(
myself()->_getSelfMysql(),
't_battle_history',
array(
'account_id' => $account,
'battle_uniid' => $battleUid,
),
$field
);
}
}