GameManager.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { _decorator, Component, Node, SpriteFrame, sys } from 'cc';
  2. import { config } from './config';
  3. import { settingData } from './data';
  4. import { http } from './http';
  5. import { userDataManager } from './manager/userDataManager';
  6. import { SdkUtil } from './sdkUtil';
  7. import { uiManager } from './manager/uiManager';
  8. import { restart_view } from './ui/restart_view';
  9. import { tools } from './tools';
  10. import { gameSocket } from './socket/gameSocket';
  11. import { exchange_car_view } from './ui/exchange_car_view/exchange_car_view';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('GameManager')
  14. export class GameManager extends Component {
  15. public static settingData:settingData = null
  16. // 延时请求数据
  17. public static requestDelay(cb,timeout:number=330) {
  18. setTimeout(()=>{
  19. cb && cb()
  20. },timeout)
  21. }
  22. // 开启webscoket
  23. public static openWebScoket() {
  24. gameSocket.Instance.connect(config.websocket_domain)
  25. }
  26. // 检查玩游戏
  27. public static checkPlayGame(parent_node:Node,play_cb) {
  28. let call_back = (()=>{
  29. play_cb()
  30. })
  31. if(userDataManager.getUserIsFreeAds()) {
  32. call_back()
  33. return
  34. }
  35. if(userDataManager.isTodayCanFreePlayGame()) {
  36. userDataManager.addTodayPlayGameNumber()
  37. call_back()
  38. return
  39. }
  40. uiManager.Instance().showUi(config.UI.ui_restart_view, parent_node, (node:Node)=>{
  41. node.getComponent(restart_view).initView((v:restart_view)=>{
  42. GameManager.showVideoAd(config.ADS_TYPE.GAME_RESTART, ()=>{
  43. v.closeSelf()
  44. call_back()
  45. })
  46. },(v:restart_view)=>{
  47. if(userDataManager.isTodayCanShare()==false) {
  48. v.closeSelf()
  49. call_back()
  50. return
  51. }
  52. SdkUtil.shareGame('',tools.sys_config.share_des,(r)=>{
  53. if(r==true) {
  54. userDataManager.addTodayShareNumber()
  55. v.closeSelf()
  56. call_back()
  57. }
  58. })
  59. })
  60. })
  61. }
  62. // 展示兑换车视图
  63. public static showExchangeCarView(car_id:number,sp_icon:string,sp_count:number, cb) {
  64. let data = {'car_id':car_id,'sp_icon':sp_icon,'sp_count':sp_count}
  65. uiManager.Instance().showUi(config.UI.ui_exchange_car_view, null, (node:Node)=>{
  66. node.getComponent(exchange_car_view).initView(data,cb)
  67. })
  68. }
  69. // 游戏开始
  70. public static gameStart() {
  71. SdkUtil.ttStartScreenRecording()
  72. }
  73. // 游戏结束
  74. public static gameOver() {
  75. setTimeout(()=>{
  76. SdkUtil.ttStopScreenRecording()
  77. },1700)
  78. }
  79. // 设置
  80. public static getSettingData():settingData {
  81. if(GameManager.settingData!=null) {
  82. return GameManager.settingData
  83. }
  84. let str = sys.localStorage.getItem(config.LOCAL_STORAGE.SETTING_DATA)
  85. if(str==undefined||str==""||str==null){
  86. GameManager.settingData = new settingData
  87. } else {
  88. let data:settingData = JSON.parse(str)
  89. GameManager.settingData = data
  90. }
  91. return GameManager.settingData;
  92. }
  93. public static saveSettingData(data:settingData) {
  94. sys.localStorage.setItem(config.LOCAL_STORAGE.SETTING_DATA, JSON.stringify(data));
  95. }
  96. // 震动
  97. public static vibrateShort() {
  98. if(GameManager.getSettingData().isOpenZhendong) {
  99. SdkUtil.vibrateShort()
  100. }
  101. }
  102. // 游戏结束结算页分享
  103. public static gameOverResultsShare() {
  104. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  105. SdkUtil.shareGameVideo()
  106. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  107. SdkUtil.shareGame('',tools.sys_config.share_des)
  108. }
  109. }
  110. //请求广播
  111. public static requestGuangbo(cb=null) {
  112. http.post(config.API.msg, null, (err,d)=>{
  113. if(!err){
  114. let nd = JSON.parse(d)
  115. if(nd.code === config.status.SUCCESS){
  116. // console.log("system_msg", nd.content)
  117. if(cb!=null){
  118. cb(nd.content)
  119. }
  120. }
  121. }
  122. }, 'GET')
  123. }
  124. //请求用户车列表
  125. public static requestUserCarList(cb=null) {
  126. http.post(config.API.user_car_list,null,(err,d)=>{
  127. if(!err){
  128. let nd = JSON.parse(d)
  129. if(nd.code === config.status.SUCCESS){
  130. // console.log("user_car_list", nd.content)
  131. if(cb!=null){
  132. cb(nd.content)
  133. }
  134. }
  135. }
  136. },'GET')
  137. }
  138. // 请求用户注册/设置地区 stype 1:注册 2:重新设置
  139. public static requestUserSetRegion(region_id:number,stype:number,cb) {
  140. let opt = {'region_id':region_id, 'stype':stype}
  141. http.post(config.API.user_set_region, opt, (err,d)=>{
  142. let nd = JSON.parse(d)
  143. if(nd.code === config.status.SUCCESS){
  144. if(cb!=null){
  145. cb(nd.content)
  146. }
  147. }
  148. })
  149. }
  150. // 请求我的排行 stype 0:全国 1:省 2:市
  151. public static requestMineRank(stype:number, cb) {
  152. let opt = {'stype': stype}
  153. http.post(config.API.user_ranking, opt, (err,d)=>{
  154. if(!err){
  155. let data = JSON.parse(d)
  156. if(data.code===config.status.SUCCESS){
  157. if(cb!=null) {
  158. cb(data.content)
  159. }
  160. }
  161. } else{
  162. console.log("user rank Data err",err)
  163. }
  164. })
  165. }
  166. // 请求排行列表
  167. public static requestRankList(region_id:number, cb) {
  168. http.post(config.API.rankings(region_id),null, (err,d)=>{
  169. if(!err){
  170. let data = JSON.parse(d)
  171. if(data.code===config.status.SUCCESS){
  172. // console.log('data=',data.content)
  173. if(cb!=null) {
  174. cb(data.content)
  175. }
  176. }
  177. } else{
  178. console.log("rankList err",err)
  179. }
  180. },'GET')
  181. }
  182. // 背包列表
  183. public static requestBagList(stype_id:number, cb) {
  184. let opt = {'stype_id':stype_id}
  185. http.post(config.API.bag_list, opt, (err,d)=>{
  186. if(!err){
  187. let nd = JSON.parse(d)
  188. if(nd.code === config.status.SUCCESS){
  189. // console.log("bag_list", nd.content)
  190. cb && cb(nd.content)
  191. }
  192. }
  193. })
  194. }
  195. // 兑换车辆
  196. public static requestExchangeCar(car_id:number, cb) {
  197. let opt = {'car_id':car_id}
  198. http.post(config.API.exchange_car, opt, (err,d)=>{
  199. if(!err){
  200. let nd = JSON.parse(d)
  201. if(nd.code === config.status.SUCCESS){
  202. // console.log("兑换车辆", nd.content)
  203. cb && cb()
  204. }
  205. }
  206. })
  207. }
  208. // 设置用户已读小红点
  209. public static requestUserSetReadRedDot(stype:number, cb) {
  210. let opt = {'stype': stype}
  211. http.post(config.API.user_up_red_dot, opt, (err,d)=>{
  212. if(!err){
  213. let nd = JSON.parse(d)
  214. if(nd.code === config.status.SUCCESS){
  215. // console.log("设置小红点", nd.content)
  216. cb && cb()
  217. }
  218. }
  219. })
  220. }
  221. // 请求tt侧边栏用户奖励
  222. public static requestTTSidebarUserReward(status=config.USER_TT_SIDEBAR_REWARD.GET, success_cb, fail_cb=null) {
  223. if(sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
  224. success_cb(null)
  225. return
  226. }
  227. if(status != config.USER_TT_SIDEBAR_REWARD.GET) {
  228. uiManager.Instance().showLoading()
  229. }
  230. http.post(config.API.unlock_number_status,{'stype':status}, (err,data)=>{
  231. if(status!= config.USER_TT_SIDEBAR_REWARD.GET) {
  232. uiManager.Instance().hideLoading()
  233. }
  234. if(!err) {
  235. let _data = JSON.parse(data)
  236. if(_data.code==config.status.SUCCESS) {
  237. if(success_cb) {
  238. success_cb(_data.content)
  239. }
  240. } else {
  241. if(fail_cb) {
  242. fail_cb()
  243. }
  244. }
  245. } else {
  246. if(fail_cb) {
  247. fail_cb()
  248. }
  249. }
  250. })
  251. }
  252. // 请求同步数据
  253. public static requestSyncNumber(opt,success_cb=null,fail_cb=null) {
  254. http.post(config.API.sync_free_number, opt, (err,data)=>{
  255. if(!err) {
  256. let _data = JSON.parse(data)
  257. if(_data.code==config.status.SUCCESS) {
  258. if(success_cb) {
  259. success_cb(_data.content)
  260. }
  261. } else {
  262. if(fail_cb) {
  263. fail_cb(_data.code)
  264. }
  265. }
  266. } else {
  267. if(fail_cb) {
  268. fail_cb(err)
  269. }
  270. }
  271. })
  272. }
  273. // 显示视频广告
  274. public static showVideoAd(ads_type=config.ADS_TYPE.UNKNOWN, success_cb, err_cb=null) {
  275. let ad_id = SdkUtil.getAdId(ads_type)
  276. SdkUtil.showVideoAd(ad_id,(res)=>{
  277. if(res.isEnded) {
  278. success_cb(res)
  279. } else {
  280. err_cb(res)
  281. }
  282. })
  283. }
  284. }