CompanyItem.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. var ItemType = require("ItemType");
  2. var UserState = require("UserState");
  3. var CompanyItem = cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. Id:cc.Integer,
  7. stockNum:cc.Integer,
  8. LimitYear:cc.Integer,
  9. Atlas:cc.SpriteAtlas,
  10. IconSp:cc.Sprite,
  11. nameLbl:cc.Sprite,
  12. priceLbl:cc.Label,
  13. costLbl:cc.Label,
  14. numLbl:cc.Label,
  15. ratioLbl:cc.Label,
  16. InLockLbl:cc.Label,
  17. CreateBtn:cc.Node,
  18. BuySaleBtns:cc.Node,
  19. canGetProfit:false,
  20. bankRupt:false, //是否已经破产
  21. Data:null,
  22. },
  23. init:function (data) {
  24. this.Data = null;
  25. this.Data = data;
  26. this.bankRupt = false;
  27. this.LimitYear = data.LimitYear - 1; //限制交易年限
  28. this.stockNum = 0;
  29. this.ratioLbl.string = (data.bonusRatio * 100).toString() + "%";
  30. this.Id = data.Id;
  31. this.IconSp.spriteFrame = this.Atlas.getSpriteFrame(data.icon);
  32. this.nameLbl.spriteFrame = this.Atlas.getSpriteFrame(data.name);
  33. this.costLbl.string = data.stockCost;
  34. this.priceLbl.string = data.stockPrice;
  35. this.numLbl.string = Math.floor(data.stockNum / 100);
  36. },
  37. refreshPrice:function(){
  38. var mapdata = cc.Mgr.MapDataMgr.getDataByItemTypeAndId(ItemType.Company, this.Data.Id);
  39. var proList = mapdata.proList;
  40. var unList = mapdata.unList;
  41. var seed = Math.random();
  42. var stockPrice = this.Data.stockPrice;
  43. ////cc.log(seed + " ++++++++++++++++++++++++++++++++++++++++++ " + proList[0].weight);
  44. if(this.bankRupt == true)
  45. {
  46. stockPrice = mapdata.inPrice;
  47. this.bankRupt = false;
  48. this.InLockLbl.node.active = false;
  49. this.CreateBtn.active = true;
  50. this.BuySaleBtns.active = false;
  51. }
  52. else
  53. {
  54. if(this.stockNum > 0) //只有股票 持有量大于0时才可用 计算该股票的 上涨概率加成
  55. {
  56. //cc.log("拥有这家公司时候 --刷第一个比例");
  57. seed += cc.Mgr.UserDataMgr.StockUpBonus / 100;
  58. if(seed < proList[0].weight) //跌
  59. {
  60. var iseed = Math.random() * (proList[0].upRatio - proList[0].lowRatio) + (1-proList[0].upRatio);
  61. stockPrice = stockPrice - this.Data.stockPrice * iseed;
  62. }
  63. else //涨
  64. {
  65. var iseed = Math.random() * (proList[1].upRatio - proList[1].lowRatio) + (proList[1].lowRatio - 1);
  66. stockPrice = stockPrice + this.Data.stockPrice * iseed;
  67. }
  68. }
  69. else
  70. {
  71. //cc.log("没有这家公司时候 --刷第二个比例");
  72. if(seed < unList[0].weight) //跌
  73. {
  74. var iseed = Math.random() * (unList[0].upRatio - unList[0].lowRatio) + (1 - unList[0].upRatio);
  75. stockPrice = stockPrice - this.Data.stockPrice * iseed;
  76. }
  77. else //涨
  78. {
  79. var iseed = Math.random() * (unList[1].upRatio - unList[1].lowRatio) + (unList[1].lowRatio - 1);
  80. stockPrice = stockPrice + this.Data.stockPrice * iseed;
  81. }
  82. }
  83. }
  84. this.Data.stockPrice = Math.floor(stockPrice * 100)/100;
  85. this.priceLbl.string = this.Data.stockPrice;
  86. this.costLbl.string = this.Data.stockCost;
  87. //有公司破产了
  88. if(this.Data.stockPrice <= this.Data.bankruptPrice)
  89. {
  90. this.bankRupt = true;
  91. this.InLockLbl.node.active = true;
  92. this.CreateBtn.active = false;
  93. this.BuySaleBtns.active = false;
  94. this.canGetProfit = false;
  95. cc.Mgr.UserDataMgr.CanGetStockProfit = false;
  96. this.InLockLbl.string = "该行业一片萧条";
  97. this.costLbl.string = "0";
  98. this.numLbl.string = "0";
  99. this.LimitYear = this.Data.LimitYear - 1;
  100. if(this.stockNum > 0)
  101. {
  102. if(cc.Mgr.UserDataMgr.BankruptAge == 0)
  103. {
  104. cc.Mgr.UserDataMgr.BankruptAge = cc.Mgr.UserDataMgr.Age;
  105. }
  106. cc.Mgr.UserDataMgr.Cash += Math.floor(this.Data.stockPrice * 0.5 * this.stockNum); //折半出售
  107. var param = {};
  108. param.Num = this.stockNum;
  109. param.price = this.stockPrice;
  110. param.Id = this.Data.Id;
  111. var reData = cc.Mgr.UserDataMgr.SaleStocks(param);
  112. this.stockNum = reData.stockNum;
  113. var param = {};
  114. param.forWhat = "";
  115. var comname = cc.Mgr.global.getTranslation("company_" +this.data.Id);
  116. param.text = "您创办的["+"<color=#e77122>"+comname+"</c>"+"]由于经营不善,已破产处理,公司股票将折半价抛售";
  117. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCommonTip , param);
  118. }
  119. }
  120. else
  121. {
  122. this.bankRupt = false;
  123. if(this.stockNum > 0 && this.LimitYear > 0)
  124. {
  125. if(this.LimitYear > 0)
  126. {
  127. this.LimitYear -= 1;
  128. this.InLockLbl.string = "创业期" + this.LimitYear + "年";
  129. }
  130. this.canGetProfit = false;
  131. }
  132. if(this.stockNum <= 0 && this.LimitYear <= 0)
  133. {
  134. this.InLockLbl.node.active = false;
  135. this.BuySaleBtns.active = false;
  136. this.CreateBtn.active = true;
  137. this.canGetProfit = false;
  138. }
  139. else if(this.LimitYear <= 0 && this.stockNum > 0)
  140. {
  141. this.InLockLbl.node.active = false;
  142. this.BuySaleBtns.active = true;
  143. this.canGetProfit = true;
  144. }
  145. var param = {};
  146. param.stockCost = this.Data.stockCost;
  147. param.stockPrice = this.Data.stockPrice;
  148. cc.Mgr.UserDataMgr.RefreshStockData(this.Id, param);
  149. }
  150. },
  151. RefreshUIShowAfter:function(){
  152. var num = cc.Mgr.UserDataMgr.getDataByItemTypeAndId(ItemType.Company, this.Id).stockNum;
  153. this.numLbl.string = Math.floor(num / 100);
  154. },
  155. RefreshUIShow:function(data){
  156. this.stockNum = data.stockNum;
  157. this.priceLbl.string = data.stockPrice.toString();
  158. this.costLbl.string = data.stockCost.toString();
  159. this.numLbl.string = Math.floor(data.stockNum / 100);
  160. if(data.stockNum > 0 && this.LimitYear <= 0)
  161. {
  162. this.InLockLbl.node.active = false;
  163. this.CreateBtn.active = false;
  164. this.BuySaleBtns.active = true;
  165. this.canGetProfit = true;
  166. }
  167. else if(data.stockNum > 0 && this.LimitYear > 0)
  168. {
  169. this.InLockLbl.node.active = true;
  170. this.CreateBtn.active = false;
  171. this.BuySaleBtns.active = false;
  172. this.canGetProfit = false;
  173. this.InLockLbl.string = "创业期" + this.LimitYear + "年";
  174. }
  175. else if(data.stockNum <= 0)
  176. {
  177. this.InLockLbl.node.active = false;
  178. this.CreateBtn.active = true;
  179. this.BuySaleBtns.active = false;
  180. this.canGetProfit = false;
  181. //出售完了 需要重新创办
  182. this.LimitYear = this.Data.LimitYear - 1;
  183. }
  184. },
  185. //创办公司
  186. OpenCompanyCreateTip:function(){
  187. cc.Mgr.AudioMgr.playSFX("click");
  188. if(cc.Mgr.UserDataMgr.userState == UserState.NotOK)
  189. {
  190. var param = {};
  191. param.text = cc.Mgr.global.getTranslation("InHealDownLine");
  192. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCommonTip, param);
  193. return;
  194. }
  195. if(cc.Mgr.UserDataMgr.hasCompany == true)
  196. {
  197. var param = {};
  198. param.forWhat = "";
  199. param.text = "<color=#e77122>您已经创办一家公司</c>";
  200. //cc.log("===提醒下已经有一家公司在名下===");
  201. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCommonTip, param);
  202. return;
  203. }
  204. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCompanyCreate, this.Data);
  205. },
  206. //出售股票
  207. OpenStuckSaleTip:function(){
  208. cc.Mgr.AudioMgr.playSFX("click");
  209. if(cc.Mgr.UserDataMgr.userState == UserState.NotOK)
  210. {
  211. var param = {};
  212. param.text = cc.Mgr.global.getTranslation("InHealDownLine");
  213. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCommonTip, param);
  214. return;
  215. }
  216. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenStuckSaleTip, this.Data);
  217. },
  218. //购入股票
  219. OpenStuckBuyTip:function(){
  220. cc.Mgr.AudioMgr.playSFX("click");
  221. if(cc.Mgr.UserDataMgr.userState == UserState.NotOK)
  222. {
  223. var param = {};
  224. param.text = cc.Mgr.global.getTranslation("InHealDownLine");
  225. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenCommonTip, param);
  226. return;
  227. }
  228. cc.director.GlobalEvent.emit(cc.Mgr.Event.OpenStuckBuyTip, this.Data);
  229. },
  230. });
  231. module.exports = CompanyItem;