List of usage examples for java.math BigDecimal ROUND_HALF_DOWN
int ROUND_HALF_DOWN
To view the source code for java.math BigDecimal ROUND_HALF_DOWN.
Click Source Link
From source file:org.sofun.core.kup.policy.KupSettledLifeCyclePolicyManager.java
/** * Kup life cycle management.//from w w w . j a v a 2 s . com * * @throws Exception */ @Schedule(minute = "*/5", hour = "*", persistent = false) public void check() throws Exception { if (!available) { return; } else { available = false; } try { log.debug("Checking Kups and apply policy..."); // // Deal with Settled Kups // final byte[] settledStatus = new byte[1]; settledStatus[0] = KupStatus.SETTLED; String[] types = new String[] { KupType.GAMBLING_FR }; ListIterator<Kup> kupsIter = kups.getKupsByStatus(settledStatus, types).listIterator(); while (kupsIter.hasNext()) { Kup kup = kupsIter.next(); // Ensure no changes have been made by another transaction in // the mean time. em.refresh(kup); if (kup.getStatus() != KupStatus.SETTLED) { continue; } Map<Integer, Float> repartition = kups.getWinningsRepartitionRulesFor(kup); final int numberOfWinners = repartition.size(); if (kup.getParticipants().size() < numberOfWinners) { kups.cancelKup(kup); log.info("Kup with id=" + String.valueOf(kup.getId()) + " has been cancelled because participants are " + "below the amount of winners."); continue; } KupRankingTable table = kup.getRankingTable(); List<MemberRankingTableEntry> entries = new ArrayList<MemberRankingTableEntry>(table.getEntries()); entries = entries.subList(0, numberOfWinners); // Cancel if winnings < bet for one of the winners. boolean cancelled = false; for (int i = 0; i < numberOfWinners - 1; i++) { final float jackpot = kup.getEffectiveJackpot(); final float amount = jackpot * repartition.get(i + 1) / 100; if (kup.getStake() > amount) { cancelled = true; break; } } if (!cancelled) { // Credit winners. final Date now = new Date(); final float jackpot = kup.getEffectiveJackpot(); if (kup.getJackpot() < kup.getGuaranteedPrice()) { final float house = kup.getGuaranteedPrice() - kup.getJackpot(); final BigDecimal houseAmount = new BigDecimal(house); houseAmount.setScale(2, BigDecimal.ROUND_HALF_UP); SofunTransaction txn = new SofunTransactionImpl(kup, now, houseAmount.floatValue(), kup.getStakeCurrency(), SofunTransactionType.TXN_GUARANTEED_PRICE, SofunTransactionType.TXN_GUARANTEED_PRICE); txn.setDebit(true); em.persist(txn); log.info("SOFUN TXN for kup w/ uuid=" + txn.getKup().getId() + " type=" + txn.getType() + " amount=" + houseAmount.floatValue()); } else { final float rake = kup.getRakeAmount(); if (rake > 0) { final BigDecimal rakeAmount = new BigDecimal(rake); rakeAmount.setScale(2, BigDecimal.ROUND_HALF_UP); SofunTransaction txn = new SofunTransactionImpl(kup, now, rakeAmount.floatValue(), kup.getStakeCurrency(), SofunTransactionType.TXN_RACK, SofunTransactionType.TXN_RACK); txn.setCredit(true); em.persist(txn); log.info("SOFUN TXN for kup w/ uuid=" + txn.getKup().getId() + " type=" + txn.getType() + " amount=" + rakeAmount.floatValue()); } } int i = 0; for (MemberRankingTableEntry entry : entries) { final float amount = jackpot * repartition.get(i + 1) / 100; Member member = entry.getMember(); // Round down (inferior cent) as specified in rules. BigDecimal wiredAmount = new BigDecimal(amount); wiredAmount = wiredAmount.setScale(2, BigDecimal.ROUND_HALF_DOWN); MemberTransaction txn = new MemberTransactionImpl(now, wiredAmount.floatValue(), kup.getStakeCurrency(), MemberTransactionType.BET_CREDIT); txn.setLabel(MemberTransactionType.BET_CREDIT); txn.setCredit(true); txn.setStatusCode("00000"); txn.setStatus(MemberTransactionStatus.INTERNAL); txn.setMemberCreditBefore(member.getMemberCredit().getCredit()); txn.setMemberCreditAfter(member.getMemberCredit().getCredit() + wiredAmount.floatValue()); member.addTransaction(txn); txn.setMember(member); log.info("Member with email=" + member.getEmail() + " finished " + String.valueOf(i + 1) + " out of " + kup.getParticipants().size() + " in kup w/ uuid=" + kup.getId() + " txn of type=" + txn.getType() + " with amount of: " + wiredAmount.floatValue()); // Record KupMemberBet bet = new KupMemberBetImpl(member, kup, txn); bet.setEffectiveDate(now); em.persist(bet); entry.setWinnings(wiredAmount.floatValue()); i++; } log.info( "Kup with id=" + String.valueOf(kup.getId()) + " has now status=" + KupStatus.PAID_OUT); kup.setStatus(KupStatus.PAID_OUT); } else { kups.cancelKup(kup); log.info("Kup with id=" + String.valueOf(kup.getId()) + " has been cancelled because the winnings are " + "below the stake for some of the winners."); } } } catch (Throwable t) { log.error(t.getMessage()); t.printStackTrace(); } finally { available = true; } // Cancel status in exceptional situations will be handled manually per // administrator request. // @see KupeService.cancelKup() }