center.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. gameType = "game"
  13. )
  14. const (
  15. opsActor = ".ops"
  16. accountActor = ".account"
  17. )
  18. const (
  19. ping = "ping"
  20. registerAccount = "registerAccount"
  21. )
  22. const (
  23. sourcePath = ".system"
  24. )
  25. // Ping 访问center节点,确认center已启动
  26. func Ping(app cfacade.IApplication) bool {
  27. nodeId := GetCenterNodeID(app)
  28. if nodeId == "" {
  29. return false
  30. }
  31. rsp := &pb.Bool{}
  32. targetPath := nodeId + opsActor
  33. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, ping, nil, rsp)
  34. if code.IsFail(errCode) {
  35. return false
  36. }
  37. return rsp.Value
  38. }
  39. // RegisterAccount 注册帐号
  40. func RegisterAccount(app cfacade.IApplication, openID, ip, plt, channel string) *param.LoginResp {
  41. req := &param.LoginReq{
  42. OpenID: openID,
  43. IP: ip,
  44. Platform: plt,
  45. Channel: channel,
  46. }
  47. targetPath := GetTargetPath(app, accountActor)
  48. rsp := &param.LoginResp{}
  49. errCode := app.ActorSystem().CallWait(sourcePath, targetPath, registerAccount, req, rsp)
  50. if code.IsFail(errCode) {
  51. clog.Warnf("[RegisterAccount] openID = %s, errCode = %v", openID, errCode)
  52. return nil
  53. }
  54. return rsp
  55. }
  56. func GetCenterNodeID(app cfacade.IApplication) string {
  57. list := app.Discovery().ListByType(centerType)
  58. if len(list) > 0 {
  59. return list[0].GetNodeId()
  60. }
  61. return ""
  62. }
  63. func GetTargetPath(app cfacade.IApplication, actorID string) string {
  64. nodeId := GetCenterNodeID(app)
  65. return nodeId + actorID
  66. }