Files
youlegames/codes/agent/game/dlweb/api/ext/Synchronize.php
2026-03-15 01:27:05 +08:00

633 lines
21 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Created by PhpStorm.
* User: abcdefg
* Date: 2017/7/24
* Time: 14:37
*/
/**
* 同步与游戏的交互日志表
*/
require_once dirname(__DIR__) . '/common/config.inc.php';
require_once dirname(__DIR__) . '/common/DatabaseHelper.php';
/**
* @note
* @param mixed $message
* @return bool
*/
function OutputDebugMessage($message) {
if (is_object($message) || is_array($message))
$message = JsonObjectToJsonString($message);
$nowdate = date('Y-m-d');
$nowtime = date('H:i:s');
$pathname = Characet(dirname($_SERVER['SCRIPT_FILENAME']) . '/debug/synchronize', 'gbk');
$filename = "{$pathname}/{$nowdate}.log";
$message = Characet($message, 'gbk');
if (!is_dir($pathname))
mkdir($pathname, 0777, true);
if ($file = fopen($filename, 'a+')) {
if (mb_strstr($message, PHP_EOL, false, USEDCHARSET) != PHP_EOL)
$message .= PHP_EOL;
fwrite($file, "{$nowtime} ====> {$message}");
fclose($file);
return true;
}
return false;
}
date_default_timezone_set('Asia/Shanghai');
/// 判断是否需要停服
if (TIMED_OFF_NEEDED) {
$Now = date('H:i:s');
if ($Now >= TIMED_OFF_BEGIN && $Now <= TIMED_OFF_END) {
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'code' => ERRORCODE_SERVERTURNOFF,
'info' => sprintf(ERRORINFO_SERVERTURNOFF, TIMED_OFF_BEGIN, TIMED_OFF_END),
]);
OutputDebugMessage($output);
die($output);
}
}
/// 本地数据库
$owner_param = new ConnectParameter(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_PERSISTENT, MASTER_CHARSET);
/// 游戏数据库
if (DEBUG_MODE)
$external_param = new ConnectParameter('rm-bp16sbf0l9cavp7h9o.mysql.rds.aliyuncs.com', 3306, 'youle_games', 'develop', 'develop123!@#', MASTER_PERSISTENT, MASTER_CHARSET);
else
$external_param = new ConnectParameter('rm-bp1749tfxu2rpq670lo.mysql.rds.aliyuncs.com', 3306, 'game_db', 'games', 'Games0791!!', MASTER_PERSISTENT, MASTER_CHARSET);
//$external_param = new ConnectParameter('rm-bp1x3i28se22s9z75o.mysql.rds.aliyuncs.com', 3306, 'game_db', 'games', 'Games2017@)!&', MASTER_PERSISTENT, MASTER_CHARSET);
/** @var IPDOHelper $owner_db */
$owner_db = new PDODelegator(DATABASE_TYPE);
/** @var IPDOHelper $external_db */
$external_db = new PDODelegator(DATABASE_TYPE);
if (!$owner_db->connect($owner_param)) {
OutputDebugMessage($owner_db->geterrors());
die (JsonObjectToJsonString($owner_db->geterrors()));
}
if (!$external_db->connect($external_param)) {
OutputDebugMessage($external_db->geterrors());
die (JsonObjectToJsonString($external_db->geterrors()));
}
$processcount = isset($_REQUEST['processcount']) ? intval($_REQUEST['processcount']) : 100;
if (empty($processcount))
$processcount = 100;
$command = <<<EOL
select
idx, uspl_agentid, uspl_channelid, uspl_playerid, uspl_unionid, uspl_opt, uspl_optdata, uspl_opttime, uspl_state, uspl_statetime
from
ct_user_process_log
where
uspl_state = 0
limit
{$processcount}
EOL;
$logs = $external_db->request($command);
$succeeded = '';
$failed = '';
$succeededcount = 0;
$failedcount = 0;
$totalcount = count($logs);
if (0 == $totalcount) {
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'total' => $totalcount,
'succeeded' => $succeededcount,
'failed' => $failedcount,
]);
die ($output);
}
$owner_db->begintransaction();
$external_db->begintransaction();
try {
if ($totalcount > 0) {
$error_code = 0;
$error_info = 'success';
foreach ($logs as $log) {
switch ($log->uspl_opt) {
case 1: /// 1:新增玩家
$line = __LINE__;
$data = JsonStringToJsonObject($log->uspl_optdata); /// {"openid":"openid100001", "unionid":"unionid100001", "nickname":"nickname100001","avatar":"","sex":1,"province":"江西","city":"南昌"}
if (!is_object($data)) {
$failed .= $log->idx . ',';
OutputDebugMessage('sync error: invalid json data' . PHP_EOL . 'data: ' . JsonObjectToJsonString($log));
$failedcount++;
break;
}
/// 插入新玩家信息
$command = /** @lang text */<<<EOL
replace into player(play_agentid, play_channelid, play_playerid, play_openid, play_unionid, play_nickname, play_avatar, play_sex, play_province, play_city, play_regtime)
values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid, $data->openid, $data->unionid,
$data->nickname, $data->avatar, $data->sex, $data->province, $data->city, $log->uspl_opttime);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
/// 查询是否有匹配的代理信息unionid或openid匹配
if (!empty($data->unionid)) {
$command = /** @lang text */<<<EOL
select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_unionid = ?
EOL;
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->unionid);
} elseif (!empty($data->openid)) {
$command = /** @lang text */<<<EOL
select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_openid = ?
EOL;
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->openid);
}
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
elseif (!empty($ret)) {
$command = /** @lang text */'update sales_user set player_id = ?, saus_salesman = 1 where idx = ?';
$ret = $owner_db->execute($command, $log->uspl_playerid, $ret[0]->idx);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
} else {
/// 新增一个代理信息
$command = /** @lang text */'update ct_agent_list set max_sales_id = max_sales_id + floor(rand() * (12-6) + 6) where agent_id = ?';
$ret = $owner_db->execute($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
$command = /** @lang text */'select max_sales_id as agen_maxsalesid, default_sales_power as agen_salespower from ct_agent_list where agent_id = ?';
$agent = $owner_db->request($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
} elseif (empty($agent)) {
$ret = false;
$line = __LINE__;
$error_code = -1;
$error_info = "找不到指定的渠道编号{$log->uspl_agentid}";
goto end_case_1;
}
$sales_id = $agent[0]->agen_maxsalesid;
$sales_power = $agent[0]->agen_salespower;
$password = md5(rand(1000, 9999));
$command = /** @lang text */<<<EOL
insert into sales_user(
saus_agentid, saus_channelid, saus_salesid, saus_power, saus_openid,
saus_unionid, saus_nickname, saus_avatar, saus_sex, saus_province,
saus_city, saus_firsttime, saus_lasttime, saus_saletime, player_id,
saus_salesman, password
) values (
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
1, ?
)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id, $sales_power, $data->openid,
$data->unionid, $data->nickname, $data->avatar, $data->sex, $data->province,
$data->city, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_playerid,
$password);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
$sales_user = [
'id' => 0,
'agentid' => $log->uspl_agentid,
'channelid' => $log->uspl_channelid,
'openid' => $data->openid,
'unionid' => $data->unionid,
'nickname' => $data->nickname,
'avatar' => $data->avatar,
'sex' => $data->sex,
'province' => $data->province,
'city' => $data->city,
'firsttime' => $log->uspl_opttime,
'lasttime' => $log->uspl_opttime,
'salesman' => 1,
'salesid' => $sales_id,
'level' => 0,
'parentid' => null,
'salestype' => 1,
'roomcard' => 0,
'bean' => 0,
'saletime' => $log->uspl_opttime,
'tel' => null,
'wechat' => null,
'invitecode' => null,
'power' => null,
'pushrate1' => 33,
'pushrate2' => 8,
'pushmoney1' => 0,
'pushmoney2' => 0,
];
$command = /** @lang text */<<<EOL
insert into ct_user_process_log(to_agent, to_channel, to_user, oper_type, oper_data, remark, oper_time, is_process)
values(?, ?, ?, 21, ?, ?, ?, 0)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id,
JsonObjectToJsonString($sales_user), '目标用户为注册的代理信息。oper_data为用户unionid。', time());
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_1;
}
}
end_case_1:
if ($ret && $owner_db->isdone()) {
$succeeded .= $log->idx . ',';
$succeededcount++;
$output = 'sync done: ' . JsonObjectToJsonString($log);
} else {
$failed .= $log->idx . ',';
$failedcount++;
$output = sprintf('sync error: %s(%d) on line %d.' . PHP_EOL . 'data: %s.', $error_info, $error_code, $line, JsonObjectToJsonString($log));
}
OutputDebugMessage($output);
break;
case 2: /// 2:绑定邀请码
$line = __LINE__;
/// 修改玩家的邀请码
$command = /** @lang text */<<<EOL
update
player
set
play_invitecode = ?
where
play_agentid = ? and play_channelid = ? and play_playerid = ?
EOL;
$ret = $owner_db->execute($command, $log->uspl_optdata, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
/// 获取对应的代理记录
$command = /** @lang text */'select idx from sales_user where saus_agentid = ? and saus_channelid = ? and player_id = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
} elseif (!empty($ret)) { /// 如果玩家有对应的代理账户则绑定对应的代理关系
$id = $ret[0]->idx;
} else { /// 如果玩家没有对应的代理账户则先生成对应的代理账户
/// 先查询对应的玩家详细信息
$command = /** @lang text */'select play_openid openid, play_unionid unionid, play_nickname nickname, play_avatar avatar, play_sex sex, play_province province, play_city city from player where play_agentid = ? and play_channelid = ? and play_playerid = ?';
$data = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
} elseif (empty($data)) {
$data = $external_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $log->uspl_playerid);
if (!$external_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
} elseif (empty($data)) {
$data = (object)[
'openid' => null,
'unionid' => $log->uspl_unionid,
'nickname' => null,
'avatar' => null,
'sex' => null,
'province' => null,
'city' => null,
];
} else {
$data = $data[0];
}
} else {
$data = $data[0];
}
/// 查询是否有匹配的代理信息unionid或openid匹配
if (!empty($data->unionid)) {
$command = /** @lang text */'select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_unionid = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->unionid);
} elseif (!empty($data->openid)) {
$command = /** @lang text */'select idx from sales_user where saus_agentid = ? and saus_channelid = ? and saus_openid = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid, $data->openid);
}
if (!$owner_db->isdone()) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
elseif (!empty($ret)) {
$id = $ret[0]->idx;
} else {
/// 新增一个代理信息
$command = /** @lang text */
'update ct_agent_list set max_sales_id = max_sales_id + floor(rand() * (12-6) + 6) where agent_id = ?';
$ret = $owner_db->execute($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
$command = /** @lang text */
'select max_sales_id as agen_maxsalesid, default_sales_power as agen_salespower from ct_agent_list where agent_id = ?';
$agent = $owner_db->request($command, $log->uspl_agentid);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
$sales_id = $agent[0]->agen_maxsalesid;
$sales_power = $agent[0]->agen_salespower;
$password = md5(rand(1000, 9999));
$command = /** @lang text */<<<EOL
insert into sales_user(
saus_agentid, saus_channelid, saus_salesid, saus_power, saus_openid,
saus_unionid, saus_nickname, saus_avatar, saus_sex, saus_province,
saus_city, saus_firsttime, saus_lasttime, saus_saletime, player_id,
saus_salesman, password
) values (
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
1, ?
)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id, $sales_power, $data->openid,
$data->unionid, $data->nickname, $data->avatar, $data->sex, $data->province,
$data->city, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_opttime, $log->uspl_playerid,
$password);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
$id = $owner_db->lastinsertid();
$sales_user = [
'id' => $id,
'agentid' => $log->uspl_agentid,
'channelid' => $log->uspl_channelid,
'openid' => $data->openid,
'unionid' => $data->unionid,
'nickname' => $data->nickname,
'avatar' => $data->avatar,
'sex' => $data->sex,
'province' => $data->province,
'city' => $data->city,
'firsttime' => $log->uspl_opttime,
'lasttime' => $log->uspl_opttime,
'salesman' => 1,
'salesid' => $sales_id,
'level' => 0,
'parentid' => null,
'salestype' => 1,
'roomcard' => 0,
'bean' => 0,
'saletime' => $log->uspl_opttime,
'tel' => null,
'wechat' => null,
'invitecode' => null,
'power' => null,
'pushrate1' => 33,
'pushrate2' => 8,
'pushmoney1' => 0,
'pushmoney2' => 0,
];
/// 记录新增代理的日志
$command = /** @lang text */<<<EOL
insert into ct_user_process_log(to_agent, to_channel, to_user, oper_type, oper_data, remark, oper_time, is_process)
values(?, ?, ?, 21, ?, ?, ?, 0)
EOL;
$ret = $owner_db->execute($command,
$log->uspl_agentid, $log->uspl_channelid, $sales_id,
JsonObjectToJsonString($sales_user), '目标用户为注册的代理信息。oper_data为用户unionid。', time());
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
}
}
/// 读取渠道是否同步邀请码的配置
$command = /** @lang text */'select sync_invitecode from ct_channel_list where agent_id = ? and channel_id = ?';
$ret = $owner_db->request($command, $log->uspl_agentid, $log->uspl_channelid);
$sync_invitecode = empty($ret) ? 0 : intval($ret[0]->sync_invitecode);
if (0 != $sync_invitecode) {
/// 修改对应代理记录的邀请码
$command = /** @lang text */<<<EOL
update
sales_user
set
saus_parentid = ?,
saus_invitecode = ?,
player_id = ?,
saus_salesman = 1
where
idx = ?
EOL;
$ret = $owner_db->execute($command, $log->uspl_optdata, $log->uspl_optdata, $log->uspl_playerid, $id);
if (!$ret) {
$line = __LINE__;
$error_code = $owner_db->GetErrorCode();
$error_info = $owner_db->GetErrorInfo();
goto end_case_2;
}
}
end_case_2:
if ($ret && $owner_db->isdone()) {
$succeeded .= $log->idx . ',';
$succeededcount++;
$output = 'sync done: ' . JsonObjectToJsonString($log);
} else {
$failed .= $log->idx . ',';
$failedcount++;
$output = sprintf('sync error: %s(%d) on line %d.' . PHP_EOL . 'data: %s.', $error_info, $error_code, $line, JsonObjectToJsonString($log));
}
OutputDebugMessage($output);
break;
default:
break;
}
}
/// 成功处理的记录把状态修改为1
if (!empty($succeeded)) {
$succeeded = substr($succeeded, 0, strlen($succeeded) - 1);
$command = /** @lang text */<<<EOL
update
ct_user_process_log
set
uspl_state = 1
where
idx in ({$succeeded})
EOL;
$external_db->execute($command);
}
/// 处理失败的记录把状态修改为-1
if (!empty($failed)) {
$failed = substr($failed, 0, strlen($failed) - 1);
$command = /** @lang text */<<<EOL
update
ct_user_process_log
set
uspl_state = -1
where
idx in ({$failed})
EOL;
$external_db->execute($command);
}
}
if (!$owner_db->commit())
throw new Exception($owner_db->GetErrorInfo(), $owner_db->GetErrorCode());
if (!$external_db->commit())
throw new Exception($external_db->GetErrorInfo(), $external_db->GetErrorCode());
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'total' => $totalcount,
'succeeded' => $succeededcount,
'failed' => $failedcount,
]);
if (($succeededcount + $failedcount) != $totalcount)
OutputDebugMessage($output);
} catch (Exception $Exception) {
$owner_db->rollback();
$external_db->rollback();
$output = JsonObjectToJsonString([
'time' => date('Y-m-d H:i:s'),
'code' => $Exception->getCode(),
'info' => $Exception->getMessage(),
]);
OutputDebugMessage($output);
}
die ($output);
/*
insert into ct_user_process_log(to_agent, to_channel, to_user, oper_type, oper_data, remark, oper_time, is_process)
select
saus_agentid,
saus_channelid,
saus_salesid,
21,
concat('{',
'"id":"', ifnull(idx, 0),
'","agentid":"', ifnull(saus_agentid, ''),
'","channelid":"', ifnull(saus_channelid, ''),
'","openid":"', ifnull(saus_openid, ''),
'","unionid":"', ifnull(saus_unionid, ''),
'","nickname":"', ifnull(saus_nickname, ''),
'","avatar":"', ifnull(saus_avatar, ''),
'","sex":"', ifnull(saus_sex, ''),
'","province":"', ifnull(saus_province, ''),
'","city":"', ifnull(saus_city, ''),
'","firsttime":"', ifnull(saus_firsttime, ''),
'","lasttime":"', ifnull(saus_lasttime, ''),
'","salesman":"', ifnull(saus_salesman, 0),
'","salesid":"', ifnull(saus_salesid, ''),
'","level":"', ifnull(saus_level, ''),
'","parentid":"', ifnull(saus_parentid, ''),
'","salestype":"', ifnull(saus_salestype, ''),
'","roomcard":"', ifnull(saus_roomcard, 0),
'","bean":"', ifnull(saus_bean, 0),
'","saletime":"', ifnull(saus_saletime, ''),
'","tel":"', ifnull(saus_tel, ''),
'","wechat":"', ifnull(saus_wechat, ''),
'","invitecode":"', ifnull(saus_invitecode, 0),
'","power":"', ifnull(saus_power, ''),
'","pushrate1":"', ifnull(saus_pushrate1, 0),
'","pushrate2":"', ifnull(saus_pushrate2, 0),
'","pushmoney1":"', ifnull(saus_pushmoney1, 0),
'","pushmoney2":"', ifnull(saus_pushmoney2, 0),
'"}'
),
'目标用户为注册的代理信息。oper_data为用户unionid。',
unix_timestamp(),
0
from
sales_user
where
saus_agentid = 'veRa0qrBf0df2K1G4de2tgfmVxB2jxpv' and saus_channelid = 'FtJf073aa0d6rI1xD8J1Y42fINTm0ziK' and saus_salesid = 240973
*/