Skip to content

Commit

Permalink
Fallbakc withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Feb 25, 2025
1 parent 5335147 commit 010e4b2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/main/java/link/locutus/discord/apiv1/enums/ResourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import link.locutus.discord.apiv1.enums.city.project.Project;
import link.locutus.discord.apiv1.enums.city.project.Projects;
import link.locutus.discord.db.entities.DBNation;
import link.locutus.discord.pnw.NationOrAlliance;
import link.locutus.discord.util.IOUtil;
import link.locutus.discord.util.MathMan;
import link.locutus.discord.util.StringMan;
Expand Down Expand Up @@ -134,13 +135,13 @@ public static Map<ResourceType, Double> roundResources(Map<ResourceType, Double>
return copy;
}

public static String resourcesToJson(String receiver, boolean isNation, Map<ResourceType, Double> rss, String note) {
public static Map<String, String> resourcesToJson(NationOrAlliance receiver, Map<ResourceType, Double> rss, String note) {
Map<String, String> post = new LinkedHashMap<>();
if (isNation) {
post.put("withrecipient", receiver);
if (receiver.isNation()) {
post.put("withrecipient", receiver.getName());
post.put("withtype", "Nation");
} else {
post.put("withrecipient", "" + receiver);
post.put("withrecipient", receiver.getName());
post.put("withtype", "Alliance");
}
for (ResourceType type : values) {
Expand All @@ -156,7 +157,7 @@ public static String resourcesToJson(String receiver, boolean isNation, Map<Reso
// for (Map.Entry<String, String> entry : post.entrySet()) {
// entry.setValue("\"" + entry.getValue() + "\"");
// }
return WebUtil.GSON.toJson(post);
return post;
}

public static Map<ResourceType, Double> parseResources(String arg) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/link/locutus/discord/util/PW.java
Original file line number Diff line number Diff line change
Expand Up @@ -1495,10 +1495,6 @@ public Integer apply(Double min, Double max) {
};
}

public static String getPostScript(String name, boolean nation, Map<ResourceType, Double> rss, String note) {
return ResourceType.resourcesToJson(name, nation, rss, note);
}

public static double getOdds(double attStrength, double defStrength, int success) {
attStrength = Math.pow(attStrength, 0.75);
defStrength = Math.pow(defStrength, 0.75);
Expand Down
32 changes: 28 additions & 4 deletions src/main/java/link/locutus/discord/util/offshore/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import link.locutus.discord.apiv3.enums.AlliancePermission;
import link.locutus.discord.config.Settings;
import link.locutus.discord.db.GuildDB;
import link.locutus.discord.db.entities.DBAlliancePosition;
import link.locutus.discord.db.entities.PendingTreaty;
import link.locutus.discord.db.entities.TaxBracket;
import link.locutus.discord.db.entities.DBNation;
import link.locutus.discord.db.entities.*;
import link.locutus.discord.network.IProxy;
import link.locutus.discord.network.PassthroughProxy;
import link.locutus.discord.pnw.NationOrAlliance;
Expand Down Expand Up @@ -739,6 +736,33 @@ public Set<TradeResult> call() throws Exception {
return login ? PW.withLogin(task, auth) : task.call();
}

public String withdrawResources(DBAlliance alliance, NationOrAlliance receiver, double[] amount, String note) {
Map<String, String> post = ResourceType.resourcesToJson(receiver, ResourceType.resourcesToMap(amount), note);
int fromBank = alliance.getAlliance_id();

return PW.withLogin(() -> {
String result = readStringFromURL(PagePriority.BANK_DEPOSIT, "" + Settings.INSTANCE.PNW_URL() + "/alliance/id=" + fromBank + "&display=bank", emptyMap());
Document dom = Jsoup.parse(result);
String token = dom.select("input[name=token]").attr("value");
post.put("token", token);
StringBuilder response = new StringBuilder();

result = readStringFromURL(PagePriority.TOKEN, "" + Settings.INSTANCE.PNW_URL() + "/alliance/id=" + fromBank + "&display=bank", post);
dom = Jsoup.parse(result);
for (Element element : dom.getElementsByClass("alert")) {
String text = element.text();
if (text.startsWith("Player Advertisement by ")) {
continue;
}
response.append('\n').append(text);
}
if (response.length() == 0) {
return "(not output)";
}
return response.toString();
}, this);
}

///////////////////////

public enum TradeResultType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,11 @@ public TransferResult transferSafe(DBNation nation, Map<ResourceType, Double> tr

public TransferResult transfer(DBNation nation, Map<ResourceType, Double> transfer, String note, Map<DBAlliance, Map<ResourceType, Double>> transferRoute) {
synchronized (BANK_LOCK) {
return transfer(null, nation, transfer, note, transferRoute);
Auth auth = null;
if (Settings.USE_V2) {
auth = getAlliance().getAuth(AlliancePermission.WITHDRAW_BANK);
}
return transfer(auth, nation, transfer, note, transferRoute);
}
}

Expand Down Expand Up @@ -1602,22 +1606,21 @@ public TransferResult transferUnsafe(Auth auth, NationOrAlliance receiver, Map<R
// todo test if game is still down
WebRoot web = WebRoot.getInstance();

BankRequestHandler handler = web.getLegacyBankHandler();
// BankRequestHandler handler = web.getLegacyBankHandler();
if (auth.getNationId() != Locutus.loader().getNationId() || auth.getAllianceId() != allianceId) {
throw new IllegalArgumentException("Game API is down currently");
}

UUID uuid = UUID.randomUUID();
Future<String> request = handler.addRequest(uuid, receiver, ResourceType.resourcesToArray(transfer), note);
DBAlliance aa = getAlliance();

try {
String response = request.get(20, TimeUnit.SECONDS);
String response = auth.withdrawResources(aa, receiver, ResourceType.resourcesToArray(transfer), note);
TransferResult category = categorize(receiver, transfer, note, response);
if (category.getStatus() == TransferStatus.SUCCESS || category.getStatus() == TransferStatus.SENT_TO_ALLIANCE_BANK) {
setLastSuccessfulTransfer(receiver, ResourceType.resourcesToArray(transfer));
}
return category;
} catch (InterruptedException | ExecutionException | TimeoutException e) {
} catch (Exception e) {
return new TransferResult(TransferStatus.OTHER, receiver, transfer, note).addMessage("Timeout: " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import link.locutus.discord.apiv1.enums.ResourceType;
import link.locutus.discord.pnw.NationOrAlliance;
import link.locutus.discord.util.PW;
import link.locutus.discord.web.WebUtil;
import org.json.JSONObject;

import java.util.Map;
Expand All @@ -19,7 +20,7 @@ public class BankRequestHandler {

public Future<String> addRequest(UUID uuid, NationOrAlliance recipient, double[] amt, String note) {
// token
String txStr = ResourceType.resourcesToJson(recipient.getName(), recipient.isNation(), ResourceType.resourcesToMap(amt), note);
String txStr = WebUtil.GSON.toJson(ResourceType.resourcesToJson(recipient, ResourceType.resourcesToMap(amt), note));
JSONObject json = new JSONObject();
json.put("transfer", new JSONObject(txStr));
json.put("token", uuid.toString());
Expand Down

0 comments on commit 010e4b2

Please sign in to comment.