|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 11.10.2017 13:16 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Fleet; |
|
7
|
|
|
|
|
8
|
|
|
use \classSupernova; |
|
9
|
|
|
use \HelperString; |
|
10
|
|
|
use \DBStaticPlanet; |
|
11
|
|
|
|
|
12
|
|
|
class MissionEspionage extends MissionData { |
|
13
|
|
|
|
|
14
|
|
|
public function flt_mission_spy() { |
|
15
|
|
|
$lang = classSupernova::$lang; |
|
16
|
|
|
|
|
17
|
|
|
if (!isset($this->dst_user['id']) || !isset($this->dst_planet['id']) || !isset($this->src_user['id'])) { |
|
18
|
|
|
fleet_send_back($this->fleet); |
|
19
|
|
|
|
|
20
|
|
|
return; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$spy_detected = false; |
|
24
|
|
|
|
|
25
|
|
|
$fleet_array = sys_unit_str2arr($this->fleet['fleet_array']); |
|
26
|
|
|
if ($fleet_array[SHIP_SPY] >= 1) { |
|
27
|
|
|
$spy_detected = $this->doSpying($lang); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (!$spy_detected) { |
|
31
|
|
|
fleet_send_back($this->fleet['fleet_array']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param $lang |
|
39
|
|
|
* |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function doSpying($lang) { |
|
43
|
|
|
$fleet_row = &$this->fleet; |
|
44
|
|
|
$target_user_row = &$this->dst_user; |
|
45
|
|
|
$target_planet_row = &$this->dst_planet; |
|
46
|
|
|
$spying_user_row = &$this->src_user; |
|
47
|
|
|
$spying_planet_row = &$this->src_planet; |
|
48
|
|
|
|
|
49
|
|
|
$fleet_array = sys_unit_str2arr($fleet_row['fleet_array']); |
|
50
|
|
|
|
|
51
|
|
|
$TargetSpyLvl = GetSpyLevel($target_user_row); |
|
52
|
|
|
$CurrentSpyLvl = GetSpyLevel($spying_user_row); |
|
53
|
|
|
$spy_diff_empire = $CurrentSpyLvl - $TargetSpyLvl; |
|
54
|
|
|
|
|
55
|
|
|
$spy_probes = $fleet_array[SHIP_SPY]; |
|
56
|
|
|
$spy_diff = $spy_diff_empire + sqrt($spy_probes) - 1; |
|
57
|
|
|
|
|
58
|
|
|
$combat_pack[0] = array( |
|
|
|
|
|
|
59
|
|
|
RES_METAL => $target_planet_row['metal'], |
|
60
|
|
|
RES_CRYSTAL => $target_planet_row['crystal'], |
|
61
|
|
|
RES_DEUTERIUM => $target_planet_row['deuterium'] |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$spy_message = "<table width=\"440\" cellspacing = \"1\"><tr><td class=\"c\" colspan=\"4\">{$lang['sys_spy_maretials']} {$target_planet_row['name']} "; |
|
65
|
|
|
$spy_message .= uni_render_coordinates_href($target_planet_row, '', 3); |
|
66
|
|
|
$spy_message .= " ({$lang['Player_']} '{$target_user_row['username']}') {$lang['On_']} "; |
|
67
|
|
|
$spy_message .= date(FMT_DATE_TIME, $fleet_row['fleet_end_time']); |
|
68
|
|
|
$spy_message .= "</td></tr><tr>"; |
|
69
|
|
|
$spy_message .= "<td width=220>{$lang['sys_metal']}</td><td width=220 align=right>" . HelperString::numberFloorAndFormat($target_planet_row['metal']) . "</td>"; |
|
70
|
|
|
$spy_message .= "<td width=220>{$lang['sys_crystal']}</td></td><td width=220 align=right>" . HelperString::numberFloorAndFormat($target_planet_row['crystal']) . "</td>"; |
|
71
|
|
|
$spy_message .= "</tr><tr>"; |
|
72
|
|
|
$spy_message .= "<td width=220>{$lang['sys_deuterium']}</td><td width=220 align=right>" . HelperString::numberFloorAndFormat($target_planet_row['deuterium']) . "</td>"; |
|
73
|
|
|
$spy_message .= "<td width=220>{$lang['sys_energy']}</td><td width=220 align=right>" . HelperString::numberFloorAndFormat($target_planet_row['energy_max']) . "</td>"; |
|
74
|
|
|
$spy_message .= "</tr>"; |
|
75
|
|
|
if ($spy_diff >= 2) { |
|
76
|
|
|
$spy_message .= "<div class='spy_medium'>" . $this->flt_spy_scan($target_planet_row, 'fleet', $lang['tech'][UNIT_SHIPS], $target_user_row) . "</div>"; |
|
77
|
|
|
$this->coe_compress_add_units(sn_get_groups('fleet'), $target_planet_row, $combat_pack[0]); |
|
78
|
|
|
} |
|
79
|
|
|
if ($spy_diff >= 3) { |
|
80
|
|
|
$spy_message .= "<div class='spy_medium'>" . $this->flt_spy_scan($target_planet_row, 'defense', $lang['tech'][UNIT_DEFENCE], $target_user_row) . "</div>"; |
|
81
|
|
|
$this->coe_compress_add_units(sn_get_groups('defense_active'), $target_planet_row, $combat_pack[0]); |
|
82
|
|
|
} |
|
83
|
|
|
if ($spy_diff >= 5) { |
|
84
|
|
|
$spy_message .= "<div class='spy_long'>" . $this->flt_spy_scan($target_planet_row, 'structures', $lang['tech'][UNIT_STRUCTURES], $target_user_row) . "</div>"; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($spy_diff_empire >= 0) { |
|
88
|
|
|
$spy_message .= "<div class='spy_long'>" . $this->flt_spy_scan($target_planet_row, 'tech', $lang['tech'][UNIT_TECHNOLOGIES], $target_user_row) . "</div>"; |
|
89
|
|
|
$this->coe_compress_add_units(array(TECH_WEAPON, TECH_SHIELD, TECH_ARMOR), $target_planet_row, $combat_pack[0], $target_user_row); |
|
90
|
|
|
} |
|
91
|
|
|
// TODO: Наемники, губернаторы, артефакты и прочее имперское |
|
92
|
|
|
|
|
93
|
|
|
$simulator_link = sn_ube_simulator_encode_replay($combat_pack, 'D'); |
|
94
|
|
|
|
|
95
|
|
|
$target_unit_list = 0; |
|
96
|
|
|
foreach (sn_get_groups('fleet') as $unit_id) { |
|
97
|
|
|
$target_unit_list += max(0, mrc_get_level($target_user_row, $target_planet_row, $unit_id, false, true)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$spy_detected = $spy_probes * $target_unit_list / 4 * pow(2, $TargetSpyLvl - $CurrentSpyLvl); |
|
101
|
|
|
|
|
102
|
|
|
if (mt_rand(0, 99) > $spy_detected) { |
|
103
|
|
|
$spy_outcome_str = sprintf($lang['sys_mess_spy_detect_chance'], $spy_detected); |
|
104
|
|
|
$spy_detected = false; |
|
105
|
|
|
} else { |
|
106
|
|
|
$spy_outcome_str = $lang['sys_mess_spy_destroyed']; |
|
107
|
|
|
$spy_detected = true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$spy_message .= "<tr><th class=\"c_c\" colspan=4>"; |
|
111
|
|
|
$spy_message .= "{$spy_outcome_str}<br />"; |
|
112
|
|
|
$spy_message .= "<a href=\"fleet.php?target_mission=1&planet_type={$fleet_row['fleet_end_type']}&galaxy={$fleet_row['fleet_end_galaxy']}&system={$fleet_row['fleet_end_system']}&planet={$fleet_row['fleet_end_planet']} \">{$lang['type_mission'][1]}</a><br />"; |
|
113
|
|
|
$spy_message .= "<a href=\"simulator.php?replay={$simulator_link}\">{$lang['COE_combatSimulator']}</a><br />"; |
|
114
|
|
|
$spy_message .= "</th></tr></table>"; |
|
115
|
|
|
// End of link generation |
|
116
|
|
|
|
|
117
|
|
|
msg_send_simple_message($spying_user_row['id'], '', $fleet_row['fleet_start_time'], MSG_TYPE_SPY, $lang['sys_mess_qg'], $lang['sys_mess_spy_report'], $spy_message); |
|
118
|
|
|
|
|
119
|
|
|
$target_message = "{$lang['sys_mess_spy_ennemyfleet']} {$spying_planet_row['name']} " . uni_render_coordinates_href($spying_planet_row, '', 3); |
|
120
|
|
|
$target_message .= " {$lang['sys_mess_spy_seen_at']} {$target_planet_row['name']} " . uni_render_coordinates($target_planet_row); |
|
121
|
|
|
|
|
122
|
|
|
$target_user_id = $fleet_row['fleet_target_owner']; |
|
123
|
|
|
|
|
124
|
|
|
if ($spy_detected) { |
|
125
|
|
|
db_fleet_delete($fleet_row['fleet_id']); |
|
126
|
|
|
|
|
127
|
|
|
$debris_planet_id = $target_planet_row['planet_type'] == PT_PLANET ? $target_planet_row['id'] : $target_planet_row['parent_planet']; |
|
128
|
|
|
|
|
129
|
|
|
$spy_cost = get_unit_param(SHIP_SPY, P_COST); |
|
130
|
|
|
|
|
131
|
|
|
DBStaticPlanet::db_planet_set_by_id($debris_planet_id, |
|
132
|
|
|
"`debris_metal` = `debris_metal` + " . floor($spy_probes * $spy_cost[RES_METAL] * 0.3) . ", `debris_crystal` = `debris_crystal` + " . floor($spy_probes * $spy_cost[RES_CRYSTAL] * 0.3)); |
|
133
|
|
|
|
|
134
|
|
|
$target_message .= "<br />{$lang['sys_mess_spy_destroyed_enemy']}"; |
|
135
|
|
|
} |
|
136
|
|
|
msg_send_simple_message($target_user_id, '', $fleet_row['fleet_start_time'], MSG_TYPE_SPY, $lang['sys_mess_spy_control'], $lang['sys_mess_spy_activity'], $target_message); |
|
137
|
|
|
|
|
138
|
|
|
return $spy_detected; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
protected function flt_spy_scan($target_planet, $group_name, $section_title, $target_user = array()) { |
|
143
|
|
|
global $lang; |
|
144
|
|
|
|
|
145
|
|
|
$result = "<tr><td class=\"c\" colspan=\"4\">{$section_title}</td></tr>"; |
|
146
|
|
|
foreach (sn_get_groups($group_name) as $unit_id) { |
|
147
|
|
|
if (($unit_amount = mrc_get_level($target_user, $target_planet, $unit_id, false, true)) > 0) { |
|
148
|
|
|
$result .= "<tr><td align=\"left\" colspan=\"3\">{$lang['tech'][$unit_id]}</td><td align=\"right\">" . HelperString::numberFloorAndFormat($unit_amount) . "</td></tr>"; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $result; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
protected function coe_compress_add_units($unit_group, $target_planet, &$compress_data, $target_user = array()) { |
|
156
|
|
|
foreach ($unit_group as $unit_id) { |
|
157
|
|
|
if (($unit_count = mrc_get_level($target_user, $target_planet, $unit_id, false, true)) > 0) { |
|
158
|
|
|
$compress_data[$unit_id] = $unit_count; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.