Exemple #1
0
    /**
     * Sett ned helsen til spilleren etter skade utført av en annen spiller
     * @param int $miste_helse hvor mye helse som skal settes ned
     * @param player $up spilleren som forårsaker dette
     * @param int $attack_type hva slags angrep det var (0=drapsforsøk, 1=utpressing)
     * @param float $skadeprosent skadeprosenten ved et angrep
     * @param int $bullets antall kuler benyttet i angrep
     */
    public function health_decrease($miste_helse, player $up, $attack_type, $skadeprosent = null, $params = array())
    {
        // lås spillerene
        $up->lock();
        $this->lock();
        // allerede død?
        if (!$this->active) {
            $up->lock_commit();
            $this->lock_commit();
            return false;
        }
        $miste_helse = (int) $miste_helse;
        $attack_type = (int) $attack_type;
        // dør spilleren?
        if ($this->data['up_health'] <= $miste_helse) {
            // gi rankpoeng
            $rankpoeng = $up->health_calc_rankpoints($this, true, $skadeprosent);
            if ($rankpoeng > 0) {
                $rankpoeng = $this->calc_rankpoints_get($rankpoeng);
                $up->increase_rank($rankpoeng, false, true);
            }
            // oppdater ff-stats
            $this->attacked_ff_update("killed");
            // drep offeret
            $ret = $this->dies(true, $up);
            if (!$ret) {
                $up->lock_commit();
                $this->lock_commit();
                return false;
            }
            // gi angriper pengene offeret hadde på hånden
            $cash = 0;
            if ($this->data['up_cash'] > 0) {
                \Kofradia\DB::get()->exec("UPDATE users_players SET up_cash = up_cash - {$this->data['up_cash']} WHERE up_id = {$this->id}");
                \Kofradia\DB::get()->exec("UPDATE users_players SET up_cash = up_cash + {$this->data['up_cash']} WHERE up_id = {$up->id}");
                $cash = $this->data['up_cash'];
                $this->data['up_cash'] = 0;
                $up->data['up_cash'] += $cash;
            }
            // øk telleren over antall drap
            \Kofradia\DB::get()->exec("UPDATE users_players SET up_attack_killed_num = up_attack_killed_num + 1 WHERE up_id = {$up->id}");
            // øk telleren over antall drap i familien spilleren er medlem i
            $up->attack_ff_update("killed");
            $ret = array_merge($ret, array("drept" => true, "rankpoeng" => $rankpoeng, "penger" => $cash));
        } else {
            $ret = array();
            // hent alle FF angriperen er medlem av eller nylig var medlem av
            $ff_ids = $this->get_ff_id_list();
            // sett ny energi og helse for offeret
            $set = "";
            $energi_mistet = $this->data['up_energy'];
            if ($skadeprosent !== null) {
                $r = 1 - 0.4 * $skadeprosent;
                $set = "up_energy = up_energy * {$r}, ";
                $this->data['up_energy'] = round($this->data['up_energy'] * $r);
            }
            $energi_mistet = $energi_mistet - $this->data['up_energy'];
            // lagre energi, helse og angriper-FF
            $time = time();
            \Kofradia\DB::get()->exec("\n\t\t\t\tUPDATE users_players\n\t\t\t\tSET {$set}up_health = up_health - {$miste_helse},\n\t\t\t\t\tup_attacked_time = {$time}, up_attacked_up_id = {$up->id},\n\t\t\t\t\tup_attacked_ff_id_list = " . \Kofradia\DB::quote(implode(",", $ff_ids)) . ",\n\t\t\t\t\tup_health_ff_time = IF(up_health_ff_time IS NULL, up_health_ff_time, IF(up_health / up_health_max >= " . self::FF_HEALTH_LOW . ", IF(up_health_ff_time = 0, {$time}, up_health_ff_time), 0))\n\t\t\t\tWHERE up_id = {$this->id}");
            $this->data['up_health'] -= $miste_helse;
            $this->data['up_attacked_time'] = $time;
            $this->data['up_attacked_up_id'] = $up->id;
            $ret['drept'] = false;
            $ret['rankpoeng'] = 0;
            $ret['rankpoeng_lost'] = 0;
            // sett ny beskyttelse på offeret
            $prot_mistet = "";
            $ret['protection_replaced'] = false;
            if ($this->protection->data) {
                $prot_skadeprosent = $skadeprosent === null ? 0.02 : $skadeprosent;
                // bruk 2 % som skadeprosent hvis det ikke er angitt (utpressing)
                $prot_mistet = $this->protection->weakened($prot_skadeprosent);
                if ($prot_mistet === false) {
                    $ret['protection_replaced'] = true;
                    $prot_mistet = "";
                } else {
                    $prot_mistet = round($prot_mistet, 5);
                }
            }
            // rettet drapsforsøk?
            $rankpoeng = 0;
            if ($attack_type == self::ATTACK_TYPE_KILL) {
                // flytte til tilfeldig bydel
                $moved = false;
                if ($this->data['up_health'] / $this->data['up_health_max'] < self::HEALTH_MOVE_AUTO) {
                    // finn en tilfeldig bydel
                    $result = \Kofradia\DB::get()->query("SELECT id, name FROM bydeler WHERE active = 1 AND id != {$this->data['up_b_id']} ORDER BY RAND()");
                    $moved_from = array("id" => $this->data['up_b_id'], "name" => $this->bydel['name']);
                    $moved = $result->fetch();
                    $this->data['up_b_id'] = $moved['id'];
                    unset($this->bydel);
                    \Kofradia\DB::get()->exec("UPDATE users_players SET up_b_id = {$moved['id']} WHERE up_id = {$this->id}");
                }
                // mister penger
                $ret['penger'] = round($this->data['up_cash'] * $miste_helse / $this->data['up_health_max']);
                $extra = "";
                if ($ret['penger'] > 0) {
                    $up->data['up_cash'] = bcadd($up->data['up_cash'], $ret['penger']);
                    $extra = ", up_cash = up_cash + {$ret['penger']}";
                    $this->data['up_cash'] = bcsub($this->data['up_cash'], $ret['penger']);
                    \Kofradia\DB::get()->exec("UPDATE users_players SET up_cash = up_cash - {$ret['penger']} WHERE up_id = {$this->id}");
                }
                // behandle etterlyst
                $ret['hitlist'] = etterlyst::player_hurt($this, $up, $miste_helse / $this->data['up_health_max']);
                // øk telleren over antall mislykkede drapsforsøk for angriper
                \Kofradia\DB::get()->exec("UPDATE users_players SET up_attack_damaged_num = up_attack_damaged_num + 1{$extra} WHERE up_id = {$up->id}");
                // øk telleren over antall mislykkede drapsforsøk i familien spilleren er medlem i
                $up->attack_ff_update("damaged");
                // oppdater ff-stats
                $this->attacked_ff_update("damaged");
                // juster rankpoeng
                $rankpoeng_lost = $up->health_calc_rankpoints($this, false, $skadeprosent);
                if ($rankpoeng_lost > 0) {
                    $rankpoeng = $this->calc_rankpoints_get($rankpoeng_lost);
                    $up->increase_rank($rankpoeng, false, true, null, "attack");
                    $this->increase_rank(-$rankpoeng_lost, false, true, -$rankpoeng, "attack");
                }
                // informer spilleren på e-post
                $email = new email();
                $email->text = 'Hei,

Din spiller ' . $this->data['up_name'] . ' har blitt angrepet av en annen spiller og du har blitt skadet.

Spilleren har nå ' . game::format_num($this->get_health_percent(), 2) . ' % helse' . ($this->protection->data ? ', ' : ' og ') . game::format_num($this->get_energy_percent(), 2) . ' % energi' . ($this->protection->data ? ' og ' . game::format_num($this->get_protection_percent(), 2) . ' % beskyttelse' : '') . '.

Pass på så du ikke risikerer at spilleren blør ihjel!

--
www.kofradia.no';
                $email->send($this->user->data['u_email'], "Din spiller {$this->data['up_name']} har blitt angrepet");
                // hendelse for spilleren
                $note_data = array(round($miste_helse / $this->data['up_health_max'], 5), round($energi_mistet / $this->data['up_energy_max'], 5), $prot_mistet, $rankpoeng, round($this->data['up_health'] / $this->data['up_health_max'], 5), round($this->data['up_energy'] / $this->data['up_energy_max'], 5), $this->protection->data ? round($this->data['up_protection_state'], 5) : "", $this->data['up_points'], $moved ? urlencode($moved_from['name']) : "", $moved ? urlencode($moved['name']) : "", $ret['penger']);
                $this->add_log("attacked", implode(":", $note_data), 0);
                $ret['rankpoeng'] = $rankpoeng;
                $ret['rankpoeng_lost'] = $rankpoeng_lost;
                $ret['health_lost_p'] = $note_data[0];
                $ret['energy_lost_p'] = $note_data[1];
                $ret['protection_lost_p'] = $note_data[2];
                $ret['health_lost'] = $miste_helse;
                $ret['energy_lost'] = $energi_mistet;
                $ret['health_new_p'] = $note_data[4];
                $ret['energy_new_p'] = $note_data[5];
                $ret['protection_new_p'] = $note_data[6];
            }
            // så lite helse at spilleren mister medlemskap i familie/firma
            if ($this->data['up_health'] / $this->data['up_health_max'] < self::FF_HEALTH_LOW) {
                $ret = array_merge($ret, $this->release_relations_low_health(null, $up, true));
            }
        }
        // behandle vitner
        // hvis det er en utpressing er det kun vitner hvis spilleren dør
        $vitner_id = array();
        $vitner = array();
        $vitner_log = array();
        $r = rand(1, 100);
        if ($r >= 20 && ($ret['drept'] || $attack_type != self::ATTACK_TYPE_UTPRESSING)) {
            // antall vitner
            $antall = 1;
            if ($ret['drept']) {
                if ($r >= 40) {
                    $antall++;
                }
                if ($r >= 60) {
                    $antall++;
                }
                if ($r >= 80) {
                    $antall++;
                }
            } else {
                if ($r >= 60) {
                    $antall++;
                }
            }
            // hent ut tilfeldige vitner
            $timelimit = time() - 86400;
            // vitnet må ha vært pålogget siste 24 timene
            // hent ut tilfeldige vitner
            $result = \Kofradia\DB::get()->query("\n\t\t\t\tSELECT up_id\n\t\t\t\tFROM users_players\n\t\t\t\tWHERE up_access_level != 0 AND up_access_level < " . ess::$g['access_noplay'] . " AND up_b_id = {$up->data['up_b_id']} AND up_id NOT IN ({$up->id}, {$this->id}) AND up_last_online >= {$timelimit} AND up_brom_expire < " . time() . " AND up_fengsel_time < " . time() . "\n\t\t\t\tORDER BY RAND()\n\t\t\t\tLIMIT {$antall}");
            while ($row = $result->fetch()) {
                $up_vitne = player::get($row['up_id']);
                $row = array("up" => $up_vitne, "visible" => rand(1, 100) >= 70);
                // send hendelse til vitne
                // drept:attack_type:ble_sett:offer_up_id (num = angriper)
                $up_vitne->add_log("vitne", ($ret['drept'] ? 1 : 0) . ":{$attack_type}:" . ($row['visible'] ? 1 : 0) . ":" . $this->id, $up->id);
                $vitner_id[] = $up_vitne->id;
                $vitner[] = $row;
                $vitner_log[] = array($up_vitne->id, $row['visible']);
            }
        }
        // hent medlemmer av familie til offeret og sjekk om de skal være vitner
        if ($ret['drept'] || $attack_type != self::ATTACK_TYPE_UTPRESSING) {
            // sett opp liste
            $ids = array();
            $list = $this->get_ff_list();
            foreach ($list as $row) {
                if ($row['ff_is_crew'] != 0 || $row['ff_type'] != ff::TYPE_FAMILIE) {
                    continue;
                }
                $ids[] = $row['ff_id'];
            }
            if (count($ids) > 0) {
                $limit = 21600;
                // pålogget innen 6 timer
                $expire = time() - $limit;
                $result = \Kofradia\DB::get()->query("\n\t\t\t\t\tSELECT DISTINCT up_id, up_last_online\n\t\t\t\t\tFROM ff_members\n\t\t\t\t\t\tJOIN users_players ON ffm_up_id = up_id AND up_b_id = {$up->data['up_b_id']} AND up_last_online > {$expire}\n\t\t\t\t\tWHERE ffm_ff_id IN (" . implode(",", $ids) . ") AND ffm_status = " . ff_member::STATUS_MEMBER . " AND ffm_up_id NOT IN ({$this->id}, {$up->id})");
                while ($row = $result->fetch()) {
                    // er allerede vitne
                    if (in_array($row['up_id'], $vitner_id)) {
                        continue;
                    }
                    // beregn sannsynlighet
                    $prob = min($limit, $row['up_last_online'] - $expire) / $limit * 0.8;
                    // 80 % maks sannsynlighet hvis pålogget akkurat nå
                    // sjekk sannsynlighet
                    if (round($prob * 1000) < rand(1, 1000)) {
                        continue;
                    }
                    $up_vitne = player::get($row['up_id']);
                    $row = array("up" => $up_vitne, "visible" => rand(1, 100) >= 70);
                    // send hendelse til vitne
                    // drept:attack_type:ble_sett:offer_up_id (num = angriper)
                    $up_vitne->add_log("vitne", ($ret['drept'] ? 1 : 0) . ":{$attack_type}:" . ($row['visible'] ? 1 : 0) . ":" . $this->id, $up->id);
                    $vitner[] = $row;
                    $vitner_log[] = array($up_vitne->id, $row['visible']);
                }
            }
        }
        $ret['vitner'] = $vitner;
        if ($params && is_array($params)) {
            $ret = array_merge($ret, $params);
        }
        // lagre logg
        $ret['bullets'] = isset($ret['bullets']) ? (int) $ret['bullets'] : 0;
        $this->attack_log($ret, $attack_type, $vitner_log, $up);
        // trigger for offer
        if (!$ret['drept'] && $attack_type == self::ATTACK_TYPE_KILL) {
            $this->trigger("attacked", array("attack" => $ret, "up" => $up));
        }
        $up->lock_commit();
        $this->lock_commit();
        return $ret;
    }