143 lines
3.8 KiB
C++
143 lines
3.8 KiB
C++
#include "precompile.h"
|
|
|
|
#include <a8/timer.h>
|
|
|
|
#include "group.h"
|
|
#include "cs_proto.pb.h"
|
|
#include "ss_proto.pb.h"
|
|
#include "dbengine.h"
|
|
#include "app.h"
|
|
|
|
void Group::Init()
|
|
{
|
|
|
|
}
|
|
|
|
void Group::UnInit()
|
|
{
|
|
for (auto& pair : member_hash_) {
|
|
delete pair.second;
|
|
}
|
|
member_hash_.clear();
|
|
}
|
|
|
|
GroupMember* Group::GetMember(const std::string& account_id)
|
|
{
|
|
auto itr = member_hash_.find(account_id);
|
|
return itr != member_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
bool Group::IsFull()
|
|
{
|
|
return member_hash_.size() < 100;
|
|
}
|
|
|
|
void Group::Rename(const std::string& new_group_name)
|
|
{
|
|
group_name = new_group_name;
|
|
MarkDirty();
|
|
}
|
|
|
|
void Group::AddMember(GroupMember* member)
|
|
{
|
|
if (member_hash_.find(member->account_id) != member_hash_.end()) {
|
|
abort();
|
|
}
|
|
member_hash_[member->account_id] = member;
|
|
|
|
MarkDirty();
|
|
if (dirty_timer_) {
|
|
a8::Timer::Instance()->ModifyTimer(dirty_timer_, 100);
|
|
}
|
|
}
|
|
|
|
void Group::RemoveMember(const std::string& account_id)
|
|
{
|
|
GroupMember* member = GetMember(account_id);
|
|
if (member) {
|
|
member_hash_.erase(account_id);
|
|
}
|
|
|
|
MarkDirty();
|
|
if (dirty_timer_) {
|
|
a8::Timer::Instance()->ModifyTimer(dirty_timer_, 100);
|
|
}
|
|
}
|
|
|
|
void Group::MarkDirty()
|
|
{
|
|
if (!dirty_) {
|
|
dirty_ = true;
|
|
dirty_timer_ = a8::Timer::Instance()->
|
|
AddDeadLineTimerAndAttach(1000 * 60,
|
|
a8::XParams()
|
|
.SetSender((void*)this),
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Group* group = (Group*)param.sender.GetUserData();
|
|
group->SaveToDB();
|
|
},
|
|
&timer_attacher.timer_list_,
|
|
[] (const a8::XParams& param)
|
|
{
|
|
Group* group = (Group*)param.sender.GetUserData();
|
|
group->dirty_timer_ = nullptr;
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
void Group::FillGroupDB(cs::MFGroupDB& group_dto)
|
|
{
|
|
|
|
}
|
|
|
|
void Group::SaveToDB()
|
|
{
|
|
cs::MFGroupDB group_db;
|
|
FillGroupDB(group_db);
|
|
std::string group_data;
|
|
group_db.SerializeToString(&group_data);
|
|
|
|
auto on_ok =
|
|
[] (a8::XParams& param, const f8::DataSet* data_set)
|
|
{
|
|
|
|
};
|
|
auto on_error =
|
|
[] (a8::XParams& param, int error_code, const std::string& error_msg)
|
|
{
|
|
|
|
};
|
|
|
|
a8::XObject conn_info = DBEngine::Instance()->GetConnInfo(group_id);
|
|
DBEngine::Instance()->
|
|
ExecAsyncScript(
|
|
conn_info,
|
|
"INSERT `group`(group_id, group_name, owner_id, creator_id, group_data "
|
|
" createtime, modifytime) "
|
|
"VALUES(%d, '%s', %d, %d, '%s', %d, %d) "
|
|
"ON DUPLICATE KEY UPDATE group_name='%s', owner_id=%d, creator_id=%d, "
|
|
" group_data='%s', modifytime=%d;",
|
|
{
|
|
group_id,
|
|
group_name,
|
|
owner_id,
|
|
creator_id,
|
|
group_data,
|
|
App::Instance()->nowtime,
|
|
App::Instance()->nowtime,
|
|
|
|
group_name,
|
|
owner_id,
|
|
creator_id,
|
|
group_data,
|
|
App::Instance()->nowtime
|
|
},
|
|
a8::XParams(),
|
|
on_ok,
|
|
on_error,
|
|
group_id
|
|
);
|
|
}
|