zoukankan      html  css  js  c++  java
  • 玩家上线

    玩家上线
    //------------------c2s_login-------------------------
    bool GameChannel::OnLogin(void* data, int len)
    {
        UserInfo* info = (UserInfo*)data;
        
        m_asyndb->getUserID(&m_on_getUserID ,info->userName);
    
        m_pUserInfo = new UserInfo;
        strcpy(m_pUserInfo->userPwd, info->userPwd);
        return true;
    }
    1.获得用户ID
    根据ID判断用户是否存在,即如果ID == -1就认为用户不存在
    void GameChannel::on_getUserID(int id)
    {
        if(-1 == id)
        {
            LoginEnum ret = LoginUserNotExist;
            send_cmd(s2c_login, &ret, sizeof(LoginEnum));
            return;
        }
        m_pUserInfo->userID = m_user_id = id;
        m_asyndb->getUserInfo(&m_on_getUserInfo, m_user_id);
    }
    
    2.根据用户提供的密码和数据库的密码进行比较看是否正确
    void GameChannel::on_getUserInfo(UserInfo info)
    {
        if(0 == strcmp(info.userPwd, m_pUserInfo->userPwd))
        {
            LoginEnum ret = LoginSuccess;
            send_cmd(s2c_login, &ret, sizeof(LoginEnum));
    
            send_actors(m_user_id);
        }
        else
        {
            LoginEnum ret = LoginPasswordOrUserError;
            send_cmd(s2c_login, &ret, sizeof(LoginEnum));
        }
    }
    //---------------------根据以上信息发送用户不存在或者密码不正确-----------------------------------
    
    //---------------------登录成功发送角色列表----------------------------
    如果验证密码是正确的,就发送用户下的角色信息
    send_json(L"角色列表", jsonOutputStream.str().c_str());
    
    //---------------------------c2s_select_actor(选择其中一个角色)----------------------------------
    
    void GameChannel::on_getActorStat(bool isOnline)//查看玩家是否在线
    {
        //如果在线 直接提示
        if(isOnline)
        {
            //...提示该玩家另一处登录
            //将该玩家踢下线。
            m_share->kick_player(m_actor_id);
        }
        m_asyndb->setActorStat(m_actor_id, true);//设置玩家在线状态
        m_asyndb->getActorInfoEx(&m_on_getActorInfoEx, m_actor_id);
    }
    //---------------------------获取玩家的详细信息------------------------
    send_cmd(s2c_local_player_info, &info, sizeof(info));//向客户端发送本地玩家信息
    //----------------------------玩家进入map------------------------------
    m_map->gs2msData(gs2ms_add_player, m_channel_id, (void*)ss.str().c_str(), ss.str().size());
    
    else if(gs2ms_add_player == pkt.cmd)
    {
        auto ply = new PlayerChannel();
    
        ply->send_ms2gs_data = [this](int cmd, int channel_id, void* data, int len)
        {
            MapPkt pkt;
            pkt.cmd = cmd;
            pkt.channel_id = channel_id;
            char* p = new char[len];
            memcpy(p, data, len);
            pkt.data = p;
            pkt.len = len;
    
    #ifdef BOOST_LOCKFREE
            while(1)
            {
                if(m_quePkts2.push(pkt))
                    break;
                else
                    boost::this_thread::interruptible_wait(1);
            }
    #else
            std::lock_guard<std::mutex>  lock(m_queue_mutex);
            m_quePkts.push(pkt);
    #endif
    
        };
        ply->initMap(this);//给playerchannel中设置地图信息
    
        //加到地图中,但是并未激活该玩家
        int id = ply->OnPlayerEnter(pkt.data, pkt.len);
        m_PlayerChannels[id] = ply;
        add_player(id);        //此时向客户端发送玩家进入地图消息,此时玩家尚未激活
    }
    
    //-------------------------激活玩家-----------------------------------
    void Map::activate_player(int mapChannelId) //激活玩家
    {
        auto plchannel = m_PlayerChannels[mapChannelId];
        if(!plchannel->isActive)
            plchannel->isActive = true;
        else
            return;
        //
        pos2d& mpos = m_spBase->getMapPos(plchannel->tgtPos); 
        set_map_coll(mpos.x, mpos.y, true);
        m_roles_mpos_mgr.add(mpos.x, mpos.y, plchannel->objId, plchannel);
    
        //在新进入块中通知
        pos2d newBlockPos = {plchannel->block_x, plchannel->block_y};
        notify_block(pos2d(), newBlockPos, plchannel, true);//通知
    
        //让玩家上线就能看到所有附近角色的动作。
        m_map_blocks[plchannel->block_x][plchannel->block_y].add(plchannel);
        if(plchannel->actInfoEx->pet)
        {
            if(plchannel->actInfoEx->petinfo.cur_hp <= 0)
            {
                return;
            }
            PetObj* pet = (PetObj*)plchannel->pet;
            pet = new PetObj();
            pet->owner = plchannel;
            plchannel->pet = pet;
            //pet->objId.id;
            pet->objId.type = ObjID_Pet;
            pet->stat.level = plchannel->actInfoEx->petinfo.level;
            wcscpy(pet->name, plchannel->actInfoEx->petinfo.name);
            pet->nBodyID = plchannel->actInfoEx->petinfo.nBodyID;
            pet->typeInfo = m_monster_type_infos[pet->nBodyID];
            pet->stat.cur_hp = plchannel->actInfoEx->petinfo.cur_hp;
            pet->stat.max_hp = plchannel->actInfoEx->petinfo.max_hp;
            pet->stat.cur_exp = plchannel->actInfoEx->petinfo.cur_exp;
            pet->stat.max_exp = plchannel->actInfoEx->petinfo.max_exp;
            std::deque<pos2d> pos;// = {0, 0};
            pos2d mpos = GetMapPos(plchannel->tgtPos);
            Round(1, mpos, pos);
            if(pos.empty())
            {
                delete plchannel->pet;
    //            plchannel->pet = nullptr;
            }
    //        pos2d mpos = GetMapPos(pos.front());
            if(m_spNPCmgr->addNPC(pet, pos.front()))
                plchannel->isCarryPet = true;
            else
            {
                delete plchannel->pet;
    //            plchannel->pet = nullptr;
            }
        }
    
    }
    
    //----------------------------------------此时玩家算是真正的进入地图了-------------------------------------------
  • 相关阅读:
    SQL 列转行
    SQL 行转列
    ActionScript 3.0 学习笔记三
    VS 2010 添加扩展工具
    VS 2010 启动慢解决办法
    [SQL Server]游标示例
    SQL Server 2005 express TCP/IP 不能连接的配置
    FCKeditor.Net v2.6.3 上传图片的配置及注意事项
    HTTP/1.1 403 Forbidden
    存储过程中常使用的逻辑控制语句
  • 原文地址:https://www.cnblogs.com/zzyoucan/p/3927237.html
Copyright © 2011-2022 走看看