aozhiwei e57b3051f5 1
2024-10-16 17:27:50 +08:00

669 lines
17 KiB
Go

package player
import (
"cs"
"q5"
"f5"
"github.com/golang/protobuf/proto"
"main/common"
"main/constant"
. "main/global"
"fmt"
"encoding/json"
"main/mt"
)
type hero struct {
heroUniId string
heroId int32
quality int32
specSkill int32
wealth int32
validLeftTime int32
currentTimes int32
totalTimes int32
skinId int32
}
type player struct {
cs.MsgHandlerImpl
socket f5.WspCliConn
accountId string
sessionId string
zoneId int32
nodeId int32
name string
avatarUrl string
headFrame string
state int32
isReady int32
permission int32
admissionItemNum int32
circuitScore int32
totalLucky int64
hero hero
team common.Team
battling bool
sortIdx int32
vipLv int32
vipExp int64
hasVipLucky int32
hasLuckySymbol int32
battleItems []*common.BattleItem
}
func (this *player) SendMsg(rspMsg proto.Message) {
if this.socket.IsValid() {
GetWspListener().SendProxyMsg(this.socket.Conn, this.socket.SocketHandle, rspMsg)
}
}
func (this *player) parseUserInfo(userInfo *common.UserInfo){
this.name = userInfo.Name
this.avatarUrl = userInfo.HeadId
this.headFrame = userInfo.HeadFrame
q5.DuckToSimple(userInfo.TotalLucky, &this.totalLucky)
q5.DuckToSimple(userInfo.AdmissionItemNum, &this.admissionItemNum)
q5.DuckToSimple(userInfo.CircuitScore, &this.circuitScore)
q5.DuckToSimple(userInfo.VipLv, &this.vipLv)
q5.DuckToSimple(userInfo.VipExp, &this.vipExp)
q5.DuckToSimple(userInfo.HasVipLucky, &this.hasVipLucky)
q5.DuckToSimple(userInfo.HasLuckySymbol, &this.hasLuckySymbol)
}
func (this *player) parseHeroInfo(heroInfo *common.HeroInfo){
this.hero.heroUniId = heroInfo.HeroUniId
this.hero.heroId = q5.ToInt32(heroInfo.HeroId)
this.hero.quality = q5.ToInt32(heroInfo.Quality)
this.hero.specSkill = q5.ToInt32(heroInfo.SpecSkill)
q5.DuckToSimple(heroInfo.Wealth, &this.hero.wealth)
q5.DuckToSimple(heroInfo.ValidLefttime, &this.hero.validLeftTime)
q5.DuckToSimple(heroInfo.CurrentTimes, &this.hero.currentTimes)
q5.DuckToSimple(heroInfo.TotalTimes, &this.hero.totalTimes)
q5.DuckToSimple(heroInfo.SkinId, &this.hero.skinId)
}
func (this *player) init(req *pendingLoginRequest, rsp *common.LoginRsp){
this.socket = req.hdr.GetSocket()
this.accountId = req.msg.GetAccountId()
this.sessionId = req.msg.GetSessionId()
this.zoneId = req.msg.GetZoneId()
this.nodeId = req.msg.GetNodeId()
this.parseUserInfo(&rsp.Info.UserInfo)
this.parseHeroInfo(&rsp.Info.HeroInfo)
}
func (this *player) againInit(req *pendingLoginRequest, rsp *common.LoginRsp){
this.socket = req.hdr.GetSocket()
this.accountId = req.msg.GetAccountId()
this.sessionId = req.msg.GetSessionId()
this.zoneId = req.msg.GetZoneId()
this.nodeId = req.msg.GetNodeId()
this.parseUserInfo(&rsp.Info.UserInfo)
this.parseHeroInfo(&rsp.Info.HeroInfo)
}
func (this *player) GetSocket() f5.WspCliConn {
return this.socket
}
func (this *player) onOffline(){
this.socket.Reset()
this.GetTeam().Leave(this)
}
func (this *player) reBind(socket f5.WspCliConn, sessionId string) {
if this.socket.IsValid() {
delete(_playerMgr.socketHash, this.socket)
}
this.socket = socket
this.sessionId = sessionId
_playerMgr.socketHash[this.socket] = this
}
func (this *player) GetAccountId() string {
return this.accountId
}
func (this *player) GetSessionId() string {
return this.sessionId
}
func (this *player) GetName() string {
return this.name
}
func (this *player) GetAvatarUrl() string {
return this.avatarUrl
}
func (this *player) GetHeadFrame() string {
return this.headFrame
}
func (this *player) GetZoneId() int32 {
return this.zoneId
}
func (this *player) GetNodeId() int32 {
return this.nodeId
}
func (this *player) IsOnline() bool {
return this.socket.IsValid()
}
func (this *player) IsReady() bool {
return this.isReady != 0
}
func (this *player) GetSpecSkill() int32 {
return this.hero.specSkill
}
func (this *player) GetHeroUniid() string {
return this.hero.heroUniId
}
func (this *player) GetPermission() int32 {
return this.permission
}
func (this *player) SetPermission(permission int32) {
this.permission = permission
}
func (this *player) GetTeam() common.Team {
return this.team
}
func (this *player) SetTeam(team common.Team) {
this.team = team
}
func (this *player) GetSortIdx() int32 {
return this.sortIdx
}
func (this *player) SetSortIdx(sortIdx int32) {
this.sortIdx = sortIdx
}
func (this *player) GenNextCopy() common.Player {
nextCopy := newPlayer()
//nextCopy.socket
nextCopy.accountId = this.accountId
nextCopy.sessionId = this.sessionId
nextCopy.zoneId = this.zoneId
nextCopy.nodeId = this.nodeId
nextCopy.name = this.name
nextCopy.avatarUrl = this.avatarUrl
nextCopy.headFrame = this.headFrame
nextCopy.state = 0
nextCopy.isReady = 0
nextCopy.permission = 0
nextCopy.hero = this.hero
nextCopy.team = nil
nextCopy.sortIdx = 0
return nextCopy
}
func (this *player) GetBattleItems() []*common.BattleItem {
var result = this.battleItems[0:]
if this.isLuckySymbolMap() {
p := new(common.BattleItem)
p.ItemId = constant.LUCKY_SYMBOL_ITEM_ID
p.ItemNum = 1
p.ItemType = constant.FUNC_TYPE_ITEM_TYPE
p.ItemSubType = constant.LUCKY_SYMBOL_ITEM_SUB_TYPE
q5.AppendSlice(&result, p)
}
return result
}
func (this *player) FillMFTeamMember(member_pb *cs.MFTeamMember) {
member_pb.AccountId = proto.String(this.accountId)
member_pb.Name = proto.String(this.name)
member_pb.Id = proto.Int32(this.sortIdx)
if this.GetTeam().IsLeader(this) {
member_pb.IsLeader = proto.Int32(1)
} else {
member_pb.IsLeader = proto.Int32(0)
}
member_pb.SpecSkill = proto.Int32(this.hero.specSkill)
{
member_pb.Hero = &cs.MFHero{}
member_pb.Hero.HeroId = proto.Int32(this.hero.heroId)
member_pb.Hero.Quality = proto.Int32(this.hero.quality)
member_pb.Hero.CurrentTimes = proto.Int32(this.hero.currentTimes)
member_pb.Hero.TotalTimes = proto.Int32(this.hero.totalTimes)
member_pb.Hero.SkinId = proto.Int32(this.hero.skinId)
}
member_pb.State = proto.Int32(this.state)
if this.IsOnline() {
member_pb.Online = proto.Int32(1)
} else {
member_pb.Online = proto.Int32(0)
}
member_pb.Permission = proto.Int32(this.permission)
member_pb.IsReady = proto.Int32(this.isReady)
member_pb.Wealth = proto.Int32(this.hero.wealth)
member_pb.TotalLucky = proto.Int64(this.totalLucky)
member_pb.ValidLefttime = proto.Int32(this.hero.validLeftTime)
member_pb.AdmissionItemNum = proto.Int32(this.admissionItemNum)
member_pb.CircuitScore = proto.Int32(this.circuitScore)
member_pb.VipLv = proto.Int32(this.vipLv)
member_pb.VipExp = proto.Int64(this.vipExp)
member_pb.HasVipLucky = proto.Int32(this.hasVipLucky)
q5.NewSlice(&member_pb.BattleItems, 0, 0)
for _, p := range(this.battleItems) {
item_pb := new(cs.MFBattleItem)
item_pb.ItemId = proto.Int32(p.ItemId)
item_pb.ItemNum = proto.Int32(p.ItemNum)
q5.AppendSlice(&member_pb.BattleItems, item_pb)
}
if this.hasLuckySymbol != 0 && this.isLuckySymbolMap() {
item_pb := new(cs.MFBattleItem)
item_pb.ItemId = proto.Int32(constant.LUCKY_SYMBOL_ITEM_ID)
item_pb.ItemNum = proto.Int32(1)
q5.AppendSlice(&member_pb.BattleItems, item_pb)
}
}
func (this *player) CMLeaveTeam(hdr *f5.MsgHdr, msg *cs.CMLeaveTeam) {
rspMsg := &cs.SMLeaveTeam{}
team := this.GetTeam()
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
this.SendMsg(rspMsg)
return
} else {
this.GetTeam().Leave(this)
}
this.SendMsg(rspMsg)
if team.GetMemberNum() > 0 {
team.SendUpdateNotify()
}
}
func (this *player) CMDisbandTeam(hdr *f5.MsgHdr, msg *cs.CMDisbandTeam) {
rspMsg := &cs.SMDisbandTeam{}
team := this.GetTeam()
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
} else {
this.GetTeam().Disband()
}
this.SendMsg(rspMsg)
team.SendUpdateNotify()
}
func (this *player) CMKickOut(hdr *f5.MsgHdr, msg *cs.CMKickOut) {
rspMsg := &cs.SMKickOut{}
team := this.GetTeam()
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
} else {
this.GetTeam().KickOut(this, msg.GetTargetId())
}
this.SendMsg(rspMsg)
team.SendUpdateNotify()
}
func (this *player) CMHandoverLeader(hdr *f5.MsgHdr, msg *cs.CMHandoverLeader) {
rspMsg := &cs.SMHandoverLeader{}
team := this.GetTeam()
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
} else {
this.GetTeam().HandoverLeader(this, msg.GetTargetId())
}
this.SendMsg(rspMsg)
team.SendUpdateNotify()
}
func (this *player) CMStartGame(hdr *f5.MsgHdr, msg *cs.CMStartGame) {
rspMsg := &cs.SMStartGame{}
if !this.GetTeam().IsLock() {
if this.GetTeam().CanStartGame(this) {
if GetHistoryMgr().CanStart(this.GetAccountId(),
this.GetTeam().GetMapId(),
this.GetTeam().GetModeId()) {
this.GetTeam().StartGame()
}
}
}
this.SendMsg(rspMsg)
}
func (this *player) CMCancel(hdr *f5.MsgHdr, msg *cs.CMCancel) {
rspMsg := &cs.SMCancel{}
if this.GetTeam().Started() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
} else {
this.GetTeam().CancelMatch()
}
this.SendMsg(rspMsg)
this.GetTeam().SendUpdateNotify()
this.GetTeam().SendStateNotify()
}
func (this *player) CMSetReady(hdr *f5.MsgHdr, msg *cs.CMSetReady) {
rspMsg := &cs.SMSetReady{}
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
} else {
if msg.GetIsReady() != 0 {
this.isReady = 1
} else {
this.isReady = 0
}
}
this.SendMsg(rspMsg)
this.GetTeam().SendUpdateNotify()
}
func (this *player) CMSetSpecSkill(hdr *f5.MsgHdr, msg *cs.CMSetSpecSkill) {
rspMsg := &cs.SMSetSpecSkill{}
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
return
} else {
this.updateChoose(
msg.GetSkillId(),
this.hero.heroUniId,
func (errCode int32, errMsg string) {
if errCode == 0 {
this.SendMsg(rspMsg)
if this.GetTeam() != nil {
this.GetTeam().SendUpdateNotify()
}
} else {
rspMsg.Errcode = proto.Int32(2)
rspMsg.Errmsg = proto.String("server internal error")
this.SendMsg(rspMsg)
}
})
}
}
func (this *player) CMChooseHero(hdr *f5.MsgHdr, msg *cs.CMChooseHero) {
rspMsg := &cs.SMChooseHero{}
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
this.SendMsg(rspMsg)
return
} else {
this.updateChoose(
this.hero.specSkill,
msg.GetHeroUniid(),
func (errCode int32, errMsg string) {
if errCode == 0 {
this.SendMsg(rspMsg)
if this.GetTeam() != nil {
this.GetTeam().SendUpdateNotify()
}
} else {
rspMsg.Errcode = proto.Int32(2)
rspMsg.Errmsg = proto.String("server internal error")
this.SendMsg(rspMsg)
}
})
}
}
func (this *player) CMChooseMap(hdr *f5.MsgHdr, msg *cs.CMChooseMap) {
rspMsg := &cs.SMChooseMap{}
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
this.SendMsg(rspMsg)
} else {
this.updateMap(
msg.GetModeId(),
msg.GetMapId(),
func (errCode int32, errMsg string) {
if errCode == 0 {
this.SendMsg(rspMsg)
if this.GetTeam() != nil {
this.GetTeam().SendUpdateNotify()
}
} else {
rspMsg.Errcode = proto.Int32(errCode)
rspMsg.Errmsg = proto.String(errMsg)
this.SendMsg(rspMsg)
}
})
}
}
func (this *player) CMRefreshUser(hdr *f5.MsgHdr, msg *cs.CMRefreshUser) {
rspMsg := new(cs.SMRefreshUser)
params := map[string]string{
"c": "User",
"a": "getBattleUser",
"account_id": this.GetAccountId(),
"session_id": this.GetSessionId(),
}
rspObj := common.UserRsp{}
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
f5.GetHttpCliMgr().SendJsStyleJsonRspRequest(
url,
params,
&rspObj,
func(rsp f5.HttpCliResponse) {
this.SendMsg(rspMsg)
if rsp.GetErr() == nil && rsp.JsonParseOk() {
this.parseUserInfo(&rspObj.Info.UserInfo)
this.parseHeroInfo(&rspObj.Info.HeroInfo)
if this.GetTeam() != nil {
this.GetTeam().SendUpdateNotify()
}
}
})
}
func (this *player) updateChoose(skillId int32, heroUniId string,
cb func (int32, string)) {
params := map[string]string{
"c": "User",
"a": "updateBattleInfo",
"account_id": this.accountId,
"session_id": this.sessionId,
"skill_id": q5.ToString(skillId),
"hero_uniid": heroUniId,
}
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
f5.GetHttpCliMgr().SendJsStyleRequest(
url,
params,
func(rsp f5.HttpCliResponse) {
if this.GetTeam() != nil && !this.GetTeam().IsLock() {
if rsp.GetErr() != nil {
cb(500, "server internal error")
return
}
}
rspObj := common.UpdateBattleInfoRsp{}
{
f5.GetSysLog().Info("updateBattleinfo:%s", rsp.GetRawData())
err := json.Unmarshal([]byte(rsp.GetRawData()), &rspObj)
if err != nil {
cb(500, "server internal error")
return
}
{
this.parseUserInfo(&rspObj.Info.UserInfo)
this.parseHeroInfo(&rspObj.Info.HeroInfo)
cb(0, "")
}
}
})
}
func (this *player) updateMap(modeId int32, mapId int32,
cb func (int32, string)) {
params := map[string]string{
"c": "User",
"a": "getMapConf",
"account_id": this.accountId,
"session_id": this.sessionId,
"mode_id": q5.ToString(modeId),
"map_id": q5.ToString(mapId),
}
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
f5.GetHttpCliMgr().SendJsStyleRequest(
url,
params,
func(rsp f5.HttpCliResponse) {
if this.GetTeam() != nil && !this.GetTeam().IsLock() {
if rsp.GetErr() != nil {
cb(500, "server internal error")
return
}
}
rspObj := common.MapInfoRsp{}
{
f5.GetSysLog().Info("updateMap:%s", rsp.GetRawData())
err := json.Unmarshal([]byte(rsp.GetRawData()), &rspObj)
if err != nil {
cb(500, "server internal error")
return
}
if rspObj.MapId == 0 {
cb(1, "map error")
return
}
if rspObj.IsOpen == 0 {
cb(2, "map no open")
return
}
{
this.GetTeam().SetMapInfo(&rspObj)
cb(0, "")
}
}
})
}
func (this *player) CMGrantInvitePermission(hdr *f5.MsgHdr, msg *cs.CMGrantInvitePermission) {
rspMsg := &cs.SMGrantInvitePermission{}
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
} else {
target := this.GetTeam().GetMemberByAccountId(msg.GetTargetId())
if target != nil {
target.(*player).permission = 1
}
}
this.SendMsg(rspMsg)
this.GetTeam().SendUpdateNotify()
}
func (this *player) CMChooseBattleItem(hdr *f5.MsgHdr, msg *cs.CMChooseBattleItem) {
rspMsg := &cs.SMChooseBattleItem{}
if this.GetTeam().IsLock() {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
this.SendMsg(rspMsg)
return
}
params := map[string]string{
"c": "User",
"a": "getBattleItem",
"account_id": this.accountId,
"session_id": this.sessionId,
"item_id": q5.ToString(*msg.ItemId),
}
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
f5.GetHttpCliMgr().SendJsStyleRequest(
url,
params,
func(rsp f5.HttpCliResponse) {
f5.GetSysLog().Info("getBattleItem:%s", rsp.GetRawData())
if this.GetTeam() != nil && !this.GetTeam().IsLock() {
if rsp.GetErr() != nil {
rspMsg.Errcode = proto.Int32(1)
rspMsg.Errmsg = proto.String("team already started")
this.SendMsg(rspMsg)
return
}
}
rspObj := struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
ItemId interface{} `json:"item_id"`
ItemNum interface{} `json:"item_num"`
ItemType interface{} `json:"item_type"`
ItemSubType interface{} `json:"item_sub_type"`
HasLuckySymbol interface{} `json:"has_lucky_symbol"`
}{}
err := json.Unmarshal([]byte(rsp.GetRawData()), &rspObj)
if err != nil || rspObj.Errcode != 0 {
rspMsg.Errcode = proto.Int32(500)
rspMsg.Errmsg = proto.String("server internal error")
this.SendMsg(rspMsg)
return
}
var itemId int32
var itemNum int32
var itemType int32
var itemSubType int32
q5.DuckToSimple(rspObj.ItemId, &itemId)
q5.DuckToSimple(rspObj.ItemNum, &itemNum)
q5.DuckToSimple(rspObj.ItemType, &itemType)
q5.DuckToSimple(rspObj.ItemSubType, &itemSubType)
q5.DuckToSimple(rspObj.HasLuckySymbol, &this.hasLuckySymbol)
if itemNum > 0 {
found := false
for _, p := range(this.battleItems) {
if p.ItemType == itemType && p.ItemSubType == itemSubType {
p.ItemId = itemId
p.ItemNum = itemNum
p.ItemType = itemType
p.ItemSubType = itemSubType
found = true
break
}
}
if !found && itemId != constant.LUCKY_SYMBOL_ITEM_ID {
p := new(common.BattleItem)
p.ItemId = itemId
p.ItemNum = itemNum
p.ItemType = itemType
p.ItemSubType = itemSubType
q5.AppendSlice(&this.battleItems, p)
}
}
this.SendMsg(rspMsg)
this.GetTeam().SendUpdateNotify()
})
}
func (this *player) IsBattling() bool {
return this.battling
}
func (this *player) SetBattling(state bool) {
this.battling = state
}
func (this *player) isLuckySymbolMap() bool {
return this.GetTeam().GetModeId() == 4
}
func newPlayer() *player {
hum := new(player)
q5.NewSlice(&hum.battleItems, 0, 2)
return hum
}