ArtistResident.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. const DWTool = require("../utils/DWTool");
  2. const HomeApi = require("../net/HomeApi");
  3. const { GameNotificationKey } = require("../utils/GameEnum");
  4. const Base64 = require("../lib/Base64").Base64;
  5. const AlertManager = require("../utils/AlertManager");
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. content: cc.Node,
  10. scrollView: cc.ScrollView,
  11. talentBtn: cc.Button,
  12. confirmBtn: cc.Button,
  13. },
  14. init(buildingInfo, uid, isSelf) {
  15. this.buildingInfo = buildingInfo;
  16. this.uid = uid;
  17. this.isSelf = isSelf;
  18. },
  19. // LIFE-CYCLE CALLBACKS:
  20. onLoad() {
  21. this.node.height = cc.view.getVisibleSize().height;
  22. this.contentY = this.content.y = (this.content.height - this.node.height) / 2;
  23. this._residentList = [];
  24. this._itemScripts = [];
  25. this._currentResidentArtist = null;
  26. this.getNetworkData();
  27. GameEvent.on(GameNotificationKey.RefreshResidentArtistList, this, () => {
  28. for (let child of this.scrollView.content.children) {
  29. child.destroy();
  30. }
  31. this.getNetworkData();
  32. });
  33. this.confirmEvent = _.debounce(() => {
  34. if (this._currentResidentArtist && !this._currentResidentArtist.isStationed) {
  35. let reportFormInfo = {};
  36. reportFormInfo["artistUid"] = this._currentResidentArtist.uid;
  37. reportFormInfo["buildingId"] = this.buildingInfo.buildingId;
  38. reportFormInfo["buildingLevel"] = this.buildingInfo.level;
  39. reportFormInfo["targetUid"] = this.uid;
  40. HomeApi.friendStation(JSON.stringify(reportFormInfo), (responseData) => {
  41. this.close();
  42. // 入驻成功, 要将现有的金币收取, 并且刷新入驻艺人列表
  43. GameEvent.fire(GameNotificationKey.ResidentArtist, this.uid, this.buildingInfo.buildingId);
  44. // 触发入驻通知,opt = 5
  45. GameEvent.fire(GameNotificationKey.NoticeRoleOpt, this.uid, 5)
  46. }, (code, msg) => {
  47. // 亲密度不够
  48. if (code === 814) {
  49. AlertManager.showIntimacyBotEnough();
  50. }
  51. console.log(msg);
  52. });
  53. } else {
  54. this.node.destroy();
  55. }
  56. }, 1000, true);
  57. this.showTalentEvent = _.debounce(() => {
  58. this.close();
  59. AlertManager.showTalentAlert();
  60. }, 1000, true);
  61. },
  62. onDestroy() {
  63. GameEvent.off(GameNotificationKey.RefreshResidentArtistList, this);
  64. },
  65. start() {
  66. this.content.y = -cc.view.getVisibleSize().height;
  67. let s = cc.sequence(cc.moveTo(0.2, 0, this.contentY + 20).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20));
  68. this.content.runAction(s);
  69. },
  70. getNetworkData() {
  71. HomeApi.friendGetArtistsByBuildingId(this.uid, this.buildingInfo.buildingId, (responseData) => {
  72. this._residentList = responseData.list || [];
  73. this.canBeStationedList = this._residentList.filter(item => item.canBeStationed == 1);
  74. this.cannotBeStationedList = this._residentList.filter(item => item.canBeStationed == 0);
  75. if (this.canBeStationedList.length === 0) {
  76. this.talentBtn.node.active = true;
  77. this.confirmBtn.node.active = false;
  78. } else {
  79. this.talentBtn.node.active = false;
  80. this.confirmBtn.node.active = true;
  81. }
  82. this.layout();
  83. }, (err) => {
  84. console.log(err);
  85. });
  86. },
  87. layout() {
  88. if (this._residentList.length === 0) {
  89. this.loadTaletPrefab()
  90. .then((result) => {
  91. let item = cc.instantiate(result);
  92. this.scrollView.content.addChild(item);
  93. });
  94. } else {
  95. if (this.canBeStationedList.length === 0) {
  96. this.loadTaletPrefab()
  97. .then((talent) => {
  98. let item = cc.instantiate(talent);
  99. this.scrollView.content.addChild(item);
  100. return this.loadDashPrefab();
  101. })
  102. .then((dash) => {
  103. let item = cc.instantiate(dash);
  104. this.scrollView.content.addChild(item);
  105. return this.loadPlaceholderItemPrefab();
  106. })
  107. .then((cannotBeStationedItem) => {
  108. for (let i = 0; i < this.cannotBeStationedList.length; i++) {
  109. let item = cc.instantiate(cannotBeStationedItem);
  110. let itemScript = item.getComponent(`ArtistResidentItemPlaceholder`);
  111. itemScript.init(this.cannotBeStationedList[i]);
  112. this.scrollView.content.addChild(item);
  113. }
  114. }).catch((err) => {
  115. console.log(err);
  116. });
  117. } else {
  118. this.loadItemPrefab()
  119. .then((canBeStationedItem) => {
  120. for (let i = 0; i < this.canBeStationedList.length; i++) {
  121. let item = cc.instantiate(canBeStationedItem);
  122. let itemScript = item.getComponent(`ArtistResidentItem`);
  123. this._itemScripts.push(itemScript)
  124. itemScript.init(this.uid, this.buildingInfo, this.isSelf, this.canBeStationedList[i], (artistData) => {
  125. this._currentResidentArtist = artistData;
  126. this._itemScripts.forEach(itemScript => {
  127. itemScript.setStyle(this._currentResidentArtist.uid);
  128. });
  129. });
  130. this.scrollView.content.addChild(item);
  131. }
  132. if (this.cannotBeStationedList.length > 0) {
  133. return this.loadDashPrefab();
  134. }
  135. return;
  136. })
  137. .then((dash) => {
  138. let item = cc.instantiate(dash);
  139. this.scrollView.content.addChild(item);
  140. return this.loadPlaceholderItemPrefab();
  141. })
  142. .then((cannotBeStationedItem) => {
  143. for (let i = 0; i < this.cannotBeStationedList.length; i++) {
  144. let item = cc.instantiate(cannotBeStationedItem);
  145. let itemScript = item.getComponent(`ArtistResidentItemPlaceholder`);
  146. itemScript.init(this.cannotBeStationedList[i]);
  147. this.scrollView.content.addChild(item);
  148. }
  149. }).catch((err) => {
  150. console.log(err);
  151. });
  152. }
  153. }
  154. },
  155. guide() {
  156. if (this._itemScripts.length > 0) {
  157. let itemScript = this._itemScripts[0];
  158. itemScript.selectItem();
  159. }
  160. },
  161. /**
  162. * 星探的预制件
  163. */
  164. loadTaletPrefab() {
  165. return new Promise((resolve, reject) => {
  166. cc.loader.loadRes('./prefabs/artist_resident_talent', cc.Prefab, (error, prefab) => {
  167. if (error) {
  168. reject(error);
  169. } else {
  170. resolve(prefab)
  171. }
  172. });
  173. });
  174. },
  175. /**
  176. * 虚线的预制件
  177. */
  178. loadDashPrefab() {
  179. return new Promise((resolve, reject) => {
  180. cc.loader.loadRes('./prefabs/artist_resident_dash', cc.Prefab, (error, prefab) => {
  181. if (error) {
  182. reject(error);
  183. } else {
  184. resolve(prefab)
  185. }
  186. });
  187. });
  188. },
  189. loadItemPrefab() {
  190. return new Promise((resolve, reject) => {
  191. cc.loader.loadRes('./prefabs/artist_resident_item', cc.Prefab, (error, prefab) => {
  192. if (error) {
  193. reject(error);
  194. } else {
  195. resolve(prefab)
  196. }
  197. });
  198. });
  199. },
  200. loadPlaceholderItemPrefab() {
  201. return new Promise((resolve, reject) => {
  202. cc.loader.loadRes('./prefabs/artist_resident_item_placeholder', cc.Prefab, (error, prefab) => {
  203. if (error) {
  204. reject(error);
  205. } else {
  206. resolve(prefab)
  207. }
  208. });
  209. });
  210. },
  211. confirm() {
  212. this.confirmEvent();
  213. },
  214. showTalent() {
  215. this.showTalentEvent();
  216. },
  217. close() {
  218. let finish = cc.callFunc(() => {
  219. this.node.destroy();
  220. }, this)
  221. this.content.runAction(cc.sequence(cc.moveTo(0.2, 0, -cc.view.getVisibleSize().height).easing(cc.easeCubicActionIn()), finish));
  222. // this.node.destroy();
  223. },
  224. // update (dt) {},
  225. });