This commit is contained in:
殷勇 2023-09-05 11:40:06 +08:00
parent b6cc8ad3bb
commit 5aea43b2ee
10 changed files with 261 additions and 261 deletions

View File

@ -110,7 +110,7 @@ func (cm *ChatMgr) ProcGuildChat(p *Player, msg *cs.CMSendChatMsg) {
if guild == nil {
return
}
guildId := guild.GuildId
guildId := guild.guildId
cm.guildMsgId++
chatMsg := new(cs.MFChatMsg)
@ -125,8 +125,8 @@ func (cm *ChatMgr) ProcGuildChat(p *Player, msg *cs.CMSendChatMsg) {
}
// TraverseMember
for _, member := range guild.Members {
guildMember := playerMgr.getPlayerByAccountId(member.AccountId)
for _, member := range guild.members {
guildMember := playerMgr.getPlayerByAccountId(member.accountId)
if guildMember != nil {
cm.SyncGuildChatMsg(guildMember)
}

View File

@ -4,26 +4,26 @@ import "q5"
// User 用户实体
type User struct {
AccountId string
Username string
FriendRequest map[string]*FriendRequest // AccountId -> FriendRequest
FriendBlackList map[string]*FriendBlackList // AccountId -> FriendBlackList
accountId string
username string
friendRequest map[string]*FriendRequest // accountId -> FriendRequest
friendBlackList map[string]*FriendBlackList // accountId -> FriendBlackList
}
// Friendship user1, user2 构成一个好友关系
type Friendship struct {
User1 *User
User2 *User
user1 *User
user2 *User
}
type FriendRequest struct {
AccountId string
IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
accountId string
isFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
}
// FriendBlackList 直接存列表, 黑名单上限就50个
type FriendBlackList struct {
AccountId string
accountId string
isRemoved int32 // default: 0, isRemoved
}
@ -33,18 +33,18 @@ func RandomUsername() string {
}
func (u *User) AddFriendRequest(account2Id string) {
if u.FriendRequest == nil {
u.FriendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
if u.friendRequest == nil {
u.friendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest := &FriendRequest{
AccountId: account2Id,
IsFriendship: 0,
accountId: account2Id,
isFriendship: 0,
}
u.FriendRequest[account2Id] = friendRequest
u.friendRequest[account2Id] = friendRequest
}
func (u *User) GetFriendRequest(account2Id string) *FriendRequest {
if friendRequest, exists := u.FriendRequest[account2Id]; exists {
if friendRequest, exists := u.friendRequest[account2Id]; exists {
if exists {
return friendRequest
}
@ -54,35 +54,35 @@ func (u *User) GetFriendRequest(account2Id string) *FriendRequest {
func (u *User) GetAllFriendRequestAccountIds() []string {
var accountIds []string
for accountId, _ := range u.FriendRequest {
for accountId := range u.friendRequest {
accountIds = append(accountIds, accountId)
}
return accountIds
}
func (u *User) RemoveFriendRequest(account2Id string) {
delete(u.FriendRequest, account2Id)
delete(u.friendRequest, account2Id)
}
func (u *User) IsInReq(targetAccountId string) bool {
if u.FriendRequest == nil {
u.FriendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
if u.friendRequest == nil {
u.friendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest, exists := u.FriendRequest[targetAccountId]
return exists && friendRequest.IsFriendship != 1
friendRequest, exists := u.friendRequest[targetAccountId]
return exists && friendRequest.isFriendship != 1
}
// AddBlacklist 加入黑名单
func (u *User) AddBlacklist(b *FriendBlackList) {
if u.FriendBlackList == nil {
u.FriendBlackList = make(map[string]*FriendBlackList, MaxBlockedMembers)
if u.friendBlackList == nil {
u.friendBlackList = make(map[string]*FriendBlackList, MaxBlockedMembers)
}
u.FriendBlackList[b.AccountId] = b
u.friendBlackList[b.accountId] = b
}
// GetBlacklist 获取黑名单
func (u *User) GetBlacklist(account2Id string) *FriendBlackList {
if friendBlackList, exists := u.FriendBlackList[account2Id]; exists {
if friendBlackList, exists := u.friendBlackList[account2Id]; exists {
if exists {
return friendBlackList
}
@ -92,17 +92,17 @@ func (u *User) GetBlacklist(account2Id string) *FriendBlackList {
// IsInBlacklist 在黑名单中
func (u *User) IsInBlacklist(account2Id string) bool {
if friendBlackList, exists := u.FriendBlackList[account2Id]; exists {
if friendBlackList, exists := u.friendBlackList[account2Id]; exists {
return exists && friendBlackList.isRemoved == 0
}
return false
}
func (u *User) RemoveBlacklist(account2Id string) {
delete(u.FriendBlackList, account2Id)
delete(u.friendBlackList, account2Id)
}
// GetBlacklistCount 获取黑名单
func (u *User) GetBlacklistCount() int {
return len(u.FriendBlackList)
return len(u.friendBlackList)
}

View File

@ -132,10 +132,10 @@ func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
fm.users = make(map[string]*User, 100)
for rows.Next() {
user := &User{
AccountId: q5.ToString(*rows.GetByIndex(0)),
Username: q5.ToString(*rows.GetByIndex(1)),
accountId: q5.ToString(*rows.GetByIndex(0)),
username: q5.ToString(*rows.GetByIndex(1)),
}
fm.users[user.AccountId] = user
fm.users[user.accountId] = user
}
fm.userCount = len(fm.users)
}
@ -165,10 +165,10 @@ func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
continue
}
friendship := &Friendship{}
friendship.User1 = user1
friendship.User2 = user2
fm.addFriendshipToMap(user1.AccountId, friendship)
fm.addFriendshipToMap(user2.AccountId, friendship)
friendship.user1 = user1
friendship.user2 = user2
fm.addFriendshipToMap(user1.accountId, friendship)
fm.addFriendshipToMap(user2.accountId, friendship)
}
}
@ -196,14 +196,14 @@ func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
receiverAccountId := q5.ToString(*rows.GetByIndex(1))
user := fm.users[receiverAccountId]
if user != nil {
if user.FriendRequest == nil {
user.FriendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
if user.friendRequest == nil {
user.friendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest := &FriendRequest{
AccountId: senderAccountId,
IsFriendship: 0,
accountId: senderAccountId,
isFriendship: 0,
}
user.FriendRequest[receiverAccountId] = friendRequest
user.friendRequest[receiverAccountId] = friendRequest
}
}
}
@ -232,15 +232,15 @@ func (fm *FriendsMgr) loadBlacklistResult(err error, rows *f5.DataSet) {
account2Id := q5.ToString(*rows.GetByIndex(1))
friendBlackList := &FriendBlackList{
AccountId: account2Id,
accountId: account2Id,
isRemoved: 0,
}
user := fm.users[account1Id]
if user != nil {
if user.FriendBlackList == nil {
user.FriendBlackList = make(map[string]*FriendBlackList, MaxBlockedMembers)
if user.friendBlackList == nil {
user.friendBlackList = make(map[string]*FriendBlackList, MaxBlockedMembers)
}
user.FriendBlackList[account1Id] = friendBlackList
user.friendBlackList[account1Id] = friendBlackList
}
}
}

View File

@ -12,9 +12,9 @@ import (
type FriendsMgr struct {
cs.MsgHandlerImpl
mu sync.RWMutex
users map[string]*User // AccountId -> 用户
users map[string]*User // accountId -> 用户
searchCaches map[string]SearchCache // SearchKeyword -> 好友搜索结果 []*User
friendships map[string][]*Friendship // AccountId -> 好友关系列表 []*Friendship
friendships map[string][]*Friendship // accountId -> 好友关系列表 []*Friendship
userCount int // 用户总数
}
@ -68,10 +68,10 @@ func (fm *FriendsMgr) searchUsers(keyword string) []*User {
listFriend := make([]*User, 0, MaxSearchResults)
lowercaseQuery := strings.ToLower(keyword)
for _, u := range fm.users {
if strings.Contains(strings.ToLower(u.Username), lowercaseQuery) {
if strings.Contains(strings.ToLower(u.username), lowercaseQuery) {
uEntity := &User{
AccountId: u.AccountId,
Username: u.Username,
accountId: u.accountId,
username: u.username,
}
listFriend = append(listFriend, uEntity)
}
@ -109,7 +109,7 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string) err
}
// 已发送请求
if _, ok := user1.FriendRequest[account2Id]; ok {
if _, ok := user1.friendRequest[account2Id]; ok {
return nil
}
@ -156,8 +156,8 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
// Create a new friendship
friendship := &Friendship{
User1: fm.users[account1Id],
User2: fm.users[account2Id],
user1: fm.users[account1Id],
user2: fm.users[account2Id],
}
fm.friendships[account1Id] = append(fm.friendships[account1Id], friendship)
fm.friendships[account2Id] = append(fm.friendships[account2Id], friendship)
@ -202,7 +202,7 @@ func (fm *FriendsMgr) deleteFriendShip(account1Id, account2Id string) error {
var found bool
for i, friendship := range user1Friendships {
if friendship.User1.AccountId == account2Id || friendship.User2.AccountId == account2Id {
if friendship.user1.accountId == account2Id || friendship.user2.accountId == account2Id {
// 删除好友请求, upsert 不存在则新增,存在则替换值
fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDeleted))
fm.friendships[account1Id] = append(user1Friendships[:i], user1Friendships[i+1:]...)
@ -216,7 +216,7 @@ func (fm *FriendsMgr) deleteFriendShip(account1Id, account2Id string) error {
}
for i, friendship := range user2Friendships {
if friendship.User1.AccountId == account1Id || friendship.User2.AccountId == account1Id {
if friendship.user1.accountId == account1Id || friendship.user2.accountId == account1Id {
// 删除好友请求, insert和replace, 不存在则新增,存在则替换值
fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusDeleted))
fm.friendships[account2Id] = append(user2Friendships[:i], user2Friendships[i+1:]...)
@ -244,7 +244,7 @@ func (fm *FriendsMgr) getFriendCount(accountId string) int {
func (fm *FriendsMgr) getFriendRequestCount(accountId string) int {
user := fm.getUser(accountId)
if user != nil {
return len(user.FriendRequest)
return len(user.friendRequest)
}
return 0
}
@ -254,16 +254,16 @@ func (fm *FriendsMgr) listFriends(accountId string) []*User {
// By default, Users member data count:10
var users []*User
for _, friendship := range fm.friendships[accountId] {
if friendship.User1.AccountId != accountId {
if friendship.user1.accountId != accountId {
uEntity := &User{
AccountId: friendship.User1.AccountId,
Username: friendship.User1.Username,
accountId: friendship.user1.accountId,
username: friendship.user1.username,
}
users = append(users, uEntity)
} else {
uEntity := &User{
AccountId: friendship.User2.AccountId,
Username: friendship.User2.Username,
accountId: friendship.user2.accountId,
username: friendship.user2.username,
}
users = append(users, uEntity)
}
@ -290,7 +290,7 @@ func (fm *FriendsMgr) addBlacklist(account1Id string, account2Id string) error {
}
user1.AddBlacklist(&FriendBlackList{
AccountId: account2Id,
accountId: account2Id,
isRemoved: 0,
})
fm.upsertBlacklist(account1Id, account2Id, BlacklistStatusDefault)
@ -323,7 +323,7 @@ func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendshi
func (fm *FriendsMgr) registerUser(accountId string, username string) error {
if fm.users[accountId] == nil {
fm.users[accountId] = &User{AccountId: accountId, Username: username}
fm.users[accountId] = &User{accountId: accountId, username: username}
return nil
}
return fmt.Errorf("user exists")
@ -340,14 +340,14 @@ func (fm *FriendsMgr) clearExpiredCaches() {
func PrintUsers(str string, userList []*User) {
for _, user := range userList {
fmt.Printf("[%s]:accountId:%s, username:%s \n", str, user.AccountId, user.Username)
fmt.Printf("[%s]:accountId:%s, username:%s \n", str, user.accountId, user.username)
}
}
func (fm *FriendsMgr) IsFriendShip(account1Id, Account2Id string) bool {
var found bool
for _, friendship := range fm.friendships[account1Id] {
if friendship.User1.AccountId == Account2Id || friendship.User2.AccountId == Account2Id {
if friendship.user1.accountId == Account2Id || friendship.user2.accountId == Account2Id {
return true
}
}
@ -356,12 +356,12 @@ func (fm *FriendsMgr) IsFriendShip(account1Id, Account2Id string) bool {
func (fm *FriendsMgr) GetFriendByAccountId(account1Id, Account2Id string) *User {
for _, friendship := range fm.friendships[account1Id] {
if friendship.User1.AccountId == Account2Id {
return friendship.User1
if friendship.user1.accountId == Account2Id {
return friendship.user1
}
if friendship.User2.AccountId == Account2Id {
return friendship.User2
if friendship.user2.accountId == Account2Id {
return friendship.user2
}
}
return nil
@ -371,7 +371,7 @@ func (fm *FriendsMgr) findFriendShipIndex(Account1Id, Account2Id string) int {
// 通常 account1Id,指自己, account2Id 指对方
if _, exists := fm.friendships[Account1Id]; exists {
for i, friendship := range fm.friendships[Account1Id] {
if friendship.User2.AccountId == Account2Id {
if friendship.user2.accountId == Account2Id {
return i
}
}

View File

@ -11,7 +11,6 @@ func TestRegisterUser(t *testing.T) {
fm.users = make(map[string]*User, 10)
fm.searchCaches = make(map[string]SearchCache, 10)
fm.friendships = make(map[string][]*Friendship, 10)
fm.pendingReqs = make(map[string]map[string]bool, 10)
// Register users
fm.registerUser("1", "google")
@ -33,13 +32,13 @@ func TestRegisterUser(t *testing.T) {
// Add friend request
// google 申请添加 apple 好友
fmt.Printf("after: pendingReqs count: %d \n", len(fm.pendingReqs))
fm.addFriendRequest("1", "2")
fm.addFriendRequest("3", "2")
fm.addFriendRequest("4", "2")
fm.addFriendRequest("5", "6")
fm.addFriendRequest("6", "2")
fmt.Printf("before: pendingReqs count: %d \n", len(fm.pendingReqs))
//fmt.Printf("after: pendingReqs count: %d \n", len(fm.pendingReqs))
//fm.addFriendRequest("1", "2")
//fm.addFriendRequest("3", "2")
//fm.addFriendRequest("4", "2")
//fm.addFriendRequest("5", "6")
//fm.addFriendRequest("6", "2")
//fmt.Printf("before: pendingReqs count: %d \n", len(fm.pendingReqs))
// Accept friend requests
fm.acceptFriendRequest("2", "1")

View File

@ -5,49 +5,49 @@ import (
)
type GuildMember struct {
AccountId string
Level int // 1: 会长, 2: 副会长, 3: 精英, 4 成员
accountId string
level int // 1: 会长, 2: 副会长, 3: 精英, 4 成员
}
type Guild struct {
AutoId int64 // 公会自增id
GuildId int64 // 公会id
Name string // 公会名称
LeaderId string // 公会leader
Avatar int32 // 头像
Notice string // 公告
JoinCond int32 // 公会加入条件
JoinCondValue int32 // 公会加入条件值
TotalStars int32 // 总星星数量
TotalKills int32 // 单局总击杀数
ChickenDinners int32 // 单局第一名数
MaxMembers int32 // 公会最大成员数 default 30
Members []*GuildMember
PendingReqs map[string]int32 // pendingAccountId -> status 0,1,2,3 pending, accept, reject, leave
autoId int64 // 公会自增id
guildId int64 // 公会id
name string // 公会名称
leaderId string // 公会leader
avatar int32 // 头像
notice string // 公告
joinCond int32 // 公会加入条件
joinCondValue int32 // 公会加入条件值
totalStars int32 // 总星星数量
totalKills int32 // 单局总击杀数
chickenDinners int32 // 单局第一名数
maxMembers int32 // 公会最大成员数 default 30
members []*GuildMember
pendingReqs map[string]int32 // pendingAccountId -> status 0,1,2,3 pending, accept, reject, leave
}
// GuildLog 公会日志
type GuildLog struct {
GuildId int64
AccountId string
LogType int32
Content string
guildId int64
accountId string
logType int32
content string
}
// PendingReq 待审核请求
type PendingReq struct {
AccountId string
Status int32 // 0 pending, 1 ok, 2 reject, 3 disband
accountId string
status int32 // 0 pending, 1 ok, 2 reject, 3 disband
}
func (g *Guild) GetGuildId() int64 {
return g.GuildId
return g.guildId
}
// findMemberIndex 根据 AccountId 查找成员在 Members 切片中的索引
func (g *Guild) findMemberIndex(AccountId string) int {
for i, member := range g.Members {
if member.AccountId == AccountId {
func (g *Guild) findMemberIndex(accountId string) int {
for i, member := range g.members {
if member.accountId == accountId {
return i
}
}
@ -60,7 +60,7 @@ func (g *Guild) GetMember(accountId string) *GuildMember {
if index == -1 {
return nil
}
return g.Members[index]
return g.members[index]
}
// IsMember 是否是公会成员
@ -71,7 +71,7 @@ func (g *Guild) IsMember(accountId string) bool {
// IsFull 成员是否已满
func (g *Guild) IsFull() error {
if int32(len(g.Members)) >= g.MaxMembers {
if int32(len(g.members)) >= g.maxMembers {
return fmt.Errorf("guild is full")
}
return nil
@ -80,39 +80,39 @@ func (g *Guild) IsFull() error {
// AddMember 添加成员
func (g *Guild) AddMember(member *GuildMember) {
if err := g.IsFull(); err == nil {
g.Members = append(g.Members, member)
g.members = append(g.members, member)
}
}
// RemoveMember 移除成员
func (g *Guild) RemoveMember(accountId string) {
if accountId == g.LeaderId {
if accountId == g.leaderId {
return
}
index := g.findMemberIndex(accountId)
if index == -1 {
return
}
copy(g.Members[index:], g.Members[index+1:])
g.Members[len(g.Members)-1] = nil
g.Members = g.Members[:len(g.Members)-1]
copy(g.members[index:], g.members[index+1:])
g.members[len(g.members)-1] = nil
g.members = g.members[:len(g.members)-1]
}
func (g *Guild) SetNotice(notice *string) {
g.Notice = *notice
g.notice = *notice
}
func (g *Guild) GetNotice() string {
return g.Notice
return g.notice
}
// AddPendingReq 添加等待审核成员
func (g *Guild) AddPendingReq(p *PendingReq) {
g.PendingReqs[p.AccountId] = p.Status
g.pendingReqs[p.accountId] = p.status
}
func (g *Guild) GetPendingReqStatus(accountId string) int32 {
if pendingReqStatus, exists := g.PendingReqs[accountId]; exists {
if pendingReqStatus, exists := g.pendingReqs[accountId]; exists {
if exists {
return pendingReqStatus
}
@ -122,10 +122,10 @@ func (g *Guild) GetPendingReqStatus(accountId string) int32 {
// RemovePendingReq 移除等待审核成员
func (g *Guild) RemovePendingReq(accountId string) {
delete(g.PendingReqs, accountId)
delete(g.pendingReqs, accountId)
}
func (g *Guild) IsInReq(accountId string) bool {
pendingStatus, exists := g.PendingReqs[accountId]
pendingStatus, exists := g.pendingReqs[accountId]
return exists && pendingStatus == PendingReqIsJoinGuildStatusJoined
}

View File

@ -42,18 +42,18 @@ func (gm *GuildMgr) loadGuildFromDBResult(err error, rows *f5.DataSet) {
guildId := q5.ToInt64(*rows.GetByIndex(1))
// put to gm.guilds
gm.guilds[guildId] = &Guild{
AutoId: q5.ToInt64(*rows.GetByIndex(0)),
GuildId: q5.ToInt64(*rows.GetByIndex(1)),
Name: q5.ToString(*rows.GetByIndex(2)),
LeaderId: q5.ToString(*rows.GetByIndex(3)),
Avatar: q5.ToInt32(*rows.GetByIndex(4)),
Notice: q5.ToString(*rows.GetByIndex(5)),
JoinCond: q5.ToInt32(*rows.GetByIndex(6)),
JoinCondValue: q5.ToInt32(*rows.GetByIndex(7)),
TotalStars: q5.ToInt32(*rows.GetByIndex(8)),
TotalKills: q5.ToInt32(*rows.GetByIndex(9)),
ChickenDinners: q5.ToInt32(*rows.GetByIndex(10)),
MaxMembers: q5.ToInt32(*rows.GetByIndex(11)),
autoId: q5.ToInt64(*rows.GetByIndex(0)),
guildId: q5.ToInt64(*rows.GetByIndex(1)),
name: q5.ToString(*rows.GetByIndex(2)),
leaderId: q5.ToString(*rows.GetByIndex(3)),
avatar: q5.ToInt32(*rows.GetByIndex(4)),
notice: q5.ToString(*rows.GetByIndex(5)),
joinCond: q5.ToInt32(*rows.GetByIndex(6)),
joinCondValue: q5.ToInt32(*rows.GetByIndex(7)),
totalStars: q5.ToInt32(*rows.GetByIndex(8)),
totalKills: q5.ToInt32(*rows.GetByIndex(9)),
chickenDinners: q5.ToInt32(*rows.GetByIndex(10)),
maxMembers: q5.ToInt32(*rows.GetByIndex(11)),
}
}
q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildFlag)
@ -89,8 +89,8 @@ func (gm *GuildMgr) loadGuildMemberFromDBResult(err error, rows *f5.DataSet) {
level = int(q5.ToInt32(*rows.GetByIndex(2)))
guildMember := &GuildMember{
AccountId: accountId,
Level: level,
accountId: accountId,
level: level,
}
if guild, ok := gm.guilds[guildId]; ok {
guild.AddMember(guildMember)
@ -127,12 +127,12 @@ func (gm *GuildMgr) loadPendingReqsFromDBResult(err error, rows *f5.DataSet) {
accountId = q5.ToString(*rows.GetByIndex(1))
pendingReq := &PendingReq{
AccountId: accountId,
Status: PendingReqIsJoinGuildStatusDefault,
accountId: accountId,
status: PendingReqIsJoinGuildStatusDefault,
}
if guild, ok := gm.guilds[guildId]; ok {
if guild.PendingReqs == nil {
guild.PendingReqs = make(map[string]int32)
if guild.pendingReqs == nil {
guild.pendingReqs = make(map[string]int32)
}
guild.AddPendingReq(pendingReq)
}
@ -180,10 +180,10 @@ func (gm *GuildMgr) loadGuildLogsFromDBResult(err error, pagination *f5.Paginati
content = q5.ToString(*pagination.Rows.GetByIndex(3))
guildLog := &GuildLog{
GuildId: guildId,
AccountId: accountId,
LogType: logType,
Content: content,
guildId: guildId,
accountId: accountId,
logType: logType,
content: content,
}
gm.guildLogs[guildId] = append(gm.guildLogs[guildId], guildLog)
}
@ -193,10 +193,10 @@ func (gm *GuildMgr) loadGuildLogsFromDBResult(err error, pagination *f5.Paginati
// createGuildDB 创建公会
func (gm *GuildMgr) createGuildDB(g *Guild) {
fields := [][]string{
{"guild_id", q5.ToString(g.GuildId)},
{"name", g.Name},
{"leader_account_id", g.LeaderId},
{"max_members", q5.ToString(g.MaxMembers)},
{"guild_id", q5.ToString(g.guildId)},
{"name", g.name},
{"leader_account_id", g.leaderId},
{"max_members", q5.ToString(g.maxMembers)},
}
var insertError error
var lastInsertId int64
@ -226,7 +226,7 @@ func (gm *GuildMgr) createGuildDB(g *Guild) {
// updateGuild 更新公会信息
func (gm *GuildMgr) updateGuild(g *Guild, fields [][]string, cb func(error)) {
where := [][]string{
{"guild_id", q5.ToString(g.GuildId)},
{"guild_id", q5.ToString(g.guildId)},
}
f5.GetJsStyleDb().Update(
FRIEND_DB,
@ -311,12 +311,12 @@ func (gm *GuildMgr) updatePendingReqs(guildId int64, accountId string, isJoinGui
func (gm *GuildMgr) upsertGuildMember(guildId int64, member *GuildMember, cb func(error)) {
where := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", member.AccountId},
{"account_id", member.accountId},
}
insertKv := [][]string{
{"guild_id", q5.ToString(guildId)},
{"account_id", member.AccountId},
{"level", q5.ToString(member.Level)},
{"account_id", member.accountId},
{"level", q5.ToString(member.level)},
{"is_leave_guild", "0"},
}
updateKv := insertKv
@ -350,7 +350,7 @@ func (gm *GuildMgr) updateGuildMembers(guildId int64, fields [][]string, cb func
// updateGuildMember 更新成员信息
func (gm *GuildMgr) updateGuildMember(g *Guild, accountId string, fields [][]string, cb func(error)) {
where := [][]string{
{"guild_id", q5.ToString(g.GuildId)},
{"guild_id", q5.ToString(g.guildId)},
{"account_id", accountId},
}
@ -368,10 +368,10 @@ func (gm *GuildMgr) updateGuildMember(g *Guild, accountId string, fields [][]str
// insertGuildLog 添加公会日志
func (gm *GuildMgr) insertGuildLog(log *GuildLog) {
fields := [][]string{
{"guild_id", q5.ToString(log.GuildId)},
{"account_id", log.AccountId},
{"log_type", q5.ToString(int(log.LogType))},
{"content", log.Content}}
{"guild_id", q5.ToString(log.guildId)},
{"account_id", log.accountId},
{"log_type", q5.ToString(int(log.logType))},
{"content", log.content}}
f5.GetJsStyleDb().Insert(
FRIEND_DB,
"t_guild_logs",

View File

@ -84,7 +84,7 @@ func (gm *GuildMgr) CreateGuild(name string, leaderId string,
cb(1, "xxx", 0)
return
}
newMember := &GuildMember{AccountId: leaderId, Level: GuildMemberLevelLeader}
newMember := &GuildMember{accountId: leaderId, level: GuildMemberLevelLeader}
gm.upsertGuildMember(guildId, newMember,
func(err error) {
if err != nil {
@ -92,13 +92,13 @@ func (gm *GuildMgr) CreateGuild(name string, leaderId string,
return
}
guild := &Guild{
AutoId: id,
GuildId: guildId,
Name: name,
LeaderId: leaderId,
MaxMembers: MaxMembers,
autoId: id,
guildId: guildId,
name: name,
leaderId: leaderId,
maxMembers: MaxMembers,
}
guild.Members = append(guild.Members, newMember)
guild.members = append(guild.members, newMember)
gm.AddGuild(guildId, guild)
})
})
@ -122,14 +122,14 @@ func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string, cb fu
}
// 可直接加入
if guild.JoinCond == JoinCondFree {
if guild.joinCond == JoinCondFree {
gm.JoinGuild(guild, applicantAccountId)
cb(0, "")
}
if guild.JoinCond == JoinCondStar {
if guild.joinCond == JoinCondStar {
var userStar int32 = 200
if userStar >= guild.JoinCondValue {
if userStar >= guild.joinCondValue {
gm.JoinGuild(guild, applicantAccountId)
cb(0, "")
}
@ -180,7 +180,7 @@ func (gm *GuildMgr) Approve(operatorAccountId, accountId string, cb func(errCode
}
// 通常默认为审核加入
if guild.JoinCond != JoinCondDefault {
if guild.joinCond != JoinCondDefault {
cb(1, "XXX")
}
@ -204,7 +204,7 @@ func (gm *GuildMgr) Approve(operatorAccountId, accountId string, cb func(errCode
if err != nil {
cb(1, "XXX")
}
guildId := guild.GuildId
guildId := guild.guildId
// 是否在申请队列中
pendingReqStatus := guild.GetPendingReqStatus(accountId)
@ -212,7 +212,7 @@ func (gm *GuildMgr) Approve(operatorAccountId, accountId string, cb func(errCode
cb(1, "XXX")
}
newMember := &GuildMember{AccountId: accountId, Level: GuildMemberLevelDefault}
newMember := &GuildMember{accountId: accountId, level: GuildMemberLevelDefault}
guild.AddMember(newMember)
gm.upsertGuildMember(guildId, newMember, func(err error) {
@ -236,7 +236,7 @@ func (gm *GuildMgr) Reject(operatorAccountId, accountId string, cb func(errCode
cb(1, "XXX")
}
// 通常默认为审核加入
if guild.JoinCond != JoinCondDefault {
if guild.joinCond != JoinCondDefault {
cb(1, "XXX")
}
operatorMember := guild.GetMember(operatorAccountId)
@ -258,7 +258,7 @@ func (gm *GuildMgr) Reject(operatorAccountId, accountId string, cb func(errCode
cb(1, "XXX")
}
gm.updatePendingReqs(guild.GuildId, accountId, PendingReqIsJoinGuildStatusReject, func(err error) {
gm.updatePendingReqs(guild.guildId, accountId, PendingReqIsJoinGuildStatusReject, func(err error) {
guild.RemovePendingReq(accountId)
cb(0, "")
})
@ -266,9 +266,9 @@ func (gm *GuildMgr) Reject(operatorAccountId, accountId string, cb func(errCode
// JoinGuild 直接加入公会
func (gm *GuildMgr) JoinGuild(guild *Guild, accountId string) {
newMember := GuildMember{AccountId: accountId, Level: GuildMemberLevelDefault}
newMember := GuildMember{accountId: accountId, level: GuildMemberLevelDefault}
guild.AddMember(&newMember)
guildId := guild.GuildId
guildId := guild.guildId
gm.upsertGuildMember(guildId, &newMember,
func(err error) {
})
@ -280,7 +280,7 @@ func (gm *GuildMgr) LeaveGuild(accountId string, cb func(errCode int32, errMsg s
if guild == nil {
cb(1, "XXX")
}
if guild.LeaderId == accountId {
if guild.leaderId == accountId {
cb(1, "XXX")
}
member := guild.GetMember(accountId)
@ -290,11 +290,11 @@ func (gm *GuildMgr) LeaveGuild(accountId string, cb func(errCode int32, errMsg s
guild.RemoveMember(accountId)
fields := [][]string{{"is_leave_guild", q5.ToString(1)}}
gm.updateGuildMember(guild, member.AccountId, fields,
gm.updateGuildMember(guild, member.accountId, fields,
func(err error) {
if err != nil {
logContent := fmt.Sprintf("LeaveGuild[%d-%s]", guild.GuildId, accountId)
gm.WriteLog(guild.GuildId, accountId, LogTypeLeave, logContent)
logContent := fmt.Sprintf("LeaveGuild[%d-%s]", guild.guildId, accountId)
gm.WriteLog(guild.guildId, accountId, LogTypeLeave, logContent)
cb(0, "")
}
})
@ -307,7 +307,7 @@ func (gm *GuildMgr) DismissMember(operatorAccountId, accountId string, cb func(e
cb(1, "XXX")
}
if accountId == guild.LeaderId || accountId == operatorAccountId {
if accountId == guild.leaderId || accountId == operatorAccountId {
cb(1, "XXX")
}
@ -327,15 +327,15 @@ func (gm *GuildMgr) DismissMember(operatorAccountId, accountId string, cb func(e
cb(1, "XXX")
}
if dismissMember.Level <= operatorMember.Level {
if dismissMember.level <= operatorMember.level {
cb(1, "XXX")
}
guild.RemoveMember(accountId)
fields := [][]string{{"is_leave_guild", q5.ToString(1)}}
gm.updateGuildMember(guild, dismissMember.AccountId, fields, func(err error) {
logContent := fmt.Sprintf("DismissMember[%d-%s-%s]", guild.GuildId, operatorAccountId, accountId)
gm.WriteLog(guild.GuildId, accountId, LogTypeDismiss, logContent)
gm.updateGuildMember(guild, dismissMember.accountId, fields, func(err error) {
logContent := fmt.Sprintf("DismissMember[%d-%s-%s]", guild.guildId, operatorAccountId, accountId)
gm.WriteLog(guild.guildId, accountId, LogTypeDismiss, logContent)
cb(0, "")
})
}
@ -347,7 +347,7 @@ func (gm *GuildMgr) PromoteMember(operatorAccountId, accountId string, cb func(e
cb(1, "XXX")
}
if accountId == guild.LeaderId || accountId == operatorAccountId {
if accountId == guild.leaderId || accountId == operatorAccountId {
cb(1, "XXX")
}
@ -357,7 +357,7 @@ func (gm *GuildMgr) PromoteMember(operatorAccountId, accountId string, cb func(e
cb(1, "XXX")
}
if operatorAccountId != guild.LeaderId {
if operatorAccountId != guild.leaderId {
cb(1, "XXX")
}
@ -366,15 +366,15 @@ func (gm *GuildMgr) PromoteMember(operatorAccountId, accountId string, cb func(e
cb(1, "XXX")
}
if member.Level == GuildMemberLevelViceLeader {
if member.level == GuildMemberLevelViceLeader {
cb(1, "XXX")
}
member.Level = GuildMemberLevelViceLeader
member.level = GuildMemberLevelViceLeader
fields := [][]string{{"level", q5.ToString(GuildMemberLevelViceLeader)}}
gm.updateGuildMember(guild, member.AccountId, fields, func(err error) {
logContent := fmt.Sprintf("PromoteMember[%d-%s-%s]", guild.GuildId, operatorAccountId, accountId)
gm.WriteLog(guild.GuildId, accountId, LogTypeDismiss, logContent)
gm.updateGuildMember(guild, member.accountId, fields, func(err error) {
logContent := fmt.Sprintf("PromoteMember[%d-%s-%s]", guild.guildId, operatorAccountId, accountId)
gm.WriteLog(guild.guildId, accountId, LogTypeDismiss, logContent)
cb(0, "")
})
}
@ -386,12 +386,12 @@ func (gm *GuildMgr) DemoteMember(operatorAccountId, accountId string, cb func(er
cb(1, "XXX")
}
if accountId == guild.LeaderId || accountId == operatorAccountId {
if accountId == guild.leaderId || accountId == operatorAccountId {
cb(1, "XXX")
}
// 仅会长可操作
if operatorAccountId != guild.LeaderId {
if operatorAccountId != guild.leaderId {
cb(1, "XXX")
}
@ -400,15 +400,15 @@ func (gm *GuildMgr) DemoteMember(operatorAccountId, accountId string, cb func(er
cb(1, "XXX")
}
if member.Level == GuildMemberLevelDefault {
if member.level == GuildMemberLevelDefault {
cb(1, "XXX")
}
member.Level = GuildMemberLevelDefault
member.level = GuildMemberLevelDefault
fields := [][]string{{"level", q5.ToString(GuildMemberLevelDefault)}}
gm.updateGuildMember(guild, member.AccountId, fields, func(err error) {
logContent := fmt.Sprintf("DemoteMember[%d-%s-%s]", guild.GuildId, operatorAccountId, accountId)
gm.WriteLog(guild.GuildId, accountId, LogTypeDismiss, logContent)
gm.updateGuildMember(guild, member.accountId, fields, func(err error) {
logContent := fmt.Sprintf("DemoteMember[%d-%s-%s]", guild.guildId, operatorAccountId, accountId)
gm.WriteLog(guild.guildId, accountId, LogTypeDismiss, logContent)
cb(0, "")
})
}
@ -421,11 +421,11 @@ func (gm *GuildMgr) Disband(operatorAccountId string, cb func(errCode int32, err
}
// 仅会长可操作
if operatorAccountId != guild.LeaderId {
if operatorAccountId != guild.leaderId {
cb(1, "XXX")
}
guildId := guild.GuildId
guildName := guild.Name
guildId := guild.guildId
guildName := guild.name
// 解散公会
updateFields := [][]string{
@ -438,7 +438,7 @@ func (gm *GuildMgr) Disband(operatorAccountId string, cb func(errCode int32, err
logContent := fmt.Sprintf("GuildDisbanded[%d-%s]", guildId, guildName)
gm.WriteLog(guildId, operatorAccountId, LogTypeDisband, logContent)
guild.Members = nil
guild.members = nil
gm.guilds[guildId] = nil
gm.guildLogs[guildId] = nil
@ -459,7 +459,7 @@ func (gm *GuildMgr) SetNotice(operatorAccountId string, notice *string, cb func(
}
// 仅会长可操作
if operatorAccountId != guild.LeaderId {
if operatorAccountId != guild.leaderId {
cb(1, "XXX")
}
@ -487,10 +487,10 @@ func (gm *GuildMgr) WriteLog(guildId int64, accountId string, logType int32, con
gm.guildLogs[guildId] = make([]*GuildLog, DefaultLogs)
}
log := &GuildLog{
GuildId: guildId,
AccountId: accountId,
LogType: logType,
Content: content,
guildId: guildId,
accountId: accountId,
logType: logType,
content: content,
}
gm.guildLogs[guildId] = append(gm.guildLogs[guildId], log)
gm.insertGuildLog(log)
@ -504,7 +504,7 @@ func (gm *GuildMgr) GetLogs(guildID int64) []*GuildLog {
func (gm *GuildMgr) SearchGuilds(keyword string) []*Guild {
var results []*Guild
for _, guild := range gm.guilds {
if containsSubstring(guild.Name, keyword) {
if containsSubstring(guild.name, keyword) {
results = append(results, guild)
}
}
@ -556,8 +556,8 @@ func (gm *GuildMgr) GetGuild(guildId int64) (*Guild, error) {
// GetGuildByAccountId 查询我的工会
func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild {
for _, guild := range gm.guilds {
for _, member := range guild.Members {
if accountId == member.AccountId {
for _, member := range guild.members {
if accountId == member.accountId {
return guild
}
}
@ -566,8 +566,8 @@ func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild {
}
func (gm *GuildMgr) checkOperatorPerm(operatorMember *GuildMember, level int) error {
if operatorMember.Level > level {
return fmt.Errorf("checkOperatorPerm: no permission[%s-%d]", operatorMember.AccountId, operatorMember.Level)
if operatorMember.level > level {
return fmt.Errorf("checkOperatorPerm: no permission[%s-%d]", operatorMember.accountId, operatorMember.level)
}
return nil
}
@ -579,7 +579,7 @@ func (gm *GuildMgr) checkJoinGuild(accountId string) bool {
func (gm *GuildMgr) GetGuildIdByAccountId(accountId string) int64 {
guild := gm.GetGuildByAccountId(accountId)
if guild != nil {
return guild.GuildId
return guild.guildId
}
return 0
}

View File

@ -15,33 +15,34 @@ var (
var newGuildId int64 = 1148210928160867328
func TestInit(t *testing.T) {
f5.Run(app)
fmt.Printf("test init")
}
func TestCreateGuild(t *testing.T) {
f5.TestRun(app)
f5.Run(app)
guildMgr := NewGuildMgr()
guildName := randomGuildName()
guildId, err := guildMgr.CreateGuild(guildName, leaderId)
if err != nil {
fmt.Println("Error:", err)
return
}
newGuildId = guildId
fmt.Println("Created guild:", guildId)
guildMgr.CreateGuild(
randomGuildName(),
leaderId,
func(errCode int32, errMsg string, guildId int64) {
newGuildId = guildId
fmt.Println("Created guild:", guildId)
})
}
func TestGuildMember(t *testing.T) {
f5.TestRun(app)
if newGuildId <= 0 {
t.Errorf("guildId error: %v", newGuildId)
}
err := guildMgr.ApplyToGuild(newGuildId, member1Id)
if err != nil {
t.Errorf("Error:%s", err)
return
}
fmt.Println("Applied to guild")
guildMgr.ApplyToGuild(
newGuildId, member1Id,
func(errCode int32, errMsg string) {
if errCode != 0 {
t.Errorf("Error:%s", errMsg)
}
fmt.Println("Applied to guild")
},
)
}
func randomGuildName() string {

View File

@ -30,8 +30,8 @@ func (p *Player) CMSearchUser(hdr *f5.MsgHdr, msg *cs.CMSearchUser) {
listUsers := friendMgr.searchUsers(msg.GetSearchKeyword())
for _, u := range listUsers {
friend := &cs.MFUser{
AccountId: &u.AccountId,
Username: &u.Username,
AccountId: &u.accountId,
Username: &u.username,
}
rspMsg.Users = append(rspMsg.Users, friend)
}
@ -45,8 +45,8 @@ func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByA
rspMsg := new(cs.SMSearchUserByAccountId)
user := friendMgr.searchByAccountId(msg.GetAccountId())
if user != nil {
rspMsg.AccountId = &user.AccountId
rspMsg.Username = &user.Username
rspMsg.AccountId = &user.accountId
rspMsg.Username = &user.username
f5.GetSysLog().Info("CMSearchUserByAccountId AccountId: %s\n", *rspMsg.AccountId)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
@ -130,16 +130,16 @@ func (p *Player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
rspMsg := &cs.SMListFriend{}
accountId := p.accountId
for _, friendship := range friendMgr.friendships[accountId] {
if friendship.User1.AccountId != accountId {
if friendship.user1.accountId != accountId {
friend := &cs.MFUser{
AccountId: &friendship.User1.AccountId,
Username: &friendship.User1.Username,
AccountId: &friendship.user1.accountId,
Username: &friendship.user1.username,
}
rspMsg.Users = append(rspMsg.Users, friend)
} else {
friend := &cs.MFUser{
AccountId: &friendship.User2.AccountId,
Username: &friendship.User2.Username,
AccountId: &friendship.user2.accountId,
Username: &friendship.user2.username,
}
rspMsg.Users = append(rspMsg.Users, friend)
}
@ -212,7 +212,7 @@ func (p *Player) CMFriendInfo(hdr *f5.MsgHdr, msg *cs.CMFriendInfo) {
// friendships
friends := friendMgr.listFriends(accountId)
for _, friend := range friends {
rspMsg.FriendshipsAccountIds = append(rspMsg.FriendshipsAccountIds, friend.AccountId)
rspMsg.FriendshipsAccountIds = append(rspMsg.FriendshipsAccountIds, friend.accountId)
}
accountIds := user.GetAllFriendRequestAccountIds()
@ -220,7 +220,7 @@ func (p *Player) CMFriendInfo(hdr *f5.MsgHdr, msg *cs.CMFriendInfo) {
// blacklist
var blacklistAccountIds []string
for bAccountId := range user.FriendBlackList {
for bAccountId := range user.friendBlackList {
blacklistAccountIds = append(blacklistAccountIds, bAccountId)
}
rspMsg.BlacklistAccountIds = blacklistAccountIds
@ -294,17 +294,17 @@ func (p *Player) CMGuildInfo(hdr *f5.MsgHdr, msg *cs.CMGuildInfo) {
return
}
membersCount := q5.ToInt32(len(guild.Members))
maxMembers := q5.ToInt32(guild.MaxMembers)
membersCount := q5.ToInt32(len(guild.members))
maxMembers := q5.ToInt32(guild.maxMembers)
rspGuild := &cs.MFGuild{
AutoId: &guild.AutoId,
GuildId: &guild.GuildId,
Name: &guild.Name,
LeaderId: &guild.LeaderId,
AutoId: &guild.autoId,
GuildId: &guild.guildId,
Name: &guild.name,
LeaderId: &guild.leaderId,
MembersCount: &membersCount,
MaxMembers: &maxMembers,
Notice: &guild.Notice,
Notice: &guild.notice,
}
rspMsg.Guild = rspGuild
rspMsg.RandomGuilds = nil
@ -346,7 +346,7 @@ func (p *Player) CMApplyList(hdr *f5.MsgHdr, msg *cs.CMApplyList) {
rspMsg := new(cs.SMApplyList)
guild := guildMgr.GetGuildByAccountId(p.accountId)
if guild != nil {
for accountId, status := range guild.PendingReqs {
for accountId, status := range guild.pendingReqs {
applyInfo := &cs.MFApplyInfo{
AccountId: &accountId,
Status: &status,
@ -496,10 +496,10 @@ func (p *Player) CMGuildLogs(hdr *f5.MsgHdr, msg *cs.CMGuildLogs) {
}
for _, g := range guildMgr.GetGuildLogs(guild.GetGuildId()) {
guildLog := &cs.MFGuildLog{
GuildId: &g.GuildId,
AccountId: &g.AccountId,
LogType: &g.LogType,
Content: &g.Content,
GuildId: &g.guildId,
AccountId: &g.accountId,
LogType: &g.logType,
Content: &g.content,
}
rspMsg.GuildLogs = append(rspMsg.GuildLogs, guildLog)
}
@ -510,16 +510,16 @@ func (p *Player) CMGuildLogs(hdr *f5.MsgHdr, msg *cs.CMGuildLogs) {
func (p *Player) FillGuild(guilds []*Guild) []*cs.MFGuild {
var resGuilds []*cs.MFGuild
for _, g := range guilds {
membersCount := q5.ToInt32(len(g.Members))
maxMembers := q5.ToInt32(g.MaxMembers)
membersCount := q5.ToInt32(len(g.members))
maxMembers := q5.ToInt32(g.maxMembers)
guild := &cs.MFGuild{
AutoId: &g.AutoId,
GuildId: &g.GuildId,
Name: &g.Name,
LeaderId: &g.LeaderId,
AutoId: &g.autoId,
GuildId: &g.guildId,
Name: &g.name,
LeaderId: &g.leaderId,
MembersCount: &membersCount,
MaxMembers: &maxMembers,
Notice: &g.Notice,
Notice: &g.notice,
}
resGuilds = append(resGuilds, guild)
}