GameManager.ts 11 KB

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