
import UserModel from "../../modules/user/UserModel";
import GameConst from "../common/GameConst";
export default class Data {
    public static user: UserModel = new UserModel();


    public static save(bRemote: boolean = false) {
        var obj = {}
        obj['user'] = Data.user.getUploadData()
        Data.savedata(obj, bRemote)
    }

    public static getGameData() {
        let strdata = localStorage.getItem(GameConst.local_data_key);
        return strdata;
    }


    public static loadData() {
        var localdata = localStorage.getItem(GameConst.local_data_key);
        if (localdata) {
            localdata = JSON.parse(localdata);
            console.log('本地数据', localdata)
            Data.user.setData(localdata['user'])

        }
    }

    private static savedatatime: number = 0;
    //本地存数据
    private static savedata(data, bRemote: boolean = false) {
        localStorage.setItem("savedatatime", Date.now().toString());
        var strdata = JSON.stringify(data);
        //  console.log('存储数据',strdata)
        localStorage.setItem(GameConst.local_data_key, strdata);

        data.game = {}
        if (Date.now() - this.savedatatime > 60 * 3 * 1000 || bRemote) {
            this.savedatatime = Date.now();
        }
    }
}