/** * \brief 保存一个用户的临时档案 * * * \param id: 用户id * \param data: 输出buf * \param dwSize: 输出数据大小 * \return 读取是否成功 */ bool GlobalTempArchiveIndex::writeTempArchive(DWORD id,char *data,DWORD dwSize) { //Zebra::logger->debug("临时存档数据内容大小%u",dwSize); checkOverdue(); mlock.lock(); TempArchive_iterator iter = tempArchive.find(id); if (iter != tempArchive.end()) { remove(iter); } char *buf = new char[sizeof(TempArchive) + dwSize]; if (!buf) { mlock.unlock(); return false; } TempArchive *ta = (TempArchive *)buf; ta->id = id ; zRTime ctv; ta->createtime = ctv; ta->dwSize = dwSize; bcopy(data,ta->data,dwSize); bool inserted = tempArchive.insert(TempArchive_value_type(ta->id,ta)).second; mlock.unlock(); if (!inserted) { SAFE_DELETE_VEC(buf); } return inserted; }
/** * \brief 初始化排队系统 * \author fqnewman */ bool CSortM::init() { //FunctionTimes times(69,__FUNCTION__); //bzero(leveltable, sizeof(leveltable)); const dbCol sortlist_read_define[] = { { "CHARID", zDBConnPool::DB_DWORD, sizeof(DWORD) }, { "LEVEL", zDBConnPool::DB_WORD, sizeof(WORD) }, { "EXP", zDBConnPool::DB_QWORD, sizeof(QWORD) }, { NULL, 0, 0} }; struct // 数据库读取结构,请不要随意修改,修改注意更新所有代码 { DWORD dwCharID; // 角色ID WORD wdLevel; // 人物等级 QWORD qwExp; // 人物经验 }__attribute__ ((packed)) *recordList,*tempPoint; recordList = NULL; connHandleID handle = SessionService::dbConnPool->getHandle(); if ((connHandleID)-1 == handle) { Zebra::logger->error("不能获取数据库句柄"); return false; } unsigned int retcode = SessionService::dbConnPool->exeSelect(handle, "`SORTLIST`", sortlist_read_define, NULL, NULL, (BYTE **)&recordList); SessionService::dbConnPool->putHandle(handle); if ((unsigned int)-1 == retcode || 0 == retcode) { return true; } if (recordList) { tempPoint = &recordList[0]; for (unsigned int i=0; i< retcode; i++) { onlineCount(tempPoint->dwCharID, tempPoint->wdLevel, tempPoint->qwExp); tempPoint++; } SAFE_DELETE_VEC(recordList); return true; } else { Zebra::logger->error("角色排序数据初始化失败,exeSelect 返回无效buf指针"); } return false; }
/** * \brief 删除一个用户档案所占用的空间 * * * \param del_iter: 需要删除的迭代 * \return */ void GlobalTempArchiveIndex::remove(TempArchive_iterator del_iter) { char *tmp = (char*)del_iter->second; SAFE_DELETE_VEC(tmp); tempArchive.erase(del_iter); }
/** * \brief 从数据库加载本角色所有的社会关系ID */ void CRelationManager::loadFromDatabase() { static const dbCol relation_define[] = { { "`CHARID`",zDBConnPool::DB_DWORD,sizeof(DWORD) }, { "`RELATIONID`",zDBConnPool::DB_DWORD,sizeof(DWORD) }, { "`RELATIONNAME`",zDBConnPool::DB_STR,sizeof(char[MAX_NAMESIZE+1]) }, { "`TYPE`",zDBConnPool::DB_BYTE,sizeof(BYTE) }, { "`DEGREE`",zDBConnPool::DB_WORD,sizeof(WORD) }, { "`LASTTIME`",zDBConnPool::DB_DWORD,sizeof(DWORD) }, { "`OCCUPATION`",zDBConnPool::DB_WORD,sizeof(WORD) }, { NULL,0,0} }; struct { DWORD dwCharID; // 会员编号 DWORD dwRelationID; // 关系ID char relationName[MAX_NAMESIZE+1]; // 关系名称 BYTE byType; // 关系类型 WORD wdDegree; // 友好度 DWORD dwLastTime; // 最后组队时间 WORD wdOccupation; // 职业 } * relationList,*tempPoint; char where[128]; connHandleID handle = SessionService::dbConnPool->getHandle(); if ((connHandleID)-1 == handle) { Xlogger->error("不能获取数据库句柄"); return; } bzero(where,sizeof(where)); _snprintf(where,sizeof(where) - 1,"CHARID = %u",user->id); DWORD retcode = SessionService::dbConnPool->exeSelect(handle,"`SAMPLERELATION`",relation_define,where,NULL,(BYTE **)&relationList); SessionService::dbConnPool->putHandle(handle); if ((DWORD)-1 == retcode) { Xlogger->error("查询错误:%d,加载简单社会关系失败。",retcode); return; } if (relationList) { tempPoint = &relationList[0]; for (DWORD i=0; i< retcode; i++) { switch(tempPoint->byType) { case Cmd::RELATION_TYPE_LOVE: case Cmd::RELATION_TYPE_BAD: case Cmd::RELATION_TYPE_FRIEND: case Cmd::RELATION_TYPE_ENEMY: { UserSession *pUser = NULL; CRelation *relation = NULL; relation = new CRelation(); if (relation) { pUser = UserSessionManager::getInstance()->getUserByID(tempPoint->dwRelationID); if (pUser) { //if (user && pUser->country != user->country) //{ // deleteDBRecord(tempPoint->dwRelationID); //} //else //{ relation->occupation = pUser->occupation; relation->online = true; pUser->relationManager.online(tempPoint->dwCharID); //} } else { relation->occupation = tempPoint->wdOccupation; } relation->charid = tempPoint->dwCharID; relation->id = tempPoint->dwRelationID; relation->type = tempPoint->byType; relation->level = tempPoint->wdDegree; relation->lasttime = tempPoint->dwLastTime; strncpy(relation->name,tempPoint->relationName,MAX_NAMESIZE); //rwlock.wrlock(); if (!addEntry(relation)) { Xlogger->error("添加[%u:%s]社会关系[%u:%s]进入管理器失败!",user->id,user->name,relation->id,relation->name); } // 对最后组队时间超过最大间隔的关系进行友好度扣减 zRTime ctv; DWORD curTime = ctv.sec(); if (curTime - relation->lasttime >= MAX_GROUP_TIME_GAP) { if (relation->level - DEDUCT_POINT >=0) relation->level-=DEDUCT_POINT; else relation->level = 0; relation->lasttime = curTime; UserSession *otherUser = NULL; otherUser = UserSessionManager::getInstance()->getUserByID(relation->id); if (otherUser) { otherUser->relationManager.setFriendDegreeByOtherUser(user->id,relation->level,curTime); } else { updateOtherOfflineUserDBRecord(relation); this->updateDBRecord(relation); } } //rwlock.unlock(); ///////////////////////////////////////////////// } else { Xlogger->error("严重错误在装载好友列表的时候无法分配出内存"); } } break; case Cmd::RELATION_TYPE_OVER: { deleteDBRecord(tempPoint->dwRelationID); user->sendSysChat(Cmd::INFO_TYPE_BREAKFRIEND,"%s已经与你恩断义绝",tempPoint->relationName); } break; default: Xlogger->error("%s的简单社会关系%s类型不正确type=%u",user->name,tempPoint->relationName,tempPoint->byType); break; } tempPoint++; } SAFE_DELETE_VEC(relationList); } return; }