|
@@ -0,0 +1,158 @@
|
|
|
|
|
+package com.td.boss.game.complayerdog.service;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
|
+import com.td.boss.common.pojo.Result;
|
|
|
|
|
+import com.td.boss.common.service.CommonServiceImpl;
|
|
|
|
|
+import com.td.boss.game.complayerdog.pojo.Disaster;
|
|
|
|
|
+import com.td.boss.game.complayerdog.repository.ComPlayerDisasterRepository;
|
|
|
|
|
+import com.td.boss.game.complayerdog.vo.DisasterVo;
|
|
|
|
|
+import com.td.boss.util.DappUtil;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@Transactional
|
|
|
|
|
+public class ComPlayerDisasterServiceImpl extends CommonServiceImpl<DisasterVo, Disaster, String> implements ComPlayerDisasterService {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ComPlayerDisasterRepository comPlayerDisasterRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public DisasterVo getAttack(String userId) {
|
|
|
|
|
+ //todo 比如1个月不上线怎么办
|
|
|
|
|
+ //todo 防护模式未实现
|
|
|
|
|
+ log.info("灾难模式");
|
|
|
|
|
+ DisasterVo disasterVo = null;
|
|
|
|
|
+ //如果本周内还有灾难
|
|
|
|
|
+ if (getDisasterNum(userId) > 0) {
|
|
|
|
|
+ //随机获取一种灾难方式灾难对象
|
|
|
|
|
+ disasterVo = getDisasterList(userId).get(RandomUtil.randomInt(0, 1));
|
|
|
|
|
+ //查看该灾难是否有防护,如果有防护需要按照防护几率计算是否防御住灾害了
|
|
|
|
|
+ if (disasterVo.getIsBuyProtected()) {
|
|
|
|
|
+ double v = RandomUtil.randomDouble(0, 1);
|
|
|
|
|
+ log.info("灾难几率:{}", v);
|
|
|
|
|
+ if (v > Convert.toDouble("0.1")) {
|
|
|
|
|
+ disasterVo.setActivityProtect(true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ disasterVo.setActivityProtect(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("灾难结果:{}", JSONUtil.toJsonStr(disasterVo));
|
|
|
|
|
+ Disaster disaster = new Disaster();
|
|
|
|
|
+ BeanUtil.copyProperties(disasterVo, disaster);
|
|
|
|
|
+ //入库
|
|
|
|
|
+ //todo 应该还有个减产的接口
|
|
|
|
|
+ comPlayerDisasterRepository.save(disaster);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("灾难结果:{}", "本周无灾难了");
|
|
|
|
|
+ return disasterVo;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<DisasterVo> getDisasterList(String userId) {
|
|
|
|
|
+ //todo 这里需要优化使用枚举获取
|
|
|
|
|
+ //查询有效防护时间
|
|
|
|
|
+ Date ziran_protected = cn.hutool.core.date.DateUtil.parse("2022-03-30");
|
|
|
|
|
+ Date yeshow_protected = cn.hutool.core.date.DateUtil.parse("2022-03-30");
|
|
|
|
|
+
|
|
|
|
|
+ List<DisasterVo> disasterList = new ArrayList<>();
|
|
|
|
|
+ disasterList.add(new DisasterVo("自然灾害", ziran_protected));
|
|
|
|
|
+ disasterList.add(new DisasterVo("野兽", yeshow_protected));
|
|
|
|
|
+
|
|
|
|
|
+ disasterList.forEach(a -> {
|
|
|
|
|
+ a.setDamage(50);
|
|
|
|
|
+ a.setDefend(90);
|
|
|
|
|
+ a.setUserId(userId);
|
|
|
|
|
+ a.setCreateTime(new Date());
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ log.info("灾难列表:{}", JSONUtil.toJsonStr(disasterList));
|
|
|
|
|
+ return disasterList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获得本周剩余灾难数量
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param userId
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private int getDisasterNum(String userId) {
|
|
|
|
|
+ //读取接口 获取 那种情况 这个接口实际是返回的上周数据。所以什么时候啦都不会变。
|
|
|
|
|
+ //本周开始时间为周日到周六,期间获取的不会变化,这周获取的是上周的数据
|
|
|
|
|
+ Map<String, Object> cntPayAndSwapAmount = DappUtil.getCntPayAndSwapAmount(userId);
|
|
|
|
|
+ log.info("接口返回支付兑换结果:{}", JSONUtil.toJsonStr(cntPayAndSwapAmount));
|
|
|
|
|
+ // 总支付cnt
|
|
|
|
|
+ Double pay_amount = Convert.toDouble(cntPayAndSwapAmount.get("pay_amount"));
|
|
|
|
|
+ // 总兑换cnt
|
|
|
|
|
+ Double swap_amount = Convert.toDouble(cntPayAndSwapAmount.get("swap_amount"));
|
|
|
|
|
+
|
|
|
|
|
+ //本周内已出现灾难次数
|
|
|
|
|
+ long countByWeek = getCountByWeek(userId);
|
|
|
|
|
+ //本周内应该出现的灾难次数
|
|
|
|
|
+ int disasterTimes = getDisasterTimes(pay_amount, swap_amount);
|
|
|
|
|
+ log.info("本周内应该出现的灾难次数:{},本周内已出现灾难次数:{}", disasterTimes, countByWeek);
|
|
|
|
|
+ return disasterTimes - Convert.toInt(countByWeek);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 本周内已出现灾难次数
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param userId
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public long getCountByWeek(String userId) {
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ DateTime begin = DateUtil.beginOfWeek(now);
|
|
|
|
|
+ DateTime end = DateUtil.endOfWeek(now);
|
|
|
|
|
+ Specification querySpecifi = new Specification<Disaster>() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Predicate toPredicate(Root<Disaster> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
|
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
|
+ predicates.add(criteriaBuilder.between(root.get("createTime"), begin, end));
|
|
|
|
|
+ predicates.add(criteriaBuilder.equal(root.get("userId"), userId));
|
|
|
|
|
+ return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ long count = comPlayerDisasterRepository.count(querySpecifi);
|
|
|
|
|
+ log.info("count:{}", count);
|
|
|
|
|
+ return count;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * a)当某个村长所属农场的支出小于等于收入30%时,按照正常频率,每周出现1次
|
|
|
|
|
+ * b)当某个村长所属农场的支出大于收入30%时,每周出现2次
|
|
|
|
|
+ * c)当某个村长所属农场的支出大于收入50%时,每周出现4次
|
|
|
|
|
+ * d)当某个村长所属农场的支出大于收入80%时,每周出现6次
|
|
|
|
|
+ * 支出对应pay_amount,收入对应swap_amount
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param pay_amount 支出对应
|
|
|
|
|
+ * @param swap_amount 收入对应
|
|
|
|
|
+ */
|
|
|
|
|
+ private int getDisasterTimes(Double pay_amount, Double swap_amount) {
|
|
|
|
|
+ if (pay_amount > swap_amount * 0.8) {
|
|
|
|
|
+ return 6;
|
|
|
|
|
+ } else if (pay_amount > swap_amount * 0.5) {
|
|
|
|
|
+ return 4;
|
|
|
|
|
+ } else if (pay_amount > swap_amount * 0.3) {
|
|
|
|
|
+ return 2;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|