.
This commit is contained in:
parent
af296638fd
commit
07fa0efab7
5 changed files with 107 additions and 12 deletions
|
|
@ -29,8 +29,8 @@ subprojects {
|
|||
// https://mvnrepository.com/artifact/com.google.guava/guava
|
||||
implementation("com.google.guava:guava:33.5.0-jre")
|
||||
|
||||
// https://www.mongodb.com/docs/drivers/java/sync/current/get-started/
|
||||
implementation(platform("org.mongodb:mongodb-driver-bom:5.6.3"))
|
||||
implementation("org.mongodb:mongodb-driver-sync")
|
||||
// https://www.mongodb.com/docs/languages/java/reactive-streams-driver/current/get-started/download-and-install/
|
||||
implementation(platform("org.mongodb:mongodb-driver-bom:5.6.1"))
|
||||
implementation("org.mongodb:mongodb-driver-reactivestreams")
|
||||
}
|
||||
}
|
||||
|
|
@ -11,9 +11,8 @@ public interface EconomyService {
|
|||
|
||||
/**
|
||||
* @param playerId uuid of player
|
||||
* @return new coins balance
|
||||
*/
|
||||
Mono<@NotNull Integer> depositCoins(UUID playerId, int amount);
|
||||
Mono<@NotNull Void> depositCoins(UUID playerId, int amount);
|
||||
|
||||
/**
|
||||
* @param playerId uuid of player
|
||||
|
|
|
|||
|
|
@ -1,4 +1,73 @@
|
|||
package de.kentoj.scrow.economy;
|
||||
|
||||
public class EconomyServiceImpl {
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import com.mongodb.client.model.Updates;
|
||||
import com.mongodb.reactivestreams.client.MongoCollection;
|
||||
import com.mongodb.reactivestreams.client.MongoDatabase;
|
||||
import de.kentoj.scrow.services.EconomyService;
|
||||
import org.bson.Document;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class EconomyServiceImpl implements EconomyService {
|
||||
|
||||
private static final String COLLECTION_NAME = "economy";
|
||||
private final @NotNull MongoDatabase database;
|
||||
|
||||
public EconomyServiceImpl(MongoDatabase database) {
|
||||
this.database = database;
|
||||
Mono.from(database.createCollection(COLLECTION_NAME)).block();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<@NotNull Integer> getCoins(UUID playerId) {
|
||||
return Mono.from(
|
||||
getCollection().find(Filters.eq("_id", playerId)).first()
|
||||
).map(result -> result.getInteger("coins")).defaultIfEmpty(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<@NotNull Void> setCoins(UUID playerId, int amount) {
|
||||
Preconditions.checkArgument(amount >= 0, "amount can not be negative");
|
||||
|
||||
return Mono.from(
|
||||
getCollection().updateOne(
|
||||
Filters.eq("_id", playerId),
|
||||
Updates.set("coins", amount),
|
||||
new UpdateOptions().upsert(true)
|
||||
)
|
||||
).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<@NotNull Void> depositCoins(UUID playerId, int amount) {
|
||||
Preconditions.checkArgument(amount >= 0, "amount can not be negative");
|
||||
|
||||
return Mono.from(getCollection().updateOne(
|
||||
Filters.eq("_id", playerId),
|
||||
Updates.inc("coins", amount),
|
||||
new UpdateOptions().upsert(true)
|
||||
)).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<@NotNull Boolean> tryWithdrawCoins(UUID playerId, int amount) {
|
||||
Preconditions.checkArgument(amount >= 0, "amount can not be negative");
|
||||
|
||||
return Mono.from(getCollection().updateOne(
|
||||
Filters.and(
|
||||
Filters.eq("_id", playerId),
|
||||
Filters.gte("coins", amount)
|
||||
),
|
||||
Updates.inc("coins", -1 * amount)
|
||||
)).map(result -> result.getModifiedCount() > 0);
|
||||
}
|
||||
|
||||
private MongoCollection<Document> getCollection() {
|
||||
return database.getCollection(COLLECTION_NAME);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,28 @@
|
|||
package de.kentoj.scrow.economy;
|
||||
|
||||
import de.kentoj.scrow.services.EconomyService;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
var a = new MongoDbHelper();
|
||||
a.close();
|
||||
var con = new MongoDbHelper();
|
||||
|
||||
var playerId = UUID.randomUUID();
|
||||
EconomyService economyService = new EconomyServiceImpl(con.getDatabase());
|
||||
Mono.when(
|
||||
economyService.setCoins(playerId, 120),
|
||||
economyService.depositCoins(playerId, 100)
|
||||
)
|
||||
.then(economyService.tryWithdrawCoins(playerId, 300))
|
||||
.doOnNext(a -> System.out.println("withdraw 1 success " + a))
|
||||
.then(economyService.tryWithdrawCoins(playerId, 50))
|
||||
.doOnNext(a -> System.out.println("withdraw 2 success " + a))
|
||||
.then(economyService.getCoins(playerId))
|
||||
.doOnNext(a -> System.out.println("coins: " + a))
|
||||
.block();
|
||||
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package de.kentoj.scrow.economy;
|
||||
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.reactivestreams.client.MongoClient;
|
||||
import com.mongodb.reactivestreams.client.MongoClients;
|
||||
import com.mongodb.reactivestreams.client.MongoDatabase;
|
||||
import lombok.Getter;
|
||||
import org.bson.UuidRepresentation;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
|
|
@ -14,7 +17,11 @@ public class MongoDbHelper implements Closeable {
|
|||
private final MongoDatabase database;
|
||||
|
||||
public MongoDbHelper() {
|
||||
con = MongoClients.create("mongodb://:@localhost:27017/db?ssl=false");
|
||||
var settings = MongoClientSettings.builder()
|
||||
.applyConnectionString(new ConnectionString("mongodb://localhost:27017/scrow?ssl=false"))
|
||||
.uuidRepresentation(UuidRepresentation.STANDARD)
|
||||
.build();
|
||||
con = MongoClients.create(settings);
|
||||
database = con.getDatabase("scrow");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue