Файл: ewar/functions/valley.php
Строк: 455
<?php
function getFight() {
$fight = readcache('valley');
if ($fight === false) {
$fight = setFight();
}
return $fight;
}
function setFight() {
$fight = array(
'status' => 1, #1 - nežaidžiam, 2 - žaidžiam.
'player' => array(
'on_life' => array(), #Atėjome į kovą. Gyvi.
'off_life' => array(), #Neatėjome į kovą. Gyvi.
'on_death' => array(), #Atėjome į kovą. Mirę.
'off_death' => array(), #Neatėjome į kovą. Mirę.
'damage' => array(), #Masyvas su žaidėjų žala.
'kick' => array() #Masyvas su žaidėjų nužudymais.
),
'turn' => array(), #Eilė.
'bot' => array(), #Botai.
'journal' => array()
);
writecache('valley', $fight);
return $fight;
}
#Funkcija atnaujinanti duomenis:
function updateFight(array $fight) {
writecache('valley', $fight);
return $fight;
}
#Funkcija įtraukianti žaidėją į eilę:
function setTurn($id, array $fight) {
$fight['turn'][$id] = $id;
updateFight($fight);
return $fight;
}
#Funkcija ištrinanti žaidėją iš eilės
function unsetTurn($id, array $fight) {
unset($fight['turn'][$id]);
updateFight($fight);
return $fight;
}
function checkTurnId($id, array $fight) {
if (in_array($id, $fight['turn'])) {
return true;
}
else
{
return false;
}
}
#Funkcija pradedanti kovą:
#$pr_bot = žaidėjų kiekis * $pr_bot = bot'ų kiekis. Parametrai config.php faile.
#$stats_bot = masuvas su random bot'ų parametrais.
function startFight(array $fight, array $stats_bot, $pr_bot = 1, $time_fight = 300, $z_udar = 10) {
$c_turn = count($fight['turn']); #Žaidėjų kiekis eilėje
if ($c_turn == 0) {
return false;
}
$c_bot = (int)($c_turn * $pr_bot); #Bot'ų kiekis
$fight['status'] = 2;
$fight['time_end'] = time() + $time_fight;
$fight['journal'] = array();
$fight['bot'] = array();
$fight['player']['on_life'] = array();
$fight['player']['off_life'] = array();
$fight['player']['on_death'] = array();
$fight['player']['off_death'] = array();
$fight['player']['damage'] = array();
$fight['player']['kick'] = array();
if (isset($fight['result'])) unset($fight['result']);
foreach($fight['turn'] as $user) {
$cache_user = mysql_fetch_assoc(mysql_query("SELECT vit, def, agi, nick, statusas, akmuo_time, zole_time, self, str, ability_1, ability_1_quality, ability_2, ability_2_quality, ability_3, ability_3_quality, ability_4, ability_4_quality, ability_5, ability_5_quality FROM vartotojai WHERE id='$user' LIMIT 1"));
$online = mysql_fetch_assoc(mysql_query("SELECT nick_id FROM online WHERE nick_id='$user' LIMIT 1"));
if (!$online OR $cache_user['self'] !== 'valley.php') {
$category = 'off_life';
}
else
{
$category = 'on_life';
}
$fight['player']['damage'][$user] = 0;
$fight['player']['kick'][$user] = 0;
$fight['journal'] = array();
unset($fight['turn'][$user]);
$fight['player'][$category][$user] = array(
'id' => $user,
'battle_hp' => $cache_user['vit'] * 2,
'other_hp' => $cache_user['vit'] * 2,
'str' => $cache_user['str'],
'def' => $cache_user['def'],
'agi' => $cache_user['agi'],
'name' => $cache_user['nick'],
'statusas' => $cache_user['statusas'],
'akmuo_time' => $cache_user['akmuo_time'],
'zole_time' => $cache_user['zole_time'],
'microtime' => microtime(),
'standard' => false,
'target' => false,
'mana' => true,
'z_udar' => time(),
'category' => $category,
'ability_1' => $cache_user['ability_1'],
'ability_1_quality' => $cache_user['ability_1_quality'],
'ability_2' => $cache_user['ability_2'],
'ability_2_quality' => $cache_user['ability_2_quality'],
'ability_3' => $cache_user['ability_3'],
'ability_3_quality' => $cache_user['ability_3_quality'],
'ability_4' => $cache_user['ability_4'],
'ability_4_quality' => $cache_user['ability_4_quality'],
'ability_5' => $cache_user['ability_5'],
'ability_5_quality' => $cache_user['ability_5_quality']
);
}
for($i = 1; $i <= $c_bot; $i ++) {
$fight['bot'][$i] = array(
'name' => '[priesas*]',
'str' => rand($stats_bot['str'][0], $stats_bot['str'][1]),
'def' => rand($stats_bot['def'][0], $stats_bot['def'][1]),
'agi' => rand($stats_bot['agi'][0], $stats_bot['agi'][1]),
'other_hp' => rand($stats_bot['hp'][0], $stats_bot['hp'][1]),
);
$fight['bot'][$i]['battle_hp'] = $fight['bot'][$i]['other_hp'];
}
$fight['count_users'] = count($fight['player']['on_life']) + count($fight['player']['off_life']);
$fight['count_bots'] = count($fight['bot']);
updateFight($fight);
return $fight;
}
#Funkcija gražinanti žaidėjo statusą kovoje:
function statusGamer($id, array $fight) {
/*
1 - aktyvus, gyvas
2 - neaktyvus, gyvas
3 - aktyvus, miręs
4 - neaktyvus, miręs
false - nedalyvauja kovoje
*/
if (@$fight['player']['on_life'][$id]) {
return 1;
}
elseif (@$fight['player']['off_life'][$id]) {
return 2;
}
elseif (@$fight['player']['on_death'][$id]) {
return 3;
}
elseif (@$fight['player']['off_death'][$id]) {
return 4;
}
else
{
return false;
}
}
#Permeta žaidėją į aktyvių sąrašą, jeigu jis atėjo kovos metu:
function moveGamer($id, array $fight) {
$fight['player']['on_life'][$id] = $fight['player']['off_life'][$id];
$fight['player']['on_life'][$id]['category'] = 'on_life';
unset($fight['player']['off_life'][$id]);
updateFight($fight);
return $fight;
}
#Taikinio generacijos funkcija:
function v_targer(array $fight) {
$count = count($fight['bot']);
if ($count > 0) {
$rand = array_rand($fight['bot']);
return $rand;
}
else
{
return false;
}
}
function getTimeUdar($time) {
$t = $time - time();
return $t;
}
function v_journal(array $fight, $count_j) {
global $language;
foreach(array_slice($fight['journal'], 0, $count_j, true) as $value) {
$value = str_replace('[kova_prasidejo*]',$language['kova_prasidejo'],$value);
$value = str_replace('[kovoje*]',$language['kovoje'],$value);
$value = str_replace('[priesu_be_tasko*]',$language['priesu_be_tasko'],$value);
$value = str_replace('[ir*]',$language['ir'],$value);
$value = str_replace('[zaideju*]',$language['zaideju'],$value);
$value = str_replace('[smoge*]',$language['smoge'],$value);
$value = str_replace('[su*]',$language['su'],$value);
$value = str_replace('[zala*]',$language['zala'],$value);
$value = str_replace('[krit*]',$language['krit'],$value);
$value = str_replace('[nuzude*]',$language['nuzude'],$value);
$value = str_replace('[akmeni_ka*]',$language['akmeni_ka'],$value);
$value = str_replace('[zole_ka*]',$language['zole_ka'],$value);
$value = str_replace('[panaudojo*]',$language['panaudojo'],$value);
$value = str_replace('[titano_ituzis*]',$language['titano_ituzis'],$value);
$value = str_replace('[galinga_gynyba*]',$language['galinga_gynyba'],$value);
$value = str_replace('[kritu_viesulas*]',$language['kritu_viesulas'],$value);
$value = str_replace('[apsaugine_stovesena*]',$language['apsaugine_stovesena'],$value);
$value = str_replace('[vampyrizmas*]',$language['vampyrizmas'],$value);
$value = str_replace('[priesas*]',$language['priesas'],$value);
echo $value . '<br/>';
}
}
function clear_journal(array $fight, $count_j) {
$count = count($fight['journal']);
if ($count > $count_j) {
foreach(array_slice($fight['journal'], $count_j, ($count - $count_j), true) as $key => $value) {
unset($fight['journal'][$key]);
}
}
return $fight;
}
#Atakuojame bot'ą:
function attackUsers(array $fight, $user, $target, $uron, $crit, $nick, $category, $z_udar, $statusas) {
if ($uron > $fight['bot'][$target]['battle_hp']) {
$uron = $fight['bot'][$target]['battle_hp'];
}
$fight['bot'][$target]['battle_hp'] -= $uron;
$fight['player']['damage'][$user] += $uron;
$fight['player'][$category][$user]['microtime'] = microtime();
$fight['player'][$category][$user]['z_udar'] = time() + $z_udar;
$zenklas = zenklas($statusas);
array_unshift($fight['journal'], '<span class="small yellow"><span class="lime"><img src="img/icons/player.png" alt="*"/> '.$zenklas.''.$nick.'</span> [smoge*] <span class="red"><img src="img/icons/bot.png" alt="*"/> [priesas*]'.$target.'</span> [su*] <span class="bold">'.$uron.'</span> [zala*]'.(($crit) ? ' [krit*].' : '.').'</span>');
if ($fight['bot'][$target]['battle_hp'] <= 0) {
unset($fight['bot'][$target]);
$fight['player']['kick'][$user] ++;
$fight['player'][$category][$user]['target'] = v_targer($fight);
array_unshift($fight['journal'], '<span class="small yellow"><span class="lime"><img src="img/icons/player.png" alt="*"/> '.$zenklas.''.$nick.'</span> [nuzude*] <span class="red"><img src="img/icons/bot.png" alt="*"/> [priesas*]'.$target.'</span>.</span>');
updateFight($fight);
header("Location: valley.php?id=battle");
exit;
}
return $fight;
}
#Bot'as atakuoja žaidėją:
function attackBot(array $fight, $user, $uron, $crit, $nick, $category, $id_bot, $statusas) {
if ($uron > $fight['player'][$category][$user]['battle_hp']) {
$uron = $fight['player'][$category][$user]['battle_hp'];
}
$fight['player'][$category][$user]['battle_hp'] -= $uron;
$zenklas = zenklas($statusas);
array_unshift($fight['journal'], '<span class="small yellow"><span class="red"><img src="img/icons/bot.png" alt="*"/> [priesas*]'.$id_bot.'</span> [smoge*] <span class="lime"><img src="img/icons/player.png" alt="*"/> '.$zenklas.''.$nick.'</span> [su*] <span class="bold">'.$uron.'</span> [zala*]'.(($crit) ? ' [krit*].' : '.').'</span>');
if ($fight['player'][$category][$user]['battle_hp'] <= 0) {
$new_category = converc_category($category);
$fight['player'][$new_category][$user] = $fight['player'][$category][$user];
unset($fight['player'][$category][$user]);
array_unshift($fight['journal'], '<span class="small yellow"><span class="red"><img src="img/icons/bot.png" alt="*"/> [priesas*]'.$id_bot.'</span> [nuzude*] <span class="lime"><img src="img/icons/player.png" alt="*"/> '.$zenklas.''.$nick.'</span>.</span>');
}
return $fight;
}
function converc_category($category) {
if ($category == 'on_life') return 'on_death';
if ($category == 'off_life') return 'off_death';
}
#Funkcija žaidžianti su neaktyviais žaidėjais:
function autoGame(array $fight, $off_player, $c_z_udar, $chanse_udar) {
$count_off_player = count($fight['player']['off_life']);
$count_on_player = count($fight['player']['on_life']);
if ($count_off_player == 0) {
return $fight;
}
if (!$off_player) {
if ($count_on_player > 1) {
$new_count = ceil($count_off_player / $count_on_player);
}
else
{
$new_count = $count_off_player;
}
}
else
{
$new_count = $off_player;
}
$new_count = ($new_count > $count_off_player) ? $count_off_player : $new_count;
$random_users = randomOffUsers($fight['player']['off_life'], $new_count);
foreach($random_users as $user) {
$z_udar = getTimeUdar($fight['player']['off_life'][$user]['z_udar']);
if ($z_udar < 0) {
if (!$fight['player']['off_life'][$user]['target']) {
$fight['player']['off_life'][$user]['target'] = v_targer($fight);
}
$target = $fight['player']['off_life'][$user]['target'];
if ($target) {
$skills = skills($fight['player']['off_life'][$user]['ability_1'],$fight['player']['off_life'][$user]['ability_2'],$fight['player']['off_life'][$user]['ability_3'],$fight['player']['off_life'][$user]['ability_4'],$fight['player']['off_life'][$user]['ability_5']);
$opponent_skills = skills(0,0,0,0,0);
$usr_params = array('str' => $fight['player']['off_life'][$user]['str'], 'agi' => $fight['player']['off_life'][$user]['agi'], 'def' => $fight['player']['off_life'][$user]['def'], 'akmuo_time' => $fight['player']['off_life'][$user]['akmuo_time']);
$opponent_params = array('def' => $fight['bot'][$target]['def'], 'zole_time' => 0, 'skills' => array(2 => $opponent_skills[2], 4 => $opponent_skills[4]));
$info = genUron(2, $fight['player']['off_life'][$user]['microtime'], $usr_params, $opponent_params, $skills);
$user_uron = $info[0];
$log_crit = $info[1];
$fight = attackUsers($fight, $user, $target, $user_uron, $log_crit, $fight['player']['off_life'][$user]['name'], 'off_life', $c_z_udar, $fight['player']['off_life'][$user]['statusas']);
if (!isset($fight['bot'][$target])) {
$target = $fight['player']['off_life'][$user]['target'];
}
#Priešas atakuoja:
if (rand(0,100) <= $chanse_udar) {
if ($target) {
$usr_params = array('str' => $fight['bot'][$target]['str'], 'agi' => $fight['bot'][$target]['agi'], 'def' => $fight['bot'][$target]['def'], 'akmuo_time' => 0);
$opponent_params = array('def' => $fight['player']['off_life'][$user]['def'], 'zole_time' => $fight['player']['off_life'][$user]['zole_time'], 'skills' => array(2 => $skills[2], 4 => $skills[4]));
$info = genUron(2, 0, $usr_params, $opponent_params, $opponent_skills);
$bot_uron = $info[0];
$log_crit = $info[1];
$fight = attackBot($fight, $user, $bot_uron, $log_crit, $fight['player']['off_life'][$user]['name'], 'off_life', $target, $fight['player']['off_life'][$user]['statusas']);
}
}
}
}
}
return $fight;
}
function randomOffUsers($array, $new_count) {
$users = array();
$count = 0;
while($count < $new_count) {
$rand = array_rand($array);
if (!in_array($rand, $users)) {
$users[] = $rand;
$count ++;
}
}
return $users;
}
function finalFight(array $fight) {
$active_users = count($fight['player']['on_life']) + count($fight['player']['off_life']);
$active_bots = count($fight['bot']);
if ($fight['time_end'] < time() OR $active_bots == 0 OR $active_users == 0) {
if ($fight['time_end'] < time() && $active_bots > 0) {
$status = 1; #Pralaimėjimas
}
else if ($active_bots == 0) {
$status = 2; #Pergalė
}
else if ($active_users == 0) {
$status = 1; #Pralaimėjimas
}
if (@$status) {
$fight['result'] = array();
$fight['status'] = 1;
$array = $fight['player']['on_life'] + $fight['player']['off_life'] + $fight['player']['on_death'] + $fight['player']['off_death'];
foreach($array as $id => $user) {
$active = ($user['category'] == 'on_life' OR $user['category'] == 'on_death') ? true : false; #Aktyvus/neaktyvus
$db_user = mysql_fetch_assoc(mysql_query("SELECT premium,effect1,effect2,effect3 FROM vartotojai WHERE id='$id' LIMIT 1"));
if ($status == 1) {
$_gold = ($fight['player']['kick'][$id] > 0) ? round($fight['player']['kick'][$id] / 2) : 0;
$_silver = rand(1,2) * round(rand(($fight['player']['damage'][$id] / 100), $fight['player']['damage'][$id]) / 2);
$_exp = rand(1,1) * round(round(round($fight['player']['damage'][$id] / 100) * 10) / 2);
if (!$active && $_gold > 1) $_gold = round($_gold / 2);
if (!$active && $_silver > 1) $_silver = round($_silver / 2);
if (!$active && $_exp > 1) $_exp = round($_exp / 2);
}
else
{
$_gold = $fight['player']['kick'][$id];
$_silver = rand(1,5) * round(rand(($fight['player']['damage'][$id] / 100), $fight['player']['damage'][$id]));
$_exp = rand(1,2) * round(round($fight['player']['damage'][$id] / 100) * 10);
if (!$active && $_gold > 1) $_gold = round($_gold / 2);
if (!$active && $_silver > 1) $_silver = round($_silver / 2);
if (!$active && $_exp > 1) $_exp = round($_exp / 2);
}
$clan_memb = mysql_fetch_assoc(mysql_query("SELECT * FROM clan_memb WHERE user = '$id'"));
if ($clan_memb) {
if ($clan_memb['v'] > 0) {
$_exp += round($_exp / 100 * $clan_memb['v']);
}
$clan = mysql_fetch_assoc(mysql_query('SELECT * FROM clans WHERE id = "'.$clan_memb['clan'].'"'));
}
if ($db_user['premium'] > time()) {
$_exp += round($_exp / 100 * 25);
}
if ($db_user['effect1'] > time()) {
$_exp += round($_exp / 100 * 25);
}
if ($db_user['effect2'] > time()) {
$_exp += round($_exp / 100 * 25);
}
if ($db_user['effect3'] > time()) {
$_exp += round($_exp / 100 * 25);
}
if ($clan) {
mysql_query('UPDATE `clans` SET `exp` = `exp` + '.$_exp.' WHERE `id` = "'.$clan['id'].'"');
mysql_query('UPDATE `clan_memb` SET `exp` = `exp` + '.$_exp.' WHERE `clan` = "'.$clan['id'].'" AND `user` = "'.$id.'"');
}
$fight['result'][$id] = array(
'_gold' => $_gold,
'_silver' => $_silver,
'_exp' => $_exp,
'active' => $active,
'status' => $status,
'kills' => $fight['player']['kick'][$id],
'damage' => $fight['player']['damage'][$id]
);
}
}
if (isset($fight['result'])) {
foreach($fight['result'] as $user => $reward) {
mysql_query("UPDATE vartotojai SET g = g+".$reward['_gold'].",s = s+".$reward['_silver'].", exp = exp+".$reward['_exp']." WHERE id = '$user' LIMIT 1");
$s = mysql_query("SELECT * FROM `strides` WHERE `user`='$user' AND `complete`='0' AND `s`='4'");
if (mysql_num_rows($s) != 0) {
$strides = mysql_fetch_assoc($s);
$stride = strides($strides['s']);
if ($strides['c'] < $stride['c']) {
$pridesime = $reward['kills'];
if ($strides['c'] + $pridesime > $stride['c']) {
$pridesime = $stride['c'] - $strides['c'];
}
if ($strides['c'] + $pridesime == $stride['c']) {
$complete = 1;
$force = mysql_fetch_assoc(mysql_query("SELECT `str`, `def`, `force` FROM `vartotojai` WHERE `id` = '$user'"));
if($force['force'] == 1) {
$force['str'] = force($force['str']);
}
else
{
$force['def'] = force($force['def']);
}
mysql_query("UPDATE `vartotojai` SET `str` = '".($force['str'] + 50)."', `vit` = `vit` + 50, `agi` = `agi` + 50, `def` = '".($force['def'] + 50)."', `force_updated` = '0' WHERE `id` = '$user'");
}
else
{
$complete = 0;
}
mysql_query("UPDATE `strides` SET `c`=`c`+$pridesime,`complete`='$complete' WHERE `user`='$user' AND `s`='".$stride['id']."'");
}
}
}
}
arsort($fight['player']['kick']);
arsort($fight['player']['damage']);
$best_killers = array();
$best_damage = array();
foreach(array_slice($fight['player']['kick'], 0, 3, true) as $uid => $kills) {
$best_killers[$uid] = $kills;
}
foreach(array_slice($fight['player']['damage'], 0, 3, true) as $uid => $damage) {
$best_damage[$uid] = $damage;
}
$best_killers = json_encode($best_killers);
$best_damage = json_encode($best_damage);
$cache = array();
$cache['best_killers'] = $best_killers;
$cache['best_damage'] = $best_damage;
$cache['time'] = time();
$cache['count_users'] = count($fight['result']);
$key = 'valley.stats';
writecache($key, $cache);
$fight['player']['on_life'] = array();
$fight['player']['off_life'] = array();
$fight['player']['on_death'] = array();
$fight['player']['off_death'] = array();
$fight['player']['damage'] = array();
$fight['player']['kick'] = array();
updateFight($fight);
header("Location: valley.php");
exit;
}
return $fight;
}
function clearLastStatistika() {
$key = 'valley.stats';
deletecache($key);
}
function getLastStatistika() {
$key = 'valley.stats';
$cache = readcache($key);
$best_killers = json_decode($cache['best_killers'], true);
$best_damage = json_decode($cache['best_damage'], true);
foreach($best_killers as $uid => $kills) {
$us = mysql_fetch_assoc(mysql_query("SELECT nick,statusas FROM vartotojai WHERE id='$uid' LIMIT 1"));
$cache['kills'][] = array('nick' => $us['nick'], 'kill' => $kills, 'id' => $uid, 'statusas' => $us['statusas']);
}
foreach($best_damage as $uid => $damage) {
$us = mysql_fetch_assoc(mysql_query("SELECT nick,statusas FROM vartotojai WHERE id='$uid' LIMIT 1"));
$cache['damage'][] = array('nick' => $us['nick'], 'damage' => $damage, 'id' => $uid, 'statusas' => $us['statusas']);
}
return $cache;
}
?>