center.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package rpcCenter
  2. import (
  3. cfacade "github.com/mhaya/facade"
  4. "github.com/mhaya/game/game_cluster/internal/code"
  5. "github.com/mhaya/game/game_cluster/internal/param"
  6. "github.com/mhaya/game/game_cluster/internal/pb"
  7. clog "github.com/mhaya/logger"
  8. )
  9. // route = 节点类型.节点handler.remote函数
  10. const (
  11. centerType = "center"
  12. )
  13. const (
  14. opsActor = ".ops"
  15. accountActor = ".account"
  16. )
  17. const (
  18. ping = "ping"
  19. registerAccount = "registerAccount"
  20. )
  21. const (
  22. sourcePath = ".system"
  23. )
  24. // Ping 访问center节点,确认center已启动
  25. func Ping(app cfacade.IApplication) bool {
  26. nodeId := GetCenterNodeID(app)
  27. if nodeId == "" {
  28. return false
  29. }
  30. rsp := &pb.Bool{}
  31. targetPath := nodeId + opsActor
  32. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, ping, nil, rsp)
  33. if code.IsFail(errCode) {
  34. return false
  35. }
  36. return rsp.Value
  37. }
  38. // RegisterAccount 注册帐号
  39. func RegisterAccount(app cfacade.IApplication, openID, ip string) *param.LoginResp {
  40. req := &param.LoginReq{
  41. OpenID: openID,
  42. IP: ip,
  43. }
  44. targetPath := GetTargetPath(app, accountActor)
  45. rsp := &param.LoginResp{}
  46. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, registerAccount, req, rsp)
  47. if code.IsFail(errCode) {
  48. clog.Warnf("[RegisterAccount] openID = %s, errCode = %v", openID, errCode)
  49. return nil
  50. }
  51. return rsp
  52. }
  53. func GetCenterNodeID(app cfacade.IApplication) string {
  54. list := app.Discovery().ListByType(centerType)
  55. if len(list) > 0 {
  56. return list[0].GetNodeId()
  57. }
  58. return ""
  59. }
  60. func GetTargetPath(app cfacade.IApplication, actorID string) string {
  61. nodeId := GetCenterNodeID(app)
  62. return nodeId + actorID
  63. }