Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
backend: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ozfox committed Jul 7, 2024
1 parent 35a929b commit 262c90d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ public void add(String endpoint, Long deviceId) {
public void remove(String endpoint) {
devices.remove(endpoint);
activeDevices.remove(endpoint);
pets.remove(endpoint);
}

@Override
public void removeAll() {
devices.clear();
activeDevices.clear();
pets.clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.Setter;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -93,7 +94,7 @@ public void doWork() {
PetEntity pet = device.getPet();

if (pet != null) {
processPetInteractions(pet);
processPetInteractions(pet, device.getIdentifier());
deteriorate(pet);

if (activeDevices.contains(device.getId())) {
Expand All @@ -105,26 +106,34 @@ public void doWork() {
}
}

private void processPetInteractions(PetEntity pet) {
private void processPetInteractions(PetEntity pet, String endpoint) {
InteractionRecord interactionRecord = petManager.getCurrentInteraction(pet.getId());

if (interactionRecord != null && !interactionRecord.isEvaluated()) {

Consumer<String> logInteractionFn = (action) ->
LOGGER.info(
"Interaction with " + pet.getName() + " on device " + endpoint + ": " + action);

if (interactionRecord.getClean() > 0) {
ucPetInteractions.cleanPet(pet);
interactionRecord.setClean(0);
logInteractionFn.accept("clean");
}
if (interactionRecord.getFeed() > 0) {
ucPetInteractions.feedPet(pet);
interactionRecord.setFeed(0);
logInteractionFn.accept("feed");
}
if (interactionRecord.getPlay() > 0) {
ucPetInteractions.playWithPet(pet);
interactionRecord.setPlay(0);
logInteractionFn.accept("play");
}
if (interactionRecord.getMedicate() > 0) {
ucPetInteractions.medicatePet(pet);
interactionRecord.setMedicate(0);
logInteractionFn.accept("medicate");
}

interactionRecord.setEvaluated(true);
Expand Down

0 comments on commit 262c90d

Please sign in to comment.