<?php


class bot_bartender{
	public $msg;
	public $team;
	public $update_money;
	public $update_alcohol;
	public $config;
	public $drinks = array();
	public $food = array();
	public $youDrunk = array();
	public $userProfile = array();
	public $eda;

	function __construct($alcohol, $money, $keyLog) {
		$this->alcohol = $alcohol;
		$this->money = $money;
		$this->logFole = 'data/bot_bartender/log/'.$keyLog.'.dat';
		$this->config = parse_ini_file('data/bot_bartender/config.ini');
		if (!$alcohol && file_exists($this->logFole)) unlink($this->logFole);

	}

	public function analysis($msg) {
		$this->msg = $msg;
		preg_match("#\!(.*?)(\s|$)#i", $msg, $arr);
		if ($this->alcohol > $this->config['font']) {
				$this->msg = $this->font($msg);
		}
		$mony_strip = 100;
		$strip = array('стрип', 'стриптиз', 'strip', 'striptiz');

		if (in_array(mb_strtolower($arr[1], 'UTF-8'), $strip)) {
			if ($mony_strip > $this->money) return 2;
			$this->eda = true;
			$this->team = 'стриптиз';
			$this->setLog();
			$this->team .= ' <img src="utils/resize.php?img='.ltrim($this->randImg(), './').'&amp;width=150&height=0" alt="" />';
			$this->update_money = $this->money-$mony_strip;
			$this->update_alcohol = 0;
			return 3;
		}

		$sql = mysql_query("SELECT * FROM bot_bartender_product WHERE name = '".mysql_real_escape_string(mb_strtolower($arr[1], 'UTF-8'))."'");
		$res = mysql_fetch_assoc($sql);
		if (!empty($res['name'])) {
			$this->eda = $res['alcohol'] < 0 ? true : false;
			$this->update_alcohol = $this->alcohol ? $this->alcohol+$res['alcohol'] : $res['alcohol'];
			if (!$this->eda && ($this->update_alcohol > $this->config['youdrunk'])) {
				return 1;
			} elseif ($res['price'] > $this->money) {
				return 2;
			} else {
				$this->update_money = $this->money-$res['price'];
				if ($this->update_alcohol >= 100) {
					$this->update_alcohol = 100;
				} elseif ($this->update_alcohol < 0) {
					$this->update_alcohol = 0;
				}
				$this->update_alcohol = abs($this->update_alcohol);
				$this->team = $res['name'];
				$this->setLog();
				if (is_file('upload/bot_bartender/'.$res['id'].'.img')) {
					$this->team .= ' <img src="utils/resize.php?img=upload/bot_bartender/'.$res['id'].'.img&amp;width=100&height=0" alt="" />';
				}

				return 3;
			}
		}
	}

	public function toast() {
		$data = file('data/bot_bartender/toast.dat');
		return $data[array_rand($data)];
	}

	public function youDrunk() {
		$data = file('data/bot_bartender/you_drunk.dat');
		return $data[array_rand($data)];
	}

	public function font($msg) {
		$msg = iconv("UTF-8", "windows-1251", $msg);
		$count = strlen($msg);
		for($i = 0; $i<$count; $i++) {
			$r = $r ? 0 : 1;
			$txt .= $r ? mb_strtoupper($msg[$i], 'windows-1251') : mb_strtolower($msg[$i], 'windows-1251');
		}
		return iconv("windows-1251",  "UTF-8", $txt);
	}

	public function setLog() {
		$fp = fopen($this->logFole, 'a+');
        flock ($fp, LOCK_EX);
        fwrite($fp, $this->team.'('.date("d.m.Y H:i").')<br/>');
        flock ($fp, LOCK_UN);
        fclose($fp);
        @chmod($this->logFole, 0666);
	}

	public function getLog() {
        return file_exists($this->logFole) ? file_get_contents($this->logFole) : 'История пока пуста...';
	}

	public function randImg() {
		$data = unSerialize(file_get_contents('data/bot_bartender/strip.dat'));
        return $data[array_rand($data)];
	}
}
?>