#include "stdafx.h"
#ifdef ENABLE_DEAL_OR_NO_DEAL
#include "char.h"
#include "char_manager.h"
#include "dealornodeal.h"
#include "utils.h"
#include "log.h"
#include "db.h"
#include "config.h"
#include "desc.h"
#include "desc_manager.h"
#include "buffer_manager.h"
#include "packet.h"
#include "desc_client.h"
#include "p2p.h"
#include "questmanager.h"
#include "event.h"
#include "party.h"
#include "start_position.h"
#include "sectree_manager.h"
#include <iostream>
#include <random>
CDealOrNoDeal::CDealOrNoDeal()
{
}
CDealOrNoDeal::~CDealOrNoDeal()
{
Destroy();
}
void CDealOrNoDeal::Destroy()
{
m_map_box.clear();
}
void CDealOrNoDeal::Start(LPCHARACTER ch, BYTE myBox)
{
if (!ch || !ch->GetDesc())
return;
if (myBox < 1 || myBox > 24)
return;
if (ch->GetDragonCoin() < 150)
{
ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("yeterliepyok"));
return;
}
ch->SetDragonCoin(ch->GetDragonCoin() - 150);
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end()) {
m_map_box.erase(it);
}
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(money.begin(), money.end(), gen);
std::vector<TBoxInfo> playerBoxes;
for (int i = 0; i < 24; ++i) {
playerBoxes.push_back(TBoxInfo(boxes[i], money[i]));
}
m_map_box.insert(std::make_pair(ch->GetVID(), playerBoxes));
ch->SetQuestFlag("dealornodeal.mybox", myBox);
ch->SetQuestFlag("dealornodeal.left_box", 0);
ch->SetQuestFlag("dealornodeal.offer", 0);
ch->SetQuestFlag("dealornodeal.lastoffer",0);
}
void CDealOrNoDeal::RemoveBox(LPCHARACTER ch, BYTE selectedBox)
{
if (!ch || !ch->GetDesc())
return;
if (selectedBox < 1 || selectedBox > 24)
return;
BYTE myBox = ch->GetQuestFlag("dealornodeal.mybox");
if (myBox == selectedBox)
return;
if (ch->GetQuestFlag("dealornodeal.left_box") >= 6 )
ch->SetQuestFlag("dealornodeal.left_box", 1);
else
ch->SetQuestFlag("dealornodeal.left_box", (ch->GetQuestFlag("dealornodeal.left_box")+1));
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end()) {
auto& playerBoxes = it->second;
for (auto iter = playerBoxes.begin(); iter != playerBoxes.end(); ) {
if (iter->boxNumber == selectedBox) {
ch->ChatPacket(CHAT_TYPE_COMMAND, "remove_box %d ", iter->boxPrice);
iter = playerBoxes.erase(iter);
ch->ChatPacket(CHAT_TYPE_COMMAND, "left_box %d ", GetLeftBox(ch));
} else {
++iter;
}
}
if (playerBoxes.empty()) {
m_map_box.erase(it);
} else {
m_map_box[ch->GetVID()] = playerBoxes;
}
}
if (GetLeftBox(ch) == 0)
CreateOffer(ch);
}
void CDealOrNoDeal::CreateOffer(LPCHARACTER ch)
{
if (!ch || !ch->GetDesc())
return;
int totalPrice = 0;
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end())
{
auto& playerBoxes = it->second;
for (auto iter = playerBoxes.begin(); iter != playerBoxes.end(); ++iter)
{
totalPrice += iter->boxPrice;
}
int offer = totalPrice / playerBoxes.size();
ch->ChatPacket(CHAT_TYPE_COMMAND, "deal_offer_time %d", offer);
if (playerBoxes.size() == 6)
ch->SetQuestFlag("dealornodeal.left_box", 2);
else
ch->SetQuestFlag("dealornodeal.left_box", 0);
ch->SetQuestFlag("dealornodeal.offer", offer);
}
}
void CDealOrNoDeal::Answer(LPCHARACTER ch, BYTE answer)
{
if (!ch || !ch->GetDesc())
return;
DWORD myOffer = ch->GetQuestFlag("dealornodeal.offer");
if (myOffer < 1)
return;
if (answer == 1)
{
ch->SetDragonCoin(ch->GetDragonCoin()+myOffer);
ch->ChatPacket(CHAT_TYPE_COMMAND, "deal_accept_offer %d ", myOffer);
Clear(ch);
}
else
{
if (ch->GetQuestFlag("dealornodeal.lastoffer") == 1)
{
ch->SetDragonCoin(ch->GetDragonCoin()+MyPrice(ch));
ch->ChatPacket(CHAT_TYPE_COMMAND, "deal_open_my_box %d", MyPrice(ch));
Clear(ch);
return;
}
ch->ChatPacket(CHAT_TYPE_COMMAND, "deal_cancel_offer %d", GetLeftBox(ch));
ch->SetQuestFlag("dealornodeal.offer", 0);
}
}
DWORD CDealOrNoDeal::MyPrice(LPCHARACTER ch)
{
if (!ch || !ch->GetDesc())
return 0;
if (ch->GetQuestFlag("dealornodeal.lastoffer") == 1)
{
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end())
{
auto& playerBoxes = it->second;
for (auto iter = playerBoxes.begin(); iter != playerBoxes.end(); ) {
if (iter->boxNumber == ch->GetQuestFlag("dealornodeal.mybox")) {
return iter->boxPrice;
iter = playerBoxes.erase(iter);
} else {
++iter;
}
}
}
}
return 0;
}
BYTE CDealOrNoDeal::GetLeftBox(LPCHARACTER ch)
{
if (!ch || !ch->GetDesc())
return 0;
BYTE offer = 0;
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end())
{
auto& playerBoxes = it->second;
if (playerBoxes.size() >= 6)
offer = 6 - ch->GetQuestFlag("dealornodeal.left_box");
else
{
ch->SetQuestFlag("dealornodeal.lastoffer",1);
offer = playerBoxes.size() - 2;
}
}
return offer;
}
void CDealOrNoDeal::Clear(LPCHARACTER ch)
{
if (!ch || !ch->GetDesc())
return;
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end()) {
m_map_box.erase(it);
}
ch->SetQuestFlag("dealornodeal.mybox", 0);
ch->SetQuestFlag("dealornodeal.left_box", 0);
ch->SetQuestFlag("dealornodeal.offer", 0);
ch->SetQuestFlag("dealornodeal.lastoffer",0);
}
void CDealOrNoDeal::Test(LPCHARACTER ch)
{
if (!ch || !ch->GetDesc())
return;
auto it = m_map_box.find(ch->GetVID());
if (it != m_map_box.end()) {
for (const auto&box : it->second) {
ch->ChatPacket(CHAT_TYPE_INFO, "Sandik: %d Para: %d ",box.boxNumber, box.boxPrice);
}
}
}
#endif