NeighbourManager.java
5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
}
}