Skip to content

Commit

Permalink
Add in-game money management (closes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Feb 26, 2023
1 parent 2579ece commit da1b6ab
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group 'com.azuriom'
version '1.2.0'
version '1.2.1'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public boolean persist() {
public String onRequest(OfflinePlayer player, String params) {
if (params.equalsIgnoreCase("money")) {
return this.plugin.getPlugin()
.getUser(player.getName())
.getUserManager()
.getUserByName(player.getName())
.map(user -> FORMATTER.format(user.getMoney()))
.orElse("?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import com.azuriom.azlink.common.data.PlayerData;
import com.azuriom.azlink.common.data.ServerData;
import com.azuriom.azlink.common.data.SystemData;
import com.azuriom.azlink.common.data.UserInfo;
import com.azuriom.azlink.common.data.WorldData;
import com.azuriom.azlink.common.http.client.HttpClient;
import com.azuriom.azlink.common.http.server.HttpServer;
import com.azuriom.azlink.common.http.server.NettyHttpServer;
import com.azuriom.azlink.common.logger.LoggerAdapter;
import com.azuriom.azlink.common.scheduler.SchedulerAdapter;
import com.azuriom.azlink.common.tasks.FetcherTask;
import com.azuriom.azlink.common.users.UserManager;
import com.azuriom.azlink.common.utils.SystemUtils;
import com.azuriom.azlink.common.utils.UpdateChecker;
import com.google.gson.Gson;
Expand All @@ -30,7 +30,6 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand All @@ -41,6 +40,7 @@ public class AzLinkPlugin {
private static final Gson GSON_PRETTY_PRINT = new GsonBuilder().setPrettyPrinting().create();

private final HttpClient httpClient = new HttpClient(this);
private final UserManager userManager = new UserManager(this);

private final AzLinkCommand command = new AzLinkCommand(this);

Expand Down Expand Up @@ -190,8 +190,8 @@ public HttpServer getHttpServer() {
return this.httpServer;
}

public Optional<UserInfo> getUser(String name) {
return this.fetcherTask.getUser(name);
public UserManager getUserManager() {
return this.userManager;
}

protected HttpServer createHttpServer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.azuriom.azlink.common.command;

import com.azuriom.azlink.common.AzLinkPlugin;
import com.azuriom.azlink.common.data.UserInfo;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public class AzLinkCommand {

private static final List<String> COMPLETIONS = Arrays.asList("status", "setup", "fetch", "port");
private static final List<String> COMPLETIONS = Arrays.asList("status", "setup", "fetch", "money", "port");
private static final List<String> MONEY_ACTIONS = Arrays.asList("add", "remove", "set");

private final AzLinkPlugin plugin;

Expand Down Expand Up @@ -47,6 +50,15 @@ public void execute(CommandSender sender, String[] args) {
return;
}

if (args[0].equalsIgnoreCase("money")) {
try {
editMoney(sender, args);
} catch (NumberFormatException e) {
sender.sendMessage("&c'" + args[3] + "' is not a valid number !");
}
return;
}

if (args[0].equalsIgnoreCase("fetch")) {
this.plugin.fetch()
.thenRun(() -> sender.sendMessage("&6Data has been fetched successfully."))
Expand Down Expand Up @@ -110,9 +122,54 @@ public List<String> tabComplete(CommandSender sender, String[] args) {
.collect(Collectors.toList());
}

if (args.length == 2 && args[0].equalsIgnoreCase("money")) {
return MONEY_ACTIONS.stream()
.filter(s -> startsWithIgnoreCase(s, args[1]))
.collect(Collectors.toList());
}

if (args.length == 3 && args[0].equalsIgnoreCase("money")) {
return this.plugin.getPlatform().getOnlinePlayers()
.map(CommandSender::getName)
.filter(name -> startsWithIgnoreCase(name, args[2]))
.collect(Collectors.toList());
}

return Collections.emptyList();
}

public void editMoney(CommandSender sender, String[] args) throws NumberFormatException {
if (args.length < 4 || !MONEY_ACTIONS.contains(args[1].toLowerCase())) {
sender.sendMessage("&cUsage: /azlink money <add|remove|set> <player> <amount>");
return;
}

String action = args[1].toLowerCase();
double amount = Double.parseDouble(args[3]);
Optional<UserInfo> user = this.plugin.getUserManager().getUserByName(args[2]);

if (amount <= 0) {
sender.sendMessage("&cThe amount must be positive.");
return;
}

if (!user.isPresent()) {
sender.sendMessage("&cUnable to find player '" + args[2] + "', please try again in few seconds or use '/azlink fetch'.");
return;
}

this.plugin.getUserManager().editMoney(user.get(), action, amount)
.thenAccept(u -> {
sender.sendMessage("&aMoney has been edited successfully.");
sender.sendMessage("&aNew balance: " + u.getMoney());
})
.exceptionally(ex -> {
sender.sendMessage("&cUnable to edit money: " + ex.getMessage());

return null;
});
}

public String getUsage() {
return "Usage: /azlink [" + String.join("|", COMPLETIONS) + "]";
}
Expand Down
14 changes: 12 additions & 2 deletions common/src/main/java/com/azuriom/azlink/common/data/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

public class UserInfo {

private final int id;
private final String name;
private final double money;
private double money;

public UserInfo(String name, double money) {
public UserInfo(int id, String name, double money) {
this.id = id;
this.name = name;
this.money = money;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public void setMoney(double money) {
this.money = money;
}

public double getMoney() {
return money;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.azuriom.azlink.common.AzLinkPlugin;
import com.azuriom.azlink.common.data.ServerData;
import com.azuriom.azlink.common.data.UserInfo;
import com.azuriom.azlink.common.data.WebsiteResponse;
import com.azuriom.azlink.common.users.EditMoneyResult;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
Expand Down Expand Up @@ -51,6 +53,14 @@ public CompletableFuture<Void> updateEmail(UUID uuid, String email) {
return request(RequestMethod.POST, "/azlink/email", params);
}

public CompletableFuture<EditMoneyResult> editMoney(UserInfo user, String action, double amount) {
String endpoint = "/azlink/user/" + user.getId() + "/money/" + action;
JsonObject params = new JsonObject();
params.addProperty("amount", amount);

return request(RequestMethod.POST, endpoint, params, EditMoneyResult.class);
}

public CompletableFuture<WebsiteResponse> postData(ServerData data) {
return request(RequestMethod.POST, "/azlink", data, WebsiteResponse.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

public class FetcherTask implements Runnable {

private final Map<String, UserInfo> usersByName = new ConcurrentHashMap<>();
private final AzLinkPlugin plugin;

private Instant lastFullDataSent = Instant.MIN;
Expand Down Expand Up @@ -59,18 +56,14 @@ public CompletableFuture<Void> fetch() {
.thenAcceptAsync(res -> handleResponse(res, sendFullData), sync);
}

public Optional<UserInfo> getUser(String name) {
return Optional.ofNullable(usersByName.get(name));
}

private void handleResponse(WebsiteResponse response, boolean sendFullData) {
if (response == null) {
return;
}

if (response.getUsers() != null) {
for (UserInfo user : response.getUsers()) {
this.usersByName.put(user.getName(), user);
this.plugin.getUserManager().addUser(user);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.azuriom.azlink.common.users;

import com.google.gson.annotations.SerializedName;

public class EditMoneyResult {

@SerializedName("old_balance")
private final double oldBalance;
@SerializedName("new_balance")
private final double newBalance;

public EditMoneyResult(double oldBalance, double newBalance) {
this.oldBalance = oldBalance;
this.newBalance = newBalance;
}

public double getOldBalance() {
return oldBalance;
}

public double getNewBalance() {
return newBalance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.azuriom.azlink.common.users;

import com.azuriom.azlink.common.AzLinkPlugin;
import com.azuriom.azlink.common.data.UserInfo;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class UserManager {

private final Map<String, UserInfo> usersByName = new ConcurrentHashMap<>();
private final AzLinkPlugin plugin;

public UserManager(AzLinkPlugin plugin) {
this.plugin = plugin;
}

public Optional<UserInfo> getUserByName(String name) {
return Optional.ofNullable(this.usersByName.get(name));
}

public void addUser(UserInfo user) {
this.usersByName.put(user.getName(), user);
}

public CompletableFuture<UserInfo> editMoney(UserInfo user, String action, double amount) {
return this.plugin.getHttpClient().editMoney(user, action, amount)
.thenApply(result -> {
user.setMoney(result.getNewBalance());
return user;
});
}
}
1 change: 1 addition & 0 deletions nukkit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
repositories {
maven { url 'https://repo.opencollab.dev/maven-releases/' }
maven { url 'https://repo.opencollab.dev/maven-snapshots/' }
}

Expand Down

0 comments on commit da1b6ab

Please sign in to comment.