GameManager.ts 12 KB

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