Completed
Push — trunk ( 91a948...0e7a2e )
by SuperNova.WS
05:44
created

admin/planet_compensate.php (1 issue)

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use Planet\DBStaticPlanet;
4
use Unit\DBStaticUnit;
5
6
define('INSIDE', true);
7
define('INSTALL', false);
8
define('IN_ADMIN', true);
9
10
require('../common.' . substr(strrchr(__FILE__, '.'), 1));
11
12
global $lang, $user;
13
14
messageBoxAdminAccessDenied(AUTH_LEVEL_ADMINISTRATOR);
15
16
$template = gettemplate('admin/planet_compensate', true);
17
18
$galaxy_src = sys_get_param_int('galaxy_src');
19
$system_src = sys_get_param_int('system_src');
20
$planet_src = sys_get_param_int('planet_src');
21
22
$galaxy_dst = sys_get_param_int('galaxy_dst');
23
$system_dst = sys_get_param_int('system_dst');
24
$planet_dst = sys_get_param_int('planet_dst');
25
26
$bonus = sys_get_param_float('bonus', 1);
27
28
$username_unsafe = sys_get_param_str_unsafe('username');
29
$username = sys_get_param_escaped('username');
30
31
if ($galaxy_src) {
32
  sn_db_transaction_start();
33
  $errors = array();
34
35
  $owner = db_user_by_username($username_unsafe, true);
36
37
  $planet = sys_o_get_updated($owner, array('galaxy' => $galaxy_src, 'system' => $system_src, 'planet' => $planet_src, 'planet_type' => 1), SN_TIME_NOW);
38
  $que = $planet['que'];
39
  $planet = $planet['planet'];
40
  if (!$planet) {
41
    $errors[] = $lang['adm_pl_comp_err_0'];
42
  }
43
44
  if ($planet['destruyed']) {
45
    $errors[] = $lang['adm_pl_comp_err_1'];
46
  }
47
48
  if ($planet['id_owner'] != $owner['id'] || !$username) {
49
    $errors[] = $lang['adm_pl_comp_err_4'];
50
  }
51
52
  $destination = sys_o_get_updated($owner, array('galaxy' => $galaxy_dst, 'system' => $system_dst, 'planet' => $planet_dst, 'planet_type' => 1), SN_TIME_NOW);
53
  $destination = $destination['planet'];
54
  if (!$destination) {
55
    $errors[] = $lang['adm_pl_comp_err_2'];
56
  }
57
58
  if ($planet['id'] == $destination['id']) {
59
    $errors[] = $lang['adm_pl_comp_err_5'];
60
  }
61
62
  if ($planet['id_owner'] != $destination['id_owner']) {
63
    $errors[] = $lang['adm_pl_comp_err_3'];
64
  }
65
66
  if (!empty($errors)) {
67
    foreach ($errors as $error) {
68
      $template->assign_block_vars('error', array(
69
        'TEXT' => $error,
70
      ));
71
    }
72
  } else {
73
    $template->assign_var('CHECK', 1);
74
75
    killer_add_planet($planet);
76
77
    $moon = DBStaticPlanet::db_planet_by_gspt($galaxy_src, $system_src, $planet_src, PT_MOON, true);
78
    if ($moon) {
79
      $moon = sys_o_get_updated($owner, $moon, SN_TIME_NOW);
80
      $moon = $moon['planet'];
81
      killer_add_planet($moon);
82
    }
83
84
    foreach (sn_get_groups('resources_loot') as $resource_id) {
85
      $resource_name = pname_resource_name($resource_id);
86
      $template->assign_var("{$resource_name}_cost", $final_cost[$resource_id]);
87
      $final_cost[$resource_id] = floor($final_cost[$resource_id] * $bonus);
88
      $template->assign_var("{$resource_name}_bonus", $final_cost[$resource_id]);
89
    }
90
91
    if ($_GET['btn_confirm']) {
92
      $time = SN_TIME_NOW + PERIOD_DAY;
93
94
      DBStaticUnit::db_unit_list_delete($planet['id_owner'], LOC_PLANET, $planet['id']);
95
      DBStaticPlanet::db_planet_set_by_id($planet['id'], "id_owner = 0, destruyed = {$time}");
96
      if ($moon) {
97
        DBStaticUnit::db_unit_list_delete($planet['id_owner'], LOC_PLANET, $moon['id']);
98
        DBStaticPlanet::db_planet_set_by_id($moon['id'], "id_owner = 0, destruyed = {$time}");
99
      }
100
101
      DBStaticPlanet::db_planet_set_by_id($destination['id'], "metal = metal + '{$final_cost[RES_METAL]}', crystal = crystal + '{$final_cost[RES_CRYSTAL]}', deuterium = deuterium + '{$final_cost[RES_DEUTERIUM]}'");
102
      $template->assign_var('CHECK', 2);
103
    }
104
  }
105
  sn_db_transaction_commit();
106
}
107
108
$template->assign_vars(array(
109
  'galaxy_src' => $galaxy_src,
110
  'system_src' => $system_src,
111
  'planet_src' => $planet_src,
112
113
  'galaxy_dst' => $galaxy_dst,
114
  'system_dst' => $system_dst,
115
  'planet_dst' => $planet_dst,
116
117
  'bonus' => $bonus,
118
119
  'username' => $username,
120
));
121
122
display($template, $lang['adm_pl_comp_title']);
123
124
function killer_add_planet($planet) {
125
  global $final_cost;
126
127
  $final_cost = array();
128
  $sn_group_resources_loot = sn_get_groups('resources_loot');
129
  /*
130
  foreach($sn_group_resources_loot as &$value)
131
  {
132
    $value = get_unit_param($value, P_NAME);
133
  }
134
  */
135
136
  foreach (sn_get_groups('structures') as $unit_id) {
137
    $build_level = mrc_get_level($user, $planet, $unit_id, true, true);
138
    if ($build_level > 0) {
139
      $unit_cost = get_unit_param($unit_id, 'cost');
140
      $build_factor = $unit_cost['factor'] != 1 ? (1 - pow($unit_cost['factor'], $build_level)) / (1 - $unit_cost['factor']) : $unit_cost['factor'];
141
      foreach ($sn_group_resources_loot as $resource_id) {
142
        $final_cost[$resource_id] += isset($unit_cost[$resource_id]) && $unit_cost[$resource_id] > 0 ? floor($unit_cost[$resource_id] * $build_factor) : 0;
143
      }
144
    }
145
  }
146
147
  foreach (sn_get_groups(array('defense', 'fleet')) as $unit_id) {
148
    $unit_count = mrc_get_level($user, $planet, $unit_id, true, true);
149
    if ($unit_count > 0) {
150
      $unit_cost = get_unit_param($unit_id, 'cost');
151
      foreach ($sn_group_resources_loot as $resource_id) {
152
        $final_cost[$resource_id] += isset($unit_cost[$resource_id]) && $unit_cost[$resource_id] > 0 ? floor($unit_cost[$resource_id] * $unit_count) : 0;
153
      }
154
    }
155
  }
156
157
  foreach ($sn_group_resources_loot as $resource_id) {
158
    $final_cost[$resource_id] += floor(mrc_get_level($user, $planet, $resource_id, true, true));
159
  }
160
}
161