Add initial project backend structure with Docker support and configuration files. Added some useful features such as async requests, automatically adding card by message from chat etc
Signed-off-by: Dmitrii <computer@yawaflua.tech> Took 9 minutes
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package git.yawaflua.tech.spmega.client;
|
||||
|
||||
import git.yawaflua.tech.spmega.client.ui.UiNotifications;
|
||||
import git.yawaflua.tech.spmega.client.ui.service.BankUiService;
|
||||
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.text.ClickEvent;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ChatListener {
|
||||
private final UiNotifications notifications = UiNotifications.instance();
|
||||
private final Pattern TRANSACTION_PATTERN = Pattern.compile(
|
||||
".*(Управление картой).*",
|
||||
Pattern.CASE_INSENSITIVE
|
||||
);
|
||||
|
||||
public void register() {
|
||||
ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, timestamp) -> {
|
||||
try {
|
||||
handleMessage(message);
|
||||
} catch (Throwable t) {
|
||||
System.err.println("[SPMega] Ошибка обработки сообщения в CHAT листенере:");
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
ClientReceiveMessageEvents.GAME.register((message, overlay) -> {
|
||||
try {
|
||||
handleMessage(message);
|
||||
} catch (Throwable t) {
|
||||
System.err.println("[SPMega] Ошибка обработки сообщения в GAME листенере:");
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleMessage(Text message) {
|
||||
if (message == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String text = message.getString();
|
||||
if (TRANSACTION_PATTERN.matcher(text).matches()) {
|
||||
List<String> clickValues = new ArrayList<>();
|
||||
collectClickEventValues(message, clickValues);
|
||||
|
||||
if (clickValues.size() >= 2) {
|
||||
String tokenId = clickValues.get(0);
|
||||
String cardId = clickValues.get(1);
|
||||
|
||||
MinecraftClient client = MinecraftClient.getInstance();
|
||||
if (client.player != null) {
|
||||
String playerUuid = client.player.getUuidAsString();
|
||||
|
||||
CompletableFuture
|
||||
.supplyAsync(() -> BankUiService.instance().addCard(cardId, tokenId, playerUuid))
|
||||
.thenAccept(msg -> {
|
||||
if (client == null) {
|
||||
return;
|
||||
}
|
||||
client.execute(() -> {
|
||||
notifications.showMessage(msg);
|
||||
});
|
||||
})
|
||||
.exceptionally(exception -> {
|
||||
if (client != null) {
|
||||
client.execute(() -> {
|
||||
notifications.show(Text.literal("Ошибка добавления карты: " + exception.getMessage()));
|
||||
});
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
System.out.println("[SPMega] Недостаточно кликабельных данных в сообщении (найдено: " + clickValues.size() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void collectClickEventValues(Text text, List<String> values) {
|
||||
if (text == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ClickEvent clickEvent = text.getStyle().getClickEvent();
|
||||
if (clickEvent != null) {
|
||||
try {
|
||||
String value = getEventValue(clickEvent);
|
||||
if (value != null && !value.isEmpty()) {
|
||||
values.add(value);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
System.err.println("[SPMega] Ошибка извлечения значения клик-события:");
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
for (Text sibling : text.getSiblings()) {
|
||||
collectClickEventValues(sibling, values);
|
||||
}
|
||||
}
|
||||
|
||||
private String getEventValue(ClickEvent clickEvent) {
|
||||
if (clickEvent instanceof ClickEvent.CopyToClipboard(String value)) {
|
||||
return value;
|
||||
} else if (clickEvent instanceof ClickEvent.RunCommand(String command1)) {
|
||||
return command1;
|
||||
} else if (clickEvent instanceof ClickEvent.SuggestCommand(String command)) {
|
||||
return command;
|
||||
} else if (clickEvent instanceof ClickEvent.OpenUrl(java.net.URI uri)) {
|
||||
return uri.toString();
|
||||
} else if (clickEvent instanceof ClickEvent.OpenFile(String path)) {
|
||||
return path;
|
||||
} else if (clickEvent instanceof ClickEvent.ChangePage(int page)) {
|
||||
return String.valueOf(page);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -167,5 +167,7 @@ public class SPMegaClient implements ClientModInitializer {
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
new ChatListener().register();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ public class PaymentScreen extends Screen {
|
||||
private Text senderCardText = Text.literal("Карта отправителя: не выбрана");
|
||||
private ButtonWidget transferButton;
|
||||
private ButtonWidget backButton;
|
||||
private boolean transferInProgress;
|
||||
|
||||
public PaymentScreen(Screen parent) {
|
||||
this(parent, "");
|
||||
@@ -192,6 +193,11 @@ public class PaymentScreen extends Screen {
|
||||
}
|
||||
|
||||
private void submit() {
|
||||
if (transferInProgress) {
|
||||
notifications.show(Text.literal("Перевод уже выполняется"));
|
||||
return;
|
||||
}
|
||||
|
||||
CardViewModel selectedCard = bankUiService.getSelectedCard();
|
||||
if (selectedCard == null) {
|
||||
notifications.show(Text.literal("Нет выбранной карты отправителя"));
|
||||
@@ -205,7 +211,8 @@ public class PaymentScreen extends Screen {
|
||||
notifications.show(Text.literal("Сумма должна быть больше 0"));
|
||||
return;
|
||||
}
|
||||
} catch (NumberFormatException exception) {
|
||||
} catch (Exception exception) {
|
||||
System.out.println(exception.getMessage());
|
||||
notifications.show(Text.literal("Некорректная сумма"));
|
||||
return;
|
||||
}
|
||||
@@ -232,18 +239,46 @@ public class PaymentScreen extends Screen {
|
||||
commentField.getText().trim()
|
||||
);
|
||||
|
||||
boolean accepted = bankUiService.submitPayment(draft);
|
||||
String serviceMessage = bankUiService.getLastMessage();
|
||||
if (!serviceMessage.isBlank()) {
|
||||
setTransferInProgress(true);
|
||||
notifications.show(Text.literal("Отправка перевода..."));
|
||||
|
||||
notifications.showMessage(serviceMessage);
|
||||
} else {
|
||||
notifications.show(accepted
|
||||
? Text.literal("Перевод выполнен")
|
||||
: Text.literal("Перевод отклонен"));
|
||||
bankUiService.submitPaymentAsync(draft)
|
||||
.thenAccept(accepted -> {
|
||||
if (this.client == null) {
|
||||
return;
|
||||
}
|
||||
this.client.execute(() -> {
|
||||
setTransferInProgress(false);
|
||||
|
||||
String serviceMessage = bankUiService.getLastMessage();
|
||||
if (!serviceMessage.isBlank()) {
|
||||
notifications.showMessage(serviceMessage);
|
||||
} else {
|
||||
notifications.show(accepted
|
||||
? Text.literal("Перевод выполнен")
|
||||
: Text.literal("Перевод отклонен"));
|
||||
}
|
||||
|
||||
updateSenderCardSelector();
|
||||
updateSenderCardText();
|
||||
});
|
||||
})
|
||||
.exceptionally(exception -> {
|
||||
if (this.client != null) {
|
||||
this.client.execute(() -> {
|
||||
setTransferInProgress(false);
|
||||
notifications.show(Text.literal("Ошибка перевода: " + exception.getMessage()));
|
||||
});
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void setTransferInProgress(boolean inProgress) {
|
||||
transferInProgress = inProgress;
|
||||
if (transferButton != null) {
|
||||
transferButton.active = !inProgress;
|
||||
}
|
||||
updateSenderCardSelector();
|
||||
updateSenderCardText();
|
||||
}
|
||||
|
||||
private boolean isValidRecipient(String recipient) {
|
||||
@@ -277,7 +312,7 @@ public class PaymentScreen extends Screen {
|
||||
private void updateSenderCardSelector() {
|
||||
CardViewModel selectedCard = bankUiService.getSelectedCard();
|
||||
if (selectedCard == null) {
|
||||
senderCardLabelButton.setMessage(Text.literal("\"00000\": \"0\" АР"));
|
||||
senderCardLabelButton.setMessage(Text.literal("TEST 00000: 0 АР"));
|
||||
senderLeftButton.active = false;
|
||||
senderRightButton.active = false;
|
||||
return;
|
||||
@@ -285,9 +320,8 @@ public class PaymentScreen extends Screen {
|
||||
|
||||
senderLeftButton.active = true;
|
||||
senderRightButton.active = true;
|
||||
String cardId = extractCardId(selectedCard.title());
|
||||
String balance = Long.toString(selectedCard.balance());
|
||||
senderCardLabelButton.setMessage(Text.literal("\"" + cardId + "\": \"" + balance + "\" АР"));
|
||||
senderCardLabelButton.setMessage(Text.literal(selectedCard.title() + ": " + balance + " АР"));
|
||||
senderCardLabelButton.active = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import net.minecraft.client.gui.widget.ButtonWidget;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Util;
|
||||
|
||||
import java.awt.*;
|
||||
import java.net.URI;
|
||||
|
||||
public class QRcodeAcceptScreen extends Screen {
|
||||
@@ -20,6 +21,8 @@ public class QRcodeAcceptScreen extends Screen {
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
|
||||
|
||||
this.addDrawableChild(ButtonWidget.builder(Text.translatable("button.spmega.qr.cancel"), button -> {
|
||||
if (this.client != null) {
|
||||
this.client.setScreen(parent);
|
||||
@@ -53,17 +56,10 @@ public class QRcodeAcceptScreen extends Screen {
|
||||
|
||||
context.drawCenteredTextWithShadow(
|
||||
this.textRenderer,
|
||||
Text.translatable("screen.spmega.qr.accept_link"),
|
||||
Text.literal("Найдена ссылка: " + url),
|
||||
this.width / 2,
|
||||
this.height / 2 - 30,
|
||||
0xFFFFFF
|
||||
);
|
||||
context.drawCenteredTextWithShadow(
|
||||
this.textRenderer,
|
||||
Text.literal(url),
|
||||
this.width / 2,
|
||||
this.height / 2 - 10,
|
||||
0xFFD27F
|
||||
this.height / 2,
|
||||
Color.WHITE.getRGB()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import git.yawaflua.tech.spmega.api.SPWorldsApiClient;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public final class BankUiService {
|
||||
private static final BankUiService INSTANCE = new BankUiService();
|
||||
@@ -110,7 +111,7 @@ public final class BankUiService {
|
||||
List<String> numbers = new ArrayList<>();
|
||||
for (SPWorldsApiClient.PlayerCard apiCard : apiCards) {
|
||||
if (apiCard.number() != null && !apiCard.number().isBlank()) {
|
||||
numbers.add(apiCard.number());
|
||||
numbers.add(apiCard.name() + " : " + apiCard.number());
|
||||
}
|
||||
}
|
||||
lastMessage = "";
|
||||
@@ -224,6 +225,10 @@ public final class BankUiService {
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Boolean> submitPaymentAsync(PaymentDraft draft) {
|
||||
return CompletableFuture.supplyAsync(() -> submitPayment(draft));
|
||||
}
|
||||
|
||||
private boolean refreshCard(
|
||||
String cardId,
|
||||
String cardToken,
|
||||
|
||||
Reference in New Issue
Block a user