NeighbourManager.java 5.14 KB
package com.lotus.town.neighgour;

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

import com.lotus.town.R;
import com.lotus.town.utils.Utils;
import com.sdk.Sdk;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class NeighbourManager {
    private static final String TAG = "NeighbourManager";
    private static NeighbourManager INSTANCE = new NeighbourManager();

    private static final String NEIGHBOUR_PREFERENCES = "neighbour";
    private static final String NEIGHBOUR_PRE_DATE = "date";
    private static final String NEIGHBOUR_PRE_N_1 = "1";
    private static final String NEIGHBOUR_PRE_N_2 = "2";
    private static final String NEIGHBOUR_PRE_N_3 = "3";

    private static final String NEIGHGOUR_1 = "汤姆";
    private static final String NEIGHGOUR_2 = "杰克";
    private static final String NEIGHGOUR_3 = "三个小镇";

    private List<Neighbour> mNeighbourList;

    private NeighbourManager() {
        SharedPreferences sharedPreferences = Sdk.app().getSharedPreferences(NEIGHBOUR_PREFERENCES, Context.MODE_PRIVATE);
        String dateStr = sharedPreferences.getString(NEIGHBOUR_PRE_DATE, null);
        String currentDataStr = Utils.getCurrentDate();

        Log.d(TAG, "NeighbourManager: dateStr: " + dateStr + ", currentDataStr: " + currentDataStr);

        if (TextUtils.equals(currentDataStr, dateStr) && dateStr != null) {
            mNeighbourList = build(sharedPreferences);
        } else {
            mNeighbourList = resetPreferences(currentDataStr, sharedPreferences);
//            mNeighbourList = build(false, sharedPreferences);
        }

        Log.d(TAG, "NeighbourManager: neighbours: " + mNeighbourList);
    }

    public static NeighbourManager getInstance() {
        return INSTANCE;
    }

    public Neighbour getNeighbour() {
        for (Neighbour neighbour : mNeighbourList) {
            if (neighbour.jumpCount < 3) {
                return neighbour;
            }
        }
        return null;
    }

    public void jump(Neighbour neighbour) {
        neighbour.jumpCount++;
        SharedPreferences sharedPreferences = Sdk.app().getSharedPreferences(NEIGHBOUR_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        for (Neighbour data : mNeighbourList) {
            editor.putString(data.getKey(), data.getData());
        }
        editor.apply();
    }

    public void earn(Neighbour neighbour, double money) {
        neighbour.earn(money);
        SharedPreferences sharedPreferences = Sdk.app().getSharedPreferences(NEIGHBOUR_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        for (Neighbour data : mNeighbourList) {
            editor.putString(data.getKey(), data.getData());
        }
        editor.apply();
    }
    
    public String getNeighbourMoney(Neighbour neighbour){
        // TODO: 2019-06-24 当前邻居获取的钱 
        return "0";
    }

    List<Neighbour> build(SharedPreferences sharedPreferences) {
        List<Neighbour> neighbourList = new ArrayList<>();
        String neighbour = sharedPreferences.getString(NEIGHBOUR_PRE_N_1, null);
        String[] neighbourData = neighbour.split(",");
        double todayMoney = neighbourData.length > 3 ? Double.valueOf(neighbourData[3]) : 0;
        neighbourList.add(new Neighbour(1, neighbourData[0], Integer.valueOf(neighbourData[1]), R.drawable.head, Double.valueOf(neighbourData[2]), todayMoney));
        neighbour = sharedPreferences.getString(NEIGHBOUR_PRE_N_2, null);
        neighbourData = neighbour.split(",");
        todayMoney = neighbourData.length > 3 ? Double.valueOf(neighbourData[3]) : 0;
        neighbourList.add(new Neighbour(2, neighbourData[0], Integer.valueOf(neighbourData[1]), R.drawable.youke, Double.valueOf(neighbourData[2]), todayMoney));
        neighbour = sharedPreferences.getString(NEIGHBOUR_PRE_N_3, null);
        neighbourData = neighbour.split(",");
        todayMoney = neighbourData.length > 3 ? Double.valueOf(neighbourData[3]) : 0;
        neighbourList.add(new Neighbour(3, neighbourData[0], Integer.valueOf(neighbourData[1]), R.drawable.youke2, Double.valueOf(neighbourData[2]), todayMoney));

        return neighbourList;
    }

    private List<Neighbour> resetPreferences(String dataStr, SharedPreferences sharedPreferences) {
        Random random = new Random(System.currentTimeMillis());
        List<Neighbour> neighbourList = new ArrayList<>();
        neighbourList.add(new Neighbour(1, NEIGHGOUR_1, 0, R.drawable.head, random.nextInt(5) + 10, 0));
        neighbourList.add(new Neighbour(2, NEIGHGOUR_2, 0, R.drawable.youke, random.nextInt(5) + 10, 0));
        neighbourList.add(new Neighbour(3, NEIGHGOUR_3, 0, R.drawable.youke2, random.nextInt(5) + 10, 0));

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();

        editor.putString(NEIGHBOUR_PRE_DATE, dataStr);
        for (Neighbour neighbour : neighbourList) {
            editor.putString(neighbour.getId() + "", neighbour.getData());
        }

        editor.apply();
        return neighbourList;
    }
}