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());
}
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);
}
}
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);
}
}
Aggregations