You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
10 KiB
253 lines
10 KiB
import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
|
|
import EventMgr from "../../../FrameWork/Event/EventMgr";
|
|
import User from "../../../FrameWork/User/User";
|
|
import Common5 from "../../../Platform/th/Common5";
|
|
import TaskManager, { MainTaskIdEnum } from "../../JuQingChat/TaskManager";
|
|
|
|
const { ccclass, property } = cc._decorator;
|
|
|
|
let JuQingStockName = {
|
|
NiFengJiTuan: "逆风集团"
|
|
}
|
|
@ccclass
|
|
export default class StockManager {
|
|
public static JuQingStockName = JuQingStockName
|
|
//每天重新刷新股票的价格数据并写入内存(bForce是否强制刷新)
|
|
public static refreshAllStocksData(day, bForce) {
|
|
let stocksConfig = Common5.allStocksConfig
|
|
let curSaveStockDay = User.getCurSaveStockDay()
|
|
if (!bForce) {
|
|
//同一天不用重复刷新
|
|
if (curSaveStockDay == day) {
|
|
return
|
|
} else {
|
|
User.setCurSaveStockDay(day)
|
|
}
|
|
}
|
|
|
|
let stocksStorageConfig = User.getStocksData()
|
|
//第一天,将初始价格和涨幅写入内存
|
|
if (day == 1 || Object.keys(stocksStorageConfig).length === 0) {
|
|
let tempTab = {}
|
|
for (var i = 0; i < stocksConfig.length; i++) {
|
|
let stockName = stocksConfig[i].stockName
|
|
let startPrice = stocksConfig[i].startPrice
|
|
|
|
// console.log("股票名称=",stockName)
|
|
tempTab[stockName] = {}
|
|
tempTab[stockName]["stockName"] = stockName
|
|
tempTab[stockName]["curPrice"] = startPrice
|
|
tempTab[stockName]["curRate"] = this.createDropOrGainsRate(day, stocksConfig[i], startPrice)
|
|
tempTab[stockName]["riskAttributes"] = stocksConfig[i].riskAttributes
|
|
// tempTab[stockName]["stockSum"] = stocksConfig[i].stockSum
|
|
}
|
|
User.setStocksData(tempTab)
|
|
//当天的股价由前一天的股价和当天涨跌幅算出
|
|
} else {
|
|
let tempTab = {}
|
|
for (var key in stocksStorageConfig) {
|
|
let stockName = key
|
|
let lastPrice = stocksStorageConfig[stockName].curPrice
|
|
let singConfig = this.getSingleCompanyConfig(stockName)
|
|
let curRate = this.createDropOrGainsRate(day, singConfig, lastPrice)
|
|
let tempPrice = lastPrice * (1 + Number(curRate) / 100)
|
|
let curPrice = Math.floor(tempPrice)
|
|
tempTab[stockName] = {}
|
|
tempTab[stockName]["stockName"] = stockName
|
|
tempTab[stockName]["curPrice"] = curPrice
|
|
tempTab[stockName]["curRate"] = curRate
|
|
tempTab[stockName]["riskAttributes"] = singConfig.riskAttributes
|
|
// tempTab[stockName]["stockSum"] = singConfig.stockSum
|
|
}
|
|
User.setStocksData(tempTab)
|
|
}
|
|
//取出作弊数据后清空
|
|
let tomorrowStockData = User.getTomorrowStockData()
|
|
if (tomorrowStockData && tomorrowStockData.changeDay > 0 && day == tomorrowStockData.changeDay) {
|
|
User.setTomorrowStockData({})
|
|
}
|
|
|
|
EventMgr.emitEvent_custom(ryw_Event.updateStockView);
|
|
}
|
|
//生成涨跌幅比率
|
|
public static createDropOrGainsRate(day, config, curPrice) {
|
|
let trueRate = this.getTomorrowGainsRateById(day, config.id)
|
|
if (trueRate != -1) {
|
|
console.log("读取了明日股票缓存" + config.stockName + trueRate)
|
|
return trueRate
|
|
}
|
|
|
|
let randomRate = 0
|
|
//随机涨或跌
|
|
let riseval = 50
|
|
if (curPrice > config.startPrice * 1.3) {
|
|
//涨的概率降低
|
|
riseval -= 30 * ((curPrice - config.startPrice * 1.3) / (config.startPrice * 2.7))
|
|
}
|
|
else if (curPrice < config.startPrice * 0.8) {
|
|
if (curPrice <= config.startPrice * 0.05) {
|
|
riseval = 100
|
|
}
|
|
else {
|
|
//涨的概率提升
|
|
riseval += (50 * ((config.startPrice * 0.8 - curPrice) / (config.startPrice * 0.8))) * 1.2
|
|
}
|
|
}
|
|
|
|
let bGains = Common5.getRandomNum(0, 100)
|
|
if (bGains <= riseval) {
|
|
//涨
|
|
randomRate = Common5.getRandomNum(config.smallestGains * 10, config.biggestGains * 10)
|
|
}
|
|
else {
|
|
//跌
|
|
randomRate = Common5.getRandomNum(config.smallestDrop * 10, config.biggestDrop * 10)
|
|
}
|
|
|
|
let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
|
|
let mainId = mainTaskInfo.Id
|
|
if (config.stockName == "龙氏集团" && mainId != MainTaskIdEnum.MainTask_2404) {
|
|
randomRate = 0
|
|
}
|
|
trueRate = (randomRate / 10).toFixed(1)
|
|
//console.log("trueRate=",trueRate)
|
|
return trueRate
|
|
}
|
|
|
|
//获取明日涨跌幅比率
|
|
public static getTomorrowGainsRateById(day, id) {
|
|
let tomorrowStockData = User.getTomorrowStockData()
|
|
if (tomorrowStockData && tomorrowStockData.changeDay > 0 && day == tomorrowStockData.changeDay) {
|
|
for (var i = 0; i < tomorrowStockData.changeStock.length; i++) {
|
|
if (tomorrowStockData.changeStock[i].id == id) {
|
|
return tomorrowStockData.changeStock[i].rate
|
|
}
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
//获取单个公司config
|
|
public static getSingleCompanyConfig(nameStr) {
|
|
let stocksConfig = Common5.allStocksConfig
|
|
for (var i = 0; i < stocksConfig.length; i++) {
|
|
if (stocksConfig[i].stockName == nameStr) {
|
|
return stocksConfig[i]
|
|
}
|
|
}
|
|
let juQingStockConfig = Common5.juQingStocksConfig
|
|
for (var i = 0; i < juQingStockConfig.length; i++) {
|
|
if (juQingStockConfig[i].stockName == nameStr) {
|
|
return juQingStockConfig[i]
|
|
}
|
|
}
|
|
}
|
|
//获取单个公司config
|
|
public static getSingleCompanyConfigById(id) {
|
|
let stocksConfig = Common5.allStocksConfig
|
|
for (var i = 0; i < stocksConfig.length; i++) {
|
|
if (stocksConfig[i].id == id) {
|
|
return stocksConfig[i]
|
|
}
|
|
}
|
|
let juQingStockConfig = Common5.juQingStocksConfig
|
|
for (var i = 0; i < juQingStockConfig.length; i++) {
|
|
if (juQingStockConfig[i].id == id) {
|
|
return juQingStockConfig[i]
|
|
}
|
|
}
|
|
}
|
|
//随机产生第二天上涨的股票及涨幅
|
|
public static setTomorrowUpStock(curDay) {
|
|
let stocksConfig = Common5.allStocksConfig
|
|
let diFenXianIdConfig = []
|
|
let gaoFenXianIdConfig = []
|
|
for (var i = 0; i < stocksConfig.length; i++) {
|
|
if (stocksConfig[i].riskAttributes == "高") {
|
|
gaoFenXianIdConfig.push(stocksConfig[i].id)
|
|
} else if (stocksConfig[i].riskAttributes == "低") {
|
|
diFenXianIdConfig.push(stocksConfig[i].id)
|
|
}
|
|
}
|
|
let selectGaoIndexIdArr = Common5.getNumberFromArray(0, gaoFenXianIdConfig.length, 2)
|
|
let selectDiIndexIdArr = Common5.getNumberFromArray(0, diFenXianIdConfig.length, 2)
|
|
//全部放入一个表中
|
|
let tempAllInfo = []
|
|
for (var i = 0; i < selectGaoIndexIdArr.length; i++) {
|
|
tempAllInfo.push(gaoFenXianIdConfig[selectGaoIndexIdArr[i]])
|
|
}
|
|
for (var i = 0; i < selectDiIndexIdArr.length; i++) {
|
|
tempAllInfo.push(diFenXianIdConfig[selectDiIndexIdArr[i]])
|
|
}
|
|
console.log("tempAllInfo==", tempAllInfo)
|
|
let saveInfo = {
|
|
changeDay: curDay + 1,
|
|
changeStock: []
|
|
}
|
|
let stockNameArrGao = ""
|
|
let stockNameArrDi = ""
|
|
for (var i = 0; i < tempAllInfo.length; i++) {
|
|
let singleConfig = this.getSingleCompanyConfigById(tempAllInfo[i])
|
|
let randomRate = Common5.getRandomNum(singleConfig.smallestGains * 10, singleConfig.biggestGains * 10)
|
|
let trueRate = (randomRate / 10).toFixed(1)
|
|
saveInfo.changeStock.push({ id: tempAllInfo[i], rate: trueRate })
|
|
if (singleConfig.riskAttributes == "高") {
|
|
stockNameArrGao += `【${singleConfig.stockName}】`
|
|
} else {
|
|
stockNameArrDi += `【${singleConfig.stockName}】`
|
|
}
|
|
|
|
}
|
|
User.setTomorrowStockData(saveInfo)
|
|
console.log("明天会涨的股票包括==", stockNameArrGao, stockNameArrDi, saveInfo)
|
|
return { stockNameArrGao: stockNameArrGao, stockNameArrDi: stockNameArrDi }
|
|
}
|
|
//增加剧情股票(写入缓存)
|
|
public static addJuQingStock() {
|
|
let juQingStockConfig = Common5.juQingStocksConfig
|
|
//已有缓存
|
|
let stocksStorageConfig = User.getStocksData()
|
|
|
|
for (var i = 0; i < juQingStockConfig.length; i++) {
|
|
let stockName = juQingStockConfig[i].stockName
|
|
let startPrice = juQingStockConfig[i].startPrice
|
|
|
|
let temp = {}
|
|
temp[stockName] = {}
|
|
temp[stockName]["stockName"] = stockName
|
|
temp[stockName]["curPrice"] = startPrice
|
|
temp[stockName]["curRate"] = this.createDropOrGainsRate(1, juQingStockConfig[i], startPrice)
|
|
temp[stockName]["riskAttributes"] = juQingStockConfig[i].riskAttributes
|
|
|
|
//将新增对象插入第一个(深拷贝)
|
|
stocksStorageConfig = { ...temp, ...stocksStorageConfig };
|
|
}
|
|
User.setStocksData(stocksStorageConfig)
|
|
}
|
|
//获得剧情股票当前数据
|
|
public static getJuQingStockData(stockName) {
|
|
let singConfig = this.getSingleCompanyConfig(stockName)
|
|
let stocksStorageConfig = User.getStocksData()
|
|
|
|
for (var key in stocksStorageConfig) {
|
|
if (key == stockName) {
|
|
let marketValue = singConfig.stockSum * stocksStorageConfig[key].curPrice
|
|
console.log("需要获取的股票信息==", stockName, marketValue)
|
|
return { stockName: stockName, marketValue: marketValue }
|
|
}
|
|
}
|
|
}
|
|
|
|
//设置特殊股票数量 stockName股票名,rate比率
|
|
public static setSpecialStockNum(stockName,rate){
|
|
let config = this.getSingleCompanyConfig(stockName)
|
|
let price = config.startPrice
|
|
let num = rate*config.stockSum
|
|
let buyStockTab = {
|
|
stockName: stockName,
|
|
buyPrice: price,
|
|
prossess: num
|
|
}
|
|
User.setMyStockData(buyStockTab)
|
|
}
|
|
}
|
|
|