GameManager.ts 11 KB

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