marineparkclient/assets/script/Model/ProxyClientDataMgr.ts
2023-08-15 11:09:12 +08:00

494 lines
20 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { EventCenter } from '../../common/ui/uiEv/EventCenter';
import gameCfg from '../config/gameCfg';
import { MSG_Event } from '../DefineMsg/LogicMsg';
import { hallproto } from '../netPb/protobundle';
import AccountManager from './data/AccountManager';
import { baseObj, composeObj, objType, valueObj } from './data/baseObject';
import DataManager from './data/DataManager';
import dataManager from './data/DataManager';
import { fileCfgType, IDDef } from './data/excelDefine';
import { propType, PropUnlock, PropLeaveUp, PropCatch, IPSignIn, PropSignIn } from './data/propObject';
import ProxyNetDataMgr from './ProxyNetDataMgr';
/**
* 模型层
* 数据代理类
*/
//离线收益
export interface offLineData{
addCoin:number, //总增加的金币
parkfee:number, //场馆收费金币
fishfee:number, //钓鱼出售金币
fishNums:number, //钓鱼数量
fishNew:number, //新解锁的鱼数量
offLineTime:number, //离线时间
}
//在线钓鱼统计
export interface fishCathOnline{
id:number,
type:number, //1标识解锁2标识出售
numSale:number, //出售数量
coinSale:number,//出售价值
}
export default class ProxyClientDataMgr {
private static instance: any;
public static ins(): ProxyClientDataMgr
{
this.instance = this.instance || new ProxyClientDataMgr()
return this.instance
}
m_version:string;
feeTimmer = 0;//收费计时器
m_bStartFishStatistics = false;//钓鱼统计
fishStatistics:Array<fishCathOnline> = Array<fishCathOnline>()//关闭钓鱼界面,在线期间的统计
//初始化游戏数据
initGameData(){
EventCenter.on(MSG_Event.ReadStartCfg,this.downConfigResOver,this)
this.checkVersion()
//
}
//检测版本号
checkVersion()
{
ProxyNetDataMgr.ins().getDataFromHttp("cfgVersion.json",(data)=>{
console.log("curServerVersion:",this.m_version)
let curcfg = cc.sys.localStorage.getItem("cfgVersion")
if(!data)
{
EventCenter.emit(MSG_Event.Net_ERRO,"网络响应超时,没有获取到版本数据")
return
}
let decodeData = JSON.parse(data)
if(!curcfg || curcfg!=decodeData.version)//网络获取
{
this.m_version = decodeData.version
ProxyNetDataMgr.ins().downConfig()//获取配置文件
}
else//读取本地文件
{
this.m_version = curcfg
this.readConfigToObj()
}
})
}
downConfigResOver(cur:number,max:number){
if(cur==max)
{
console.log("downConfigResOver",cur,max)
cc.sys.localStorage.setItem("cfgVersion",this.m_version)
EventCenter.targetOff(this)
}
else
console.log("downConfigResOver",cur,max)
}
//
//获取资产
getValueObj(id:number)
{
let findDt = AccountManager.ins().getGameInfo().findMeans(id)
if(findDt.getObjType()==objType.value)
return (findDt as valueObj)
else
return new valueObj()
}
//获取组合对象
getComposeObj(id:number)
{
let findDt = AccountManager.ins().getGameInfo().findMeans(id)
if(findDt&&findDt.getObjType()==objType.compose)
return (findDt as composeObj)
else
return new composeObj()
}
//获取签到属性
getSingInProp()
{
return AccountManager.ins().getGameInfo().getPropByType(propType.signIn)
}
//读取配置转对象
readConfigToObj(){
let exp = cc.sys.localStorage.getItem("express.txt");
DataManager.ins().readConfigToObj(exp,fileCfgType.ruleFunc)
EventCenter.emit(MSG_Event.ReadStartCfg,1,5)
let level = cc.sys.localStorage.getItem("level.txt");
DataManager.ins().readConfigToObj(level,fileCfgType.level)
EventCenter.emit(MSG_Event.ReadStartCfg,2,5)
let obj = cc.sys.localStorage.getItem("obj.txt");
DataManager.ins().readConfigToObj(obj,fileCfgType.obj)
EventCenter.emit(MSG_Event.ReadStartCfg,3,5)
let sale = cc.sys.localStorage.getItem("sale.txt");
DataManager.ins().readConfigToObj(sale,fileCfgType.sale)
EventCenter.emit(MSG_Event.ReadStartCfg,4,5)
let unlock = cc.sys.localStorage.getItem("unlock.txt");
DataManager.ins().readConfigToObj(unlock,fileCfgType.unlock)
EventCenter.emit(MSG_Event.ReadStartCfg,5,5)
}
//保存数据
saveGameDataById(id:number){
if(id!=IDDef.COIN)//不是金币
{
console.log("saveGameDataById:",id)
}
let gameInfo = AccountManager.ins().getGameInfo()
let saveData = ProxyNetDataMgr.ins().saveGameData
if(id==IDDef.SIGNIN)//签到保存
{
let sInData = saveData.singInData
let objPropSIn = this.getSingInProp() as PropSignIn
sInData.weekOnetime = objPropSIn.getWeekOneTime()
sInData.singInlist = []
for(let idx=1;idx<=7;++idx){
let sInList = objPropSIn.getSignInfo(idx)
if(sInList.eSigin==1||sInList.eSigin==2)//已签到
{
let pbSInDt = hallproto.SingInData.singIn.create({weekDay:sInList.weekDay,sInType:sInList.eSigin})
sInData.singInlist.push(pbSInDt)
}
}
}
else{//资产类型
let obj = gameInfo.findMeans(id)
if(!obj)return
let meansDataTemp = hallproto.MeansData.create()
meansDataTemp.id = id
meansDataTemp.prop = {}//hallproto.MeansProp.create()
if(obj.getObjType()==objType.value)//数量
meansDataTemp.prop.num = (obj as valueObj).getCount()
let unlockProp = obj.getPropByType(propType.unlock)
let levelProp = obj.getPropByType(propType.level)
let catchProp = obj.getPropByType(propType.catch)
if(unlockProp)//解锁
{
meansDataTemp.prop.unlockfee = (unlockProp as PropUnlock).getFee()
meansDataTemp.prop.bUnlock = (unlockProp as PropUnlock).getUnLock()
}
if(levelProp)//升级
meansDataTemp.prop.level = (levelProp as PropLeaveUp).getLevel()
if(catchProp)//可捕获对象
meansDataTemp.prop.catchFishes = (catchProp as PropCatch).getAble()
let isIn = false;
for(let idx=0;idx<saveData.meansData.length;++idx)
{
if(saveData.meansData[idx].id==id){
saveData.meansData[idx].prop = meansDataTemp.prop
isIn = true;
break;
}
}
if(isIn==false)//添加
saveData.meansData.push(meansDataTemp)
}
let uint8GameInfoBody = hallproto.CMD_GAMEINFO.encode(saveData).finish()
cc.sys.localStorage.setItem("gameData",JSON.stringify(saveData))//Uint8ArrayToString(uint8GameInfoBody))//保存到本地
}
//默认解锁升级
unlockLevelUpDefault(){
//默认解锁
let dtCfg = DataManager.ins().getDataConfig()
let gameInfo = AccountManager.ins().getGameInfo()
let members = gameInfo.getMemberMap()
for (let [itemId,value] of members){
let unlockCfg = DataManager.ins().getUnlockCfgById(itemId)
if(!unlockCfg)//没有解锁条件可默认解锁
{
this.doUnlockItem(itemId)
}
}
}
//离线收益
OffLineRevenue(){
let curSecend = Math.floor(new Date().getTime()/1000)
let lastParkFeeTime = cc.sys.localStorage.getItem("lastSaveParkFee")
if(!lastParkFeeTime)return;//没有离线收益
let delaySecond = curSecend - Number(lastParkFeeTime)
if(delaySecond>6*60)//6分钟
{
let gameInfo = AccountManager.ins().getGameInfo()
//场馆收益
let delayJieduan = []
let cfgOffLinePark = gameCfg.offLineParkTime
let tempSecend = delaySecond
for(let idx=0;idx<cfgOffLinePark.length;++idx)
{
if(tempSecend<=0)break
let mins = cfgOffLinePark[idx][0] //分钟
let jdTime = tempSecend>=(mins*60)?mins*60:tempSecend //分钟
delayJieduan.push(jdTime)
tempSecend -= jdTime
}
let allAddCoin = 0
for(let idx=0;idx<delayJieduan.length;++idx)
{
allAddCoin += gameInfo.admissionFee()*cfgOffLinePark[idx][1]*delayJieduan[idx]
}
allAddCoin = Math.floor(allAddCoin)
let offlineDt:offLineData={
addCoin: allAddCoin,
parkfee: allAddCoin,
fishNums: 0,
fishNew: 0,
fishfee: 0,
offLineTime:delaySecond,
}
//钓鱼收益
let offLineMinuts = delaySecond/60;//分钟
if(offLineMinuts>gameCfg.offLineFishTime[2])offLineMinuts=gameCfg.offLineFishTime[2] //最大时间
let CountTimes = Math.floor(offLineMinuts/gameCfg.offLineFishTime[3])//6分钟一次计算
if(CountTimes>0)//间隔时间
{
let resultCatch = gameInfo.doCatchFish(CountTimes,gameCfg.offLineFishTime[0],gameCfg.offLineFishTime[1])
//新捕获的鱼
for(let idx=0;idx<resultCatch.vecNewCatched.length;++idx)//保存数据
{
offlineDt.fishNums += 1;
ProxyClientDataMgr.ins().saveGameDataById(resultCatch.vecNewCatched[idx])
}
//总捕获鱼数量
for(let [k,v] of resultCatch.mapCatched){
offlineDt.fishNums += v;
}
offlineDt.fishNew = resultCatch.vecNewCatched.length;
offlineDt.fishfee = resultCatch.allFee
offlineDt.addCoin += offlineDt.fishfee;
}
if(offlineDt.addCoin>0)
{
let coinObj = gameInfo.getMember(IDDef.COIN)
if(coinObj)
(coinObj as valueObj).changeCount(offlineDt.addCoin)
}
ProxyClientDataMgr.ins().saveGameDataById(IDDef.COIN)//金币变化保存
EventCenter.emit(MSG_Event.OffLineRevenue,offlineDt)
}
cc.sys.localStorage.setItem("lastSaveParkFee",String(curSecend))
}
//启动场馆收费
startParkFee(){
var secMin = gameCfg.parkFeeTime[0],
secMax = gameCfg.parkFeeTime[1];
let randTm = Math.floor(Math.random() * (secMax - secMin + 1) + secMin); //Generate Random number between 15 - 30
function delayTimeOver()
{
let gameInfo = AccountManager.ins().getGameInfo()
let secendCoin = randTm*gameInfo.admissionFee()
let coinObj = gameInfo.getMember(IDDef.COIN)
if(coinObj)
(coinObj as valueObj).changeCount(secendCoin)
console.log("=========getParkFee"+secendCoin+" 当前金币:"+(coinObj as valueObj).getCount())
EventCenter.emit(MSG_Event.ParkFeeCoin,secendCoin)
ProxyClientDataMgr.ins().saveGameDataById(IDDef.COIN)//金币变化保存
let curSecend = Math.floor(new Date().getTime()/(1*1000))
cc.sys.localStorage.setItem("lastSaveParkFee",String(curSecend))
randTm = Math.floor(Math.random() * (secMax - secMin + 1) + secMin); //Generate Random number between 15 - 30
setTimeout(delayTimeOver, randTm * 1000);
}
setTimeout(delayTimeOver, randTm * 1000);
}
//出海捕鱼
goFishing(){
let self = this;
setInterval(function(){ //在线 一分钟一条鱼
let gameInfo = AccountManager.ins().getGameInfo()
let resultCatch = gameInfo.doCatchFish(1,50,50)//在线按50的比例捕获
let catchedData:fishCathOnline={
type : 0,
numSale : 0,
id : 0,
coinSale: 0,
}
if(resultCatch.mapCatched.size==1)//钓到一条
{
let desp = "=========goFishing,"
for (let [idkey, num] of resultCatch.mapCatched) {
catchedData.id = idkey;
catchedData.numSale = num;
let fishName = DataManager.ins().getObjCfgById(idkey)._name
desp += "["+String(idkey)+"]"+fishName+" num:"+String(num)+"条 "
}
console.log(desp)
if(resultCatch.vecNewCatched.length>0)//解锁新鱼
{
catchedData.type = 1;
desp = "钓到新解锁鱼:"
for(let idx=0;idx<resultCatch.vecNewCatched.length;++idx)
{
let catchId = resultCatch.vecNewCatched[idx]
let fishName = DataManager.ins().getObjCfgById(catchId)._name
desp += String(catchId)+" "+ fishName + " "
}
console.log(desp)
}
else{
catchedData.type = 2;
catchedData.coinSale = resultCatch.allFee;
}
}
if(resultCatch.allFee>0)
{
let coinObj = gameInfo.getMember(IDDef.COIN)
if(coinObj)
(coinObj as valueObj).changeCount(resultCatch.allFee)
console.log("出售钓得的鱼获得金币:"+resultCatch.allFee+" 当前金币:"+(coinObj as valueObj).getCount())
ProxyClientDataMgr.ins().saveGameDataById(IDDef.COIN)//金币变化保存
}
EventCenter.emit(MSG_Event.FishSaleCoin,catchedData)
//新捕获的鱼
for(let idx=0;idx<resultCatch.vecNewCatched.length;++idx)//保存数据
{
ProxyClientDataMgr.ins().saveGameDataById(resultCatch.vecNewCatched[idx])
}
if(self.m_bStartFishStatistics==true)//加入统计
{
let bIn = false;
for(let idx=0;idx<self.fishStatistics.length;++idx){
if(self.fishStatistics[idx].id == catchedData.id &&catchedData.type==2 && self.fishStatistics[idx].type==2)//出售统计
{
bIn = true;
self.fishStatistics[idx].numSale += catchedData.numSale;
self.fishStatistics[idx].coinSale += catchedData.coinSale;
break;
}
}
if(bIn==false)//新加入
self.fishStatistics.push(catchedData)
}
// let curMin = Math.floor(new Date().getTime()/(60*1000))//分钟
// cc.sys.localStorage.setItem("lastSaveFishTime",String(curMin))
}, gameCfg.fishingTime*1000);
}
//获取后台钓鱼统计
doFishStatistics(bStart:boolean)
{
this.m_bStartFishStatistics = bStart;//启动统计
if(bStart==true)//开启统计
this.fishStatistics = new Array<fishCathOnline>()
return this.fishStatistics;
}
//解锁物品
doUnlockItem(id:number){
let gameInfo = AccountManager.ins().getGameInfo()
let coinSecond = gameInfo.admissionFee()//每秒收益
let [resp,bSave] = gameInfo.doUnlockObj(id,coinSecond)
if(resp==true)
{
if(bSave)
ProxyClientDataMgr.ins().saveGameDataById(id)//
//子对象默认解锁
let data = gameInfo.findMeans(id)
if(data.getObjType()==objType.compose)//有成员
{
let members = (data as composeObj).getMemberMap()
for(let [mId,mVl] of members){
let unlockCfg = DataManager.ins().getUnlockCfgById(mId)
if(!unlockCfg)//没有解锁条件,可默认解锁
{
this.doUnlockItem(mId);
}
}
}
}
}
//升级物品
doGroupUpItem(id:number){
let gameInfo = AccountManager.ins().getGameInfo()
if(gameInfo.doLevelUpObj(id)==true)
ProxyClientDataMgr.ins().saveGameDataById(id)//
}
//获取解锁描述
getUnlockDesp(id:number){
let gameInfo = AccountManager.ins().getGameInfo()
return gameInfo.getUnLockDesp(id)//
}
//获取等级buff描述
getBuffDesp(id:number,lv?:number){
let gameInfo = AccountManager.ins().getGameInfo()
return gameInfo.getBuffDesp(id,lv)//
}
//获取升级描述
getGroupUpDesp(id:number){
let gameInfo = AccountManager.ins().getGameInfo()
return gameInfo.getUpLevelDesp(id)//
}
//是否可解锁
isUnLockAble(id:number){
let gameInfo = AccountManager.ins().getGameInfo()
return gameInfo.isUnlockObjAble(id)//
}
//是否可升级
isGroupUpAble(id:number){
let gameInfo = AccountManager.ins().getGameInfo()
return gameInfo.isUpLevelObjAble(id)//
}
//当日是否可签到
isTodaySignInAble(){
let saveData = ProxyNetDataMgr.ins().saveGameData
let signWeekDay = saveData.singInData.weekOnetime //签到的周一时间
let curDate = new Date();//当日时间
let dt0DianTm = new Date(curDate.getFullYear(),curDate.getMonth(),curDate.getDate())
let curWeekDay = (dt0DianTm.getDay()-1+7)%7+1//当前星期x
let dwWeekOneTm = new Date()
dwWeekOneTm.setTime(dt0DianTm.getTime()-(curWeekDay-1)*24*3600*1000)//星期一的毫秒数
if(dwWeekOneTm.getTime() === (+signWeekDay))//同一周数据
{
for(let idx=0;idx<saveData.singInData.singInlist.length;++idx)
{
let curSIn = saveData.singInData.singInlist[idx]
if(curSIn.weekDay == curWeekDay)//当天已签到
return false;
}
}
return true;//可签到
}
//签到,wDay-7
doSignIn(wDay:number){
let gameInfo = AccountManager.ins().getGameInfo()
let sInProp = gameInfo.getPropByType(propType.signIn) as PropSignIn
if(sInProp.isSignAble(wDay)==true){
sInProp.doSign(wDay);//签到
//获取奖励
let sInData = sInProp.getSignInfo(wDay) as IPSignIn
let allAddCoin = 0;
if(sInData.rewardType == 1)//增加金币数量
{
allAddCoin = sInData.rewardValue
}
else{//按当前收益增加小时数量
let curFee = gameInfo.admissionFee()//当前每秒收益
let allFee = curFee*sInData.rewardValue*(60*60)*curFee;
allAddCoin = Math.floor(allFee)
}
if(allAddCoin>0)
{
let coinObj = gameInfo.getMember(IDDef.COIN)
if(coinObj)
(coinObj as valueObj).changeCount(allAddCoin)
console.log("签到星期"+wDay+"获得金币:"+allAddCoin+" 当前金币:"+(coinObj as valueObj).getCount())
ProxyClientDataMgr.ins().saveGameDataById(IDDef.COIN)//金币变化保存
EventCenter.emit(MSG_Event.OperChangeCoin,allAddCoin)
}
ProxyClientDataMgr.ins().saveGameDataById(IDDef.SIGNIN)//签到保存
EventCenter.emit(MSG_Event.Net_DoSignInResp,{coin:allAddCoin,weekDay:wDay})
}
}
}