scManager.java 11 KB
package com.lotus.town;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.lotus.town.event.LtEvent;
import com.lotus.town.event.McEvent;
import com.lotus.town.service.em.EmManager;
import com.lotus.town.utils.Utils;
import com.sdk.Sdk;
import com.sdk.utils.TimeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.TimeZone;

public class scManager {
    private static final String TAG = "scManager";
    private static scManager sScManager = null;
    private Context mContext;
    private static Object sLock = new Object();
    private SharedPreferences mSharedPreference;
    private static final String LOTUS_KEY = "l_t_k";
    private static final String QW_LIVE_KEY = "q_w_l_k";
    private static final String MONEY_KEY = "m_y_k";
    private static final String TODAY_MONEY_KEY = "t_m_y_k";

    private static final long LOTUS_OFFLINE_REWARD_INTERVAL = 60 * 60 * 1000;
    private static final int LOTUS_OFFLINE_REWARD_UNIT = 1;
    private static final String LOTUS_OFFLINE_DATE = "l_t_o_d";
    private static final String LOTUS_OFFLINE_REWARD_TOTAL = "l_t_o_r_t";
    private static final String LOTUS_OFFLINE_REWARD_CURRENT = "l_t_o_r_c";
    private static final String LOTUS_OFFLINE_LAST_REWARD_TIME = "l_t_o_l_r_t";

    private static final int QINGWA_MAX_LEVEL = 20;
    private static final int QINGWA_LEVEL_POWER_UNIT = 20;
    private static final String QINGWA_CURRENT_LEVEL = "q_w_c_l";
    private static final String QINGWA_CURRENT_POWER = "q_w_c_p";

    private static final String VERSION_KEY = "version";
    private static final int VERSION = 1;

