mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 16:49:12 +00:00
Start designing the inventory transaction system
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
package org.apollo.game.model.inv.transaction;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apollo.game.model.inv.Inventory;
|
||||||
|
|
||||||
|
public final class ItemTransaction {
|
||||||
|
|
||||||
|
private final Queue<TransactionBlock> queuedBlocks = new ArrayDeque<>();
|
||||||
|
private final List<TransactionBlock> committedBlocks = new ArrayList<>();
|
||||||
|
private final Set<Inventory> inventories = new HashSet<>();
|
||||||
|
private TransactionListener listener;
|
||||||
|
|
||||||
|
public void setListener(TransactionListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sync(Inventory inventory) {
|
||||||
|
inventories.add(inventory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void append(TransactionBlock block) {
|
||||||
|
queuedBlocks.add(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refresh() {
|
||||||
|
inventories.forEach(Inventory::forceRefresh);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void rollback() {
|
||||||
|
committedBlocks.forEach(TransactionBlock::revert);
|
||||||
|
listener.alertFailure();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean commit() {
|
||||||
|
for (TransactionBlock change : queuedBlocks) {
|
||||||
|
if (!change.commit()) {
|
||||||
|
rollback();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
committedBlocks.add(change);
|
||||||
|
}
|
||||||
|
refresh();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.apollo.game.model.inv.transaction;
|
||||||
|
|
||||||
|
public interface TransactionBlock {
|
||||||
|
|
||||||
|
boolean commit();
|
||||||
|
|
||||||
|
void revert();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package org.apollo.game.model.inv.transaction;
|
||||||
|
|
||||||
|
import org.apollo.game.model.Item;
|
||||||
|
import org.apollo.game.model.entity.Player;
|
||||||
|
import org.apollo.game.model.inv.Inventory;
|
||||||
|
|
||||||
|
public final class TransactionBuilder {
|
||||||
|
|
||||||
|
private final ItemTransaction transaction = new ItemTransaction();
|
||||||
|
private final Inventory inventory;
|
||||||
|
|
||||||
|
private TransactionBuilder(Inventory inventory) {
|
||||||
|
transaction.sync(inventory);
|
||||||
|
this.inventory = inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder failureMessage(Player player, String message) {
|
||||||
|
return listener(new TransactionFailureListener(player, message));
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder listener(TransactionListener listener) {
|
||||||
|
transaction.setListener(listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder swap(Inventory source, int fromSlot, int toSlot) {
|
||||||
|
// TODO: Swap item block
|
||||||
|
transaction.sync(source);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder add(Item item) {
|
||||||
|
// TODO: Add item block
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder remove(Item item) {
|
||||||
|
// TODO: Remove item block
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder move(Inventory source, Item item) {
|
||||||
|
// TODO: Move item block
|
||||||
|
transaction.sync(source);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder take(Inventory destination, Item item) {
|
||||||
|
// TODO: Move item block
|
||||||
|
transaction.sync(destination);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemTransaction build() {
|
||||||
|
return transaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TransactionBuilder create(Inventory inventory) {
|
||||||
|
return new TransactionBuilder(inventory);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.apollo.game.model.inv.transaction;
|
||||||
|
|
||||||
|
import org.apollo.game.model.entity.Player;
|
||||||
|
|
||||||
|
public final class TransactionFailureListener implements TransactionListener {
|
||||||
|
|
||||||
|
private final Player player;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public TransactionFailureListener(Player player, String message) {
|
||||||
|
this.player = player;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void alertFailure() {
|
||||||
|
player.sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.apollo.game.model.inv.transaction;
|
||||||
|
|
||||||
|
import org.apollo.game.model.Item;
|
||||||
|
import org.apollo.game.model.inv.Inventory;
|
||||||
|
|
||||||
|
public abstract class TransactionItem {
|
||||||
|
|
||||||
|
protected final Inventory inventory;
|
||||||
|
|
||||||
|
protected TransactionItem(Inventory inventory) {
|
||||||
|
this.inventory = inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract Item delete();
|
||||||
|
|
||||||
|
public abstract boolean deleteSuccessful();
|
||||||
|
|
||||||
|
public abstract void revert();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.apollo.game.model.inv.transaction;
|
||||||
|
|
||||||
|
public interface TransactionListener {
|
||||||
|
|
||||||
|
void alertFailure();
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user