00001 /* 00002 * The Mana World 00003 * Copyright (C) 2004 The Mana World Development Team 00004 * 00005 * This file is part of The Mana World. 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 #include "net/tmwserv/buysellhandler.h" 00023 00024 #include "net/tmwserv/protocol.h" 00025 00026 #include "net/messagein.h" 00027 00028 #include "beingmanager.h" 00029 #include "item.h" 00030 #include "localplayer.h" 00031 #include "npc.h" 00032 00033 #include "gui/buy.h" 00034 #include "gui/chat.h" 00035 #include "gui/sell.h" 00036 00037 extern BuyDialog *buyDialog; 00038 extern SellDialog *sellDialog; 00039 extern Window *buySellDialog; 00040 00041 namespace TmwServ { 00042 00043 BuySellHandler::BuySellHandler() 00044 { 00045 static const Uint16 _messages[] = { 00046 GPMSG_NPC_BUY, 00047 GPMSG_NPC_SELL, 00048 0 00049 }; 00050 handledMessages = _messages; 00051 } 00052 00053 void BuySellHandler::handleMessage(MessageIn &msg) 00054 { 00055 Being *being = beingManager->findBeing(msg.readInt16()); 00056 if (!being || being->getType() != Being::NPC) 00057 { 00058 return; 00059 } 00060 00061 current_npc = being->getId(); 00062 00063 switch (msg.getId()) 00064 { 00065 case GPMSG_NPC_BUY: 00066 buyDialog->reset(); 00067 buyDialog->setMoney(player_node->getMoney()); 00068 buyDialog->setVisible(true); 00069 00070 while (msg.getUnreadLength()) 00071 { 00072 int itemId = msg.readInt16(); 00073 int amount = msg.readInt16(); 00074 int value = msg.readInt16(); 00075 buyDialog->addItem(itemId, amount, value); 00076 } 00077 break; 00078 00079 case GPMSG_NPC_SELL: 00080 sellDialog->setMoney(player_node->getMoney()); 00081 sellDialog->reset(); 00082 sellDialog->setVisible(true); 00083 00084 while (msg.getUnreadLength()) 00085 { 00086 int itemId = msg.readInt16(); 00087 int amount = msg.readInt16(); 00088 int value = msg.readInt16(); 00089 sellDialog->addItem(new Item(itemId, amount, false), value); 00090 } 00091 break; 00092 } 00093 } 00094 00095 } // namespace TmwServ