1
This commit is contained in:
parent
938464da79
commit
d398ad4a7f
@ -147,5 +147,11 @@ function getCommanderConfig($commander_table, $commander_id)
|
||||
$commander_id = (int)$commander_id;
|
||||
return array_key_exists($commander_id, $commander_table) ? $commander_table[$commander_id] : null;
|
||||
}
|
||||
|
||||
function getRobotConfig($robot_table, $robot_id)
|
||||
{
|
||||
$robot_id = (int)$robot_id;
|
||||
return array_key_exists($robot_id, $robot_table) ? $robot_table[$robot_id] : null;
|
||||
}
|
||||
checkMysqlConfig();
|
||||
checkRedisConfig();
|
||||
|
@ -58,7 +58,7 @@ class Quest{
|
||||
$quest_state = 1;
|
||||
$this->triggerQuest(QUEST_DAY_COMPLETE, 1, 1, $account_id);
|
||||
}
|
||||
} else if ($q['condition'] == 13 && $quest_type == 1){
|
||||
} else if ($q['condition'] == 11 && $quest_type == 1){
|
||||
if ($row['quest_num'] + $quest_num >= $q['value']) {
|
||||
$quest_num = $q['value'];
|
||||
$quest_state = 1;
|
||||
@ -101,7 +101,7 @@ class Quest{
|
||||
$quest_state = 1;
|
||||
$this->triggerQuest(QUEST_DAY_COMPLETE, 1, 1, $account_id);
|
||||
}
|
||||
} else if ($q['condition'] == 13 && $quest_type == 1){
|
||||
} else if ($q['condition'] == 11 && $quest_type == 1){
|
||||
if ($row['quest_num'] >= $q['value']) {
|
||||
$quest_num = $q['value'];
|
||||
$quest_state = 1;
|
||||
|
240
webapp/controller/MatchController.class.php
Normal file
240
webapp/controller/MatchController.class.php
Normal file
@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
|
||||
class MatchController{
|
||||
|
||||
protected function getRedis($team_uuid)
|
||||
{
|
||||
$redis_conf = getRedisConfig(crc32($team_uuid));
|
||||
$r = new phpcommon\Redis(array(
|
||||
'host' => $redis_conf['host'],
|
||||
'port' => $redis_conf['port'],
|
||||
'passwd' => $redis_conf['passwd']
|
||||
|
||||
));
|
||||
return $r;
|
||||
}
|
||||
|
||||
protected function getRobot($robot_id, $num)
|
||||
{
|
||||
$address = '../res/robot@robot' . $num . '.php';
|
||||
$robot_meta_cluster = require($address);
|
||||
$robot_meta = getRobotConfig($robot_meta_cluster, $robot_id);
|
||||
$rob = array(
|
||||
'id' => $robot_meta['id'],
|
||||
'name' => $robot_meta['name'],
|
||||
'avatar_url' => $robot_meta['avatar_url'],
|
||||
);
|
||||
return $rob;
|
||||
}
|
||||
|
||||
public function randMatch()
|
||||
{
|
||||
$node_id = 1;
|
||||
if (isset($_REQUEST['node_id'])) {
|
||||
$node_id = (int)$_REQUEST['node_id'];
|
||||
}
|
||||
$room_uuid = $_REQUEST['room_uuid'];
|
||||
//查找当前房间
|
||||
$r = array();
|
||||
if ($room_uuid == null) {
|
||||
$r = $this->getRedis($node_id);
|
||||
$current_db_str = $r->get('game2001:match:current_room');
|
||||
if (empty($current_db_str)) {
|
||||
$room_uuid = $node_id . '_' . md5($_REQUEST['account_id']) . time();
|
||||
$current_db = array(
|
||||
'room_uuid' => $room_uuid,
|
||||
);
|
||||
$r -> set('game2001:match:current_room', json_encode($current_db));
|
||||
} else {
|
||||
$current_db = json_decode($current_db_str, true);
|
||||
$room_uuid = $current_db['room_uuid'];
|
||||
$r = $this->getRedis($current_db['room_uuid']);
|
||||
}
|
||||
} else {
|
||||
$r = $this->getRedis($room_uuid);
|
||||
}
|
||||
//获取当前房间信息
|
||||
$join_time = 5;
|
||||
$room_state = 0;
|
||||
$downtime = 0;
|
||||
$room = $this->getRedis($room_uuid);
|
||||
$room_db_str = $room->get($room_uuid);
|
||||
if (empty($room_db_str)) {
|
||||
$room_db = array(
|
||||
'room_uuid' => $room_uuid,
|
||||
'create_time' => time(),
|
||||
'last_join_time' => time(),
|
||||
'prepare_time' => 0,
|
||||
'member_list' => array(
|
||||
array(
|
||||
'account_id' => $_REQUEST['account_id'],
|
||||
'name' => $_REQUEST['name'],
|
||||
'avatar_url' => $_REQUEST['avatar_url'],
|
||||
)
|
||||
));
|
||||
$room -> set($room_uuid, json_encode($room_db));
|
||||
$room -> pexpire($room_uuid, 1000 * 300);
|
||||
} else {
|
||||
$room_db = json_decode($room_db_str, true);
|
||||
if (empty($room_db)) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
|
||||
return;
|
||||
}
|
||||
//判断玩家是否在当前房间
|
||||
$flag = 0;
|
||||
foreach ($room_db['member_list'] as $member) {
|
||||
if ($member['account_id'] == $_REQUEST['account_id']) {
|
||||
$flag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//判断是否需要创建新房间
|
||||
if ((count($room_db['member_list']) >= 10 && $flag != 1)
|
||||
|| (time() - $room_db['prepare_time'] >= 3 && $room_db['prepare_time'] != 0)) {
|
||||
//创建新房间
|
||||
$room_uuid = $node_id . '_' . md5($_REQUEST['account_id']) . time();
|
||||
$room_db = array(
|
||||
'room_uuid' => $room_uuid,
|
||||
'create_time' => time(),
|
||||
'last_join_time' => time(),
|
||||
'prepare_time' => 0,
|
||||
'member_list' => array(
|
||||
array(
|
||||
'account_id' => $_REQUEST['account_id'],
|
||||
'name' => $_REQUEST['name'],
|
||||
'avatar_url' => $_REQUEST['avatar_url'],
|
||||
)
|
||||
));
|
||||
$room -> set($room_uuid, json_encode($room_db));
|
||||
$room -> pexpire($room_uuid, 1000 * 300);
|
||||
//改变当前房间room_uuid
|
||||
$current_db_str = $r->get('game2001:match:current_room');
|
||||
$current_db = json_decode($current_db_str, true);
|
||||
$current_db['room_uuid'] = $room_uuid;
|
||||
$r -> set('game2001:match:current_room', json_encode($current_db));
|
||||
} else if (count($room_db['member_list']) < 10) {
|
||||
//判断是否添加机器人
|
||||
if ($flag == 1) {
|
||||
if (time() >= $join_time + $room_db['last_join_time']) {
|
||||
//添加机器人
|
||||
$num = count($room_db['member_list']);
|
||||
$random = Rand(1, 100);
|
||||
$robot_id = 1000 + ($num - 1) * 100 + $random;
|
||||
$rob = $this->getRobot($robot_id, $num);
|
||||
array_push ($room_db['member_list'], array(
|
||||
'account_id' => $rob['id'],
|
||||
'name' => $rob['name'],
|
||||
'avatar_url' => $rob['avatar_url'],
|
||||
));
|
||||
$room_db['last_join_time'] = time();
|
||||
}
|
||||
} else {
|
||||
//加入房间
|
||||
array_push ($room_db['member_list'], array(
|
||||
'account_id' => $_REQUEST['account_id'],
|
||||
'name' => $_REQUEST['name'],
|
||||
'avatar_url' => $_REQUEST['avatar_url'],
|
||||
));
|
||||
$room_db['last_join_time'] = time();
|
||||
}
|
||||
//更新倒计时状态或者开始游戏
|
||||
if (count($room_db['member_list']) >= 10) {
|
||||
if ($room_db['prepare_time'] == 0) {
|
||||
$room_db['prepare_time'] = time();
|
||||
$downtime = 3 - (time() - $room_db['prepare_time']);
|
||||
$room_state = 1;
|
||||
} else {
|
||||
if (time() - $room_db['prepare_time'] >= 3) {
|
||||
$downtime = 0;
|
||||
$room_state = 2;
|
||||
} else {
|
||||
$downtime = 3 - (time() - $room_db['prepare_time']);
|
||||
}
|
||||
}
|
||||
}
|
||||
$room -> set($room_uuid, json_encode($room_db));
|
||||
$room -> pexpire($room_uuid, 1000 * 300);
|
||||
} else if ($room_db['prepare_time'] != 0) {
|
||||
if (time() - $room_db['prepare_time'] >= 3) {
|
||||
$downtime = 0;
|
||||
$room_state = 2;
|
||||
} else {
|
||||
$downtime = 3 - (time() - $room_db['prepare_time']);
|
||||
}
|
||||
}
|
||||
}
|
||||
$room = $this->getRedis($room_uuid);
|
||||
$room_db_str = $room->get($room_uuid);
|
||||
$room_db = json_decode($room_db_str, true);
|
||||
$member_list = array();
|
||||
foreach ($room_db['member_list'] as $member) {
|
||||
array_push($member_list, array(
|
||||
'account_id' => $member['account_id'],
|
||||
'name' => $member['name'],
|
||||
'avatar_url' => $member['avatar_url'],
|
||||
));
|
||||
}
|
||||
|
||||
echo json_encode(array(
|
||||
'errcode' => 0,
|
||||
'errmsg'=> '',
|
||||
'room_uuid' => $room_uuid,
|
||||
'room_state' => $room_state,
|
||||
'downtime' => $downtime,
|
||||
'member_list' => $member_list,
|
||||
));
|
||||
}
|
||||
|
||||
public function cancleMatch()
|
||||
{
|
||||
$account_id = $_REQUEST['account_id'];
|
||||
$room_uuid = $_REQUEST['room_uuid'];
|
||||
$r = $this->getRedis($room_uuid);
|
||||
if (!$r) {
|
||||
die();
|
||||
return;
|
||||
}
|
||||
$room_db_str = $r->get($room_uuid);
|
||||
if (empty($room_db_str)) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 1,'session失效1');
|
||||
return;
|
||||
}
|
||||
$room_db = json_decode($room_db_str, true);
|
||||
if (empty($room_db)) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 1,'session失效2');
|
||||
return;
|
||||
}
|
||||
if ($room_db['prepare_time'] != 0) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 2,'游戏即将开始,无法退出匹配');
|
||||
return;
|
||||
}
|
||||
$flag = 0;
|
||||
foreach ($room_db['member_list'] as $member) {
|
||||
if ($member['account_id'] == $account_id) {
|
||||
break;
|
||||
}
|
||||
$flag++;
|
||||
}
|
||||
unset($room_db['member_list'][$flag]);
|
||||
$room_db['member_list'] = array_values($room_db['member_list']);
|
||||
$r->set($room_uuid, json_encode($room_db));
|
||||
$r -> pexpire($room_uuid, 1000 * 300);
|
||||
|
||||
echo json_encode(array(
|
||||
'errcode' => 0,
|
||||
'errmsg' => '',
|
||||
));
|
||||
}
|
||||
|
||||
public function startGame()
|
||||
{
|
||||
$account_id = $_REQUEST['account_id'];
|
||||
$room_uuid = $_REQUEST['room_uuid'];
|
||||
echo json_encode(array(
|
||||
'errcode' => 0,
|
||||
'errmsg'=> '',
|
||||
));
|
||||
}
|
||||
}
|
||||
?>
|
@ -298,7 +298,7 @@ class ShareController{
|
||||
}
|
||||
} else {
|
||||
foreach ($rows as $row) {
|
||||
$sh = $this->getShare($ach_id);
|
||||
$sh = $this->getShare($row['ach_id']);
|
||||
$array = $this->getExplode($sh['rewards']);
|
||||
array_push($info_list, array(
|
||||
'achivement_id' => $row['ach_id'],
|
||||
|
@ -36,6 +36,7 @@ class ShopController{
|
||||
$it = array(
|
||||
'id' => $item_meta['id'],
|
||||
'price' => $item_meta['price'],
|
||||
'type'=> $item_meta['type']
|
||||
);
|
||||
return $it;
|
||||
}
|
||||
@ -52,6 +53,7 @@ class ShopController{
|
||||
'discount' => $shop_meta['discount'],
|
||||
'discount_weight' => $shop_meta['discount_weight'],
|
||||
'tip' => $shop_meta['tip'],
|
||||
'buy_type' => $shop_meta['buy_type']
|
||||
);
|
||||
return $s;
|
||||
}
|
||||
@ -121,6 +123,7 @@ class ShopController{
|
||||
'price' => $shop['price'],
|
||||
'tip' => $shop['tip'],
|
||||
'status' => $shop['status'],
|
||||
'buy_type' => $shop['buy_type']
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -162,6 +165,7 @@ class ShopController{
|
||||
$price = 0;
|
||||
$status = 0;
|
||||
$flag = 0;
|
||||
$buy_type = 0;
|
||||
$r = $this->getRedis($shop_uuid);
|
||||
$user_db_str = $r->get($shop_uuid);
|
||||
if (empty($user_db_str)) {
|
||||
@ -179,6 +183,7 @@ class ShopController{
|
||||
$item_num = $shop['item_num'];
|
||||
$price = $shop['price'];
|
||||
$status = $shop['status'];
|
||||
$buy_type = $shop['buy_type'];
|
||||
$flag = 1;
|
||||
break;
|
||||
}
|
||||
@ -192,7 +197,7 @@ class ShopController{
|
||||
return;
|
||||
}
|
||||
//扣除货币
|
||||
$this->SubCoin($price, $account_id);
|
||||
$this->SubCoin($price, $account_id, $buy_type);
|
||||
//修改购买状态
|
||||
foreach ($user_db['shop_list'] as &$shop) {
|
||||
if ($shop['shop_id'] == $shop_id) {
|
||||
@ -278,7 +283,7 @@ class ShopController{
|
||||
'errmsg'=> '',
|
||||
'shop_uuid' => $shop_uuid,
|
||||
'shop_list' => $shop_list,
|
||||
'shop_flush_times' => $row['shop_flush_times'] + 1
|
||||
'shop_flush_times' => $row['shop_flush_times'] + 1,
|
||||
));
|
||||
}
|
||||
|
||||
@ -293,6 +298,7 @@ class ShopController{
|
||||
$discount = 0;
|
||||
$price = 0;
|
||||
$key = 0;
|
||||
$buy_type = 0;
|
||||
$s = $this->getShop($i);
|
||||
//确定商品id和数量
|
||||
$weight_sum = 0;
|
||||
@ -317,7 +323,8 @@ class ShopController{
|
||||
$it = $this->getItem($item_id);
|
||||
if ($s['tip'] != 3) {
|
||||
$discount = $s['discount'];
|
||||
$price = $it['price'];
|
||||
$price = 0;
|
||||
$buy_type = $s['buy_type'];
|
||||
} else {
|
||||
$keys = 0;
|
||||
$weight_sum = 0;
|
||||
@ -335,8 +342,20 @@ class ShopController{
|
||||
}
|
||||
}
|
||||
$discount_array = $this->getExplode($s['discount']);
|
||||
$buy_type_array = $this->getExplode($s['buy_type']);
|
||||
$discount = $discount_array[$keys][0];
|
||||
$price = round($it['price'] * $item_num * ($discount * 1.0 / 10));
|
||||
$buy_type = $buy_type_array[$keys][0];
|
||||
$coin_type_array = $this->getExplode($it['type']);
|
||||
$price_array = $this->getExplode($it['price']);
|
||||
$price_key = 0;
|
||||
for ($j = 0; $j < count($coin_type_array); $j++) {
|
||||
if ($coin_type_array[$j][0] == $buy_type) {
|
||||
$price_key = $j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$price = round($price_array[$j][0] * $item_num * ($discount * 1.0 / 10));
|
||||
}
|
||||
array_push($shop_list, array(
|
||||
'shop_id' => $s['shop_id'],
|
||||
@ -346,12 +365,13 @@ class ShopController{
|
||||
'price' => $price,
|
||||
'tip' => $s['tip'],
|
||||
'status' => 0,
|
||||
'buy_type' => $buy_type
|
||||
));
|
||||
}
|
||||
return $shop_list;
|
||||
}
|
||||
|
||||
protected function subCoin($coin_num, $account_id)
|
||||
protected function subCoin($coin_num, $account_id, $buy_type)
|
||||
{
|
||||
$conn = $this->getMysql($account_id);
|
||||
if (!$conn) {
|
||||
@ -359,23 +379,40 @@ class ShopController{
|
||||
return;
|
||||
}
|
||||
//扣除货币
|
||||
$rowCoin = $conn->execQueryOne('SELECT coin_num FROM user WHERE accountid=:accountid;',
|
||||
$rowCoin = $conn->execQueryOne('SELECT coin_num, diamond_num FROM user WHERE accountid=:accountid;',
|
||||
array(
|
||||
':accountid' => $account_id
|
||||
));
|
||||
if ($rowCoin['coin_num'] < $coin_num) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 5, '金币不足');
|
||||
die();
|
||||
}
|
||||
$ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' .
|
||||
' WHERE accountid=:accountid;',
|
||||
array(
|
||||
':accountid' => $account_id,
|
||||
':coin_num' => $rowCoin['coin_num'] - $coin_num,
|
||||
':modify_time' => time()
|
||||
));
|
||||
if (!$ret) {
|
||||
die();
|
||||
if ($buy_type == 1) {
|
||||
if ($rowCoin['coin_num'] < $coin_num) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 5, '金币不足');
|
||||
die();
|
||||
}
|
||||
$ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' .
|
||||
' WHERE accountid=:accountid;',
|
||||
array(
|
||||
':accountid' => $account_id,
|
||||
':coin_num' => $rowCoin['coin_num'] - $coin_num,
|
||||
':modify_time' => time()
|
||||
));
|
||||
if (!$ret) {
|
||||
die();
|
||||
}
|
||||
} else if ($buy_type == 2) {
|
||||
if ($rowCoin['diamond_num'] < $coin_num) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 6, '钻石不足');
|
||||
die();
|
||||
}
|
||||
$ret = $conn->execScript('UPDATE user SET diamond_num=:diamond_num, modify_time=:modify_time ' .
|
||||
' WHERE accountid=:accountid;',
|
||||
array(
|
||||
':accountid' => $account_id,
|
||||
':diamond_num' => $rowCoin['diamond_num'] - $coin_num,
|
||||
':modify_time' => time()
|
||||
));
|
||||
if (!$ret) {
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user