aozhiwei 00db6f45fc 1
2024-04-08 13:25:11 +08:00

497 lines
13 KiB
Go

package player
import (
"cs"
"q5"
"f5"
"fmt"
"mt"
"github.com/golang/protobuf/proto"
"main/common"
"main/constant"
. "main/global"
)
type player struct {
cs.MsgHandlerImpl
socket f5.WspCliConn
accountId string
sessionId string
}
func (this *player) init(req *pendingLoginRequest, rspObj *common.LoginRsp){
this.socket = req.hdr.GetSocket()
this.accountId = req.msg.GetAccountId()
this.sessionId = req.msg.GetSessionId()
}
func (this *player) GetAccountId() string {
return this.accountId
}
func (this *player) GetSessionId() string {
return this.sessionId
}
func (this *player) IsOnline() bool {
return this.socket.IsValid()
}
func (this *player) SendMsg(rspMsg proto.Message) {
GetWspListener().SendProxyMsg(this.socket.Conn, this.socket.SocketHandle, rspMsg)
}
func (this *player) reBind(socket f5.WspCliConn) {
if this.socket.IsValid() {
delete(_playerMgr.socketHash, this.socket)
}
this.socket = socket
_playerMgr.socketHash[this.socket] = this
}
func (this *player) CMPing(hdr *f5.MsgHdr, msg *cs.CMPing) {
rspMsg := new(cs.SMPing)
this.SendMsg(rspMsg)
}
func (this *player) CMSearchUser(hdr *f5.MsgHdr, msg *cs.CMSearchUser) {
GetCacheMgr().AsyncSearch(
msg.GetSinceId(),
msg.GetUsername(),
func (errCode int32, errMsg string, sinceId int64, accountIds []string) {
rspMsg := new(cs.SMSearchUser)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
rspMsg.SinceId = proto.Int64(sinceId)
q5.NewSlice(&rspMsg.Users, 0, int32(len(accountIds)))
for _, accountId := range(accountIds) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFUser)
q5.AppendSlice(&rspMsg.Users, ele)
userProfile.FillMFUser(ele)
}
}
this.SendMsg(rspMsg)
})
}
func (this *player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByAccountId) {
GetCacheMgr().AsyncGetUsers(
[]string{msg.GetAccountId()},
func (errCode int32, errMsg string) {
rspMsg := new(cs.SMSearchUserByAccountId)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
rspMsg.Users = new(cs.MFUser)
userProfile := GetCacheMgr().GetUserProfile(msg.GetAccountId())
userProfile.FillMFUser(rspMsg.Users)
this.SendMsg(rspMsg)
})
}
func (this *player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
GetFriendMgr().AsyncGetApplyList(
0,
this.GetAccountId(),
func (errCode int32, errMsg string, sinceId int64, accountIds []string) {
rspMsg := new(cs.SMListPendingFriendRequest)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
GetCacheMgr().AsyncGetUsers(
accountIds,
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(accountIds) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFUser)
q5.AppendSlice(&rspMsg.Users, ele)
userProfile.FillMFUser(ele)
}
}
this.SendMsg(rspMsg)
})
})
}
func (this *player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
friendList := GetFriendMgr().GetFriendList(this.GetAccountId())
GetCacheMgr().AsyncGetUsers(
friendList,
func (errCode int32, errMsg string) {
rspMsg := new(cs.SMListFriend)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(friendList) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFUser)
q5.AppendSlice(&rspMsg.Users, ele)
userProfile.FillMFUser(ele)
}
}
this.SendMsg(rspMsg)
})
}
func (this *player) CMBlacklist(hdr *f5.MsgHdr, msg *cs.CMBlacklist) {
blackList := GetFriendMgr().GetBlackList(this.GetAccountId())
GetCacheMgr().AsyncGetUsers(
blackList,
func (errCode int32, errMsg string) {
rspMsg := new(cs.SMBlacklist)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(blackList) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := q5.NewSliceElement(&rspMsg.Users)
userProfile.FillMFUser(*ele)
}
}
this.SendMsg(rspMsg)
})
}
func (this *player) CMAddFriendRequest(hdr *f5.MsgHdr, msg *cs.CMAddFriendRequest) {
rspMsg := new(cs.SMAddFriendRequest)
if GetFriendMgr().IsFriend(this.GetAccountId(), msg.GetTargetAccountId()) {
rspMsg.IsFriendship = proto.Int32(1)
this.SendMsg(rspMsg)
return
}
if this.GetAccountId() == msg.GetTargetAccountId() {
this.SendMsg(rspMsg.Err(2, "Cannot invite oneself"))
return
}
GetFriendMgr().AsyncApplyFriend(
this.GetAccountId(),
msg.GetTargetAccountId(),
func (errCode int32, errMsg string) {
this.SendMsg(rspMsg.Err(errCode, errMsg))
return
})
}
func (this *player) CMAcceptFriendRequest(hdr *f5.MsgHdr, msg *cs.CMAcceptFriendRequest) {
rspMsg := new(cs.SMAcceptFriendRequest)
if this.GetAccountId() == msg.GetTargetAccountId() {
this.SendMsg(rspMsg.Err(2, "Cannot add oneself"))
return
}
if GetFriendMgr().IsFriend(this.GetAccountId(), msg.GetTargetAccountId()) {
this.SendMsg(rspMsg)
return
}
doFunc := func () {
f5.NewLockAsyncTask(
[][]string{
{constant.MEMBER_LOCK_KEY, this.GetAccountId()},
{constant.MEMBER_LOCK_KEY, msg.GetTargetAccountId()},
},
func (cb *f5.LockAsyncTask) {
GetFriendMgr().AsyncAcceptApply(
this.GetAccountId(),
msg.GetTargetAccountId(),
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg)
})
})
}
f5.GetJsStyleDb().OrmSelectOne(
constant.FRIEND_DB,
"t_friend_apply",
[][]string{
{"sender_id", msg.GetTargetAccountId()},
{"target_id", this.GetAccountId()},
{"status", q5.ToString(constant.FRIEND_APPLY_STATUS_NONE)},
},
func (err error, ds *f5.DataSet) {
if err != nil {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
if ds.Next() {
doFunc()
} else {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
})
}
func (this *player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendRequest) {
rspMsg := new(cs.SMRejectFriendRequest)
if GetFriendMgr().IsFriend(this.GetAccountId(), msg.GetTargetAccountId()) {
this.SendMsg(rspMsg)
return
}
doFunc := func () {
f5.NewLockAsyncTask(
[][]string{
{constant.MEMBER_LOCK_KEY, this.GetAccountId()},
{constant.MEMBER_LOCK_KEY, msg.GetTargetAccountId()},
},
func (cb *f5.LockAsyncTask) {
GetFriendMgr().AsyncRejectApply(
this.GetAccountId(),
msg.GetTargetAccountId(),
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg)
})
})
}
f5.GetJsStyleDb().OrmSelectOne(
constant.FRIEND_DB,
"t_friend_apply",
[][]string{
{"sender_id", msg.GetTargetAccountId()},
{"target_id", this.GetAccountId()},
{"status", q5.ToString(constant.FRIEND_APPLY_STATUS_NONE)},
},
func (err error, ds *f5.DataSet) {
if err != nil {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
if ds.Next() {
doFunc()
} else {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
})
}
func (this *player) CMDeleteFriendShip(hdr *f5.MsgHdr, msg *cs.CMDeleteFriendShip) {
rspMsg := new(cs.SMDeleteFriendShip)
f5.NewLockAsyncTask(
[][]string{
{constant.MEMBER_LOCK_KEY, this.GetAccountId()},
{constant.MEMBER_LOCK_KEY, msg.GetTargetAccountId()},
},
func (cb *f5.LockAsyncTask) {
GetFriendMgr().AsyncDeleteFriend(
this.GetAccountId(),
msg.GetTargetAccountId(),
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg)
})
})
}
func (this *player) CMAddBlacklist(hdr *f5.MsgHdr, msg *cs.CMAddBlacklist) {
rspMsg := new(cs.SMAddBlacklist)
GetFriendMgr().AsyncAddBlack(
this.GetAccountId(),
msg.GetTargetAccountId(),
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg.Err(errCode, errMsg))
})
}
func (this *player) CMRemoveBlacklist(hdr *f5.MsgHdr, msg *cs.CMRemoveBlacklist) {
rspMsg := new(cs.SMRemoveBlacklist)
f5.NewLockAsyncTask(
[][]string{
{constant.MEMBER_LOCK_KEY, this.GetAccountId()},
{constant.MEMBER_LOCK_KEY, msg.GetTargetAccountId()},
},
func (cb *f5.LockAsyncTask) {
GetFriendMgr().AsyncRemoveBlack(
this.GetAccountId(),
msg.GetTargetAccountId(),
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg)
})
})
}
func (this *player) CMInviteFriendMsg(hdr *f5.MsgHdr, msg *cs.CMInviteFriendMsg) {
}
func (this *player) CMSendChatMsg(hdr *f5.MsgHdr, msg *cs.CMSendChatMsg) {
}
func (this *player) CMReadMsgAndOpenChatNotify(hdr *f5.MsgHdr, msg *cs.CMReadMsgAndOpenChatNotify) {
}
func (this *player) CMSetCurrPrivateChatTarget(hdr *f5.MsgHdr, msg *cs.CMSetCurrPrivateChatTarget) {
}
func (this *player) CMGuildInfo(hdr *f5.MsgHdr, msg *cs.CMGuildInfo) {
rspMsg := new(cs.SMGuildInfo)
guild := GetGuildMgr().GetGuildByAccountId(msg.GetAccountId())
if guild == nil {
//this.SendMsg(rspMsg.Err(1, "guild not exists"))
this.SendMsg(rspMsg)
return
}
rspMsg.Guild = new(cs.MFGuild)
guild.AsyncFillMFGuild(rspMsg.Guild,
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg)
})
}
func (this *player) CMRecommendGuildList(hdr *f5.MsgHdr, msg *cs.CMRecommendGuildList) {
rspMsg := new(cs.SMRecommendGuildList)
this.SendMsg(rspMsg)
}
func (this *player) CMGetTopGuildsByTotalStars(hdr *f5.MsgHdr, msg *cs.CMGetTopGuildsByTotalStars) {
}
func (this *player) CMCreateGuild(hdr *f5.MsgHdr, msg *cs.CMCreateGuild) {
rspMsg := new(cs.SMCreateGuild)
GetGuildMgr().AsyncCreateGuild(
this.GetAccountId(),
msg.GetAvatar(),
msg.GetName(),
func (errCode int32, errMsg string, guildId string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(errCode, errMsg))
return
}
guild := GetGuildMgr().GetGuildByGuildId(guildId)
if guild != nil {
rspMsg.Guild = new(cs.MFGuild)
guild.AsyncFillMFGuild(rspMsg.Guild,
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
this.SendMsg(rspMsg)
})
} else {
this.SendMsg(rspMsg.Err(500, "server internal error"))
return
}
})
f5.NewLockAsyncTask(
[][]string{
{constant.GUILD_NAME_LOCK_KEY, msg.GetName()},
{constant.GUILD_MEMBER_LOCK_KEY, this.GetAccountId()},
},
func (task *f5.LockAsyncTask) {
if GetGuildMgr().GetGuildByAccountId(this.GetAccountId()) != nil {
this.SendMsg(rspMsg.Err(3, "You already have a cube"))
return
}
if GetGuildMgr().GetGuildByGuildName(msg.GetName()) != nil {
this.SendMsg(rspMsg.Err(4, "Cube name already exists"))
return
}
params := map[string]string{
"c": "Bag",
"a": "createGuildConsume",
"account_id": this.GetAccountId(),
"session_id": this.GetSessionId(),
}
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
rspObj := new(common.HeadRsp)
f5.GetHttpCliMgr().SendJsStyleJsonRspRequest(
url,
params,
&rspObj,
func(rsp f5.HttpCliResponse) {
})
})
}
func (this *player) CMApplyToGuild(hdr *f5.MsgHdr, msg *cs.CMApplyToGuild) {
}
func (this *player) CMApplyList(hdr *f5.MsgHdr, msg *cs.CMApplyList) {
}
func (this *player) CMApprove(hdr *f5.MsgHdr, msg *cs.CMApprove) {
}
func (this *player) CMReject(hdr *f5.MsgHdr, msg *cs.CMReject) {
}
func (this *player) CMLeaveGuild(hdr *f5.MsgHdr, msg *cs.CMLeaveGuild) {
}
func (this *player) CMDismissMember(hdr *f5.MsgHdr, msg *cs.CMDismissMember) {
}
func (this *player) CMSetMemberLevel(hdr *f5.MsgHdr, msg *cs.CMSetMemberLevel) {
}
func (this *player) CMDisband(hdr *f5.MsgHdr, msg *cs.CMDisband) {
}
func (this *player) CMSetNotice(hdr *f5.MsgHdr, msg *cs.CMSetNotice) {
}
func (this *player) CMSetAvatar(hdr *f5.MsgHdr, msg *cs.CMSetAvatar) {
}
func (this *player) CMSetName(hdr *f5.MsgHdr, msg *cs.CMSetName) {
}
func (this *player) CMSetJoinCond(hdr *f5.MsgHdr, msg *cs.CMSetJoinCond) {
}
func (this *player) CMGuildMembersList(hdr *f5.MsgHdr, msg *cs.CMGuildMembersList) {
}
func (this *player) CMSearchGuilds(hdr *f5.MsgHdr, msg *cs.CMSearchGuilds) {
}
func (this *player) CMGuildLogs(hdr *f5.MsgHdr, msg *cs.CMGuildLogs) {
}
func (this *player) onOffline() {
}
func newPlayer() *player {
p := new(player)
return p
}