GameManager.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, show_loading:boolean=false) {
  140. if(show_loading) {uiManager.Instance().showLoading()}
  141. let opt = {'region_id':region_id, 'stype':stype}
  142. http.post(config.API.user_set_region, opt, (err,d)=>{
  143. if(show_loading) {uiManager.Instance().hideLoading()}
  144. let nd = JSON.parse(d)
  145. if(nd.code === config.status.SUCCESS){
  146. if(cb!=null){
  147. cb(nd.content)
  148. }
  149. }
  150. })
  151. }
  152. // 请求我的排行 stype 0:全国 1:省 2:市
  153. public static requestMineRank(stype:number, cb) {
  154. let opt = {'stype': stype}
  155. http.post(config.API.user_ranking, opt, (err,d)=>{
  156. if(!err){
  157. let data = JSON.parse(d)
  158. if(data.code===config.status.SUCCESS){
  159. if(cb!=null) {
  160. cb(data.content)
  161. }
  162. }
  163. } else{
  164. console.log("user rank Data err",err)
  165. }
  166. })
  167. }
  168. // 请求排行列表
  169. public static requestRankList(region_id:number, cb, show_loading:boolean=false) {
  170. if(show_loading) {uiManager.Instance().showLoading()}
  171. http.post(config.API.rankings(region_id),null, (err,d)=>{
  172. if(show_loading) {uiManager.Instance().hideLoading()}
  173. if(!err){
  174. let data = JSON.parse(d)
  175. if(data.code===config.status.SUCCESS){
  176. // console.log('data=',data.content)
  177. cb && cb(data.content)
  178. }
  179. } else{
  180. console.log("rankList err",err)
  181. }
  182. },'GET')
  183. }
  184. // 背包列表
  185. public static requestBagList(stype_id:number, cb, show_loading:boolean=false) {
  186. if(show_loading) {uiManager.Instance().showLoading()}
  187. let opt = {'stype_id':stype_id}
  188. http.post(config.API.bag_list, opt, (err,d)=>{
  189. if(show_loading) {uiManager.Instance().hideLoading()}
  190. if(!err){
  191. let nd = JSON.parse(d)
  192. if(nd.code === config.status.SUCCESS){
  193. // console.log("bag_list", nd.content)
  194. cb && cb(nd.content)
  195. }
  196. }
  197. })
  198. }
  199. // 兑换车辆
  200. public static requestExchangeCar(car_id:number, cb, show_loading:boolean=false) {
  201. if(show_loading) {uiManager.Instance().showLoading()}
  202. let opt = {'car_id':car_id}
  203. http.post(config.API.exchange_car, opt, (err,d)=>{
  204. if(show_loading) {uiManager.Instance().hideLoading()}
  205. if(!err){
  206. let nd = JSON.parse(d)
  207. if(nd.code === config.status.SUCCESS){
  208. // console.log("兑换车辆", nd.content)
  209. cb && cb()
  210. }
  211. }
  212. })
  213. }
  214. // 设置用户已读小红点
  215. public static requestUserSetReadRedDot(stype:number, cb) {
  216. let opt = {'stype': stype}
  217. http.post(config.API.user_up_red_dot, opt, (err,d)=>{
  218. if(!err){
  219. let nd = JSON.parse(d)
  220. if(nd.code === config.status.SUCCESS){
  221. // console.log("设置小红点", nd.content)
  222. cb && cb()
  223. }
  224. }
  225. })
  226. }
  227. // 请求tt侧边栏用户奖励
  228. public static requestTTSidebarUserReward(status=config.USER_TT_SIDEBAR_REWARD.GET, success_cb, fail_cb=null) {
  229. if(sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
  230. success_cb(null)
  231. return
  232. }
  233. if(status != config.USER_TT_SIDEBAR_REWARD.GET) {
  234. uiManager.Instance().showLoading()
  235. }
  236. http.post(config.API.unlock_number_status,{'stype':status}, (err,data)=>{
  237. if(status!= config.USER_TT_SIDEBAR_REWARD.GET) {
  238. uiManager.Instance().hideLoading()
  239. }
  240. if(!err) {
  241. let _data = JSON.parse(data)
  242. if(_data.code==config.status.SUCCESS) {
  243. if(success_cb) {
  244. success_cb(_data.content)
  245. }
  246. } else {
  247. if(fail_cb) {
  248. fail_cb()
  249. }
  250. }
  251. } else {
  252. if(fail_cb) {
  253. fail_cb()
  254. }
  255. }
  256. })
  257. }
  258. // 请求同步数据
  259. public static requestSyncNumber(opt,success_cb=null,fail_cb=null) {
  260. http.post(config.API.sync_free_number, opt, (err,data)=>{
  261. if(!err) {
  262. let _data = JSON.parse(data)
  263. if(_data.code==config.status.SUCCESS) {
  264. if(success_cb) {
  265. success_cb(_data.content)
  266. }
  267. } else {
  268. if(fail_cb) {
  269. fail_cb(_data.code)
  270. }
  271. }
  272. } else {
  273. if(fail_cb) {
  274. fail_cb(err)
  275. }
  276. }
  277. })
  278. }
  279. // 显示视频广告
  280. public static showVideoAd(ads_type=config.ADS_TYPE.UNKNOWN, success_cb, err_cb=null) {
  281. let ad_id = SdkUtil.getAdId(ads_type)
  282. SdkUtil.showVideoAd(ad_id,(res)=>{
  283. if(res.isEnded) {
  284. success_cb(res)
  285. } else {
  286. err_cb(res)
  287. }
  288. })
  289. }
  290. }