Area.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import Player from "../game/data/udata/Player";
  2. import Main from "../main/Main";
  3. import { GameViewType } from "../main/ViewManage";
  4. import ViewObject from "../main/ViewObject";
  5. import PlotHome from "../plot/PlotHome";
  6. import PlotView from "../plot/PlotView";
  7. import CHttp, { HttpStateType, ReveData } from "../util/CHttp";
  8. import CMath from "../util/CMath";
  9. import HistoryArea from "./HistoryArea";
  10. import Tap_set_Ann from "./Tap_set_Ann";
  11. /**
  12. * 游戏分区
  13. */
  14. export interface Zone {
  15. id: number,
  16. hardwareId: number,
  17. name: string,
  18. rec: boolean,//是否推荐
  19. inew: boolean,//是否新区
  20. }
  21. /**
  22. * 历史角色数据
  23. */
  24. export interface History_Role {
  25. zoneId: number,
  26. name: string,
  27. level: number,
  28. time: number,//最后登陆的时间
  29. }
  30. /**
  31. * 选择分区界面
  32. */
  33. const { ccclass, property } = cc._decorator;
  34. @ccclass
  35. export default class Area extends ViewObject {
  36. @property(cc.Label)
  37. mZoneName: cc.Label = null;//服务器名字
  38. public zones: Array<Zone> = null;
  39. public roles: Array<History_Role> = null;
  40. public gameServer: string = null;
  41. onLoad() {
  42. this.loadZone();
  43. }
  44. start(){
  45. this.opentap_set_Ann()
  46. }
  47. public loadZone() {
  48. let http = this.main.loginHttp;
  49. let msg = {
  50. channel: this.main.profile.mChannel.toString(),
  51. userId: this.main.userData.id.toString()
  52. }
  53. this.main.startLoad();
  54. http.sendForm('/getZone', msg, (state, reve: any) => {
  55. this.main.stopLoad();
  56. if (state == HttpStateType.SUCCESS) {
  57. this.zones = reve.zones;
  58. this.roles = reve.roles;
  59. this.gameServer = reve.gameServer;
  60. this.init();
  61. } else {
  62. this.main.showTips('网络异常');
  63. }
  64. })
  65. }
  66. public onclickBack() {
  67. this.exitDistroy();
  68. }
  69. public init() {
  70. let optZone = this.getOptZone();
  71. this.setOptZone(optZone);
  72. }
  73. public setOptZone(optZone) {
  74. this.main.userData.zone = optZone;
  75. this.mZoneName.string = optZone.name
  76. }
  77. public getZoneById(id: number) {
  78. if (this.zones == null) {
  79. return null;
  80. }
  81. for (let i = 0; i < this.zones.length; i++) {
  82. const element = this.zones[i];
  83. if (element.id == id) {
  84. return element;
  85. }
  86. }
  87. return null;
  88. }
  89. /**
  90. * 获取最优化分区
  91. */
  92. public getOptZone(): Zone {
  93. let latelyZone = null;
  94. //最近玩过的分区
  95. latelyZone = this.getLatelyZone();
  96. if (latelyZone) {
  97. return latelyZone;
  98. }
  99. //推荐分区
  100. latelyZone = this.getRecZone();
  101. if (latelyZone) {
  102. return latelyZone;
  103. }
  104. //随机一个区
  105. return this.zones[CMath.getRandom(0, this.zones.length - 1)];;
  106. }
  107. /**
  108. * 获取最近玩过的区
  109. */
  110. private getLatelyZone(): Zone {
  111. if (this.roles == null) {
  112. return null;
  113. }
  114. let latelyRole: History_Role = null;
  115. for (let i = 0; i < this.roles.length; i++) {
  116. const element = this.roles[i];
  117. if (latelyRole == null) {
  118. latelyRole = element;
  119. } else {
  120. if (element.time > latelyRole.time) {
  121. latelyRole = element;
  122. }
  123. }
  124. }
  125. return this.getZoneById(latelyRole.zoneId);
  126. }
  127. /**
  128. * 获取推荐分区
  129. */
  130. private getRecZone(): Zone {
  131. if (this.zones == null) {
  132. return null;
  133. }
  134. let recs = [];
  135. for (let i = 0; i < this.zones.length; i++) {
  136. const element = this.zones[i];
  137. if (element.rec) {
  138. recs.push(element);
  139. }
  140. }
  141. if (recs.length <= 0) {
  142. return null;
  143. }
  144. return recs[CMath.getRandom(0, recs.length - 1)];
  145. }
  146. /**
  147. * 打开历史登陆的分区
  148. */
  149. public openHistoryArea() {
  150. this.main.startLoad();
  151. this.main.viewManage.loadFunc(GameViewType.historyArea, (viewObject: ViewObject) => {
  152. let historyArea = viewObject as HistoryArea;
  153. historyArea.area = this;
  154. viewObject.show();
  155. this.main.stopLoad();
  156. });
  157. }
  158. /**
  159. * 进入游戏
  160. */
  161. public onclickOK() {
  162. let userData = this.main.userData;
  163. let optZone = this.main.userData.zone
  164. let http: CHttp = new CHttp(this.gameServer + '/H' + optZone.hardwareId + '/');
  165. let msg = {
  166. channel: this.main.profile.mChannel,
  167. channelAttr: '测试',
  168. id: userData.id,
  169. token: userData.token,
  170. zoneId: optZone.id
  171. }
  172. this.main.startLoad();
  173. http.token = '08f0656590e33d39021d41714994383d';
  174. http.sendJson('login', msg, (state, reve: ReveData) => {
  175. this.main.stopLoad();
  176. if (state == HttpStateType.SUCCESS) {
  177. if (reve.retCode == 0) {
  178. this.main.player = new Player(reve.data);
  179. http.token = this.main.player.role.token;
  180. http.id = this.main.player.role.id;
  181. http.userId = this.main.userData.id;
  182. this.main.gameHttp = http;
  183. this.main.gameHttp.unshiftEvent(this.main.player);
  184. if (reve.data.isNew) {
  185. // if(true){
  186. this.openPlot()
  187. } else {
  188. this.openHome(this.main);
  189. }
  190. } else {
  191. this.main.showTips(reve.message);
  192. }
  193. } else {
  194. this.main.showTips('网络异常');
  195. }
  196. });
  197. }
  198. public openHome(main: Main) {
  199. main.viewManage.loadFunc(GameViewType.home, (viewObject: ViewObject) => {
  200. this.__distroyAll();
  201. let plotHome = viewObject.node.getComponent(PlotHome)
  202. plotHome.openMenu()
  203. viewObject.show();
  204. });
  205. }
  206. public openPlot() {
  207. let main = this.main;
  208. this.main.viewManage.loadFunc(GameViewType.plot_view, (viewObject: ViewObject) => {
  209. this.__distroyAll();
  210. let plotView = viewObject as PlotView;
  211. plotView.setCloseCallback(() => {
  212. main.viewManage.loadFunc(GameViewType.home, (viewObject: ViewObject) => {
  213. let plotHome = viewObject.node.getComponent(PlotHome)
  214. plotHome.exDialog()
  215. viewObject.show();
  216. });
  217. })
  218. viewObject.show();
  219. });
  220. }
  221. /**
  222. * 公告
  223. */
  224. public opentap_set_Ann() {
  225. this.main.viewManage.loadFunc(GameViewType.tap_set_Ann, (viewObject: ViewObject) => {
  226. viewObject.show();
  227. });
  228. }
  229. }