Search in sources :

Example 1 with Wheel

use of com.tvd12.example.lucky_wheel.entity.Wheel in project ezyfox-server-example by tvd12.

the class WheelService method postInit.

@EzyPostInit
public void postInit() {
    wheel = wheelRepo.findById("wheel");
    if (wheel == null) {
        wheel = loadWheelFromJsonFile();
        wheelRepo.save(wheel);
    }
    emptyFragments = wheel.getFragments().stream().filter(it -> it.getPrizeType() == WheelPrizeType.EMPTY).collect(Collectors.toList());
    AtomicInteger lastRange = new AtomicInteger(0);
    randomRanges = wheel.getFragments().stream().map(it -> lastRange.addAndGet((int) ((it.getRatio() / 100.0) * RANDOM_MAX_VALUE))).collect(Collectors.toList());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EzyPostInit(com.tvd12.ezyfox.bean.annotation.EzyPostInit)

Example 2 with Wheel

use of com.tvd12.example.lucky_wheel.entity.Wheel in project ezyfox-server-example by tvd12.

the class WheelService method loadWheelFromJsonFile.

private Wheel loadWheelFromJsonFile() {
    ObjectMapper objectMapper = new ObjectMapper();
    InputStream inputStream = new EzyAnywayInputStreamLoader().load("wheel.json");
    try {
        Wheel res = objectMapper.readValue(inputStream, Wheel.class);
        res.setId("wheel");
        return res;
    } catch (IOException e) {
        throw new IllegalStateException("can not load wheel", e);
    }
}
Also used : Wheel(com.tvd12.example.lucky_wheel.entity.Wheel) EzyAnywayInputStreamLoader(com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with Wheel

use of com.tvd12.example.lucky_wheel.entity.Wheel in project ezyfox-server-example by tvd12.

the class WheelService method decreaseQuantity.

public void decreaseQuantity(int result) {
    synchronized (lock) {
        wheel = wheelRepo.findById("wheel");
        WheelFragment fragment = wheel.getFragments().get(result);
        int curQuantity = fragment.getQuantity();
        int newQuantity = (curQuantity > 0) ? curQuantity - 1 : 0;
        fragment.setQuantity(newQuantity);
        wheelRepo.save(wheel);
    }
}
Also used : WheelFragment(com.tvd12.example.lucky_wheel.entity.WheelFragment)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Wheel (com.tvd12.example.lucky_wheel.entity.Wheel)1 WheelFragment (com.tvd12.example.lucky_wheel.entity.WheelFragment)1 EzyPostInit (com.tvd12.ezyfox.bean.annotation.EzyPostInit)1 EzyAnywayInputStreamLoader (com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1