diff --git a/common/rpc/rpcGameUser.go b/common/rpc/rpcGameUser.go new file mode 100644 index 0000000..56fcec0 --- /dev/null +++ b/common/rpc/rpcGameUser.go @@ -0,0 +1,41 @@ +package rpc + +import ( + "encoding/json" + "game/common/model/user" + "game/common/proto/pb" + "game/common/userBindService" + "github.com/fox/fox/ipb" + "github.com/fox/fox/log" + "github.com/fox/fox/service" + "time" +) + +const ( + timeout = time.Second * 30 +) + +// 获取玩家数据 +func RpcGetGameUser(bindService *userBindService.UserBindService, s service.IService, uid int64) (*user.GameUser, pb.ErrCode) { + node, err := bindService.HashServiceNode(pb.ServiceTypeId_STI_DB, uid) + if err != nil { + log.ErrorF("db service node error:%v", err) + return nil, pb.ErrCode_SystemErr + } + us := &user.GameUser{ + User: user.User{ + ID: uid, + }, + UserResources: user.UserResources{ + UID: uid, + }, + } + rpcMsg := ipb.MakeRpcMsg[user.GameUser](GetGameUser, uid, us) + rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg) + if err != nil { + log.ErrorF("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error()) + return nil, pb.ErrCode_SystemErr + } + _ = json.Unmarshal(rspMsg.Msg, us) + return us, pb.ErrCode_OK +} diff --git a/common/rpcName/rpcName.go b/common/rpc/rpcName.go similarity index 96% rename from common/rpcName/rpcName.go rename to common/rpc/rpcName.go index 09b285b..9f211ad 100644 --- a/common/rpcName/rpcName.go +++ b/common/rpc/rpcName.go @@ -1,4 +1,4 @@ -package rpcName +package rpc const ( // 用户、用户资源及帐号相关操作 diff --git a/server/colorgame/room/colorPlayer.go b/server/colorgame/room/colorPlayer.go index 67eb20e..d1212ba 100644 --- a/server/colorgame/room/colorPlayer.go +++ b/server/colorgame/room/colorPlayer.go @@ -6,11 +6,21 @@ import ( ) type ColorPlayer struct { - user.User + *user.User + *user.UserResources gateTopicName string roomId int } +func NewColorPlayer(gateTopicName string, roomId int, u *user.User, res *user.UserResources) *ColorPlayer { + return &ColorPlayer{ + User: u, + UserResources: res, + gateTopicName: gateTopicName, + roomId: roomId, + } +} + func (p *ColorPlayer) Id() int64 { return p.ID } diff --git a/server/colorgame/server/processor.go b/server/colorgame/server/processor.go index 1b68f17..d589052 100644 --- a/server/colorgame/server/processor.go +++ b/server/colorgame/server/processor.go @@ -1,7 +1,14 @@ package server import ( + "game/common/model/user" + "game/common/proto/pb" + "game/common/rpc" + "github.com/fox/fox/ipb" + "github.com/fox/fox/ksync" "github.com/fox/fox/processor" + "github.com/fox/fox/service" + "time" ) const ( @@ -10,6 +17,46 @@ const ( func (s *ColorService) initProcessor() { s.processor.RegisterMessages(processor.RegisterMetas{ - //pb.MsgId_C2SUserLoginId: {pb.C2SUserLogin{}, s.onLoginOrRegister}, + pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, s.onLoginOrRegister}, }) } + +// 登录或注册 +func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) { + ksync.GoSafe(func() { + account, code, node := s.checkLoginOrRegister(req) + userId := int64(0) + rsp := &pb.S2CUserLogin{Code: code} + if account != nil && code == pb.ErrCode_OK { + // 拉取用户数据 + us := &user.User{} + us, code = s.getUser(userId, req.Username) + if code == pb.ErrCode_OK { + rsp.UserId = us.ID + rsp.Token, _ = generateToken(account.ID, account.Username) + userId = rsp.UserId + } + } + s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, userId, int32(pb.MsgId_S2CUserLoginId), rsp) + + if account != nil && account.ID > 0 { + loginLog := &user.UserLoginLog{ + AccountID: account.ID, + LoginIP: req.Ip, + LoginTime: time.Now(), + DeviceInfo: req.DeviceId, + LoginResult: code == pb.ErrCode_OK, + FailReason: code.String(), + } + switch code { + case pb.ErrCode_LoginUserOrPwdErr, pb.ErrCode_OK: + rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpc.LogUserAccountLogin, 0, loginLog) + ksync.GoSafe(func() { + _, _ = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg) + }, nil) + + } + } + + }, nil) +} diff --git a/server/db/server/processor.go b/server/db/server/processor.go index 5d2201b..c8b0057 100644 --- a/server/db/server/processor.go +++ b/server/db/server/processor.go @@ -1,19 +1,19 @@ package server import ( - "game/common/rpcName" + "game/common/rpc" "github.com/fox/fox/processor" ) func (s *DbService) initRpcProcessor() { s.RpcProcessor.RegisterMessages(map[string]processor.RpcHandler{ - rpcName.CreateUserAccount: s.onCreateUserAccount, - rpcName.GetUserAccount: s.onGetUserAccount, - rpcName.UpdateUserPassword: s.onUpdateUserAccount, - rpcName.LogUserAccountLogin: s.onLogUserAccountLogin, - rpcName.GetUserByUid: s.onGetUserByUid, - rpcName.GetUserByAccountId: s.onGetUserByAccountId, - rpcName.GetUserResources: s.onGetUserResources, - rpcName.GetGameUser: s.onGetGameUser, + rpc.CreateUserAccount: s.onCreateUserAccount, + rpc.GetUserAccount: s.onGetUserAccount, + rpc.UpdateUserPassword: s.onUpdateUserAccount, + rpc.LogUserAccountLogin: s.onLogUserAccountLogin, + rpc.GetUserByUid: s.onGetUserByUid, + rpc.GetUserByAccountId: s.onGetUserByAccountId, + rpc.GetUserResources: s.onGetUserResources, + rpc.GetGameUser: s.onGetGameUser, }) } diff --git a/server/login/server/processor.go b/server/login/server/processor.go index f8ff444..4f542b7 100644 --- a/server/login/server/processor.go +++ b/server/login/server/processor.go @@ -4,7 +4,7 @@ import ( "encoding/json" "game/common/model/user" "game/common/proto/pb" - "game/common/rpcName" + "game/common/rpc" "game/common/utils" "github.com/fox/fox/etcd" "github.com/fox/fox/ipb" @@ -41,7 +41,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User DeviceID: req.DeviceId, LastLoginIP: req.Ip, } - rpcMsg := ipb.MakeRpcMsg[user.UserAccount](rpcName.GetUserAccount, 0, us) + rpcMsg := ipb.MakeRpcMsg[user.UserAccount](rpc.GetUserAccount, 0, us) rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg) if err != nil { log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error())) @@ -60,7 +60,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User RegisterIP: req.Ip, RegisterTime: time.Now(), } - rpcMsg = ipb.MakeRpcMsg[user.UserAccount](rpcName.CreateUserAccount, 0, us) + rpcMsg = ipb.MakeRpcMsg[user.UserAccount](rpc.CreateUserAccount, 0, us) rspMsg, err = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg) if err != nil { log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error())) @@ -71,7 +71,7 @@ func (s *LoginService) checkLoginOrRegister(req *pb.C2SUserLogin) (us *user.User log.ErrorF(s.Log("call rpc:%v err", rpcMsg.RpcMsgId)) return nil, pb.ErrCode_SystemErr, node } - log.DebugF("收到rcp:%v返回数据:%v", rpcName.CreateUserAccount, string(rspMsg.Msg)) + log.DebugF("收到rcp:%v返回数据:%v", rpc.CreateUserAccount, string(rspMsg.Msg)) } if !utils.CheckPassword(req.Password, us.Password) { log.ErrorF(s.Log("用户密码:%v 数据库中密码:%v", req.Password, us.Password)) @@ -101,7 +101,7 @@ func (s *LoginService) getUser(accountId int64, tName string) (*user.User, pb.Er us := &user.User{ AccountId: accountId, } - rpcMsg := ipb.MakeRpcMsg[user.User](rpcName.GetUserByAccountId, 0, us) + rpcMsg := ipb.MakeRpcMsg[user.User](rpc.GetUserByAccountId, 0, us) rsp, err := s.Call(service.RpcTopicEx(tName), timeout, rpcMsg) if err != nil { log.ErrorF(s.Log("call rpc:%v err:%s", rpcMsg.RpcMsgId, err.Error())) @@ -144,7 +144,7 @@ func (s *LoginService) onLoginOrRegister(iMsg *ipb.InternalMsg, req *pb.C2SUserL } switch code { case pb.ErrCode_LoginUserOrPwdErr, pb.ErrCode_OK: - rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpcName.LogUserAccountLogin, 0, loginLog) + rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpc.LogUserAccountLogin, 0, loginLog) ksync.GoSafe(func() { _, _ = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg) }, nil) diff --git a/server/match/server/match.go b/server/match/server/match.go index b35cc8a..708abf2 100644 --- a/server/match/server/match.go +++ b/server/match/server/match.go @@ -1,10 +1,9 @@ package server import ( - "encoding/json" "game/common/model/user" "game/common/proto/pb" - "game/common/rpcName" + "game/common/rpc" "game/common/utils" "github.com/fox/fox/ipb" "github.com/fox/fox/ksync" @@ -12,30 +11,6 @@ import ( "github.com/fox/fox/service" ) -func (s *MatchService) getGameUser(uid int64) (*user.GameUser, pb.ErrCode) { - node, err := s.bindService.HashServiceNode(pb.ServiceTypeId_STI_DB, uid) - if err != nil { - log.ErrorF("db service node error:%v", err) - return nil, pb.ErrCode_SystemErr - } - us := &user.GameUser{ - User: user.User{ - ID: uid, - }, - UserResources: user.UserResources{ - UID: uid, - }, - } - rpcMsg := ipb.MakeRpcMsg[user.GameUser](rpcName.GetGameUser, uid, us) - rspMsg, err := s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg) - if err != nil { - log.ErrorF(s.Log("call rpc:%v err:%s ", rpcMsg.RpcMsgId, err.Error())) - return nil, pb.ErrCode_SystemErr - } - _ = json.Unmarshal(rspMsg.Msg, us) - return us, pb.ErrCode_OK -} - // 匹配房间 func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) { ksync.GoSafe(func() { @@ -56,7 +31,7 @@ func (s *MatchService) onMatchRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) var us *user.GameUser rsp := &pb.S2CMatchRoom{} - us, rsp.Code = s.getGameUser(iMsg.UserId) + us, rsp.Code = rpc.RpcGetGameUser(s.bindService, s, iMsg.UserId) if rsp.Code != pb.ErrCode_OK { s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId, int32(pb.MsgId_S2CMatchRoomId), rsp) return