    public scManager(Context context){
        mContext = context;
        mSharedPreference = mContext.getSharedPreferences("s_c",Context.MODE_PRIVATE);

        final SharedPreferences.Editor editor = mSharedPreference.edit();
        Long lastTime = mSharedPreference.getLong("r_t",0);

        int oldVersion = mSharedPreference.getInt(VERSION_KEY, 0);
        if (oldVersion != VERSION) {
            if (oldVersion == 0) {
                try {
                    int lotus = mSharedPreference.getInt(LOTUS_KEY, 0);
                    editor.remove(LOTUS_KEY);
                    editor.putFloat(LOTUS_KEY, lotus);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            editor.putInt(VERSION_KEY, VERSION);
            editor.apply();
        }

        if(TimeUtils.isSameDay(lastTime,System.currentTimeMillis(), TimeZone.getDefault())){
            return;
        }
        editor.putLong("r_t",System.currentTimeMillis());
        editor.putFloat(LOTUS_KEY, 0f);
        editor.putString(TODAY_MONEY_KEY,"");
        editor.putBoolean(QW_LIVE_KEY,false).commit();
    }

    public static scManager getInstance(Context context){
        if(sScManager == null){
            synchronized (sLock){
                if(sScManager == null){
                    sScManager = new scManager(context);
                }
            }
        }
        return sScManager;
    }

    public void init(){

    }

    public void reportOpen() {
        calculateOfflineReward();
    }

    public float getLotusOfflineReward() {
        float reward = mSharedPreference.getFloat(LOTUS_OFFLINE_REWARD_CURRENT, 0);
        SharedPreferences.Editor editor = mSharedPreference.edit();
        editor.putFloat(LOTUS_OFFLINE_REWARD_CURRENT, 0);
        editor.apply();
        return reward;
    }

    private float getTotalLotusOfflineReward() {
        return mSharedPreference.getFloat(LOTUS_OFFLINE_REWARD_TOTAL, 0);
    }

    private void calculateOfflineReward() {
        String currentDate = Utils.getCurrentDate();
        String lastRewardDate = getLastOfflineRewardDate();
        long currentTime = System.currentTimeMillis();

        if (!TextUtils.equals(currentDate, lastRewardDate)) {
            SharedPreferences.Editor editor = mSharedPreference.edit();
            editor.remove(LOTUS_OFFLINE_REWARD_TOTAL);
            editor.remove(LOTUS_OFFLINE_LAST_REWARD_TIME);
            editor.remove(LOTUS_OFFLINE_REWARD_CURRENT);
            editor.putString(LOTUS_OFFLINE_DATE, currentDate);
            if (lastRewardDate == null) {
                editor.putFloat(LOTUS_KEY, getLotus());
            } else {
                editor.putFloat(LOTUS_KEY, 0);
            }
            editor.apply();
        }

        long lastTime = getLastOfflineRewardTime();
        long interval = currentTime - lastTime;
        Log.d(TAG, "calculateOfflineReward: currentTime: " + currentTime + ", lastTime: " + lastTime);

        float totalReward = getTotalLotusOfflineReward();
        float rewardSpeed = getOfflineRewardSpeed();
        if (totalReward >= 24 * rewardSpeed) {
            Log.d(TAG, "calculateOfflineReward: all offline reward got");
            return;
        }
        float addReward = 0;

        if (interval >= LOTUS_OFFLINE_REWARD_INTERVAL) {
            int hourTick = (int) (interval / LOTUS_OFFLINE_REWARD_INTERVAL);
            Log.d(TAG, "calculateOfflineReward: hourTick: " + hourTick);
            addReward = hourTick * rewardSpeed;
        } else if (totalReward < 1) {
            addReward += LOTUS_OFFLINE_REWARD_UNIT;
        }

        if (Utils.getCurrentHourOfDay() == 23 && (addReward + totalReward) < 24 * rewardSpeed) {
            addReward += rewardSpeed;
        }

        Log.d(TAG, "calculateOfflineReward: addReward: " + addReward + ", totalReward: " + totalReward);

        if (addReward > 0) {
            float currentReward = getLotusOfflineReward();

            SharedPreferences.Editor editor = mSharedPreference.edit();
            editor.putFloat(LOTUS_OFFLINE_REWARD_TOTAL, totalReward + addReward);
            editor.putFloat(LOTUS_OFFLINE_REWARD_CURRENT, currentReward + addReward);
            editor.putLong(LOTUS_OFFLINE_LAST_REWARD_TIME, currentTime);
            editor.apply();
            addLotus(addReward);
        }
    }

    private String getLastOfflineRewardDate() {
        return mSharedPreference.getString(LOTUS_OFFLINE_DATE, null);
    }

    private long getLastOfflineRewardTime() {
        long lastTime = mSharedPreference.getLong(LOTUS_OFFLINE_LAST_REWARD_TIME, 0);
        return lastTime != 0 ? lastTime : Utils.getZeroTimeToday();
    }

    public void reportMoney(Context context){
        double money = scManager.getInstance(context).getMoney() * 1000;
        Sdk.logger().logEventValue(context, new Double(money).intValue());
    }

    public float getLotus(){
        return mSharedPreference.getFloat(LOTUS_KEY,0f);
    }

    public void addLotus(float count){
        float lotus = mSharedPreference.getFloat(LOTUS_KEY,0);
        lotus = lotus + count;
        mSharedPreference.edit().putFloat(LOTUS_KEY,lotus).commit();
        EventBus.getDefault().post(new LtEvent(lotus));
    }

    public double getTodayMoney(){
        double m = 0;
        try {
            String money = mSharedPreference.getString(TODAY_MONEY_KEY, "0");
            m = Double.parseDouble(money);
        } catch (Exception e){
            return 0;
        }
        return m;
    }

    public void todayEarnMoney(double money){
        double m = 0;
        try {
            String ms = mSharedPreference.getString(TODAY_MONEY_KEY, "0");
            m = Double.parseDouble(ms);
        } catch (Exception e){
        }
        m = m+money;
        EmManager.em((int)(money*1000));
        mSharedPreference.edit().putString(TODAY_MONEY_KEY,""+m).commit();
        reportMoney(mContext);
        EventBus.getDefault().post(new McEvent(m));
    }

    public double getMoney(){
        double m = 0;
        try {
            String money = mSharedPreference.getString(MONEY_KEY, "0");
            m = Double.parseDouble(money);
        } catch (Exception e){
            return 0;
        }
        return m;
    }

    public void resetMoney(int money){
        double m = money/1000;
        mSharedPreference.edit().putString(MONEY_KEY,m+"").commit();
    }

    public void earnMoney(double money){
        double m = 0;
        try {
            String ms = mSharedPreference.getString(MONEY_KEY, "0");
            m = Double.parseDouble(ms);
        } catch (Exception e){
        }
        m = m+money;
        todayEarnMoney(money);
        mSharedPreference.edit().putString(MONEY_KEY,""+m).commit();
        EventBus.getDefault().post(new McEvent(m));
    }

    public boolean withDrawMoney(double money){
        double m = 0;
            try {
            String ms = mSharedPreference.getString(MONEY_KEY, "0");
            m = Double.parseDouble(ms);
        } catch (Exception e){
        }
        if(m < money){
            return false;
        }

        m = m - money;
        mSharedPreference.edit().putString(MONEY_KEY,""+m).commit();
        EventBus.getDefault().post(new McEvent(m));
        return true;

    }

    public boolean isWqDead(){
        return mSharedPreference.getBoolean(QW_LIVE_KEY,false);
    }

    public void qWDead(){
        mSharedPreference.edit().putBoolean(QW_LIVE_KEY,true).commit();
    }

    public void wQRevival(){
        mSharedPreference.edit().putBoolean(QW_LIVE_KEY,false).commit();
    }

    public int addPower(int power) {
        int distancePower;
        int currentLevel = getQingwaCurrentLevel();
        int newLevel = currentLevel;
        int currentPower = getQingwaPower() + power;
        if (currentPower >= currentLevel * QINGWA_LEVEL_POWER_UNIT) {
            distancePower = 0;
            if (currentLevel <= getMaxQingwaLevel()) {
                currentPower -= currentLevel * QINGWA_LEVEL_POWER_UNIT;
                newLevel += 1;
                saveQingwaLevel(newLevel);
            }
        } else {
            distancePower = currentLevel * QINGWA_LEVEL_POWER_UNIT - currentPower;
        }

        Log.d(TAG, "addPower: currentLevel: " + currentLevel + ", newLevel:" + newLevel + ", currentPower: " + currentPower + ", distancePower: " + distancePower);

        saveQingwaPower(currentPower);
        return distancePower;
    }

    public int getPowerDistance(){
        int distancePower;
        int currentPower = getQingwaPower();
        int currentLevel = getQingwaCurrentLevel();
        distancePower = currentLevel * QINGWA_LEVEL_POWER_UNIT - currentPower;
        return distancePower;
    }

    private int getMaxQingwaLevel() {
        return QINGWA_MAX_LEVEL;
    }

    public int getQingwaCurrentLevel() {
        return mSharedPreference.getInt(QINGWA_CURRENT_LEVEL, 1);
    }

    private void saveQingwaLevel(int level) {
        SharedPreferences.Editor editor = mSharedPreference.edit();
        editor.putInt(QINGWA_CURRENT_LEVEL, level);
        editor.apply();
    }

    public int getQingwaPower() {
        return mSharedPreference.getInt(QINGWA_CURRENT_POWER, 0);
    }

    private void saveQingwaPower(int power) {
        SharedPreferences.Editor editor = mSharedPreference.edit();
        editor.putInt(QINGWA_CURRENT_POWER, power);
        editor.apply();
    }

    public float getOfflineRewardSpeed() {
        return getQingwaCurrentLevel() / 2f * LOTUS_OFFLINE_REWARD_UNIT;
    }
}