summaryrefslogblamecommitdiff
path: root/src/gui/partywindow.cpp
blob: 996073bd120d40c74aa0f8efb764a0f9a18102f5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                  
                                                       


                                        
                                                                        



                                                                        
                                                                   




                                                                     
                                                               
                                                                             

   
                            
 
                                
 


                         
                    
                             
 
                       
                          
                              
 












                            
 
                           
                      
                         
                       
                         
                         
                     
                      
                                       
 
                      

 

                           
                         

 
                                                       








                                                         
                                                  
 
                                                     





                             
                                                    
 




                                         
                              
                                                           




                  
                                                          
 

                                                       

                          
     
                                        
         
                                

              

     

              
 
                                                                     
                                                        
 






                                                 
                                                                        
                         
                                 

 






                                                           
                                      
 
                       
 
                                                                            
                                  

 















                                                       

                                                               
 
                                                 

                            
                                                                               


                                       
 
                    
                 






                                                                        
                                          
 
                  








                                                                       
 
                                        

                         
                                                                       
                                                                    
                           


                             
                                                                       
                                                                     
                           
     
 
 
                                                           





                                                                             
                                
 
                  
 

                                                                     
                         

                     
/*
 *  The Mana World
 *  Copyright (C) 2008  The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "gui/partywindow.h"

#include "gui/widgets/chattab.h"

#include "beingmanager.h"
#include "player.h"

#include "net/net.h"
#include "net/partyhandler.h"

#include "utils/dtor.h"
#include "utils/gettext.h"
#include "utils/stringutils.h"

PartyMember::PartyMember():
    avatar(new Avatar)
{
}

PartyMember::~PartyMember()
{
    delete avatar;
}


PartyWindow::PartyWindow() :
    Window(_("Party"))
{
    setWindowName("Party");
    setVisible(false);
    setSaveVisible(true);
    setResizable(true);
    setSaveVisible(true);
    setCloseButton(true);
    setMinWidth(120);
    setMinHeight(200);
    setDefaultSize(590, 200, 150, 200);

    loadWindowState();
}

PartyWindow::~PartyWindow()
{
    delete_all(mMembers);
}

void PartyWindow::setPartyName(const std::string &name)
{
    setCaption(strprintf(_("Party (%s)"), name.c_str()));
}

void PartyWindow::clearPartyName()
{
    setCaption(_("Party"));
}

PartyMember *PartyWindow::findMember(int id) const
{
    PartyList::const_iterator it = mMembers.find(id);
    if (it == mMembers.end())
        return NULL;
    else
        return it->second;
}

PartyMember *PartyWindow::findOrCreateMember(int id)
{
    PartyMember *member = findMember(id);

    if (!member)
    {
        member = new PartyMember;
        mMembers[id] = member;
        add(member->avatar, 0, (mMembers.size() - 1) * 14);
    }

    return member;
}

int PartyWindow::findMember(const std::string &name) const
{
    PartyList::const_iterator itr = mMembers.begin(),
                              itr_end = mMembers.end();

    while (itr != itr_end)
    {
        if ((*itr).second->name == name)
        {
            return (*itr).first;
        }
        ++itr;
    }

    return -1;
}

void PartyWindow::updateMember(int id, const std::string &memberName,
                               bool leader, bool online)
{
    PartyMember *member = findOrCreateMember(id);
    member->name = memberName;
    member->leader = leader;
    member->online = online;
    member->avatar->setName(memberName);
    member->avatar->setOnline(online);

    Player *player = dynamic_cast<Player*>(beingManager->findBeing(id));
    if (player && online)
        player->setInParty(true);
}

void PartyWindow::updateMemberHP(int id, int hp, int maxhp)
{
    PartyMember *player = findOrCreateMember(id);
    player->avatar->setHp(hp);
    player->avatar->setMaxHp(maxhp);
}

void PartyWindow::removeMember(int id)
{
    mMembers.erase(id);

    if (Player *player = dynamic_cast<Player*>(beingManager->findBeing(id)))
        player->setInParty(false);
}

void PartyWindow::removeMember(const std::string &name)
{
    removeMember(findMember(name));
}

void PartyWindow::updateOnlne(int id, bool online)
{
    PartyMember *player = findMember(id);

    if (!player)
        return;

    player->online = online;
    player->avatar->setOnline(online);
}

void PartyWindow::showPartyInvite(const std::string &inviter,
                                  const std::string &partyName)
{
    // check there isnt already an invite showing
    if (mPartyInviter != "")
    {
        localChatTab->chatLog("Received party request, but one already exists",
                            BY_SERVER);
        return;
    }

    std::string msg;
    // log invite
    if (partyName.empty())
        msg = strprintf("%s has invited you to join their party",
                                    inviter.c_str());
    else
        msg = strprintf("%s has invited you to join the %s party",
                                    inviter.c_str(), partyName.c_str());

    localChatTab->chatLog(msg, BY_SERVER);

    // show invite
    acceptDialog = new ConfirmDialog("Accept Party Invite", msg, this);
    acceptDialog->addActionListener(this);

    mPartyInviter = inviter;
}

void PartyWindow::action(const gcn::ActionEvent &event)
{
    const std::string &eventId = event.getId();

    // check if they accepted the invite
    if (eventId == "yes")
    {
        localChatTab->chatLog("Accepted invite from " + mPartyInviter);
        Net::getPartyHandler()->inviteResponse(mPartyInviter, true);
        mPartyInviter = "";
    }
    else if (eventId == "no")
    {
        localChatTab->chatLog("Rejected invite from " + mPartyInviter);
        Net::getPartyHandler()->inviteResponse(mPartyInviter, false);
        mPartyInviter = "";
    }
}

void clearMembersSub(const std::pair<int, PartyMember*> &p)
{
    Player *player = dynamic_cast<Player*>(beingManager->findBeing(p.first));
    if (player)
        player->setInParty(false);
}

void PartyWindow::clearMembers()
{
    clearLayout();

    std::for_each(mMembers.begin(), mMembers.end(), clearMembersSub);

    delete_all(mMembers);
    mMembers.clear();
}