orm_common.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package model
  2. import (
  3. "encoding/base64"
  4. "rocommon"
  5. "rocommon/service"
  6. "rocommon/util"
  7. "time"
  8. )
  9. const (
  10. SDKPlatform_PC = "PC"
  11. SDKPlatform_Quick = "SDKQuick"
  12. SDKPlatform_Hw_Quick = "SDKHwQuick"
  13. SDKPlatform_NBSDK = "SDKNB"
  14. SDKPlatform_UniSDK = "SDKUni" //SDKUni
  15. SDKPlatform_YouYi = "SDKYOUYI"
  16. SDKPlatform_YouYi_IOS = "SDKYOUYI_IOS"
  17. SDKPlatform_Quick_Android = "Quick_Android"
  18. SDKPlatform_Quick_Ios = "Quick_IOS"
  19. /* sub_platform
  20. changemeng_xjgl 畅梦包
  21. changemeng_gdt_xjgl 畅梦广通包
  22. quick_wy_gzyw 网易quick
  23. wangyi_youyi 游易网易
  24. */
  25. )
  26. const (
  27. //服务器关闭/开启注册功能
  28. ServerPrefix = "server:reg"
  29. ServerCompetitionPrefix = "server:com"
  30. //地图通关进度排名
  31. MapRankPrefix = "rank:map"
  32. //困难地图通关进度排名
  33. MapHardRankPrefix = "rank_hard:map"
  34. //噩梦地图通关进度排名
  35. MapHard2RankPrefix = "rank_hard2:map"
  36. //历史最大战力
  37. MaxFightPowerRankPrefix = "rank:fight"
  38. //当前积分排行(用来匹配使用)
  39. ArenaRankMatchPrefix = "rank:arenamatch"
  40. //当前积分排行(参加过玩家的积分排行)
  41. ArenaRankPrefix = "rank:arena"
  42. ArenaPreRankPrefix = "rank:arenapre" //上赛季参赛玩家积分排行列表
  43. //积分达到对应段位后的排行(积分下降后从该排行榜中移除)
  44. ArenaTopRankPrefix = "rank:arenatop"
  45. //rank:arenatop1 历史top排行
  46. //competition
  47. //赛季对应大地图排行数据
  48. CompetitionMapRankPrefix = "rank:comp:map" //1
  49. CompetitionScoreRankPrefix = "rank:com:score" //2
  50. CompetitionDuoBaoScoreRankPrefix = "rank:com:wheel" //3
  51. CompetitionZhanBuScoreRankPrefix = "rank:com:zhanbu" //4
  52. CompetitionIdolScoreRankPrefix = "rank:com:idol" //5
  53. CompetitionIdolBoxRankPrefix = "rank:com:idolbox"
  54. CompetitionIdolFansVotePrefix = "rank:com:idolfans"
  55. TowerLevelRankPrefix = "rank:tower"
  56. TowerWjLevelRankPrefix = "rank:towerwj"
  57. TowerFightpowerRankPrefix = "rank:towerfight"
  58. CardCollectRankPrefix = "rank:cardcollect"
  59. KeepSakeCollectRankPrefix = "rank:keepsakecollect"
  60. //战斗记录(不包括过程数据)
  61. BattleRecordMapPrefix = "brecord:map"
  62. //战斗过程详细数据,结构序列化数据,key-val格式用来设置过期使用
  63. BattleRecordDetailPrefix = "brecord"
  64. //验证存储合法性
  65. BattleRecordDetailCheckPrefix = "brecord:id"
  66. //公会base
  67. GuildBasePrefix = "guild:guildbase"
  68. //公会成员
  69. GuildMemberPrefix = "guild:guildmember"
  70. //公会申请列表
  71. GuildApplyPrefix = "guild:guildapply"
  72. //公会日志列表
  73. GuildLogPrefix = "guild:guildlog"
  74. //玩家申请列表
  75. RoleApplyPrefix = "guild:roleapply"
  76. //公会名字索引
  77. GuildNameIndexPrefix = "guild:nameindex"
  78. //公会
  79. GuildRoleToGuildPrefix = "guild:roletoguildindex"
  80. //公会最新ID
  81. GuildCurGuildIdPrefix = "guild:curguildidindex"
  82. //公会推荐列表
  83. GuildRecommendPrefix = "guild:recommendindex"
  84. //公会boss
  85. GuildBossPrefix = "guild:bossindex"
  86. //公会魔王
  87. GuildDemonPrefix = "guild:guilddemon"
  88. //公会魔王奖励
  89. GuildDemonRewardPrefix = "guild:guilddemonreward"
  90. //公会魔王,公会最总伤害排名
  91. GuildDemonGuildPrefix = "guild:demonguildindex"
  92. //公会魔王,公会MVP伤害排名
  93. GuildDemonMVPPrefix = "guild:demonmvpindex"
  94. //爬塔排行榜系统数据
  95. RushTowerPrefix = "tower:rushtower"
  96. //爬塔最终榜单
  97. RushTowerRankPrefix = "tower:rushrank"
  98. //爬塔排行榜系统数据
  99. RushArenaPrefix = "arena:rusharena"
  100. //爬塔最终榜单
  101. RushArenaRankPrefix = "arena:rushrank"
  102. //推图冲榜积分榜
  103. RushMapScorePrefix = "arena:rushmapscore"
  104. //推图冲榜系统数据
  105. RushMapPrefix = "arena:rushmap"
  106. //推图冲榜最终积分榜
  107. RushMapRankPrefix = "arena:rushmaprank"
  108. //勇士积分排行榜
  109. ExpeditionScoreRankPrefix = "rank:expedition"
  110. //宠物冲榜相关
  111. RushPetScorePrefix = "pet:rushpetscore"
  112. //宠物冲榜系统数据
  113. RushPetPrefix = "pet:rushpet"
  114. //宠物冲榜最终数据
  115. RushPetRankPrefix = "pet:rushpetrank"
  116. //宠物冲榜,宠物数据
  117. RushPetDataPrefix = "pet:rushpetdatarank"
  118. //技能冲榜相关
  119. RushSkillScorePrefix = "pet:rushskillscore"
  120. //技能冲榜系统数据
  121. RushSkillPrefix = "pet:rushskill"
  122. //技能冲榜最终数据
  123. RushSkillRankPrefix = "pet:rushskillrank"
  124. //pay
  125. PayOrderPrefix = "pay" //所有订单信息
  126. PayOrderOKIdListPrefix = "payoklist:" //成功但是没有获取奖励的订单id payoklist:uid
  127. //百人道场
  128. DaoChang100Prefix = "dc100" //map有数据表示挑战过该位置(机器人/玩家)
  129. DaoChang100RewardPrefix = "dc100posreward" //list占位奖励列表
  130. RoleDaoChang100Prefix = "role_dc100" //map
  131. RoleDaoChang100LogPrefix = "role_dc100_log"
  132. RoleDaoChang100GuildRankPrefix = "dc100guild"
  133. RoleDaoChang100WheeLogPrefix = "dc100wheel_log" //百人道场转盘日志
  134. //精彩活动
  135. ActivitiesCollectionDataPrefix = "act:num"
  136. )
  137. // 排行榜每日重置时使用,切记!!!
  138. func GetRankScoreDailyReset(val uint64, passTime uint64) uint64 {
  139. //score
  140. var scoreStr = val
  141. if scoreStr >= (1 << 40) {
  142. scoreStr = (1 << 40) - 1
  143. }
  144. //时间修改成以一天内的时间
  145. tmpTime := util.GetCurrentTimeNow()
  146. passTime = uint64(tmpTime.Hour()*60*60+tmpTime.Minute()*60+tmpTime.Second()+tmpTime.Day()) * 1000
  147. scoreStr <<= 24
  148. scoreStr |= 0xfffffffff - passTime/50
  149. return scoreStr
  150. }
  151. func GetValByRankScoreDailyReset(score float64) uint64 {
  152. val := uint64(score) >> 24
  153. return val
  154. }
  155. // 通用排行榜积分处理
  156. func GetRankScoreCommon(val uint64, passTime uint64) uint64 {
  157. //score
  158. var scoreStr = val
  159. if scoreStr >= (1 << 28) {
  160. scoreStr = (1 << 28) - 1
  161. }
  162. scoreStr <<= 36
  163. scoreStr |= 0xfffffffff - passTime/50
  164. return scoreStr
  165. }
  166. func GetValByRankScoreCommon(score float64) uint64 {
  167. val := uint64(score) >> 36
  168. return val
  169. }
  170. type CompetitionType int32
  171. const (
  172. CompetitionType_ZhaoMu CompetitionType = iota + 1 //招募季
  173. CompetitionType_XuanBa //选拔季 贡献道具获取积分
  174. CompetitionType_DuoBao //夺宝季 转盘获取积分
  175. CompetitionType_ZhanBu //占卜季
  176. CompetitionType_Idol //偶像季
  177. )
  178. // 数据库相关操作
  179. func GetMessageFromRedis(module, key string, msg interface{}) error {
  180. ret, err := service.GetRedis().Get(module + key).Result()
  181. if err != nil {
  182. if err != service.NIL {
  183. util.InfoF("GetMessageFromRedis get key=%v err=%v\n", module+key, err)
  184. }
  185. return err
  186. }
  187. if ret == "" {
  188. //util.InfoF("key(%v:%v)->val empty", module, key)
  189. return nil
  190. }
  191. return GetDecodeMessage(msg, ret)
  192. }
  193. func SetMessageToRedis(module, key string, msg interface{}) error {
  194. msgData, err := rocommon.GetCodec().Marshal(msg)
  195. if err != nil {
  196. return err
  197. }
  198. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  199. ret, err := service.GetRedis().Set(module+key, msgStr, 0).Result()
  200. if err != nil {
  201. util.InfoF("SetMessageToRedis err=%v ret=%v", module+key, ret)
  202. return err
  203. }
  204. //log.Printf("[SetMessageToRedis] [%v]ret:%v\n", module + key, ret)
  205. return nil
  206. }
  207. func SetRankMessageToRedis(key string, msg interface{}) error {
  208. msgData, err := rocommon.GetCodec().Marshal(msg)
  209. if err != nil {
  210. return err
  211. }
  212. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  213. ret, err := service.GetRedis().Set(key, msgStr, time.Second*1000).Result()
  214. if err != nil {
  215. util.InfoF("SetRankMessageToRedis err=%v ret=%v", key, ret)
  216. return err
  217. }
  218. //log.Printf("[SetMessageToRedis] [%v]ret:%v\n", module + key, ret)
  219. return nil
  220. }
  221. func SetMessageToRedisExpire(module, key string, msg interface{}) error {
  222. msgData, err := rocommon.GetCodec().Marshal(msg)
  223. if err != nil {
  224. return err
  225. }
  226. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  227. ret, err := service.GetRedis().HSet(module, key, msgStr).Result()
  228. if err != nil {
  229. util.InfoF("SetMessageToRedis err=%v ret=%v", module+key, ret)
  230. return err
  231. }
  232. //log.Printf("[SetMessageToRedis] [%v]ret:%v\n", module + key, ret)
  233. return nil
  234. }
  235. func GetEncodeMessage(msg interface{}) (error, string) {
  236. msgData, err := rocommon.GetCodec().Marshal(msg)
  237. if err != nil {
  238. return err, ""
  239. }
  240. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  241. return nil, msgStr
  242. }
  243. func GetDecodeMessage(msg interface{}, msgStr string) error {
  244. str, err := base64.StdEncoding.DecodeString(msgStr)
  245. if err != nil {
  246. //log.Printf("[GetMessageFromRedis] [%v] err:%v\n", module + key, err)
  247. return err
  248. }
  249. return rocommon.GetCodec().Unmarshal(str, msg)
  250. }
  251. func ExistKey(key string) bool {
  252. ret, err := service.GetRedis().Exists(key).Result()
  253. if err != nil {
  254. return false
  255. }
  256. return ret > 0
  257. }
  258. func SetDataToRedis(key string, data string) error {
  259. ret, err := service.GetRedis().Set(key, data, 0).Result()
  260. if err != nil {
  261. util.InfoF("[SetDataToRedis] err:[%v]ret:%v", key, ret)
  262. return err
  263. }
  264. //log.Printf("[SetMessageToRedis] [%v]ret:%v\n", module + key, ret)
  265. return nil
  266. }
  267. // 区分不同平台
  268. func ConvertPlatform(openId, platform string) string {
  269. if platform == SDKPlatform_YouYi || platform == SDKPlatform_YouYi_IOS {
  270. //除了quick和nb其他渠道都要加上渠道标识
  271. openId = platform + openId
  272. }
  273. return openId
  274. }