Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Last active August 29, 2015 14:22
Show Gist options
  • Save BlackScorp/0477f318c4f60010f979 to your computer and use it in GitHub Desktop.
Save BlackScorp/0477f318c4f60010f979 to your computer and use it in GitHub Desktop.
Refactoring legacy code step by step
<?php
session_start();
require_once('./global.php');
exec_header();
//$bgEngine->gamenavigation->siteCheck("object.php");
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
$id = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : null;
$gameField = isset($bgEngine->gamefields->usergamefield[$id]) ? $bgEngine->gamefields->usergamefield[$id] : '';
$isHome = false;
$isReturn = false;
if ($id && $gameField) {
$isHome = $gameField['home'] == 1;
$isReturn = $gameField['gameobjectid']]['return'] == 1;
}
$isConfirmed = isset($_REQUEST['do']) && $_REQUEST['do'] == 'yes' ? true : false;
#-----------------------------------------------------------------------------------------
# LEAVE OBJECT (GIVE UP OBJECT)
#-----------------------------------------------------------------------------------------
if ($action == 'leave' && !$isHome && $isReturn) {
if ($isConfirmed) {
$bgEngine->gamefields->leaveGamefield($id);
unset($_SESSION['gf']);
$bgEngine->gamefields->readActField();
header("Location:object.php");
} else {
$bgEngine->tengine->display("game_header.tpl");
$bgEngine->tengine->display("game_box_error.tpl");
$bgEngine->tengine->display("game_box_info.tpl");
$bgEngine->tengine->display("game_object_leave.tpl");
$bgEngine->tengine->display("game_footer.tpl");
require_once(CWD . '/game/footer.php');
}
exit;
}
$gameFieldId = $_SESSION['gf'];
$objectId = isset($_GET['id'])?(int)$_GET['id']:$gameFieldId;
$addOrDeleteShortCut = isset($_POST['shortcut_add']) || isset($_POST['shortcut_delete']);
$addShortCut = isset($_POST['shortcut_add']);
$deleteShortCut = isset($_POST['shortcut_delete']);
$cateogries = $_POST['categories'];
$objectNameExists = isset($_POST['object_name']) && strlen(trim($_POST['object_name'])) > 0;
$objectName = $objectNameExists?substr($_POST['object_name'], 0, 25):'';
if ($addOrDeleteShortCut) {
$infoBoxMessage = '';
#-----------------------------------------------------------------------------------------
# ADD NEW SHORTCUT
#-----------------------------------------------------------------------------------------
if ($addShortCut) {
$query = $bgEngine->db->query("INSERT INTO " . TABLE_PREFIX . "usershortcut
(" . TABLE_PREFIX . "usershortcut.cat_id,
" . TABLE_PREFIX . "usershortcut.gamefieldid)
VALUE
(:cat,
:gf)");
$infoBoxMessage = $bgEngine->tengine->getconfigvars('shortcuts_add_success');
}
#-----------------------------------------------------------------------------------------
# DELETE SHORTCUT
#-----------------------------------------------------------------------------------------
if ($deleteShortCut) {
$query = $bgEngine->db->query("DELETE FROM " . TABLE_PREFIX . "usershortcut
WHERE " . TABLE_PREFIX . "usershortcut.cat_id = :cat
AND " . TABLE_PREFIX . "usershortcut.gamefieldid = :gf");
$infoBoxMessage = $bgEngine->tengine->getconfigvars('shortcuts_delete_success');
$bgEngine->shortcuts->readCategorys();
}
$query->bind(':cat', $cateogries);
$query->bind(':gf', $objectId);
$query->execute();
$bgEngine->shortcuts->readShortcuts();
$bgEngine->shortcuts->buildLinks('head');
$bgEngine->shortcuts->createTree();
$bgEngine->tengine->assign("shortcuts", $bgEngine->shortcuts->tree);
$bgEngine->tengine->assign("info_box_message", $infoBoxMessage);
}
#-----------------------------------------------------------------------------------------
# READ SHORTCUT CATEGORIES
#-----------------------------------------------------------------------------------------
$query = $bgEngine->db->query("SELECT " . Q_CACHE . " " . TABLE_PREFIX . "usershortcut_cat.*
FROM " . TABLE_PREFIX . "usershortcut_cat
WHERE " . TABLE_PREFIX . "usershortcut_cat.userid = :userid
ORDER BY " . TABLE_PREFIX . "usershortcut_cat.name");
$query->bind(':userid', $bgEngine->userinfo['userid']);
$query->execute();
$data = array();
while ($result = $query->fetch_assoc()) {
$data[] = $result;
}
$bgEngine->tengine->assign("shortcut_cats", $data);
#-----------------------------------------------------------------------------------------
# READ OBJECT INFORMATION (ANY OBJECTS)
#-----------------------------------------------------------------------------------------
$query = $bgEngine->db->query('SELECT ' . Q_NO_CACHE . ' ' . TABLE_PREFIX . 'gamefield.*,
' . TABLE_PREFIX . 'gameobject.*,
' . TABLE_PREFIX . 'gameobject.fields as objectfields,
' . TABLE_PREFIX . 'userfield.userid,
' . TABLE_PREFIX . 'userfield.name,
' . TABLE_PREFIX . 'userfield.fields,
' . TABLE_PREFIX . 'userfield.home,
' . TABLE_PREFIX . 'user.username,
' . TABLE_PREFIX . 'user.partyid,
' . TABLE_PREFIX . 'useralliance.*,
' . TABLE_PREFIX . 'alliance.*,
' . TABLE_PREFIX . 'user.userid,
' . TABLE_PREFIX . 'login.online_state,
' . TABLE_PREFIX . 'userhighscore.lastactivity
FROM ' . TABLE_PREFIX . 'gamefield
LEFT JOIN ' . TABLE_PREFIX . 'userfield ON ' . TABLE_PREFIX . 'userfield.gamefieldid = ' . TABLE_PREFIX . 'gamefield.gamefieldid
JOIN ' . TABLE_PREFIX . 'gameobject ON ' . TABLE_PREFIX . 'gameobject.gameobjectid = ' . TABLE_PREFIX . 'gamefield.gameobjectid
LEFT JOIN ' . TABLE_PREFIX . 'user ON ' . TABLE_PREFIX . 'user.userid = ' . TABLE_PREFIX . 'userfield.userid
LEFT JOIN ' . TABLE_PREFIX . 'loginuser ON ' . TABLE_PREFIX . 'loginuser.userid = ' . TABLE_PREFIX . 'user.userid
LEFT JOIN ' . TABLE_PREFIX . 'login ON ' . TABLE_PREFIX . 'login.loginid = ' . TABLE_PREFIX . 'loginuser.loginid
LEFT JOIN ' . TABLE_PREFIX . 'useralliance ON ' . TABLE_PREFIX . 'useralliance.userid = ' . TABLE_PREFIX . 'user.userid
LEFT JOIN ' . TABLE_PREFIX . 'alliance ON ' . TABLE_PREFIX . 'alliance.allianceid = ' . TABLE_PREFIX . 'useralliance.allianceid
LEFT JOIN ' . TABLE_PREFIX . 'userhighscore ON ' . TABLE_PREFIX . 'userhighscore.userid = ' . TABLE_PREFIX . 'user.userid
WHERE ' . TABLE_PREFIX . 'gamefield.gamefieldid = :fieldid');
$query->bind(':fieldid', $objectId);
$query->execute();
$result = $query->fetch_assoc();
//------------------------- SETUP ARMY LIMITS (04/2015) -----------------------
$maxtowerlvl = $bgEngine->formular->executeFormular($bgEngine->groupinfo['squad_formular']);
$bgEngine->tengine->assign("maxtowerlvl", $maxtowerlvl);
$maxarmysize = $maxtowerlvl * 100;
$repulseLevel = $bgEngine->gamefields->usergamefield[$gameFieldId]['r' . REPULSE_ID]; //affected by buff 68: Repulse
$maxarmysize -= $repulseLevel * 50;
$maxarmysize = max(0, $maxarmysize);
$bgEngine->tengine->assign("repulseLevel", $repulseLevel);
$bgEngine->tengine->assign("maxarmysize", $maxarmysize);
$maxspellsize = $bgEngine->formular->executeFormular($bgEngine->groupinfo['squad_formular']);
$mindblockLevel = $bgEngine->gamefields->usergamefield[$gameFieldId]['r' . MINDBLOCK_ID];
$maxspellsize -= $mindblockLevel;
$maxspellsize = max(0, $maxspellsize);
$bgEngine->tengine->assign("mindblockLevel", $mindblockLevel);
$bgEngine->tengine->assign("maxspellsize", $maxspellsize);
$shacklesLevel = $bgEngine->gamefields->usergamefield[$gameFieldId]['r' . SHACKLES_ID];
$bgEngine->tengine->assign("shacklesLevel", $shacklesLevel);
$bgEngine->tengine->assign("maxherosize", 1);
$bgEngine->tengine->assign("maxunitsize", $maxtowerlvl + 5);
$inspectingOwnObject = false;
$inspectingForeignObject = false;
#-----------------------------------------------------------------------------------------
# INSPECTING YOUR OWN OBJECT
#-----------------------------------------------------------------------------------------
if ($result['userid'] == $bgEngine->userinfo['userid'] && $result['gamefieldid'] == $gameFieldId) {
$inspectingOwnObject = true;
//read squads
$bgEngine->squads->setGamefieldId($gameFieldId, true);
$bgEngine->squads->resetSquads();
$bgEngine->squads->readSquads();
$bgEngine->movement = new bgEngineMovement($bgEngine);
$bgEngine->movement->checkSquadMovement();
$bgEngine->tengine->assign("units", $bgEngine->gamefields->usergamefield[$bgEngine->squads->gamefieldId]['units']);
$bgEngine->tengine->assign("squads", $bgEngine->squads->squad);
$bgEngine->tengine->assign("squadsstation", $bgEngine->squads->squadStation);
//read units
$bgEngine->units->readBuildlist();
$bgEngine->units->fetchUnits();
//read buildings
$bgEngine->buildings->checkBuildings();
$bgEngine->buildings->fetchBuildings();
$bgEngine->buildings->readUnitAssignment();
//read researches
$bgEngine->researches->checkResearch();
$bgEngine->researches->fetchResearch();
$bgEngine->tengine->assign("buildings", $bgEngine->gamefields->usergamefield[$objectId]['buildings']);
$bgEngine->tengine->assign("researches", $bgEngine->gamefields->usergamefield[$objectId]['researches']);
$bgEngine->tengine->assign("units", $bgEngine->gamefields->usergamefield[$objectId]['units']);
if (isset($result['gameobjectid'])) {
if ($objectNameExists) {
$query = $bgEngine->db->query('UPDATE ' . TABLE_PREFIX . 'userfield
SET ' . TABLE_PREFIX . 'userfield.name = :objectname
WHERE ' . TABLE_PREFIX . 'userfield.gamefieldid = :fieldid');
$query->bind(':objectname', $objectName);
$query->bind(':fieldid', $gameFieldId);
$query->execute();
$result['name'] = $objectName;
$bgEngine->gamefields->usergamefield[$gameFieldId]['name'] = $objectName;
$bgEngine->tengine->assign("gamefields", $bgEngine->gamefields->usergamefield);
}
$bgEngine->tengine->display("game_box_error.tpl");
$bgEngine->tengine->display("game_box_info.tpl");
$bgEngine->tengine->display("game_object.tpl");
}
}
#-----------------------------------------------------------------------------------------
# INSPECTING AN FOREIGN OBJECT
#-----------------------------------------------------------------------------------------
else {
$inspectingForeignObject = true;
if (isset($result['gameobjectid'])) {
//Need Hero level for item count
foreach ($bgEngine->gamefields->usergamefield[$gameFieldId]['units'] as $unitid => $unitdata) {
if ($bgEngine->gamefields->usergamefield[$gameFieldId]["u" . $unitid] > 0) {
$bgEngine->units->calcUnitAttribute(UNIT_STAT_LEVEL, $unitid);
}
}
$bgEngine->tengine->assign("units", $bgEngine->gamefields->usergamefield[$gameFieldId]['units']);
$bgEngine->tengine->display("game_object_stranger.tpl");
}
}
if (isset($result['gameobjectid'])) {
$result['partytitle'] = $bgEngine->tengine->getconfigvars('party_' . $result['partyid']);
//recalculate fields
$bgEngine->formular->addSyntax('BASIC_FIELDS', $result['fields']);
$result['fields'] = $bgEngine->formular->executeFormular($bgEngine->groupinfo['fields_formular']);
$result['isonline'] = isOnline($result['online_state'], $result['lastactivity'], $result['allianceid'], $bgEngine->userinfo['allianceid']);
$coords = '';
foreach ($bgEngine->gamefields->gamefieldlevel as $fieldlvl) {
$coords .= $result['field' . $fieldlvl['gamefieldlevelid']] . ':';
if ($inspectingOwnObject) {
$mapnum = $result['field' . $fieldlvl['gamefieldlevelid']];
}
}
$coords = trim($coords, ':');
if ($inspectingOwnObject) {
$result['mapnum'] = $mapnum;
}
$result['coordstring'] = $coords;
$result['usernameurl'] = urlencode($result['username']);
$result['name'] = empty($result['name']) ? $bgEngine->tengine->getconfigvars('gameobject_' . $result['gameobjectid']) : $result['name'];
$bgEngine->tengine->assign("entry", $result);
$bgEngine->tengine->display("game_header.tpl");
$bgEngine->tengine->display("game_footer.tpl");
}
require_once(CWD . '/game/footer.php');
#=========================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment