Skip to content

Commit

Permalink
Future planning + general fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NickImpact committed Mar 26, 2024
1 parent 3ec8760 commit cf13457
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 73 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fabric-loader=0.15.7
fabric-api=0.92.0+1.20.1

# Is the project currently in a snapshot state?
snapshot = false
release-candidate = 2
snapshot = true
release-candidate = 3
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public final class EconomyConfig {
}));
public static final ConfigKey<String> SQL_TABLE_PREFIX = notReloadable(stringKey("storage.table-prefix", "economy_"));


public static final ConfigKey<Boolean> APPLY_RESTRICTIONS = booleanKey("restrictions.enabled", true);
public static final ConfigKey<BigDecimal> MIN_BALANCE = key(adapter -> {
double value = adapter.getDouble("restrictions.minimum-balance", 0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import net.impactdev.impactor.api.economy.transactions.details.EconomyTransactionType;
import net.impactdev.impactor.api.economy.transactions.EconomyTransferTransaction;
import net.impactdev.impactor.api.events.ImpactorEvent;
import net.impactdev.impactor.api.scheduler.v2.Scheduler;
import net.impactdev.impactor.api.scheduler.v2.Schedulers;
import net.impactdev.impactor.api.utility.printing.PrettyPrinter;
import net.impactdev.impactor.core.economy.EconomyConfig;
import net.impactdev.impactor.core.economy.ImpactorEconomyService;
Expand Down Expand Up @@ -336,6 +338,7 @@ public boolean virtual() {
return EconomyTransaction.compose()
.account(this)
.type(EconomyTransactionType.RESET)
.amount(this.currency.defaultAccountBalance())
.build();
}

Expand All @@ -346,7 +349,7 @@ public boolean virtual() {
.account(this)
.currency(this.currency)
.amount(amount)
.type(EconomyTransactionType.DEPOSIT);
.type(EconomyTransactionType.RESET);

EconomyTransactionEvent.Pre pre = this.createAndFirePre(amount, EconomyTransactionType.RESET);
if(pre.cancelled()) {
Expand All @@ -360,7 +363,7 @@ public boolean virtual() {
.currency(this.currency)
.account(this)
.amount(this.currency.defaultAccountBalance())
.type(EconomyTransactionType.DEPOSIT)
.type(EconomyTransactionType.RESET)
.result(EconomyResultType.FAILED)
.build()
);
Expand Down Expand Up @@ -411,7 +414,7 @@ private <T> T enact(BigDecimal amount, EconomyTransactionType type, TransactionP
p.newline();
});

Impactor.instance().scheduler().sync().execute(() -> printer.log(BaseImpactorPlugin.instance().logger(), PrettyPrinter.Level.ERROR));
Schedulers.require(Scheduler.SYNCHRONOUS).executor().execute(() -> printer.log(BaseImpactorPlugin.instance().logger(), PrettyPrinter.Level.ERROR));
return fallback.get();
}
}
Expand Down Expand Up @@ -457,6 +460,8 @@ private EconomyTransactionEvent.Pre createAndFirePre(BigDecimal amount, EconomyT
private EconomyTransaction createAndFirePost(EconomyTransaction transaction) throws PostResult.CompositeException {
EconomyTransactionEvent.Post event = new ImpactorEconomyTransactionEvent.Post(transaction);
this.postAndVerify(event);

((ImpactorEconomyService) this.service).storage().logTransaction(transaction);
return transaction;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* This file is part of Impactor, licensed under the MIT License (MIT).
*
* Copyright (c) 2018-2022 NickImpact
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package net.impactdev.impactor.core.economy.placeholders;

import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Suppliers;
import net.impactdev.impactor.api.economy.EconomyService;
import net.impactdev.impactor.api.economy.accounts.Account;
import net.impactdev.impactor.api.economy.currency.Currency;
import net.impactdev.impactor.api.platform.sources.PlatformSource;
import net.impactdev.impactor.api.text.placeholders.PlaceholderArguments;
import net.impactdev.impactor.api.text.placeholders.PlaceholderParser;
import net.impactdev.impactor.api.utility.Context;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class AccountPlaceholderParser implements PlaceholderParser {

private static final Supplier<EconomyService> service = Suppliers.memoize(EconomyService::instance);

private record AccountRequest(UUID uuid, Currency currency) {}

private final AsyncLoadingCache<AccountRequest, Account> cache = Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.buildAsync(request -> service.get().account(request.currency, request.uuid).join());

@Override
public @NotNull Component parse(@Nullable PlatformSource viewer, @NotNull Context context) {
PlaceholderArguments arguments = context.require(PlaceholderArguments.class);
Currency currency = this.currency(arguments);

if(viewer == null) {
return Component.empty();
}

Account account = this.cache.get(new AccountRequest(viewer.uuid(), currency)).getNow(null);
if(account != null) {
return currency.format(account.balance());
}

return Component.text("Fetching balance...");
}

@SuppressWarnings("PatternValidation")
private Currency currency(PlaceholderArguments arguments) {
if(arguments.hasNext()) {
String namespace = arguments.popOrDefault();
String value = arguments.popOrDefault();

if(namespace == null || value == null) {
return service.get().currencies().primary();
}

return service.get().currencies().currency(Key.key(namespace, value)).orElse(service.get().currencies().primary());
}

return service.get().currencies().primary();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import net.impactdev.impactor.api.Impactor;
import net.impactdev.impactor.api.economy.accounts.Account;
import net.impactdev.impactor.api.economy.currency.Currency;
import net.impactdev.impactor.api.platform.Platform;
import net.impactdev.impactor.api.platform.players.PlatformPlayerService;
import net.impactdev.impactor.api.economy.transactions.EconomyTransaction;
import net.impactdev.impactor.api.scheduler.v2.Scheduler;
import net.impactdev.impactor.api.scheduler.v2.Schedulers;
import net.impactdev.impactor.api.storage.Storage;
import net.impactdev.impactor.api.utility.ExceptionPrinter;
import net.impactdev.impactor.api.utility.printing.PrettyPrinter;
import net.impactdev.impactor.core.plugin.BaseImpactorPlugin;
import net.impactdev.impactor.core.utility.future.ThrowingRunnable;
import net.impactdev.impactor.core.utility.future.ThrowingSupplier;

import java.util.Objects;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -120,6 +120,18 @@ public CompletableFuture<Void> delete(Currency currency, UUID uuid) {
});
}

@CanIgnoreReturnValue
public CompletableFuture<Void> logTransaction(EconomyTransaction transaction) {
// return run(() -> this.implementation.logTransaction(transaction));
return CompletableFuture.completedFuture(null);
}

@CanIgnoreReturnValue
public CompletableFuture<Void> sync(Account account, Instant since) {
// return run(() -> this.implementation.sync(account, since));
return CompletableFuture.completedFuture(null);
}

@CanIgnoreReturnValue
public CompletableFuture<Boolean> purge() {
return supply(this.implementation::purge);
Expand All @@ -136,7 +148,7 @@ private static CompletableFuture<Void> run(ThrowingRunnable runnable) {
}
throw new CompletionException(e);
}
}, Impactor.instance().scheduler().async());
}, Schedulers.require(Scheduler.ASYNCHRONOUS).executor());
}

private static <T> CompletableFuture<T> supply(ThrowingSupplier<T> supplier) {
Expand All @@ -150,33 +162,22 @@ private static <T> CompletableFuture<T> supply(ThrowingSupplier<T> supplier) {
}
throw new CompletionException(e);
}
}, Impactor.instance().scheduler().async());
}, Schedulers.require(Scheduler.ASYNCHRONOUS).executor());
}

private static final class AccountKey {
private final Currency currency;
private final UUID owner;

public AccountKey(Currency currency, UUID owner) {
this.currency = currency;
this.owner = owner;
}
private record AccountKey(Currency currency, UUID owner) {

public static AccountKey of(Currency currency, UUID owner) {
return new AccountKey(currency, owner);
}
return new AccountKey(currency, owner);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountKey that = (AccountKey) o;
return currency.equals(that.currency) && owner.equals(that.owner);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountKey that = (AccountKey) o;
return currency.equals(that.currency) && owner.equals(that.owner);
}

@Override
public int hashCode() {
return Objects.hash(currency, owner);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import com.google.common.collect.Multimap;
import net.impactdev.impactor.api.economy.accounts.Account;
import net.impactdev.impactor.api.economy.currency.Currency;
import net.impactdev.impactor.api.economy.transactions.EconomyTransaction;
import net.impactdev.impactor.api.storage.connection.StorageConnection;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.UUID;

public interface EconomyStorageImplementation extends StorageConnection {
Expand All @@ -44,6 +47,10 @@ public interface EconomyStorageImplementation extends StorageConnection {

void delete(Currency currency, UUID uuid) throws Exception;

void logTransaction(EconomyTransaction transaction) throws Exception;

void sync(Account account, Instant since) throws Exception;

boolean purge() throws Exception;

}
Loading

0 comments on commit cf13457

Please sign in to comment.