Skip to content

Commit

Permalink
Redesign database infrastructure
Browse files Browse the repository at this point in the history
All tables that have an `(account_id, game_id)` primary key will be
changed to `(player_id, game_id)`. Pretty much every instance of an
account ID is replaced with a player ID in all game-scoped code.

This is the first step in a major redesign to associate tables to the
most closely-related table, rather than having everything depend on
the high-level `account` table.

Some features that this change will help facilitate:
* Multiple players per account per game (to replace "multi accounts").
* NPCs not needing an associated account.
* Free-standing ships/weapons.
  • Loading branch information
hemberger committed Nov 12, 2020
1 parent 0710abf commit e1c155a
Show file tree
Hide file tree
Showing 126 changed files with 589 additions and 593 deletions.
21 changes: 11 additions & 10 deletions admin/Default/account_edit_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,18 @@
if (!empty($delete)) {
foreach ($delete as $game_id => $value) {
if ($value == 'TRUE') {
$deletePlayer = SmrPlayer::getPlayerByAccountID($account_id, $game_id);
$playerID = $deletePlayer->getPlayerID();
$sql = $deletePlayer->getSQL();

// Check for bank transactions into the alliance account
$db->query('SELECT * FROM alliance_bank_transactions WHERE payee_id=' . $db->escapeNumber($account_id) . ' AND game_id=' . $db->escapeNumber($game_id) . ' LIMIT 1');
$db->query('SELECT * FROM alliance_bank_transactions WHERE payee_player_id=' . $db->escapeNumber($playerID) . ' AND game_id=' . $db->escapeNumber($game_id) . ' LIMIT 1');
if ($db->getNumRows() != 0) {
// Can't delete
$actions[] = 'player has made alliance transaction';
continue;
}

$sql = 'account_id=' . $db->escapeNumber($account_id) . ' AND game_id=' . $db->escapeNumber($game_id);

// Check anon accounts for transactions
$db->query('SELECT * FROM anon_bank_transactions WHERE ' . $sql . ' LIMIT 1');
if ($db->getNumRows() != 0) {
Expand All @@ -157,18 +159,17 @@
continue;
}

$db->query('DELETE FROM alliance_thread
WHERE sender_id=' . $db->escapeNumber($account_id) . ' AND game_id=' . $db->escapeNumber($game_id));
$db->query('DELETE FROM alliance_thread WHERE ' . $sql);
$db->query('DELETE FROM bounty WHERE ' . $sql);
$db->query('DELETE FROM galactic_post_applications WHERE ' . $sql);
$db->query('DELETE FROM galactic_post_article
WHERE writer_id=' . $db->escapeNumber($account_id) . ' AND game_id=' . $db->escapeNumber($game_id));
$db->query('DELETE FROM galactic_post_article WHERE ' . $sql);
$db->query('DELETE FROM galactic_post_writer WHERE ' . $sql);
$db->query('DELETE FROM message WHERE ' . $sql);
$db->query('DELETE FROM message_notify
WHERE (from_id=' . $db->escapeNumber($account_id) . ' OR to_id=' . $db->escapeNumber($account_id) . ') AND game_id=' . $db->escapeNumber($game_id));
$db->query('UPDATE planet SET owner_id=0,planet_name=\'\',password=\'\',shields=0,drones=0,credits=0,bonds=0
WHERE owner_id=' . $db->escapeNumber($account_id) . ' AND game_id=' . $db->escapeNumber($game_id));
WHERE (from_player_id=' . $db->escapeNumber($playerID) . ' OR to_player_id=' . $db->escapeNumber($playerID) . ') AND game_id=' . $db->escapeNumber($game_id));
$db->query('UPDATE planet SET owner_player_id=0,planet_name=\'\',password=\'\',shields=0,drones=0,credits=0,bonds=0
WHERE ' . $sql);

$db->query('DELETE FROM player_attacks_planet WHERE ' . $sql);
$db->query('DELETE FROM player_attacks_port WHERE ' . $sql);
$db->query('DELETE FROM player_has_alliance_role WHERE ' . $sql);
Expand Down
8 changes: 4 additions & 4 deletions admin/Default/admin_message_send.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

if ($gameID != 20000) {
$game = SmrGame::getGame($gameID);
$gamePlayers = [['AccountID' => 0, 'Name' => 'All Players (' . $game->getName() . ')']];
$db->query('SELECT account_id,player_id,player_name FROM player WHERE game_id = ' . $db->escapeNumber($gameID) . ' ORDER BY player_name');
$gamePlayers = [['PlayerID' => 0, 'Name' => 'All Players (' . $game->getName() . ')']];
$db->query('SELECT player_id,player_name FROM player WHERE game_id = ' . $db->escapeNumber($gameID) . ' ORDER BY player_name');
while ($db->nextRecord()) {
$gamePlayers[] = [
'AccountID' => $db->getInt('account_id'),
'PlayerID' => $db->getInt('player_id'),
'Name' => $db->getField('player_name') . ' (' . $db->getInt('player_id') . ')',
];
}
$template->assign('GamePlayers', $gamePlayers);
$template->assign('SelectedAccountID', $var['account_id'] ?? 0);
$template->assign('SelectedPlayerID', $var['player_id'] ?? 0);
}
if (isset($var['preview'])) {
$template->assign('Preview', $var['preview']);
Expand Down
16 changes: 8 additions & 8 deletions admin/Default/admin_message_send_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$expire = Request::getFloat('expire');
$game_id = $var['SendGameID'];
if ($game_id != ALL_GAMES_ID) {
$account_id = Request::getInt('account_id');
$playerID = Request::getInt('player_id');
}

if (Request::get('action') == 'Preview message') {
Expand All @@ -14,7 +14,7 @@
$container['preview'] = $message;
$container['expire'] = $expire;
if ($game_id != ALL_GAMES_ID) {
$container['account_id'] = $account_id;
$container['player_id'] = $playerID;
}
forward($container);
}
Expand All @@ -29,20 +29,20 @@

$receivers = [];
if ($game_id != ALL_GAMES_ID) {
if ($account_id == 0) {
if ($playerID == 0) {
// Send to all players in the requested game
$db->query('SELECT account_id FROM player WHERE game_id = ' . $db->escapeNumber($game_id));
$db->query('SELECT player_id FROM player WHERE game_id = ' . $db->escapeNumber($game_id));
while ($db->nextRecord()) {
$receivers[] = [$game_id, $db->getInt('account_id')];
$receivers[] = [$game_id, $db->getInt('player_id')];
}
} else {
$receivers[] = [$game_id, $account_id];
$receivers[] = [$game_id, $player_id];
}
} else {
//send to all players in games that haven't ended yet
$db->query('SELECT game_id,account_id FROM player JOIN game USING(game_id) WHERE end_time > ' . $db->escapeNumber(TIME));
$db->query('SELECT game_id, player_id FROM player JOIN game USING(game_id) WHERE end_time > ' . $db->escapeNumber(TIME));
while ($db->nextRecord()) {
$receivers[] = [$db->getInt('game_id'), $db->getInt('account_id')];
$receivers[] = [$db->getInt('game_id'), $db->getInt('player_id')];
}
}
// Send the messages
Expand Down
2 changes: 1 addition & 1 deletion admin/Default/anon_acc_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

$db->query('SELECT *
FROM anon_bank_transactions
JOIN player USING(account_id, game_id)
JOIN player USING(player_id, game_id)
WHERE anon_id = '.$db->escapeNumber($anonID) . '
AND game_id = '.$db->escapeNumber($gameID) . '
ORDER BY transaction_id');
Expand Down
6 changes: 3 additions & 3 deletions admin/Default/box_reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

$container = create_container('box_reply_processing.php');
transfer('game_id');
transfer('sender_id');
transfer('sender_account_id');
transfer('box_type_id');
$template->assign('BoxReplyFormHref', SmrSession::getNewHREF($container));
$template->assign('Sender', SmrPlayer::getPlayer($var['sender_id'], $var['game_id']));
$template->assign('SenderAccount', SmrAccount::getAccount($var['sender_id']));
$template->assign('Sender', SmrPlayer::getPlayer($var['sender_account_id'], $var['game_id']));
$template->assign('SenderAccount', SmrAccount::getAccount($var['sender_account_id']));
if (isset($var['Preview'])) {
$template->assign('Preview', $var['Preview']);
}
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/box_reply_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$container['BanPoints'] = $banPoints;
$container['RewardCredits'] = $rewardCredits;
transfer('game_id');
transfer('sender_id');
transfer('sender_account_id');
transfer('box_type_id');
$container['Preview'] = $message;
forward($container);
Expand All @@ -19,7 +19,7 @@
//do we have points?
if ($banPoints > 0) {
$suspicion = 'Inappropriate Actions';
$senderAccount = SmrAccount::getAccount($var['sender_id']);
$senderAccount = SmrAccount::getAccount($var['sender_account_id']);
$senderAccount->addPoints($banPoints, $account, BAN_REASON_BAD_BEHAVIOR, $suspicion);
}

Expand Down
12 changes: 6 additions & 6 deletions admin/Default/box_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@
'ID' => $messageID
);

$senderID = $db->getInt('sender_id');
if ($senderID == 0) {
$senderAccountID = $db->getInt('sender_account_id');
if ($senderAccountID == 0) {
$senderName = 'User not logged in';
} else {
$senderAccount = SmrAccount::getAccount($senderID);
$senderName = $senderAccount->getLogin() . ' (' . $senderID . ')';
$senderAccount = SmrAccount::getAccount($senderAccountID);
$senderName = $senderAccount->getLogin() . ' (' . $senderAccountID . ')';
if ($validGame) {
$senderPlayer = SmrPlayer::getPlayer($senderID, $gameID);
$senderPlayer = SmrPlayer::getPlayerByAccountID($senderAccountID, $gameID);
$senderName .= ' a.k.a ' . $senderPlayer->getDisplayName();
$container = create_container('skeleton.php', 'box_reply.php');
$container['sender_id'] = $senderID;
$container['sender_account_id'] = $senderAccountID;
$container['game_id'] = $gameID;
transfer('box_type_id');
$messages[$messageID]['ReplyHREF'] = SmrSession::getNewHREF($container);
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/log_anonymous_account.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

// get all anon bank transactions that are logged in an array
$db->query('SELECT * FROM anon_bank_transactions
JOIN account USING(account_id)
JOIN player USING(player_id)
WHERE account_id IN ('.$db->escapeArray($log_account_ids) . ')
ORDER BY game_id DESC, anon_id ASC');
$anon_logs = [];
while ($db->nextRecord()) {
$transaction = strtolower($db->getField('transaction'));
$anon_logs[$db->getInt('game_id')][$db->getInt('anon_id')][] = [
'login' => $db->getField('login'),
'player_name' => $db->getField('player_name'),
'amount' => number_format($db->getInt('amount')),
'date' => date(DATE_FULL_SHORT, $db->getInt('time')),
'type' => $transaction,
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/manage_draft_leaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

// Get the list of current draft leaders for the selected game
$currentLeaders = array();
$db->query('SELECT account_id, home_sector_id FROM draft_leaders WHERE game_id=' . $db->escapeNumber($selectedGameID));
$db->query('SELECT player_id, home_sector_id FROM draft_leaders WHERE game_id=' . $db->escapeNumber($selectedGameID));
while ($db->nextRecord()) {
$homeSectorID = $db->getInt('home_sector_id');
$leader = SmrPlayer::getPlayer($db->getInt('account_id'), $selectedGameID);
$leader = SmrPlayer::getPlayer($db->getInt('player_id'), $selectedGameID);
$currentLeaders[] = [
'Name' => $leader->getDisplayName(),
'HomeSectorID' => $homeSectorID === 0 ? 'None' : $homeSectorID,
Expand Down
7 changes: 3 additions & 4 deletions admin/Default/manage_draft_leaders_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
SmrSession::updateVar('processing_msg', null);

// Get the POST variables
$playerId = Request::getInt('player_id');
$playerID = Request::getInt('player_id');
$homeSectorID = Request::getInt('home_sector_id');
$action = Request::get('submit');

try {
$selectedPlayer = SmrPlayer::getPlayerByPlayerID($playerId, $gameId);
$selectedPlayer = SmrPlayer::getPlayer($playerID, $gameId);
} catch (PlayerNotFoundException $e) {
$msg = "<span class='red'>ERROR: </span>" . $e->getMessage();
SmrSession::updateVar('processing_msg', $msg);
forward(create_container('skeleton.php', 'manage_draft_leaders.php', $var));
}

$name = $selectedPlayer->getDisplayName();
$accountId = $selectedPlayer->getAccountID();
$game = $selectedPlayer->getGame()->getDisplayName();

if ($action == "Assign") {
if ($selectedPlayer->isDraftLeader()) {
$msg = "<span class='red'>ERROR: </span>$name is already a draft leader in game $game!";
} else {
$db->query('INSERT INTO draft_leaders (account_id, game_id, home_sector_id) VALUES (' . $db->escapeNumber($accountId) . ', ' . $db->escapeNumber($gameId) . ', ' . $db->escapeNumber($homeSectorID) . ')');
$db->query('INSERT INTO draft_leaders (player_id, game_id, home_sector_id) VALUES (' . $db->escapeNumber($playerID) . ', ' . $db->escapeNumber($gameId) . ', ' . $db->escapeNumber($homeSectorID) . ')');
}
} elseif ($action == "Remove") {
if (!$selectedPlayer->isDraftLeader()) {
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/manage_post_editors.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

// Get the list of current editors for the selected game
$currentEditors = array();
foreach (Globals::getGalacticPostEditorIDs($selectedGameID) as $editorID) {
$editor = SmrPlayer::getPlayer($editorID, $selectedGameID);
foreach (Globals::getGalacticPostEditorPlayerIDs($selectedGameID) as $editorPlayerID) {
$editor = SmrPlayer::getPlayer($editorPlayerID, $selectedGameID);
$currentEditors[] = $editor->getDisplayName();
}
$template->assign('CurrentEditors', $currentEditors);
Expand Down
5 changes: 2 additions & 3 deletions admin/Default/manage_post_editors_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
$action = Request::get('submit');

try {
$selected_player = SmrPlayer::getPlayerByPlayerID($player_id, $game_id);
$selected_player = SmrPlayer::getPlayer($player_id, $game_id);
} catch (PlayerNotFoundException $e) {
$msg = "<span class='red'>ERROR: </span>" . $e->getMessage();
SmrSession::updateVar('processing_msg', $msg);
forward(create_container('skeleton.php', 'manage_post_editors.php', $var));
}

$name = $selected_player->getDisplayName();
$account_id = $selected_player->getAccountID();
$game = $selected_player->getGame()->getDisplayName();

if ($action == "Assign") {
if ($selected_player->isGPEditor()) {
$msg = "<span class='red'>ERROR: </span>$name is already an editor in game $game!";
} else {
$db->query('INSERT INTO galactic_post_writer (account_id, game_id) VALUES (' . $db->escapeNumber($account_id) . ', ' . $db->escapeNumber($game_id) . ')');
$db->query('INSERT INTO galactic_post_writer (player_id, game_id) VALUES (' . $db->escapeNumber($player_id) . ', ' . $db->escapeNumber($game_id) . ')');
}
} elseif ($action == "Remove") {
if (!$selected_player->isGPEditor()) {
Expand Down
8 changes: 4 additions & 4 deletions admin/Default/notify_reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

$container = create_container('notify_reply_processing.php');
transfer('game_id');
transfer('offended');
transfer('offender');
transfer('offended_player_id');
transfer('offender_player_id');
$template->assign('NotifyReplyFormHref', SmrSession::getNewHREF($container));
$offender = getMessagePlayer($var['offender'], $var['game_id']);
$offended = getMessagePlayer($var['offended'], $var['game_id']);
$offender = getMessagePlayer($var['offender_player_id'], $var['game_id']);
$offended = getMessagePlayer($var['offended_player_id'], $var['game_id']);
if (is_object($offender)) {
$template->assign('OffenderAccount', $offender->getAccount());
}
Expand Down
13 changes: 6 additions & 7 deletions admin/Default/notify_reply_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
$offendedBanPoints = Request::getInt('offendedBanPoints');
if (Request::get('action') == 'Preview messages') {
$container = create_container('skeleton.php', 'notify_reply.php');
transfer('offender');
transfer('offended');
transfer('offender_player_id');
transfer('offended_player_id');
transfer('game_id');
transfer('sender_id');
$container['PreviewOffender'] = $offenderReply;
$container['OffenderBanPoints'] = $offenderBanPoints;
$container['PreviewOffended'] = $offendedReply;
Expand All @@ -18,24 +17,24 @@


if ($offenderReply != '') {
SmrPlayer::sendMessageFromAdmin($var['game_id'], $var['offender'], $offenderReply);
SmrPlayer::sendMessageFromAdmin($var['game_id'], $var['offender_player_id'], $offenderReply);

//do we have points?
if ($offenderBanPoints > 0) {
$suspicion = 'Inappropriate In-Game Message';
$offenderAccount = SmrAccount::getAccount($var['offender']);
$offenderAccount = SmrPlayer::getPlayer($var['offender_player_id'], $var['game_id'])->getAccount();
$offenderAccount->addPoints($offenderBanPoints, $account, BAN_REASON_BAD_BEHAVIOR, $suspicion);
}
}

if ($offendedReply != '') {
//next message
SmrPlayer::sendMessageFromAdmin($var['game_id'], $var['offended'], $offendedReply);
SmrPlayer::sendMessageFromAdmin($var['game_id'], $var['offended_player_id'], $offendedReply);

//do we have points?
if ($offendedBanPoints > 0) {
$suspicion = 'Inappropriate In-Game Message';
$offenderAccount = SmrAccount::getAccount($var['offended']);
$offenderAccount = SmrPlayer::getPlayer($var['offended_player_id'], $var['game_id'])->getAccount();
$offenderAccount->addPoints($offendedBanPoints, $account, BAN_REASON_BAD_BEHAVIOR, $suspicion);
}
}
Expand Down
8 changes: 4 additions & 4 deletions admin/Default/notify_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
$messages = [];
while ($db->nextRecord()) {
$gameID = $db->getInt('game_id');
$sender = getMessagePlayer($db->getInt('from_id'), $gameID);
$receiver = getMessagePlayer($db->getInt('to_id'), $gameID);
$sender = getMessagePlayer($db->getInt('from_player_id'), $gameID);
$receiver = getMessagePlayer($db->getInt('to_player_id'), $gameID);

$container = create_container('skeleton.php', 'notify_reply.php');
$container['offender'] = $db->getInt('from_id');
$container['offended'] = $db->getInt('to_id');
$container['offender_player_id'] = $db->getInt('from_player_id');
$container['offended_player_'] = $db->getInt('to_player_id');
$container['game_id'] = $gameID;

$offender = $sender;
Expand Down
3 changes: 2 additions & 1 deletion admin/Default/npc_manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
// Get the existing NPC players for the selected game
$db->query('SELECT * FROM player WHERE game_id=' . $db->escapeNumber($selectedGameID) . ' AND npc=' . $db->escapeBoolean(true));
while ($db->nextRecord()) {
$playerID = $db->getInt('player_id');
$accountID = $db->getInt('account_id');
$npcs[$accountID]['player'] = SmrPlayer::getPlayer($accountID, $selectedGameID, false, $db);
$npcs[$accountID]['player'] = SmrPlayer::getPlayer($playerID, $selectedGameID, false, $db);
}

$template->assign('Npcs', $npcs);
2 changes: 1 addition & 1 deletion admin/Default/npc_manage_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$alliance = SmrAlliance::getAllianceByName($allianceName, $gameID);
if (is_null($alliance)) {
$alliance = SmrAlliance::createAlliance($gameID, $allianceName);
$alliance->setLeaderID($npcPlayer->getAccountID());
$alliance->setLeaderPlayerID($npcPlayer->getPlayerID());
$alliance->update();
$alliance->createDefaultRoles();
}
Expand Down
10 changes: 2 additions & 8 deletions admin/Default/ship_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

$template->assign('PageTopic', 'Ship Integrity Check');

$db->query('SELECT * FROM ship_type_support_hardware, player, ship_has_hardware, hardware_type ' .
'WHERE ship_type_support_hardware.ship_type_id = player.ship_type_id AND ' .
'player.account_id = ship_has_hardware.account_id AND ' .
'player.game_id = ship_has_hardware.game_id AND ' .
'ship_type_support_hardware.hardware_type_id = ship_has_hardware.hardware_type_id AND ' .
'ship_has_hardware.hardware_type_id = hardware_type.hardware_type_id AND ' .
'amount > max_amount');
$db->query('SELECT * FROM ship_type_support_hardware JOIN hardware_type USING (hardware_type_id) JOIN ship_has_hardware USING (hardware_type_id) JOIN player USING (player_id, game_id, ship_type_id) WHERE amount > max_amount');

$excessHardware = [];
while ($db->nextRecord()) {
$container = create_container('ship_check_processing.php');
$container['account_id'] = $db->getInt('account_id');
$container['player_id'] = $db->getInt('player_id');
$container['hardware'] = $db->getInt('hardware_type_id');
$container['game_id'] = $db->getInt('game_id');
$container['max_amount'] = $db->getInt('max_amount');
Expand Down
Loading

0 comments on commit e1c155a

Please sign in to comment.