mirror of
https://github.com/CarmJos/UltraDepository.git
synced 2024-09-19 19:55:45 +00:00
[v1.1.2] 采用Map的形式序列化用户数据。
This commit is contained in:
parent
bfbf55c9fc
commit
92b9c45911
41
.examples/userdata/uuid.json
Normal file
41
.examples/userdata/uuid.json
Normal file
@ -0,0 +1,41 @@
|
||||
{
|
||||
"farmer": {},
|
||||
"hunter": {
|
||||
"CHICKEN:0": {
|
||||
"amount": 3
|
||||
},
|
||||
"FEATHER:0": {
|
||||
"amount": 2
|
||||
}
|
||||
},
|
||||
"miner": {
|
||||
"COAL:0": {
|
||||
"amount": 9,
|
||||
"sold": 10
|
||||
},
|
||||
"LAPIS_LAZULI:0": {
|
||||
"amount": 9
|
||||
},
|
||||
"REDSTONE:0": {
|
||||
"sold": 129
|
||||
},
|
||||
"COBBLESTONE:0": {
|
||||
"amount": 12
|
||||
},
|
||||
"CLAY_BALL:0": {
|
||||
"amount": 112
|
||||
},
|
||||
"DIAMOND:0": {
|
||||
"amount": 975,
|
||||
"sold": 1500
|
||||
}
|
||||
},
|
||||
"fishman": {
|
||||
"COD:0": {
|
||||
"amount": 3
|
||||
},
|
||||
"PUFFERFISH:0": {
|
||||
"amount": 64
|
||||
}
|
||||
}
|
||||
}
|
@ -9,6 +9,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -148,4 +150,21 @@ public class UserData {
|
||||
this.storage.saveUserData(this);
|
||||
}
|
||||
|
||||
public Map<String, Map<String, Map<String, Integer>>> serializeToMap() {
|
||||
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
||||
|
||||
getDepositories().forEach((depositoryID, depositoryData) -> {
|
||||
Map<String, Map<String, Integer>> depositoryDataMap = new LinkedHashMap<>();
|
||||
depositoryData.getContents().forEach((itemType, itemData) -> {
|
||||
Map<String, Integer> itemDataMap = new HashMap<>();
|
||||
if (itemData.getAmount() > 0) itemDataMap.put("amount", itemData.getAmount());
|
||||
if (itemData.getSold() > 0) itemDataMap.put("sold", itemData.getSold());
|
||||
if (!itemDataMap.isEmpty()) depositoryDataMap.put(itemType, itemDataMap);
|
||||
});
|
||||
if (!depositoryDataMap.isEmpty()) values.put(depositoryID, depositoryDataMap);
|
||||
});
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,8 +15,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class FileStorage implements DataStorage {
|
||||
@ -104,21 +102,8 @@ public class FileStorage implements DataStorage {
|
||||
YamlConfiguration userDataConfig = new YamlConfiguration();
|
||||
userDataConfig.set("date", data.getDateInt());
|
||||
|
||||
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
||||
|
||||
data.getDepositories().forEach((depositoryID, depositoryData) -> {
|
||||
Map<String, Map<String, Integer>> depositoryDataMap = new LinkedHashMap<>();
|
||||
depositoryData.getContents().forEach((itemType, itemData) -> {
|
||||
Map<String, Integer> itemDataMap = new HashMap<>();
|
||||
if (itemData.getAmount() > 0) itemDataMap.put("amount", itemData.getAmount());
|
||||
if (itemData.getSold() > 0) itemDataMap.put("sold", itemData.getSold());
|
||||
if (!itemDataMap.isEmpty()) depositoryDataMap.put(itemType, itemDataMap);
|
||||
});
|
||||
if (!depositoryDataMap.isEmpty()) values.put(depositoryID, depositoryDataMap);
|
||||
});
|
||||
|
||||
try {
|
||||
userDataConfig.createSection("depositories", values);
|
||||
userDataConfig.createSection("depositories", data.serializeToMap());
|
||||
userDataConfig.save(new File(getDataContainer(), data.getUserUUID() + ".yml"));
|
||||
} catch (IOException ioException) {
|
||||
Main.error("在保存玩家 #" + data.getUserUUID() + " 的数据时出现异常。");
|
||||
|
@ -18,7 +18,6 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.ResultSet;
|
||||
@ -133,12 +132,16 @@ public class MySQLStorage implements DataStorage {
|
||||
for (Map.Entry<String, JsonElement> entry : dataElement.getAsJsonObject().entrySet()) {
|
||||
Depository depository = Main.getDepositoryManager().getDepository(entry.getKey());
|
||||
if (depository == null) continue;
|
||||
|
||||
DepositoryData contentsData = parseContentsData(depository, data, entry.getValue());
|
||||
if (contentsData != null) data.setDepository(contentsData);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Main.debug("通过 MySQLStorage 加载 " + uuid + " 的用户数据完成,"
|
||||
+ "耗时 " + (System.currentTimeMillis() - start) + "ms。");
|
||||
|
||||
return data;
|
||||
}
|
||||
Main.debug("当前库内不存在玩家 " + uuid + " 的数据,视作新档。");
|
||||
@ -153,15 +156,13 @@ public class MySQLStorage implements DataStorage {
|
||||
long start = System.currentTimeMillis();
|
||||
Main.debug("正通过 MySQLStorage 保存 " + data.getUserUUID() + " 的用户数据...");
|
||||
|
||||
JsonObject dataObject = new JsonObject();
|
||||
|
||||
data.getDepositories().forEach((id, contents) -> dataObject.add(id, serializeContentsData(contents)));
|
||||
|
||||
try {
|
||||
|
||||
getSQLManager().createReplace(SQLTables.USER_DATA.getName())
|
||||
.setColumnNames("uuid", "data", "day")
|
||||
.setParams(data.getUserUUID(), GSON.toJson(dataObject), data.getDate())
|
||||
.setParams(data.getUserUUID(), GSON.toJson(data.serializeToMap()), data.getDate())
|
||||
.execute();
|
||||
|
||||
} catch (SQLException exception) {
|
||||
Main.error("在保存玩家 #" + data.getUserUUID() + " 的数据时出现异常。");
|
||||
Main.error("Error occurred when saving #" + data.getUserUUID() + " data.");
|
||||
@ -191,10 +192,13 @@ public class MySQLStorage implements DataStorage {
|
||||
private DepositoryData parseContentsData(Depository source, UserData owner, JsonObject contentsObject) {
|
||||
DepositoryData data = DepositoryData.emptyContents(source, owner);
|
||||
for (Map.Entry<String, JsonElement> entry : contentsObject.entrySet()) {
|
||||
|
||||
DepositoryItem item = source.getItems().get(entry.getKey());
|
||||
if (item == null) continue;
|
||||
|
||||
DepositoryItemData itemData = parseItemData(item, data, entry.getValue());
|
||||
if (itemData != null) data.getContents().put(item.getTypeID(), itemData);
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@ -207,26 +211,8 @@ public class MySQLStorage implements DataStorage {
|
||||
int amount = itemObject.has("amount") ? itemObject.get("amount").getAsInt() : 0;
|
||||
int sold = itemObject.has("sold") ? itemObject.get("sold").getAsInt() : 0;
|
||||
if (amount == 0 && sold == 0) return null;
|
||||
else return new DepositoryItemData(source, owner, amount, sold);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private JsonObject serializeContentsData(@Nullable DepositoryData contentsData) {
|
||||
if (contentsData == null) return null;
|
||||
JsonObject contentsObject = new JsonObject();
|
||||
contentsData.getContents().entrySet().stream()
|
||||
// 只存取有数值的部分,减少数据量
|
||||
.filter(entry -> entry.getValue().getSold() > 0 || entry.getValue().getAmount() > 0)
|
||||
.forEach(entry -> contentsObject.add(entry.getKey(), serializeItemData(entry.getValue())));
|
||||
return contentsObject;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsonObject serializeItemData(@NotNull DepositoryItemData itemData) {
|
||||
JsonObject itemObject = new JsonObject();
|
||||
if (itemData.getAmount() > 0) itemObject.addProperty("amount", itemData.getAmount());
|
||||
if (itemData.getSold() > 0) itemObject.addProperty("sold", itemData.getSold());
|
||||
return itemObject;
|
||||
return new DepositoryItemData(source, owner, amount, sold);
|
||||
}
|
||||
|
||||
}
|
||||
|
41
src/test/java/GsonMapTest.java
Normal file
41
src/test/java/GsonMapTest.java
Normal file
@ -0,0 +1,41 @@
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class GsonMapTest {
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Map<String, Map<String, Map<String, Integer>>> values = new LinkedHashMap<>();
|
||||
|
||||
|
||||
for (int u = 0; u < 3; u++) {
|
||||
Map<String, Map<String, Integer>> depositoryDataMap = new LinkedHashMap<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Map<String, Integer> itemDataMap = new HashMap<>();
|
||||
int amount = Math.max(0, new Random().nextInt(15));
|
||||
int sold = Math.max(0, new Random().nextInt(15));
|
||||
if (amount > 0) itemDataMap.put("amount", amount);
|
||||
if (sold > 0) itemDataMap.put("sold", sold);
|
||||
if (!itemDataMap.isEmpty()) {
|
||||
depositoryDataMap.put(UUID.randomUUID().toString().substring(0, 4), itemDataMap);
|
||||
}
|
||||
}
|
||||
if (!depositoryDataMap.isEmpty()) {
|
||||
values.put(UUID.randomUUID().toString().substring(0, 8), depositoryDataMap);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(values.size());
|
||||
|
||||
|
||||
String jsonValues = GSON.toJson(values);
|
||||
System.out.println(jsonValues);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user