public function addCombat($nodeId, $data) { global $db, $game; $this->getResources(); $this->getUnits(); $this->getLocation(); $node = new node(); $okUnits = 1; $army = array(); foreach ($data['input']['attacker']['groups'] as $group) { if (isset($army[$group['unitId']])) { $army[$group['unitId']] += $group['quantity']; } else { $army[$group['unitId']] = $group['quantity']; } } $speed = 999999999; foreach ($this->units as $key => $group) { if (isset($army[$key])) { if ($army[$key] > $group['value']) { $okUnits = 0; } if ($game['units'][$this->data['faction']][$key]['speed'] < $speed) { $speed = $game['units'][$this->data['faction']][$key]['speed']; } } } $combatCost = $game['factions'][$this->data['id']]['costs']['combat']; $okCombatCost = $this->checkCost($combatCost, 'combat'); if ($okUnits) { if ($okCombatCost['ok']) { if ($node->get('id', $nodeId) == 'done') { $node->getLocation(); $distance = sqrt(pow(abs($this->location['x'] - $node->location['x']), 2) + pow(abs($this->location['y'] - $node->location['y']), 2)); $duration = $distance / ($speed * $game['users']['speed']['combat']); $combatId = misc::newId('combat'); $ok = 1; $cuBuffer = array(); foreach ($army as $key => $value) { $cuBuffer[] = '("' . $combatId . '", "' . $key . '", "' . $value . '")'; $this->units[$key]['value'] -= $value; $db->query('update units set value="' . $this->units[$key]['value'] . '" where node="' . $this->data['id'] . '" and id="' . $key . '"'); if ($db->affected_rows() == -1) { $ok = 0; } $upkeepResource = $game['units'][$this->data['faction']][$key]['upkeepResource']; $upkeep = $game['units'][$this->data['faction']][$key]['upkeep']; $this->resources[$upkeepResource]['value'] += $upkeep * $value; $db->query('update resources set value="' . $this->resources[$upkeepResource]['value'] . '" where node="' . $this->data['id'] . '" and id="' . $upkeepResource . '"'); if ($db->affected_rows() == -1) { $ok = 0; } } foreach ($combatCost as $cost) { $this->resources[$cost['resource']]['value'] -= $cost['value'] * $game['users']['cost']['combat']; $db->query('update resources set value="' . $this->resources[$cost['resource']]['value'] . '" where node="' . $this->data['id'] . '" and id="' . $cost['resource'] . '"'); if ($db->affected_rows() == -1) { $ok = 0; } } $db->query('insert into combat (id, sender, recipient, focus, stage, start, duration) values ("' . $combatId . '", "' . $this->data['id'] . '", "' . $node->data['id'] . '", "' . $data['input']['attacker']['focus'] . '", "0", "' . strftime('%Y-%m-%d %H:%M:%S', time()) . '", "' . $duration . '")'); if ($db->affected_rows() == -1) { $ok = 0; } $db->query('insert into combat_units (combat, id, value) values ' . implode(', ', $cuBuffer)); if ($db->affected_rows() == -1) { $ok = 0; } if ($ok) { $status = 'done'; } else { $status = 'error'; } } else { $status = 'noNode'; } } else { $status = 'notEnoughResources'; } } else { $status = 'notEnoughUnits'; } return $status; }
foreach ($_GET as $key => $value) { if ($key == 'nodeId') { $_GET[$key] = misc::clean($value, 'numeric'); } else { $_GET[$key] = misc::clean($value); } } switch ($_GET['action']) { case 'get': if (isset($_GET['nodeId'])) { $node = new node(); $status = $node->get('id', $_GET['nodeId']); if ($status == 'done') { if ($node->data['user'] == $_SESSION[$shortTitle . 'User']['id']) { $node->checkAll(time()); $node->getLocation(); $node->getQueue('build'); $node->getQueue('combat'); $buildQueue = array(); for ($i = 0; $i < $game['factions'][$node->data['faction']]['modules']; $i++) { $buildQueue[$i] = 0; } foreach ($node->queue['build'] as $item) { $buildQueue[$item['slot']] = 1; } } else { $message = $ui['accessDenied']; } } else { $message = $ui[$status]; }