Skip to content

Commit

Permalink
Merge pull request #43 from lanedirt/35-add-unit-properties-to--techn…
Browse files Browse the repository at this point in the history
…ology-popup

Add unit properties to  technology popup
  • Loading branch information
lanedirt committed Apr 7, 2024
2 parents 2f600c0 + 2e45607 commit 5c43c8f
Show file tree
Hide file tree
Showing 8 changed files with 836 additions and 270 deletions.
223 changes: 174 additions & 49 deletions app/Http/Controllers/TechtreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace OGame\Http\Controllers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use OGame\Http\Traits\IngameTrait;
use OGame\Services\ObjectService;
Expand All @@ -15,12 +18,13 @@ class TechtreeController extends Controller
* Returns techtree ajax content.
*
* @param int $id
* @return Response
* @return Application|Factory|\Illuminate\Foundation\Application|View
*/
public function ajax(Request $request, ObjectService $objects, PlayerService $player)
public function ajax(Request $request, ObjectService $objects, PlayerService $player): View|\Illuminate\Foundation\Application|Factory|false|Application
{
$object_id = $request->input('object_id');
$tab = $request->input('tab');
// TODO: is this planet still needed?
$planet = $player->planets->current();

// Load object
Expand All @@ -34,64 +38,20 @@ public function ajax(Request $request, ObjectService $objects, PlayerService $pl
'current_level' => $player->planets->current()->getObjectLevel($object_id),
]);
} elseif ($tab == 2) {
$current_level = $player->planets->current()->getObjectLevel($object_id);

// Tech info (resource production tables etc)
$production_table = [];
if (!empty($object['production'])) {
$production_amount_current_level = 0;
foreach ($planet->getBuildingProduction($object_id, $current_level) as $type => $amount) {
if ($amount > 0) {
$production_amount_current_level = $amount;
}
}

// Create production table array value
// TODO: add unittest to verify that production calculation is correctly for various buildings.
$min_level = (($current_level - 2) > 1) ? $current_level - 2 : 1;
for ($i = $min_level; $i < $min_level + 15; $i++) {
$production_amount_previous_level = 0;
foreach ($planet->getBuildingProduction($object_id, $i - 1) as $type => $amount) {
if ($amount > 0) {
$production_amount_previous_level = $amount;
}
}

$production_array = $planet->getBuildingProduction($object_id, $i);
$production_amount = 0;
foreach ($production_array as $type => $amount) {
if ($amount > 0) {
$production_amount = $amount;
}
}

$production_table[] = [
'level' => $i,
'production' => $production_amount,
'production_difference' => $production_amount - $production_amount_current_level,
'production_difference_per_level' => ($i == $current_level) ? 0 : (($i - 1 < $current_level) ? ($production_amount - $production_amount_previous_level) * -1 : $production_amount - $production_amount_previous_level),
'energy_balance' => $planet->getBuildingProduction($object_id, $i)['energy'],
'energy_difference' => ($i == $current_level) ? 0 : ($planet->getBuildingProduction($object_id, $i)['energy'] - $planet->getBuildingProduction($object_id, $current_level)['energy']),
'deuterium_consumption' => $planet->getBuildingProduction($object_id, $i)['deuterium'],
'deuterium_consumption_per_level' => ($i == $current_level) ? 0 : ($planet->getBuildingProduction($object_id, $i)['deuterium'] - $planet->getBuildingProduction($object_id, $current_level)['deuterium']),
'protected' => 0,
];
}
}

return view('ingame.techtree.techinfo')->with([
'object' => $object,
'object_id' => $object_id,
'planet' => $planet,
'current_level' => $player->planets->current()->getObjectLevel($object_id),
'production_table' => $production_table,
'production_table' => $this->getProductionTable($object, $player),
'rapidfire_table' => $this->getRapidfireTable($object, $objects),
'properties_table' => $this->getPropertiesTable($object),
]);
} elseif ($tab == 3) {
return view('ingame.techtree.technology')->with([
'object' => $object,
'object_id' => $object_id,
'planet' => $planet,
'current_level' => $player->planets->current()->getObjectLevel($object_id),
]);
} elseif ($tab == 4) {
return view('ingame.techtree.applications')->with([
Expand All @@ -104,4 +64,169 @@ public function ajax(Request $request, ObjectService $objects, PlayerService $pl

return false;
}

/**
* Returns techtree production table.
*
* @param int $id
* @return Application|Factory|View|\Illuminate\Foundation\Application
*/
public function getProductionTable($object, PlayerService $player): \Illuminate\Foundation\Application|View|Factory|Application
{
$object_id = $object['id'];
$planet = $player->planets->current();
$current_level = $player->planets->current()->getObjectLevel($object['id']);

$production_table = [];
if (!empty($object['production'])) {
$production_amount_current_level = 0;
foreach ($planet->getBuildingProduction($object['id'], $current_level) as $type => $amount) {
if ($amount > 0) {
$production_amount_current_level = $amount;
}
}

// Create production table array value
// TODO: add unittest to verify that production calculation is correctly for various buildings.
$min_level = (($current_level - 2) > 1) ? $current_level - 2 : 1;
for ($i = $min_level; $i < $min_level + 15; $i++) {
$production_amount_previous_level = 0;
foreach ($planet->getBuildingProduction($object['id'], $i - 1) as $type => $amount) {
if ($amount > 0) {
$production_amount_previous_level = $amount;
}
}

$production_array = $planet->getBuildingProduction($object['id'], $i);
$production_amount = 0;
foreach ($production_array as $type => $amount) {
if ($amount > 0) {
$production_amount = $amount;
}
}

$production_table[] = [
'level' => $i,
'production' => $production_amount,
'production_difference' => $production_amount - $production_amount_current_level,
'production_difference_per_level' => ($i == $current_level) ? 0 : (($i - 1 < $current_level) ? ($production_amount - $production_amount_previous_level) * -1 : $production_amount - $production_amount_previous_level),
'energy_balance' => $planet->getBuildingProduction($object['id'], $i)['energy'],
'energy_difference' => ($i == $current_level) ? 0 : ($planet->getBuildingProduction($object['id'], $i)['energy'] - $planet->getBuildingProduction($object['id'], $current_level)['energy']),
'deuterium_consumption' => $planet->getBuildingProduction($object['id'], $i)['deuterium'],
'deuterium_consumption_per_level' => ($i == $current_level) ? 0 : ($planet->getBuildingProduction($object['id'], $i)['deuterium'] - $planet->getBuildingProduction($object['id'], $current_level)['deuterium']),
'protected' => 0,
];
}
}

return view('ingame.techtree.info.production')->with([
'object' => $object,
'planet' => $planet,
'production_table' => $production_table,
'current_level' => $player->planets->current()->getObjectLevel($object_id),
]);
}

/**
* Returns techtree rapidfire table.
*
* @param int $id
* @return Application|Factory|\Illuminate\Foundation\Application|View
*/
public function getRapidfireTable($object, ObjectService $objects): View|\Illuminate\Foundation\Application|Factory|Application
{
// Loop through all other objects and see if they have rapidfire against this object
// if so, create a new array with the rapidfire data same as above.
$rapidfire_from = [];
foreach ($objects->getObjects() as $from_object) {
if (empty($from_object['rapidfire'])) {
continue;
}

foreach ($from_object['rapidfire'] as $target_objectid => $data) {
if ($target_objectid == $object['id']) {
$rapidfire_from[$from_object['id']] = $data;
$rapidfire_from[$from_object['id']]['object'] = $from_object;
}
}
}

// Get rapidfire against other objects.
$rapidfire_against = [];
if (!empty($object['rapidfire'])) {
foreach ($object['rapidfire'] as $target_objectid => $data) {
// Add objefct name to rapidfire array
$target_object = $objects->getObjects($target_objectid);
$rapidfire_against[$target_objectid] = $data;
$rapidfire_against[$target_objectid]['object'] = $target_object;
}
}

return view('ingame.techtree.info.rapidfire')->with([
'object' => $object,
'rapidfire_from' => $rapidfire_from,
'rapidfire_against' => $rapidfire_against,
]);
}

/**
* Returns techtree properties table.
*
* @param int $id
* @return Application|Factory|\Illuminate\Foundation\Application|View
*/
public function getPropertiesTable($object): View|\Illuminate\Foundation\Application|Factory|Application
{
// Add tooltips for object properties
if (empty($object['properties'])) {
return view();
}

foreach ($object['properties'] as $property_key => $property_value) {
$object['tooltips'][$property_key] = $this->getPropertyTooltip($object, $property_key);
}

return view('ingame.techtree.info.properties')->with([
'object' => $object,
]);
}

/**
* Returns techtree property tooltip.
*
* @param int $id
* @return Application|Factory|\Illuminate\Foundation\Application|View
*/
public function getPropertyTooltip($object, $property_key): View|\Illuminate\Foundation\Application|Factory|Application
{
switch ($property_key) {
case 'structural_integrity':
$property_name = 'Structural Integrity';
break;
case 'shield':
$property_name = 'Shield Strength';
break;
case 'attack':
$property_name = 'Attack Strength';
break;
case 'speed':
$property_name = 'Speed';
break;
case 'capacity':
$property_name = 'Capacity';
break;
case 'fuel':
$property_name = 'Fuel Consumption';
break;
}

// TODO: add calculation for property values taking into account research.
$calculated_value = $object['properties'][$property_key];

return view('ingame.techtree.info.property_tooltip')->with([
'property_name' => $property_name,
'property_value' => $object['properties'][$property_key],
'calculated_value' => $calculated_value,
]);
}
}
Loading

0 comments on commit 5c43c8f

Please sign in to comment.