287 lines
12 KiB
PHP
287 lines
12 KiB
PHP
<?php
|
|
|
|
namespace services;
|
|
|
|
require_once('mt/Item.php');
|
|
require_once('mt/Equip.php');
|
|
require_once('mt/Season.php');
|
|
require_once('mt/Rank.php');
|
|
|
|
require_once('models/Season.php');
|
|
require_once('models/Battle.php');
|
|
|
|
use mt;
|
|
use phpcommon\SqlHelper;
|
|
use models\Season;
|
|
use models\Battle;
|
|
|
|
class BattleDataService extends BaseService {
|
|
|
|
private $seasonDb = array();
|
|
|
|
public function updateBattleData()
|
|
{
|
|
$this->currSeasonMeta = mt\Season::getCurrentSeason();
|
|
if (!$this->currSeasonMeta) {
|
|
return;
|
|
}
|
|
$this->seasonDb = Season::find($this->currSeasonMeta['id']);
|
|
if (!$this->seasonDb) {
|
|
Season::add($this->currSeasonMeta['id']);
|
|
$this->seasonDb = Season::find($this->currSeasonMeta['id']);
|
|
}
|
|
if (!$this->seasonDb) {
|
|
return;
|
|
}
|
|
$this->updateScore();
|
|
$hisBattleData = Battle::getMyBattleData();
|
|
if (!isset($hisBattleData)) {
|
|
$hisBattleData = array(
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
}
|
|
$this->apply($hisBattleData);
|
|
Battle::add(json_encode($hisBattleData));
|
|
$seasonBattleData = json_decode($this->seasonDb['battle_data'], true);
|
|
if (!isset($seasonBattleData['today_data'])) {
|
|
$seasonBattleData['today_data'] = array(
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
}
|
|
if (!isset($seasonBattleData['season_data'])) {
|
|
$seasonBattleData['season_data'] = array(
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
}
|
|
if (!isset($seasonBattleData['his_week_data'])) {
|
|
$seasonBattleData['his_week_data'] = array(
|
|
);
|
|
}
|
|
if (!isset($seasonBattleData['this_week_data'])) {
|
|
$seasonBattleData['this_week_data'] = array(
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
}
|
|
if (myself()->_getDaySeconds($seasonBattleData['today_data']['modifytime']) <
|
|
myself()->_getNowDaySeconds()) {
|
|
$seasonBattleData['today_data'] = array(
|
|
'createtime' => $seasonBattleData['today_data']['createtime'],
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
}
|
|
if (myself()->_getDaySeconds($seasonBattleData['this_week_data']['modifytime']) <
|
|
myself()->_getMondaySeconds()) {
|
|
$seasonBattleData['this_week_data'] = array(
|
|
'createtime' => $seasonBattleData['this_week_data']['createtime'],
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
}
|
|
$oldSeasonDataKills = getXVal($seasonBattleData['season_data'], 'total_kills_times', 0);
|
|
$this->apply($seasonBattleData['season_data']);
|
|
$newSeasonDataKills = getXVal($seasonBattleData['season_data'], 'total_kills_times', 0);
|
|
if ($newSeasonDataKills > $oldSeasonDataKills) {
|
|
Season::update($this->currSeasonMeta['id'], array(
|
|
'kills_modifytime' => myself()->_getNowTime(),
|
|
));
|
|
Battle::update(array(
|
|
'kills_modifytime' => myself()->_getNowTime(),
|
|
));
|
|
}
|
|
$this->apply($seasonBattleData['today_data']);
|
|
$this->apply($seasonBattleData['this_week_data']);
|
|
Season::update(
|
|
$this->currSeasonMeta['id'],
|
|
array(
|
|
'battle_data' => json_encode($seasonBattleData),
|
|
)
|
|
);
|
|
}
|
|
|
|
private function apply(&$battleData)
|
|
{
|
|
$this->incValue($battleData, 'total_battle_times', 1);
|
|
if (getReqVal('team_mode', 0) == 0) {
|
|
$this->incValue($battleData, 'total_single_battle_times', 1);
|
|
$this->minValue($battleData, 'single_battle_rank', getReqVal('ranked', 0));
|
|
} else {
|
|
$this->minValue($battleData, 'team_battle_rank', getReqVal('ranked', 0));
|
|
$this->incValue($battleData, 'total_team_battle_times', 1);
|
|
}
|
|
if (getReqVal('ranked', 0) == 1) {
|
|
$this->incValue($battleData, 'total_win_times', 1);
|
|
}
|
|
$kills = getReqVal('kills', 0);
|
|
if ($kills > 0) {
|
|
$this->incValue($battleData, 'total_kills_times', $kills);
|
|
$this->maxValue($battleData, 'max_kills_times', $kills);
|
|
}
|
|
$damageOut = getReqVal('damage_out', 0);
|
|
if ($damageOut > 0) {
|
|
$this->incValue($battleData, 'total_damage_out', $damageOut);
|
|
$this->maxValue($battleData, 'max_damage_out', $damageOut);
|
|
}
|
|
$damageIn = getReqVal('damage_in', 0);
|
|
if ($damageIn > 0) {
|
|
$this->incValue($battleData, 'total_damage_in', $damageIn);
|
|
$this->maxValue($battleData, 'max_damage_in', $damageIn);
|
|
}
|
|
$recoverHp = getReqVal('recover_hp', 0);
|
|
if ($recoverHp > 0) {
|
|
$this->incValue($battleData, 'total_recover_hp', $recoverHp);
|
|
$this->maxValue($battleData, 'max_recover_hp', $recoverHp);
|
|
}
|
|
$aliveTime = getReqVal('alive_time', 0);
|
|
if ($aliveTime > 0) {
|
|
$this->incValue($battleData, 'total_alive_time', $aliveTime);
|
|
$this->maxValue($battleData, 'max_alive_time', $aliveTime);
|
|
}
|
|
$this->incValue($battleData, 'rescue_teammate_times', getReqVal('rescue_teammate_times', 0));
|
|
$this->incValue($battleData, 'diving_times', getReqVal('diving_times', 0));
|
|
$this->incValue($battleData, 'open_airdrop_times', getReqVal('open_airdrop_times', 0));
|
|
$this->incValue($battleData, 'use_medicine_times', getReqVal('use_medicine_times', 0));
|
|
$this->incValue($battleData, 'destory_car_times', getReqVal('destory_car_times', 0));
|
|
$this->incValue($battleData, 'use_camouflage_times', getReqVal('use_camouflage_times', 0));
|
|
$this->incValue($battleData, 'use_skill_times', getReqVal('use_skill_times', 0));
|
|
$this->incValue($battleData, 'ride_car_move_distance', getReqVal('ride_car_move_distance', 0));
|
|
$this->incValue($battleData, 'ride_car_kills', getReqVal('ride_car_kills', 0));
|
|
$this->maxValue($battleData, 'max_single_battle_hero_lv', getReqVal('max_single_battle_hero_lv', 0));
|
|
$this->procWeaponsEquip($battleData);
|
|
$this->procWeaponsSlot($battleData);
|
|
$this->procHeros($battleData);
|
|
if (!isset($battleData['createtime'])) {
|
|
$battleData['createtime'] = myself()->_getNowTime();
|
|
}
|
|
$battleData['modifytime'] = myself()->_getNowTime();
|
|
}
|
|
|
|
private function procWeaponsEquip(&$battleData)
|
|
{
|
|
if (!isset($battleData['weapons_type_data'])) {
|
|
$battleData['weapons_type_data'] = array();
|
|
}
|
|
$weaponsTypeDb = &$battleData['weapons_type_data'];
|
|
{
|
|
$tmpStrs1 = explode('|', getReqVal('weapons_type', ''));
|
|
foreach ($tmpStrs1 as $str) {
|
|
$tmpStrs2 = explode(':', $str);
|
|
if (count($tmpStrs2) >= 4) {
|
|
list($weaponId, $kills, $damageOut, $obtainCount) = $tmpStrs2;
|
|
$weaponMeta = mt\Equip::get($weaponId);
|
|
if ($weaponMeta) {
|
|
$key = $weaponMeta['equip_type'] . '_' . $weaponMeta['equip_subtype'];
|
|
if (!isset($weaponsTypeDb[$key])) {
|
|
$weaponsTypeDb[$key] = array();
|
|
}
|
|
$this->incValue($weaponsTypeDb, 'kills', $kills);
|
|
$this->incValue($weaponsTypeDb, 'damage_out', $damage_out);
|
|
$this->incValue($weaponsTypeDb, 'obtain_count', $obtainCount);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function procWeaponsSlot(&$battleData)
|
|
{
|
|
if (!isset($battleData['weapons_slot_data'])) {
|
|
$battleData['weapons_slot_data'] = array();
|
|
}
|
|
$weaponsSlotDb = &$battleData['weapons_slot_data'];
|
|
{
|
|
$tmpStrs1 = explode('|', getReqVal('weapons_slot', ''));
|
|
foreach ($tmpStrs1 as $str) {
|
|
$tmpStrs2 = explode(':', $str);
|
|
if (count($tmpStrs2) >= 4) {
|
|
list($weaponId, $use_times) = $tmpStrs2;
|
|
$weaponMeta = mt\Equip::get($weaponId);
|
|
if ($weaponMeta && $weaponMeta['inventory_slot'] > 0) {
|
|
$key = $weaponMeta['inventory_slot'];
|
|
if (!isset($weaponsSlotDb[$key])) {
|
|
$weaponsSlotDb[$key] = array();
|
|
}
|
|
$this->incValue($weaponsSlotDb, 'use_times', $kills);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function procHeros(&$battleData)
|
|
{
|
|
if (!isset($battleData['hero_data'])) {
|
|
$battleData['hero_data'] = array();
|
|
}
|
|
$heroDb = &$battleData['hero_data'];
|
|
{
|
|
$tmpStrs1 = explode('|', getReqVal('heros', ''));
|
|
foreach ($tmpStrs1 as $str) {
|
|
$tmpStrs2 = explode(':', $str);
|
|
if (count($tmpStrs2) >= 4) {
|
|
list($heroId, $skillLv, $weaponLv) = $tmpStrs2;
|
|
$heroMeta = mt\Item::get($heroId);
|
|
if ($heroMeta && $heroMeta['type'] == mt\Item::HERO_TYPE) {
|
|
$key = $heroMeta['id'];
|
|
if (!isset($heroDb[$key])) {
|
|
$heroDb[$key] = array();
|
|
}
|
|
$this->maxValue($heroDb, 'skill_lv', $skillLv);
|
|
$this->maxValue($heroDb, 'weapon_lv', $weaponLv);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function incValue(&$battleData, $key, $val)
|
|
{
|
|
if ($val > 0) {
|
|
$battleData[$key] = getXVal($battleData, $key, 0) + $val;
|
|
}
|
|
}
|
|
|
|
private function minValue(&$battleData, $key, $val)
|
|
{
|
|
$battleData[$key] = min(getXVal($battleData, $key, 0), $val);
|
|
}
|
|
|
|
private function maxValue(&$battleData, $key, $val)
|
|
{
|
|
$battleData[$key] = max(getXVal($battleData, $key, 0), $val);
|
|
}
|
|
|
|
private function updateScore()
|
|
{
|
|
$userInfo = myself()->_getOrmUserInfo();
|
|
$rankScore = getReqVal('rank_score', 0);
|
|
if ($rankScore > 0) {
|
|
$newRank = $userInfo['rank'];
|
|
$newScore = $userInfo['score'];
|
|
mt\Rank::calcNewRankAndScore($userInfo['rank'], $userInfo['score'], $newRank, $newScore, $rankScore);
|
|
if ($newRank >= $userInfo['rank'] && $newScore != $userInfo['score']) {
|
|
myself()->_updateUserInfo(array(
|
|
'rank' => $newRank,
|
|
'score' => $newScore,
|
|
'history_best_rank' => max($userInfo['rank'], $newRank),
|
|
'score_modifytime' => myself()->_getNowTime(),
|
|
'best_rank_modifytime' => $newRank > $userInfo['rank'] ?
|
|
myself()->_getNowTime() : $userInfo['best_rank_modifytime'],
|
|
));
|
|
Season::update($this->currSeasonMeta['id'], array(
|
|
'rank' => $newRank,
|
|
'score' => $newScore,
|
|
'history_best_rank' => max($userInfo['rank'], $newRank),
|
|
'score_modifytime' => myself()->_getNowTime(),
|
|
'score_modifytime' => myself()->_getNowTime(),
|
|
'best_rank_modifytime' => $newRank > $userInfo['rank'] ?
|
|
myself()->_getNowTime() : $userInfo['best_rank_modifytime'],
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|