Skip to content

Commit

Permalink
GH-150 Simplify code by short circuiting
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Mar 22, 2022
1 parent d69294d commit 442415f
Showing 1 changed file with 120 additions and 118 deletions.
238 changes: 120 additions & 118 deletions reg_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,147 +119,149 @@ function handleRegistration() {
}
}

if (empty($JSONResponse['Errors'])) {
unset($JSONResponse['Errors']);

$newPlanetCoordinates = Registration\Utils\Galaxy\findNewPlanetPosition([
'preferredGalaxy' => $normalizedInput['galaxyNo']
]);

if ($newPlanetCoordinates !== null) {
$passwordHash = Session\Utils\LocalIdentityV1\hashPassword([
'password' => $normalizedInput['password'],
]);

$insertNewUserResult = Registration\Utils\Queries\insertNewUser([
'username' => $normalizedInput['username'],
'passwordHash' => $passwordHash,
'langCode' => $normalizedInput['langCode'],
'email' => $normalizedInput['email']['escaped'],
'registrationIP' => $userSessionIP,
'currentTimestamp' => $Now,
]);
$UserID = $insertNewUserResult['userId'];

// Create a Planet for User
include($_EnginePath.'includes/functions/CreateOnePlanetRecord.php');

$PlanetID = CreateOnePlanetRecord(
$newPlanetCoordinates['galaxy'],
$newPlanetCoordinates['system'],
$newPlanetCoordinates['planet'],
$UserID,
$_Lang['MotherPlanet'],
true
);

Registration\Utils\Queries\incrementUsersCounterInGameConfig();
Registration\Utils\Queries\updateAllMailChanges([
'email' => $normalizedInput['email']['escaped']
]);
if (!empty($JSONResponse['Errors'])) {
die('regCallback('.json_encode($JSONResponse).');');
}

$referrerUserId = Registration\Utils\General\getRegistrationReferrerId();
unset($JSONResponse['Errors']);

if ($referrerUserId !== null) {
$registrationIPs = [
'r' => trim($userSessionIP),
'p' => trim(Users\Session\getCurrentOriginatingIP())
];
$newPlanetCoordinates = Registration\Utils\Galaxy\findNewPlanetPosition([
'preferredGalaxy' => $normalizedInput['galaxyNo']
]);

if (empty($registrationIPs['p'])) {
unset($registrationIPs['p']);
}
if ($newPlanetCoordinates !== null) {
$passwordHash = Session\Utils\LocalIdentityV1\hashPassword([
'password' => $normalizedInput['password'],
]);

$existingMatchingEnterLogIds = Registration\Utils\Queries\findEnterLogIPsWithMatchingIPValue([
'ips' => $registrationIPs,
]);
$insertNewUserResult = Registration\Utils\Queries\insertNewUser([
'username' => $normalizedInput['username'],
'passwordHash' => $passwordHash,
'langCode' => $normalizedInput['langCode'],
'email' => $normalizedInput['email']['escaped'],
'registrationIP' => $userSessionIP,
'currentTimestamp' => $Now,
]);
$UserID = $insertNewUserResult['userId'];

// Create a Planet for User
include($_EnginePath.'includes/functions/CreateOnePlanetRecord.php');

$PlanetID = CreateOnePlanetRecord(
$newPlanetCoordinates['galaxy'],
$newPlanetCoordinates['system'],
$newPlanetCoordinates['planet'],
$UserID,
$_Lang['MotherPlanet'],
true
);

Registration\Utils\Queries\incrementUsersCounterInGameConfig();
Registration\Utils\Queries\updateAllMailChanges([
'email' => $normalizedInput['email']['escaped']
]);

Registration\Utils\Queries\insertReferralsTableEntry([
'referrerUserId' => $referrerUserId,
'referredUserId' => $UserID,
'timestamp' => $Now,
'registrationIPs' => $registrationIPs,
'existingMatchingEnterLogIds' => $existingMatchingEnterLogIds,
]);
$referrerUserId = Registration\Utils\General\getRegistrationReferrerId();

$Message = false;
$Message['msg_id'] = '038';
$Message['args'] = array('');
$Message = json_encode($Message);
if ($referrerUserId !== null) {
$registrationIPs = [
'r' => trim($userSessionIP),
'p' => trim(Users\Session\getCurrentOriginatingIP())
];

SendSimpleMessage($referrerUserId, 0, $Now, 70, '007', '016', $Message);
if (empty($registrationIPs['p'])) {
unset($registrationIPs['p']);
}

$ActivationCode = md5(mt_rand(0, 99999999999));
$existingMatchingEnterLogIds = Registration\Utils\Queries\findEnterLogIPsWithMatchingIPValue([
'ips' => $registrationIPs,
]);

// Update User with new data
Registration\Utils\Queries\updateUserFinalDetails([
'userId' => $UserID,
'motherPlanetId' => $PlanetID,
'motherPlanetGalaxy' => $newPlanetCoordinates['galaxy'],
'motherPlanetSystem' => $newPlanetCoordinates['system'],
'motherPlanetPlanetPos' => $newPlanetCoordinates['planet'],
'referrerId' => $referrerUserId,
'activationCode' => (
REGISTER_REQUIRE_EMAILCONFIRM ?
$ActivationCode :
null
)
Registration\Utils\Queries\insertReferralsTableEntry([
'referrerUserId' => $referrerUserId,
'referredUserId' => $UserID,
'timestamp' => $Now,
'registrationIPs' => $registrationIPs,
'existingMatchingEnterLogIds' => $existingMatchingEnterLogIds,
]);

// Send a invitation private msg
$Message = false;
$Message['msg_id'] = '022';
$Message['msg_id'] = '038';
$Message['args'] = array('');
$Message = json_encode($Message);

SendSimpleMessage($UserID, 0, $Now, 70, '004', '009', $Message);
SendSimpleMessage($referrerUserId, 0, $Now, 70, '007', '016', $Message);
}

if (REGISTER_REQUIRE_EMAILCONFIRM) {
include($_EnginePath.'includes/functions/SendMail.php');
$ActivationCode = md5(mt_rand(0, 99999999999));

// Update User with new data
Registration\Utils\Queries\updateUserFinalDetails([
'userId' => $UserID,
'motherPlanetId' => $PlanetID,
'motherPlanetGalaxy' => $newPlanetCoordinates['galaxy'],
'motherPlanetSystem' => $newPlanetCoordinates['system'],
'motherPlanetPlanetPos' => $newPlanetCoordinates['planet'],
'referrerId' => $referrerUserId,
'activationCode' => (
REGISTER_REQUIRE_EMAILCONFIRM ?
$ActivationCode :
null
)
]);

$mailContent = Registration\Components\RegistrationConfirmationMail\render([
'userId' => $UserID,
'login' => $normalizedInput['username'],
'password' => $normalizedInput['password'],
'gameName' => $_GameConfig['game_name'],
'universe' => $_Lang['RegMail_UniName'],
'activationCode' => $ActivationCode,
])['componentHTML'];
// Send a invitation private msg
$Message = false;
$Message['msg_id'] = '022';
$Message['args'] = array('');
$Message = json_encode($Message);

$mailTitle = parsetemplate(
$_Lang['mail_title'],
[
'gameName' => $_GameConfig['game_name']
]
);
SendSimpleMessage($UserID, 0, $Now, 70, '004', '009', $Message);

SendMail($normalizedInput['email']['escaped'], $mailTitle, $mailContent);
}
if (REGISTER_REQUIRE_EMAILCONFIRM) {
include($_EnginePath.'includes/functions/SendMail.php');

if (isGameStartTimeReached($Now)) {
$sessionTokenValue = Session\Utils\Cookie\packSessionCookie([
'userId' => $UserID,
'username' => $normalizedInput['username'],
'obscuredPasswordHash' => Session\Utils\Cookie\createCookiePasswordHash([
'passwordHash' => $passwordHash,
]),
'isRememberMeActive' => 0,
]);

$JSONResponse['Code'] = 1;
$JSONResponse['Cookie'][] = [
'Name' => getSessionCookieKey(),
'Value' => $sessionTokenValue
];
$JSONResponse['Redirect'] = GAMEURL_UNISTRICT.'/overview.php';
} else {
$JSONResponse['Code'] = 2;
}
$mailContent = Registration\Components\RegistrationConfirmationMail\render([
'userId' => $UserID,
'login' => $normalizedInput['username'],
'password' => $normalizedInput['password'],
'gameName' => $_GameConfig['game_name'],
'universe' => $_Lang['RegMail_UniName'],
'activationCode' => $ActivationCode,
])['componentHTML'];

$mailTitle = parsetemplate(
$_Lang['mail_title'],
[
'gameName' => $_GameConfig['game_name']
]
);

SendMail($normalizedInput['email']['escaped'], $mailTitle, $mailContent);
}

if (isGameStartTimeReached($Now)) {
$sessionTokenValue = Session\Utils\Cookie\packSessionCookie([
'userId' => $UserID,
'username' => $normalizedInput['username'],
'obscuredPasswordHash' => Session\Utils\Cookie\createCookiePasswordHash([
'passwordHash' => $passwordHash,
]),
'isRememberMeActive' => 0,
]);

$JSONResponse['Code'] = 1;
$JSONResponse['Cookie'][] = [
'Name' => getSessionCookieKey(),
'Value' => $sessionTokenValue
];
$JSONResponse['Redirect'] = GAMEURL_UNISTRICT.'/overview.php';
} else {
$JSONResponse['Errors'][] = 15;
$JSONResponse['BadFields'][] = 'email';
$JSONResponse['Code'] = 2;
}
} else {
$JSONResponse['Errors'][] = 15;
$JSONResponse['BadFields'][] = 'email';
}

die('regCallback('.json_encode($JSONResponse).');');
Expand Down

0 comments on commit 442415f

Please sign in to comment.