<?php
/**
* Check have user chestst
* @param $user_id int
* @return int
*/
function haveChests($user_id)
{
$db = Elfi::getInstanse();
$count = $db->count(
"SELECT `id` FROM `chests_user` WHERE `user_id`=%d",
[$user_id]
);
return $count;
}
/**
* Get chests
* @param integer $user_id
* @return array
*/
function getChests($user_id)
{
$db = Elfi::getInstanse();
$data = $db->query(
"SELECT * FROM `chests_user` WHERE `user_id`=%d",
[$user_id]
);
$data = mysql_fetch_assoc($data);
return $data;
}
/**
* get chest info by id
*/
function getChestInfo($chest_id)
{
$db = Elfi::getInstanse();
$data = $db->fetch(
"SELECT * FROM `chests` WHERE `id`=%d",
[$chest_id]
);
return $data;
}
/**
* Give chest by id into user
* @return bool
*/
function giveChest($user_id,$chest_id)
{
$db = Elfi::getInstanse();
if ($db->query(
"INSERT INTO `chests_user` SET `user_id`=%d,`chest_id`=%d,`time`=%d",
[$user_id,$chest_id,time()]
))
{
return true;
}
return false;
}
function canOpenThis($id,$user_id)
{
$db = Elfi::getInstanse();
if ($db->count(
"SELECT `id` FROM `chests_user` WHERE `id`=%d and `user_id`=%d",
[$id,$user_id]
) == 1)
{
return true;
}
return false;
}
function openThis($id,$user_id)
{
$myChest = mysql_fetch_assoc(
mysql_query(
"SELECT * FROM `chests_user` WHERE `id`='".$id."' and `user_id`='".$user_id."'"
)
);
$chest = mysql_fetch_assoc(
mysql_query(
"SELECT * FROM `chests` WHERE `id`='".$myChest['chest_id']."'"
)
);
if($chest)
{
switch ($chest['type'])
{
case 'normal':
$rewards = ['sapfyr','izumrud'];
$rand = $rewards[mt_rand(0,count($rewards)-1)];
if ($rand == 'sapfyr')
{
$reward = 'sapfyr';
$pc = mt_rand(1,3);
} else {
$reward = 'izumrud';
$pc = 1;
}
mysql_query(
"UPDATE `user` SET ".$reward."=".$reward."+'$pc' WHERE `id`='".$user_id."'"
) or die (mysql_error());
$r = ['reward'=>'<img src ="/images/icon/res/'.$reward.'.png">','pc'=>$pc];
deleteChest($id);
return $r;
break;
case 'middle':
$rnd = mt_rand(1,500);
if ($rnd < 50)
{
$data = mysql_query(
"SELECT * FROM `kompl` WHERE `id_komp`='3' ORDER BY RAND() LIMIT 1"
);
$data = mysql_fetch_assoc($data);
$stat = explode(';', $data['stats']);
mysql_query(
"INSERT INTO `kompl_us` set `type` = '".$data['type']."', `komplekt`=".$data['id_komp'].", `id_komp` = '".$data['id']."', `id_us` = '".$user_id."', `sila`='$stat[1]', `prot`='$stat[2]', `hp`='$stat[0]', `fleece`='$data[fleece]'"
);
$r = ['reward'=>'<font color = "#F5D188">'.$data['name'].'</font>','pc'=>''];
deleteChest($id);
} else {
$rs = mt_rand(1,3);
mysql_query(
"UPDATE `user` SET `almaz`=`almaz`+'$rs' WHERE `id`='".$user_id."'"
);
$r = ['reward'=>'<img src ="/images/icon/res/almaz.png">','pc'=>$rs];
deleteChest($id);
}
return $r;
break;
case 'heroic':
$rand = mt_rand(1,3);
if ($rand == 1)
{
$myHunt = mysql_query(
"SELECT * FROM `hunting` WHERE `usr`='".$user_id."'"
);
$myHunt = mysql_fetch_assoc($myHunt);
if ($myHunt['level'] < 10)
{
$tt = mt_rand(1,10000);
if ($tt < 2000)
{
$reward = '+1 <img src ="/images/main/hunting.png"> уровень охотника!';
mysql_query("UPDATE `hunting` SET `level` = `level`+1 WHERE `usr` = '$user_id'");
} else {
$reward = ' <img src = "/images/icon/gold.png"> 30 золота.';
mysql_query(
"UPDATE `user` SET `gold`=`gold`+'3000' WHERE `id`='".$user_id."'"
);
}
} else {
$reward = ' <img src = "/images/icon/gold.png"> 30 золота.';
mysql_query(
"UPDATE `user` SET `gold`=`gold`+'3000' WHERE `id`='".$user_id."'"
);
}
} elseif ($rand == 2) {
$myUS = mysql_query(
"SELECT * FROM `user` WHERE `id`='".$user_id."'"
);
$myUS = mysql_fetch_assoc($myUS);
if ($myUS['level'] < 150)
{
mysql_query(
"UPDATE `user` SET `level`=`level`+'1',`exp`='0' WHERE `id`='".$user_id."'"
);
$reward = '+1 <img src = "/images/icon/level.png"> уровень';
} else {
mysql_query(
"UPDATE `user` SET `gold`=`gold`+'7000' WHERE `id`='".$user_id."'"
);
$reward = ' <img src = "/images/icon/gold.png"> 70 ';
}
} elseif ($rand == 3) {
mysql_query(
"UPDATE `user` SET `gold`=`gold`+'10000' WHERE `id`='".$user_id."'"
);
$reward = ' <img src = "/images/icon/gold.png"> 100 ';
}
deleteChest($id);
$rret = ['reward'=>$reward,'pc'=>null];
return $rret;
break;
case 'newyear':
$rnd = mt_rand(1,300);
$myUS = mysql_query(
"SELECT * FROM `user` WHERE `id`='".$user_id."'"
);
$myUS = mysql_fetch_assoc($myUS);
if ($rnd < 50 && $myUS['level']<=147)
{
mysql_query(
"UPDATE `user` SET `level`=`level`+'2' WHERE `id`='".$user_id."'"
);
$r = ['reward'=>'<img src ="/images/icon/level.png">','pc'=>2];
deleteChest($id);
} else {
$rso = mt_rand(200,1600);
$rs = $rso*100;
mysql_query(
"UPDATE `user` SET `gold`=`gold`+'$rs' WHERE `id`='".$user_id."'"
);
$r = ['reward'=>'<img src ="/images/icon/gold.png">','pc'=>$rso];
deleteChest($id);
}
return $r;
break;
default:
break;
}
}
}
function deleteChest($id)
{
mysql_query(
"DELETE FROM `chests_user` WHERE `id`='".$id."'"
);
return true;
}