AvocadoAmber/AvocadoAmber/classes/item/item.class.php

290 lines
7.7 KiB
PHP

<?php
class Item extends Module
{
private $_rawdata;
public static function exists($item_id)
{
global $g5;
if (isset($g5["connect_db"]) && isset($g5["item_table"])) {
$data = sql_fetch("SELECT it_name FROM {$g5['item_table']} WHERE it_id = '{$item_id}'");
return !empty($data);
}
return false;
}
public static function getItem($item_id)
{
global $g5;
if (isset($g5["connect_db"]) && isset($g5["item_table"])) {
$result = sql_fetch("SELECT * FROM {$g5['item_table']} WHERE it_id = '{$item_id}'");
if (!empty($result)) {
return $result;
}
}
return [];
}
public static function getImage($item_id)
{
global $g5;
if (isset($g5["connect_db"]) && isset($g5["item_table"])) {
$result = sql_fetch("SELECT it_img FROM {$g5['item_table']} WHERE it_id = '{$item_id}'");
if (!empty($result)) {
return $result["it_img"];
}
}
return [];
}
public static function getDetailImage($item_id)
{
global $g5;
if (isset($g5["connect_db"]) && isset($g5["item_table"])) {
$result = sql_fetch("SELECT it_1 FROM {$g5['item_table']} WHERE it_id = '{$item_id}'");
if (!empty($result)) {
return $result["it_1"];
}
}
return [];
}
public static function getName($item_id)
{
global $g5;
if (isset($g5["connect_db"]) && isset($g5["item_table"])) {
$result = sql_fetch("SELECT it_name FROM {$g5['item_table']} WHERE it_id = '{$item_id}'");
if (!empty($result)) {
return $result["it_name"];
}
}
return [];
}
public static function getInventoryItem($inventory_id)
{
global $g5;
if (isset($g5["connect_db"]) && isset($g5["item_table"]) && isset($g5["inventory_table"]) ) {
$result = sql_fetch("SELECT * FROM {$g5['inventory_table']} inv, {$g5['item_table']} itm WHERE inv.in_id = '{$inventory_id}' AND inv.it_id = itm.it_id");
if (!empty($result)) {
return $result;
}
}
return [];
}
public static function sendItem($character_id, $receive_character_id, $inventory_id, $memo = "")
{
global $g5, $member;
if (isset($g5["connect_db"]) && isset($g5["item_table"]) && isset($g5["inventory_table"]) ) {
$send_chara = Character::getDetail($character_id, ["ch_name"]);
$recv_chara = Character::getDetail($receive_character_id, ["ch_name"]);
$item = self::getInventoryItem($inventory_id);
if (!empty($item)) {
self::addGetLog($item["it_id"], $character_id, $item, "전송");
$inven_sql = "UPDATE {$g5['inventory_table']} SET
ch_id = '{$recv_chara['ch_id']}',
ch_name = '{$recv_chara['ch_name']}',
se_ch_id = '{$send_chara['ch_id']}',
se_ch_name = '{$send_chara['ch_name']}',
re_ch_id = '{$recv_chara['ch_id']}',
re_ch_name = '{$recv_chara['ch_name']}',
in_memo = '{$memo}'
where in_id = '{$inventory_id}'";
sql_query($inven_sql);
$recv_mb_id = $recv_chara['mb_id'];
$memo_content = "[ {$send_chara['ch_name']}님이 보내신 《{$item['it_name']}》선물이 도착 하였습니다. ] 캐릭터 인벤토리를 확인하세요.";
send_memo($member['mb_id'], $recv_mb_id, $memo_content);
return true;
}
}
return false;
}
public static function giveItem($character_id, $item_id, $item = null, $count = 1, $type = "획득", $isorder = false)
{
global $g5;
if (!$item['it_id']) {
$item = self::getItem($item_id);
}
$ch = Character::getCharacter($character_id);
if ($ch['ch_id']) {
for ($i = 0; $i < $count; $i++) {
$inven_sql = " insert into {$g5['inventory_table']}
set ch_id = '{$ch['ch_id']}',
it_id = '{$item['it_id']}',
it_name = '{$item['it_name']}',
ch_name = '{$ch['ch_name']}'";
if ($isorder) {
self::insertOrderLog($character_id, $item_id);
}
self::addGetLog($item_id, $character_id, $item, $type);
sql_query($inven_sql);
}
}
return $count;
}
public static function insertOrderLog($character_id, $item_id)
{
global $g5;
$member_id = Character::getMemberID($character_id);
if ($member_id) {
$time = G5_TIME_YMDHIS;
sql_query("INSERT INTO {$g5['order_table']}
set ch_id = '{$character_id}',
it_id = '{$item_id}',
or_datetime = '{$time}',
mb_id = '{$member_id}'");
}
}
/**
* @param int $inventory_id
* @param int $delete
* @return void
*/
public static function takeItem($inventory_id, $delete = 0, $type = "사용")
{
global $g5;
self::addUseLog($inventory_id, $type . ($delete != 0 ? " (삭제)" : " (보존)"));
if ($delete === 0) {
sql_query("DELETE FROM {$g5['inventory_table']} WHERE in_id = '{$inventory_id}'");
}
}
/**
* @param int $character_id
* @param int $item_id
* @return array
*/
public static function randomBox($character_id, $item_id)
{
global $g5;
$seed = rand(0, 100);
$result = [];
$item_result = sql_fetch("SELECT re_it_id AS it_id
FROM {$g5['explorer_table']}
WHERE it_id = '{$item_id}'
AND '{$seed}' BETWEEN ie_per_s AND ie_per_e
ORDER BY RAND()
LIMIT 1");
if ($item_result['it_id']) {
// 아이템 획득에 성공한 경우, 해당 아이템을 인벤토리에 삽입
// 아이템 획득에 성공 시
$item_result['it_name'] = self::getName($item_result['it_id']);
self::giveItem($character_id, $item_result['it_id']);
$result['it_id'] = $item_result['it_id'];
$result['it_name'] = $item_result['it_name'];
}
return $result;
}
/**
* @param int $it_id
* @param int $ch_id
* @param array|object $item
* @param string $type
*/
public static function addGetLog($it_id, $ch_id, $item = null, $type = '획득')
{
global $g5;
try {
if ($item == null || array_key_exists('it_id', $item)) {
$item = self::getItem($it_id);
}
$item = $item ? addslashes(json_encode($item, JSON_UNESCAPED_UNICODE)) : "{}";
$sql = "INSERT INTO `{$g5['item_modify_table']}` SET
`it_id` = '{$it_id}',
`ch_id` = '{$ch_id}',
`it_count` = 1,
`im_using_type` = '{$type}',
`im_origin_item` = '{$item}'";
sql_query($sql);
if (sql_error_info()) {
throw new exception(sql_error_info());
}
} catch(Exception $x) {
}
}
/**
* @param int $in_id
* @param string $type
*/
public static function addUseLog($in_id, $type)
{
global $g5;
try {
$origin_item = sql_fetch("SELECT * FROM {$g5['inventory_table']} where in_id = '{$in_id}'");
$item = $origin_item ? addslashes(json_encode($origin_item, JSON_UNESCAPED_UNICODE)) : "{}";
$it_id = $origin_item['it_id'];
$ch_id = $origin_item['ch_id'];
$sql = "INSERT INTO `{$g5['item_modify_table']}` SET
`it_id` = '{$it_id}',
`ch_id` = '{$ch_id}',
`it_count` = 1,
`im_using_type` = '{$type}',
`im_origin_item` = '{$item}'";
sql_query($sql);
if (sql_error_info()) {
throw new exception(sql_error_info());
}
} catch(Exception $x) {
}
}
public function __construct($item_id)
{
global $g5;
$data = self::getItem($item_id);
if ($data) {
$this->_rawdata = $data;
parent::init($this->_rawdata);
}
}
public function getLegacy()
{
return $this->_rawdata;
}
}