inter_db.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package interaction
  2. import (
  3. "call_center/call/rpc/internal/role"
  4. "call_center/db/rpc/pb"
  5. "call_center/public/exception"
  6. "context"
  7. "encoding/json"
  8. "google.golang.org/grpc/metadata"
  9. "log"
  10. "time"
  11. )
  12. /*
  13. 数据库交互类
  14. */
  15. type (
  16. InterDb interface {
  17. Start()
  18. ChatLogToDb(player *role.Player, playerId string, serviceId string, content string, fromPlayer bool)
  19. ChatRecordToDb(player *role.Player, serviceId string, playerId string, state pb.EDbRecordState)
  20. GetChatRecord(serviceId string) []*pb.DbChatRecord
  21. GetChatLog(playerId string) []*pb.DbChatLog
  22. }
  23. interDb struct {
  24. client pb.DbClient
  25. dbStream pb.Db_DbLoginClient
  26. }
  27. )
  28. func NewInterDb(cli pb.DbClient) InterDb {
  29. log.Println("<NewInterDb> cli:", cli)
  30. dbInst := &interDb{client: cli}
  31. return dbInst
  32. }
  33. func (sel *interDb) Start() {
  34. log.Println("<Start> client:", sel.client)
  35. duration := time.Second * 5
  36. ticker := time.NewTicker(duration)
  37. for range ticker.C {
  38. exception.Try(func() {
  39. sel.runDb()
  40. }).Catch(func(e exception.Exception) {
  41. }).Finally(func() {
  42. })
  43. }
  44. }
  45. func (sel *interDb) runDb() {
  46. ctx := context.Background()
  47. // 赋值client
  48. dbClient := sel.client
  49. // context塞入信息
  50. md := metadata.Pairs("Key", "Val")
  51. ctx = metadata.NewOutgoingContext(ctx, md)
  52. stream, err := dbClient.DbLogin(ctx)
  53. if err != nil {
  54. // log.Println(err)
  55. return
  56. }
  57. log.Printf("<inter_db.runDB> db连接成功, 地址:%v", &dbClient)
  58. // 赋值stream
  59. sel.dbStream = stream
  60. // 获取配置信息
  61. req := new(pb.DbMsgReq)
  62. cmd := new(pb.DbCommandMsg)
  63. cmd.CmdType = pb.EDbCommand_E_DB_COMMAND_GET_CONFIG
  64. req.Cmd = cmd
  65. if res, err := dbClient.DbCall(ctx, req); err == nil {
  66. arrList := res.GetCmd().GetArrayConfig().GetDataList()
  67. for _, conf := range arrList {
  68. var confValueMap map[string]interface{}
  69. confName := conf.ConfName
  70. confKey := conf.ConfKey
  71. err := json.Unmarshal([]byte(conf.ConfValue), &confValueMap)
  72. if err != nil {
  73. continue
  74. }
  75. log.Println(confName, confKey, confValueMap)
  76. }
  77. log.Println(arrList)
  78. }
  79. for {
  80. receive, err := stream.Recv()
  81. if err != nil {
  82. sel.dbStream = nil
  83. log.Println("<inter_db.runDB> conn broken, err:", err)
  84. break
  85. }
  86. //res := receive.GetCmd()
  87. //switch res.CmdType {
  88. //
  89. //}
  90. log.Println("<inter_db.runDB> begin receive...:", receive)
  91. }
  92. log.Println("<inter_db.runDB> dbClient stopped, begin to reconnect...")
  93. }
  94. func (sel *interDb) send(cmd *pb.DbCommandMsg) {
  95. if sel.dbStream == nil {
  96. log.Println("<interDb.send> dbStream is nil")
  97. return
  98. }
  99. err := sel.dbStream.Send(&pb.DbMsgReq{Cmd: cmd})
  100. if err != nil {
  101. log.Println("<interDb.send> Send err:", err)
  102. return
  103. }
  104. }
  105. func (sel *interDb) ChatLogToDb(player *role.Player, playerId string, serviceId string, content string, fromPlayer bool) {
  106. if sel.dbStream == nil {
  107. log.Println("<interDb.ChatLogToDb> InterDb not init")
  108. }
  109. exception.Try(func() {
  110. chatInfo := new(pb.DbChatLog)
  111. chatInfo.Content = content
  112. chatInfo.TimeStamp = time.Now().Unix()
  113. chatInfo.SessionId = player.SessionId
  114. chatInfo.IsVisitor = player.IsVisitor
  115. chatInfo.GameId = player.GameId
  116. if fromPlayer == true {
  117. chatInfo.ChatType = pb.EDbChatType_E_DB_CHAT_TYPE_PLAYER
  118. chatInfo.FromId = playerId
  119. chatInfo.ToId = serviceId
  120. } else {
  121. chatInfo.ChatType = pb.EDbChatType_E_DB_CHAT_TYPE_SERVICE
  122. chatInfo.FromId = serviceId
  123. chatInfo.ToId = playerId
  124. }
  125. cmd := new(pb.DbCommandMsg)
  126. cmd.CmdType = pb.EDbCommand_E_DB_COMMAND_PUSH_CHAT_LOG
  127. cmd.Data = &pb.DbCommandMsg_ChatLog{ChatLog: chatInfo}
  128. sel.send(cmd)
  129. log.Println("<interDb.ChatLogToDb> msg: ", chatInfo)
  130. }).Catch(func(e exception.Exception) {
  131. }).Finally(func() {
  132. })
  133. }
  134. func (sel *interDb) ChatRecordToDb(player *role.Player, serviceId string, playerId string, state pb.EDbRecordState) {
  135. if sel.dbStream == nil {
  136. log.Println("<interDb.ChatRecordToDb> InterDb not init")
  137. }
  138. exception.Try(func() {
  139. recordInfo := new(pb.DbChatRecord)
  140. recordInfo.TimeStamp = time.Now().Unix()
  141. recordInfo.SessionId = player.SessionId
  142. recordInfo.IsVisitor = player.IsVisitor
  143. recordInfo.GameId = player.GameId
  144. recordInfo.State = state
  145. recordInfo.PlayerId = playerId
  146. recordInfo.ServiceId = serviceId
  147. cmd := new(pb.DbCommandMsg)
  148. cmd.CmdType = pb.EDbCommand_E_DB_COMMAND_PUSH_CHAT_RECORD
  149. cmd.Data = &pb.DbCommandMsg_ChatRecord{ChatRecord: recordInfo}
  150. sel.send(cmd)
  151. log.Println("<interDb.ChatRecordToDb> msg: ", recordInfo)
  152. }).Catch(func(e exception.Exception) {
  153. }).Finally(func() {
  154. })
  155. }
  156. func (sel *interDb) GetChatRecord(serviceId string) []*pb.DbChatRecord {
  157. if sel.dbStream == nil {
  158. log.Println("<interDb.ChatLogToDb> InterDb not init")
  159. }
  160. req := new(pb.DbMsgReq)
  161. req.Cmd = &pb.DbCommandMsg{CmdStr: serviceId, CmdType: pb.EDbCommand_E_DB_COMMAND_GET_CHAT_RECORD}
  162. res, err := sel.client.DbCall(context.Background(), req)
  163. if err != nil {
  164. log.Println("<interDb.GetChatRecord> err:", err, " id:", serviceId)
  165. return nil
  166. }
  167. resCmd := res.GetCmd()
  168. if resCmd != nil {
  169. arrRecord := resCmd.GetArrayChatRecord()
  170. if arrRecord != nil {
  171. return arrRecord.GetDataList()
  172. }
  173. }
  174. log.Println("<interDb.GetChatRecord> record empty, id:", serviceId)
  175. return nil
  176. }
  177. func (sel *interDb) GetChatLog(playerId string) []*pb.DbChatLog {
  178. req := new(pb.DbMsgReq)
  179. req.Cmd = &pb.DbCommandMsg{CmdStr: playerId, CmdType: pb.EDbCommand_E_DB_COMMAND_GET_CHAT_LOG}
  180. res, err := sel.client.DbCall(context.Background(), req)
  181. if err != nil {
  182. log.Println("<interDb.GetChatLog> err:", err, " id:", playerId)
  183. return nil
  184. }
  185. resCmd := res.GetCmd()
  186. if resCmd != nil {
  187. dataList := res.GetCmd().GetArrayChatLog()
  188. if dataList != nil {
  189. return dataList.GetDataList()
  190. }
  191. }
  192. return nil
  193. }