feat: code
This commit is contained in:
96
common/src/main/java/online/mineroo/common/cache/UserInfoCache.java
vendored
Normal file
96
common/src/main/java/online/mineroo/common/cache/UserInfoCache.java
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
package online.mineroo.common.cache;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import online.mineroo.common.request.UserRequest.SimpleUserInfoResponse;
|
||||
|
||||
public class UserInfoCache {
|
||||
private ConcurrentHashMap<String, UUID> uuidMap = new ConcurrentHashMap<>();
|
||||
private ConcurrentHashMap<UUID, SimpleUserCacheInfo> simpleUserInfo = new ConcurrentHashMap<>();
|
||||
|
||||
public UserInfoCache() {}
|
||||
|
||||
public UUID insert(UUID uuid, SimpleUserInfoResponse simpleUserInfoResponse) {
|
||||
this.simpleUserInfo.put(uuid, new SimpleUserCacheInfo(simpleUserInfoResponse));
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public UUID insert(String name, SimpleUserInfoResponse simpleUserInfoResponse) {
|
||||
if (this.uuidMap.containsKey(name.toLowerCase())) {
|
||||
UUID uuid = this.uuidMap.get(name.toLowerCase());
|
||||
this.simpleUserInfo.put(uuid, new SimpleUserCacheInfo(simpleUserInfoResponse));
|
||||
return uuid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SimpleUserInfoResponse get(UUID uuid) {
|
||||
SimpleUserCacheInfo info = this.simpleUserInfo.get(uuid);
|
||||
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// LocalDateTime expireTime = info.getTime().plusHours(24);
|
||||
// if (LocalDateTime.now().isAfter(expireTime)) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
return info.getData();
|
||||
}
|
||||
|
||||
public SimpleUserInfoResponse get(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
UUID uuid = this.uuidMap.get(name.toLowerCase());
|
||||
if (uuid == null) {
|
||||
return null;
|
||||
}
|
||||
return this.get(uuid);
|
||||
}
|
||||
|
||||
public void remove(UUID uuid) {
|
||||
if (uuid == null) {
|
||||
return;
|
||||
}
|
||||
this.simpleUserInfo.remove(uuid);
|
||||
this.uuidMap.values().removeIf(val -> val.equals(uuid));
|
||||
}
|
||||
|
||||
public void updateNameMapping(String name, UUID uuid) {
|
||||
if (name != null && uuid != null) {
|
||||
this.uuidMap.put(name.toLowerCase(), uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
// LocalDateTime now = LocalDateTime.now();
|
||||
//
|
||||
// simpleUserInfo.entrySet().removeIf(entry -> {
|
||||
// return now.isAfter(entry.getValue().getTime().plusHours(24));
|
||||
// });
|
||||
//
|
||||
// uuidMap.entrySet().removeIf(entry -> !simpleUserInfo.containsKey(entry.getValue()));
|
||||
}
|
||||
|
||||
public static class SimpleUserCacheInfo {
|
||||
private SimpleUserInfoResponse data;
|
||||
private LocalDateTime time;
|
||||
|
||||
public SimpleUserCacheInfo(SimpleUserInfoResponse data) {
|
||||
this.data = data;
|
||||
this.time = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public SimpleUserInfoResponse getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public LocalDateTime getTime() {
|
||||
return this.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,30 @@ public class UserRequest {
|
||||
this.networkService = networkService;
|
||||
}
|
||||
|
||||
public static class SimpleUserInfoResponse implements Serializable {
|
||||
@SerializedName("username") private String username;
|
||||
@SerializedName("nickname") private String nickname;
|
||||
@SerializedName("avatar") private String avatar;
|
||||
@SerializedName("user_id") private int userId;
|
||||
|
||||
// -- Getters
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public String getNickname() {
|
||||
return this.nickname;
|
||||
}
|
||||
|
||||
public String getAvatar() {
|
||||
return this.avatar;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return this.userId;
|
||||
}
|
||||
}
|
||||
|
||||
public class PlayerBindStatusResponse implements Serializable {
|
||||
@SerializedName("status") private PlayerBindStatusEnum status;
|
||||
|
||||
@@ -244,4 +268,31 @@ public class UserRequest {
|
||||
return createPlayerBindErrorResponse("Network Error: " + e.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<SimpleUserInfoResponse> getUserInfo(UUID uuid) {
|
||||
String path = "/server/user/info";
|
||||
String simpleUuid = uuid.toString().replace("-", "");
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
params.put("uuid", simpleUuid);
|
||||
|
||||
return networkService.getData(path, params)
|
||||
.thenApply(response -> {
|
||||
Gson gson = new Gson();
|
||||
if (response.getStatusCode() == 200) {
|
||||
try {
|
||||
return gson.fromJson(response.getBody(), SimpleUserInfoResponse.class);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to parse UserInfo JSON: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.exceptionally(e -> {
|
||||
logger.error("Mineroo Bind: Network exception", e);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user