This commit is contained in:
songliang 2023-06-06 11:27:45 +08:00
parent 4d74ea5397
commit 406d2c6db2
3 changed files with 317 additions and 0 deletions

63
doc/FirstTopup.py Normal file
View File

@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
import _common
class Reward(object):
def __init__(self):
self.fields = [
['id', 0, 'id'],
['group', '', 'group'],
['goods_id', 0, 'goods_id'],
['goods_num', 0, '堆叠数量'],
['shop_icon', 0, '商店图标'],
]
class FirstTopup(object):
def __init__(self):
self.apis = [
{
'name': 'info',
'desc': '首充信息',
'group': 'FirstTopup',
'url': 'webapp/index.php?c=FirstTopup&a=info',
'params': [
_common.ReqHead(),
],
'response': [
_common.RspHead(),
['complete', 0, '是否已经完成首充 0-未完成 1-已完成'],
['firstTopupList', [Reward], '奖励信息'],
['status', [0], '领取状态 0-未领取 1-可领取 2-已领取'],
]
},
{
'name': 'begin',
'desc': '开始首充',
'group': 'FirstTopup',
'url': 'webapp/index.php?c=FirstTopup&a=begin',
'params': [
_common.ReqHead(),
],
'response': [
_common.RspHead(),
]
},
{
'name': 'get',
'desc': '获取首充奖励',
'group': 'FirstTopup',
'url': 'webapp/index.php?c=FirstTopup&a=get',
'params': [
_common.ReqHead(),
['group', 0, '奖励组id'],
],
'response': [
_common.RspHead(),
['group', 0, '奖励组id'],
['status', [0], '领取状态 0-未领取 1-可领取 2-已领取'],
['reward', [Reward], '奖励信息'],
]
},
]

View File

@ -0,0 +1,198 @@
<?php
require_once 'mt/FirstTopup.php';
require_once('services/PropertyChgService.php');
use mt\FirstTopup;
use phpcommon\SqlHelper;
use models\Bag;
use models\Hero;
use models\HeroSkin;
use models\Gun;
use models\GunSkin;
use models\Chip;
class FirstTopupController extends BaseAuthedController
{
public function info()
{
$complete = false;
$conn = myself()->_getMysql('');
$status = $this->getStatusFromDB($conn);
// 检查所有 奖励都 领取完成
$complete = ($status[0] == 2 && $status[1] == 2 && $status[2] == 2) ? 1 : 0;
$this->_rspData(
array(
'complete' => $complete,
'firstTopupList' => mt\FirstTopup::getGroups(),
'status' => $status,
)
);
}
public function begin()
{
$conn = myself()->_getMysql('');
$exist = SqlHelper::selectOne(
$conn,
't_first_topup',
array('account_id'),
array('account_id' => myself()->_getAccountId())
);
if ($exist) {
$this->_rspErr(1, '首充奖励活动已经开启');
return;
}
// 开始首充奖励活动进程
$chk = SqlHelper::insert(
$conn,
't_first_topup',
array(
'account_id' => myself()->_getAccountId(),
'createtime' => myself()->_getNowTime(),
'status1' => 0,
'status2' => 0,
'status3' => 0,
)
);
if ($chk) {
$this->_rspOk();
} else {
$this->_rspErr(1, '首充奖励活动开启失败');
}
}
public function get()
{
$group = getReqVal('group', 1);
$conn = myself()->_getMysql('');
$status = $this->getStatusFromDB($conn);
$test = $status[$group - 1];
if ($test == 1) {
$status[$group - 1] = 2;
$chk = SqlHelper::update(
$conn,
't_first_topup',
array(
'account_id' => myself()->_getAccountId(),
),
array(
'status' . $group => 2,
),
);
// 发放奖励
$reward = mt\FirstTopup::getByGroup($group);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < count($reward); $i++) {
$item = $reward[$i];
$itemMeta = mt\Item::get($item['goods_id']);
for ($j = 0; $j < $item['goods_num']; $j++) {
$this->internalAddItem($propertyChgService, $itemMeta, 1);
}
}
$this->_rspData(
array(
'group' => $group,
'status' => $status,
'reward' => $reward,
)
);
} else if ($test >= 2) {
$this->_rspErr(2, "already received the reward");
} else if ($test < 1) {
// 未到领取时间 英文 怎么说
$this->_rspErr(1, "not yet to receive the reward");
}
}
private function getStatus($group, $time)
{
$beginDayTime = myself()->_getDaySeconds($time);
$now = myself()->_getNowTime();
$diff = $now - ($beginDayTime + $group * 24 * 3600);
if ($diff >= 0) {
return 1;
} else {
return 0;
}
}
private function getStatusFromDB($conn)
{
// 从数据库中获取 status
$row = SqlHelper::selectOne(
$conn,
't_first_topup',
array('createtime', 'status1', 'status2', 'status3'),
array('account_id' => myself()->_getAccountId())
);
// 0 未领取 1 可领取 2 已领取
$status = [(int)$row['status1'], (int)$row['status2'], (int)$row['status3']];
$time = $row['createtime'];
for ($i = 0; $i < 3; $i++) {
if ($status[$i] < 2) {
// 检测是否到了可以领取的时间
$status[$i] = $this->getStatus($i, $time);
}
}
return $status;
}
private function internalAddItem($propertyChgService, $itemMeta, $count)
{
switch ($itemMeta['type']) {
case mt\Item::HERO_TYPE: {
Hero::addHero($itemMeta);
$propertyChgService->addHeroChg();
$propertyChgService->addUserChg();
}
break;
case mt\Item::HERO_SKIN_TYPE: {
HeroSkin::addSkin($itemMeta);
$propertyChgService->addHeroSkinChg();
}
break;
case mt\Item::GUN_TYPE: {
Gun::addGun($itemMeta);
$propertyChgService->addGunChg();
}
break;
case mt\Item::GUN_SKIN_TYPE: {
GunSkin::addSkin($itemMeta);
$propertyChgService->addGunSkinChg();
}
break;
case mt\Item::CHIP_TYPE: {
Chip::addChip($itemMeta);
$propertyChgService->addChip();
}
break;
default: {
Bag::addItem($itemMeta['id'], $count);
$propertyChgService->addBagChg();
}
break;
}
}
}

56
webapp/mt/FirstTopup.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace mt;
use phpcommon;
class FirstTopup
{
public static function get($id)
{
return getXVal(self::getMetaList(), $id);
}
public static function getByGroup($group)
{
self::all();
return getXVal(self::$groupList, $group);
}
public static function getGroups()
{
self::all();
return self::$groupList;
}
public static function all()
{
if (!self::$firstTopupList) {
self::$firstTopupList = array();
self::$groupList = array();
foreach (self::getMetaList() as $meta) {
array_push(self::$firstTopupList, $meta);
if (!getXVal(self::$groupList, $meta['group'], null)) {
self::$groupList[$meta['group']] = array();
}
array_push(self::$groupList[$meta['group']], $meta);
}
}
return self::$firstTopupList;
}
protected static function getMetaList()
{
if (!self::$metaList) {
self::$metaList = getMetaTable('FirstTopup@FirstTopup.php');
}
return self::$metaList;
}
protected static $groupList;
protected static $firstTopupList;
protected static $metaList;
}