GameManager.ts 9.1 KB

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