|
@@ -1,31 +1,32 @@
|
|
|
package com.qxgmat.service.extend;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.pagehelper.Page;
|
|
|
import com.nuliji.tools.AbstractService;
|
|
|
-import com.nuliji.tools.PageResult;
|
|
|
import com.nuliji.tools.Transform;
|
|
|
-import com.qxgmat.data.constants.enums.logic.ExerciseLogic;
|
|
|
+import com.qxgmat.data.constants.enums.QuestionDifficult;
|
|
|
+import com.qxgmat.data.constants.enums.QuestionSubject;
|
|
|
+import com.qxgmat.data.constants.enums.QuestionType;
|
|
|
import com.qxgmat.data.constants.enums.status.DirectionStatus;
|
|
|
-import com.qxgmat.data.dao.entity.UserPaper;
|
|
|
-import com.qxgmat.data.dao.entity.UserReport;
|
|
|
+import com.qxgmat.data.dao.entity.ExaminationPaper;
|
|
|
+import com.qxgmat.data.dao.entity.ExaminationStruct;
|
|
|
+import com.qxgmat.data.dao.entity.UserQuestion;
|
|
|
import com.qxgmat.data.relation.QuestionNoRelationMapper;
|
|
|
import com.qxgmat.data.relation.UserPaperRelationMapper;
|
|
|
import com.qxgmat.data.relation.UserReportRelationMapper;
|
|
|
+import com.qxgmat.data.relation.entity.QuestionDifficultRelation;
|
|
|
import com.qxgmat.data.relation.entity.QuestionNoRelation;
|
|
|
-import com.qxgmat.data.relation.entity.UserExercisePaperRelation;
|
|
|
import com.qxgmat.service.UserPaperService;
|
|
|
-import com.qxgmat.service.inline.ExercisePaperService;
|
|
|
-import com.qxgmat.service.inline.QuestionNoService;
|
|
|
-import com.qxgmat.service.inline.QuestionService;
|
|
|
-import com.qxgmat.service.inline.UserReportService;
|
|
|
+import com.qxgmat.service.inline.*;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
public class ExaminationService extends AbstractService {
|
|
|
+ private static Random rand = new Random();
|
|
|
|
|
|
@Resource
|
|
|
private QuestionService questionService;
|
|
@@ -37,6 +38,12 @@ public class ExaminationService extends AbstractService {
|
|
|
private QuestionNoRelationMapper questionNoRelationMapper;
|
|
|
|
|
|
@Resource
|
|
|
+ private ExaminationStructService examinationStructService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ExaminationPaperService examinationPaperService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
private UserPaperRelationMapper userPaperRelationMapper;
|
|
|
|
|
|
@Resource
|
|
@@ -47,6 +54,89 @@ public class ExaminationService extends AbstractService {
|
|
|
|
|
|
@Resource
|
|
|
private UserReportService userReportService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * verbal考试相关设置
|
|
|
+ */
|
|
|
+ final public Integer verbalMaxLevel = 27;
|
|
|
+ final public Integer verbalMinLevel = 9;
|
|
|
+ final public Integer verbalInitLevel = 13;
|
|
|
+ final public Integer verbalPre = 9;
|
|
|
+ final public Integer verbalSC = 14;
|
|
|
+ final public Integer verbalCR = 9;
|
|
|
+ final public Integer verbalRC = 13;
|
|
|
+ // 出RC阅读题的题号
|
|
|
+ final public Integer[] verbalRCPosition = new Integer[]{5, 15, 24, 33};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * quant考试相关设置
|
|
|
+ */
|
|
|
+ final public Integer[] quantBaseLevel = new Integer[]{8, 8, 8, 7};
|
|
|
+ // 阶段的最大题目数
|
|
|
+ final public Integer[] quantNumber = new Integer[]{0, 8, 16, 24, 31};
|
|
|
+ final public Integer quantMinRatio = 1;
|
|
|
+ final public Integer quantMaxRatio = 3;
|
|
|
+ final public Integer quantInitLevel = 11;
|
|
|
+ final public Integer quantPS = 17;
|
|
|
+ final public Integer quantDS = 14;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据第三层结构建立paper
|
|
|
+ * @param entity
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public ExaminationStruct addPaper(ExaminationStruct entity){
|
|
|
+ entity = examinationStructService.add(entity);
|
|
|
+ if (entity.getLevel() == 3){
|
|
|
+ ExaminationPaper paper = examinationPaperService.add(ExaminationPaper.builder()
|
|
|
+ .isAdapt(entity.getIsAdapt())
|
|
|
+ .structTwo(entity.getParentId())
|
|
|
+ .structThree(entity.getId())
|
|
|
+ .title(entity.getTitleZh())
|
|
|
+ .build()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public ExaminationStruct editPaper(ExaminationStruct entity){
|
|
|
+ entity = examinationStructService.add(entity);
|
|
|
+ if (entity.getLevel() == 3){
|
|
|
+ ExaminationPaper paper = examinationPaperService.getByThree(entity.getId());
|
|
|
+ if(paper == null){
|
|
|
+ paper = examinationPaperService.add(ExaminationPaper.builder()
|
|
|
+ .isAdapt(entity.getIsAdapt())
|
|
|
+ .structTwo(entity.getParentId())
|
|
|
+ .structThree(entity.getId())
|
|
|
+ .title(entity.getTitleZh())
|
|
|
+ .build()
|
|
|
+ );
|
|
|
+ }else{
|
|
|
+ examinationPaperService.edit(ExaminationPaper.builder()
|
|
|
+ .id(paper.getId())
|
|
|
+ .isAdapt(entity.getIsAdapt())
|
|
|
+ .structTwo(entity.getParentId())
|
|
|
+ .structThree(entity.getId())
|
|
|
+ .title(entity.getTitleZh())
|
|
|
+ .build()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Boolean deletePaper(Integer id){
|
|
|
+ Boolean result = examinationStructService.delete(id);
|
|
|
+ ExaminationPaper paper = examinationPaperService.getByThree(id);
|
|
|
+ if(paper != null){
|
|
|
+ examinationPaperService.delete(paper.getId());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @param page
|
|
@@ -57,8 +147,6 @@ public class ExaminationService extends AbstractService {
|
|
|
* @param paperId
|
|
|
* @param place
|
|
|
* @param difficult
|
|
|
- * @param startTime
|
|
|
- * @param endTime
|
|
|
* @param order
|
|
|
* @param direction
|
|
|
* @return
|
|
@@ -84,38 +172,397 @@ public class ExaminationService extends AbstractService {
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取下一阶段难度分
|
|
|
+ * @param currentLevel
|
|
|
+ * @param number
|
|
|
+ * @param correct
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Integer verbalNextLevel(Integer currentLevel, Integer number, long correct){
|
|
|
+ long correctRate = correct * 100 / number;
|
|
|
+ if (correctRate < 60){
|
|
|
+ currentLevel -= 5;
|
|
|
+ if (currentLevel < verbalMinLevel) return verbalMinLevel;
|
|
|
+ }else if(correctRate > 80){
|
|
|
+ currentLevel += 5;
|
|
|
+ if (currentLevel > verbalMaxLevel) return verbalMaxLevel;
|
|
|
+ }
|
|
|
+ return currentLevel;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- * 查找用户作业, 并关联用户最后一次作业
|
|
|
- * @param page
|
|
|
- * @param size
|
|
|
+ * 获取下一阶段难度分
|
|
|
+ * @param currentLevel
|
|
|
+ * @param number
|
|
|
+ * @param correct
|
|
|
+ * @param step
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Integer quantNextLevel(Integer currentLevel, Integer number, long correct, Integer step){
|
|
|
+ long correctRate = correct * 100 / number;
|
|
|
+ Integer max = quantMaxRatio * quantBaseLevel[step];
|
|
|
+ Integer min = quantMinRatio * quantBaseLevel[step];
|
|
|
+ if (correctRate < 60){
|
|
|
+ currentLevel -= 4;
|
|
|
+ if (currentLevel < min) return min;
|
|
|
+ }else if(correctRate > 80){
|
|
|
+ currentLevel += 4;
|
|
|
+ if (currentLevel > max) return max;
|
|
|
+ }
|
|
|
+ return currentLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分组统计不同题型的数量
|
|
|
+ * @param userQuestionList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Integer> statTypeNumber(List<UserQuestion> userQuestionList){
|
|
|
+ Map<String, Integer> result = new HashMap<>();
|
|
|
+ for(UserQuestion question : userQuestionList){
|
|
|
+ String type = question.getQuestionType();
|
|
|
+ if (!result.containsKey(type)){
|
|
|
+ result.put(type, 0);
|
|
|
+ }
|
|
|
+ result.put(type, result.get(type) + 1);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回还需的题目类型
|
|
|
+ * @param typeNumber
|
|
|
+ * @param questionTypes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<String> needQuestionTypes(Map<String, Integer> typeNumber, List<String> questionTypes){
|
|
|
+ List<String> targetTypes = new ArrayList<>();
|
|
|
+ for(String type : questionTypes){
|
|
|
+ int target = 0;
|
|
|
+ switch(type){
|
|
|
+ case "sc":
|
|
|
+ target = verbalSC;
|
|
|
+ break;
|
|
|
+ case "cr":
|
|
|
+ target = verbalCR;
|
|
|
+ break;
|
|
|
+ case "rc":
|
|
|
+ target = verbalRC;
|
|
|
+ break;
|
|
|
+ case "ps":
|
|
|
+ target = quantPS;
|
|
|
+ break;
|
|
|
+ case "ds":
|
|
|
+ target = quantDS;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (target == 0 || target > typeNumber.get(type)){
|
|
|
+ targetTypes.add(type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return targetTypes;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过当前完成的题目数,判断是否该抽取阅读题
|
|
|
+ * @param number
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean verbalRC(Integer number){
|
|
|
+ for(Integer n : verbalRCPosition){
|
|
|
+ if (n == number) return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过当前完成的题目数,判断当前阶段
|
|
|
+ * @param number
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Integer quantStep(Integer number) {
|
|
|
+ Integer step = 0;
|
|
|
+ Integer all = 0;
|
|
|
+ for(Integer n : quantBaseLevel){
|
|
|
+ all += n;
|
|
|
+ if (all >= number) return step;
|
|
|
+ step += 1;
|
|
|
+ }
|
|
|
+ return step;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化第一阶段题目
|
|
|
+ * 一篇4题的阅读压轴
|
|
|
+ * { "ids": [], "level": 0 }
|
|
|
* @param structId
|
|
|
- * @param userId
|
|
|
- * @param logic
|
|
|
- * @param logicExtend
|
|
|
- * @param times
|
|
|
* @return
|
|
|
*/
|
|
|
- public PageResult<UserExercisePaperRelation> list(int page, int size, Number structId, Number userId, ExerciseLogic logic, String logicExtend, Integer times){
|
|
|
- String logicKey = logic != null ? logic.key : "";
|
|
|
- Page<UserPaper> p = page(()->{
|
|
|
- userPaperRelationMapper.listExercisePaper(structId, userId, logicKey, logicExtend, times);
|
|
|
- },page, size);
|
|
|
+ public JSONObject initVerbal(Integer structId){
|
|
|
+ JSONObject info = new JSONObject();
|
|
|
+ Integer[] rcQ = questionNoService.randomExaminationRc(structId, 4, null);
|
|
|
+ List<QuestionNoRelation> rcRelationList = questionNoService.listWithRelationByIds(rcQ);
|
|
|
+ Integer rcLevel = computeNoLevel(rcRelationList);
|
|
|
+ Integer targetLevel = 0;
|
|
|
+ Collection types = QuestionType.FromSubject(QuestionSubject.VERBAL);
|
|
|
+ List<QuestionDifficultRelation> difficultList = questionNoService.allExaminationByType(structId, types);
|
|
|
+ List<QuestionDifficultRelation> selectedList = null;
|
|
|
+ do{
|
|
|
+ selectedList = randomList(difficultList, verbalPre - 4);
|
|
|
+ targetLevel = rcLevel + computeDifficultLevel(selectedList);
|
|
|
+ }while(targetLevel >= verbalInitLevel);
|
|
|
+ List<Integer> targetIds = new ArrayList<>(verbalPre);
|
|
|
+ for(QuestionDifficultRelation relation : selectedList){
|
|
|
+ targetIds.add(relation.getId());
|
|
|
+ }
|
|
|
+ targetIds.addAll(Arrays.asList(rcQ));
|
|
|
+ info.put("level", targetLevel);
|
|
|
+ info.put("ids", targetIds);
|
|
|
+ return info;
|
|
|
+ }
|
|
|
|
|
|
- Collection ids = Transform.getIds(p, UserPaper.class, "id");
|
|
|
+ /**
|
|
|
+ * 生成下一阶段题目
|
|
|
+ * 一篇3题的阅读压轴
|
|
|
+ * { "ids": [], "level": 0 }
|
|
|
+ * @param structId
|
|
|
+ * @param level
|
|
|
+ * @param typeNumbers
|
|
|
+ * @param ids
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject generateVerbal(Integer structId, Integer level, Map<String, Integer> typeNumbers, Collection ids){
|
|
|
+ JSONObject info = new JSONObject();
|
|
|
+ Integer[] rcQ = questionNoService.randomExaminationRc(structId, 3, ids);
|
|
|
+ List<QuestionNoRelation> rcRelationList = questionNoService.listWithRelationByIds(rcQ);
|
|
|
+ Integer rcLevel = computeNoLevel(rcRelationList);
|
|
|
+ Collection types = QuestionType.FromSubject(QuestionSubject.VERBAL);
|
|
|
+ List<QuestionDifficultRelation> difficultList = questionNoService.allExaminationByType(structId, types);
|
|
|
+ List<QuestionDifficultRelation> selectedList = new ArrayList<>(verbalPre - 3);
|
|
|
+ Integer[] levels = generateLevel(level - rcLevel,verbalPre - 3);
|
|
|
+ do{
|
|
|
+ QuestionDifficultRelation r = random(difficultList, ids, levels[selectedList.size()]);
|
|
|
+ int typeNumber = typeNumbers.get(r.getQuestionType()) + 1;
|
|
|
+ switch(r.getQuestionType()){
|
|
|
+ case "sc":
|
|
|
+ if (typeNumber > verbalSC) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "cr":
|
|
|
+ if (typeNumber > verbalCR) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ typeNumbers.put(r.getQuestionType(), typeNumber);
|
|
|
+ selectedList.add(r);
|
|
|
+ ids.add(r.getId());
|
|
|
+ }while(selectedList.size() == verbalPre -3);
|
|
|
+ List<Integer> targetIds = new ArrayList<>(verbalPre);
|
|
|
+ for(QuestionDifficultRelation relation : selectedList){
|
|
|
+ targetIds.add(relation.getId());
|
|
|
+ }
|
|
|
+ targetIds.addAll(Arrays.asList(rcQ));
|
|
|
+ info.put("ids", targetIds);
|
|
|
+ info.put("level", level);
|
|
|
+ return info;
|
|
|
+ }
|
|
|
|
|
|
- // 获取详细数据
|
|
|
- List<UserPaper> list = userPaperService.select(ids);
|
|
|
- List<UserExercisePaperRelation> pr = Transform.convert(list, UserExercisePaperRelation.class);
|
|
|
+ /**
|
|
|
+ * 初始化第一阶段题目
|
|
|
+ * 一篇4题的阅读压轴
|
|
|
+ * { "ids": [], "level": 0 }
|
|
|
+ * @param structId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject initQuant(Integer structId){
|
|
|
+ JSONObject info = new JSONObject();
|
|
|
+ Integer targetLevel = 0;
|
|
|
+ Collection types = QuestionType.FromSubject(QuestionSubject.QUANT);
|
|
|
+ Integer number = quantBaseLevel[0];
|
|
|
+ List<QuestionDifficultRelation> difficultList = questionNoService.allExaminationByType(structId, types);
|
|
|
+ List<QuestionDifficultRelation> selectedList = null;
|
|
|
+ do{
|
|
|
+ selectedList = randomList(difficultList, number);
|
|
|
+ targetLevel = computeDifficultLevel(selectedList);
|
|
|
+ }while(targetLevel >= verbalInitLevel);
|
|
|
+ List<Integer> targetIds = new ArrayList<>(verbalPre);
|
|
|
+ for(QuestionDifficultRelation relation : selectedList){
|
|
|
+ targetIds.add(relation.getId());
|
|
|
+ }
|
|
|
+ info.put("level", targetLevel);
|
|
|
+ info.put("ids", targetIds);
|
|
|
+ return info;
|
|
|
+ }
|
|
|
|
|
|
- // 获取最后一次作业结果
|
|
|
- List<UserReport> reportList = userReportRelationMapper.listLast(ids);
|
|
|
- Collection reportIds = Transform.getIds(reportList, UserReport.class, "id");
|
|
|
- Transform.replace(reportList, userReportService.select(reportIds), UserReport.class, "id");
|
|
|
+ /**
|
|
|
+ * 生成下一阶段题目
|
|
|
+ * 一篇3题的阅读压轴
|
|
|
+ * { "ids": [], "level": 0 }
|
|
|
+ * @param structId
|
|
|
+ * @param level
|
|
|
+ * @param typeNumbers
|
|
|
+ * @param ids
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject generateQuant(Integer structId, Integer level, Map<String, Integer> typeNumbers, Collection ids, Integer step){
|
|
|
+ JSONObject info = new JSONObject();
|
|
|
+ Collection types = QuestionType.FromSubject(QuestionSubject.QUANT);
|
|
|
+ Integer number = quantBaseLevel[step];
|
|
|
+ List<QuestionDifficultRelation> difficultList = questionNoService.allExaminationByType(structId, types);
|
|
|
+ List<QuestionDifficultRelation> selectedList = null;
|
|
|
+ Integer[] levels = generateLevel(level,number);
|
|
|
+ do{
|
|
|
+ QuestionDifficultRelation r = random(difficultList, ids, levels[selectedList.size()]);
|
|
|
+ int typeNumber = typeNumbers.get(r.getQuestionType()) + 1;
|
|
|
+ switch(r.getQuestionType()){
|
|
|
+ case "ps":
|
|
|
+ if (typeNumber > quantPS) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "ds":
|
|
|
+ if (typeNumber > quantDS) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ typeNumbers.put(r.getQuestionType(), typeNumber);
|
|
|
+ selectedList.add(r);
|
|
|
+ ids.add(r.getId());
|
|
|
+ }while(selectedList.size() == number);
|
|
|
+ List<Integer> targetIds = new ArrayList<>(number);
|
|
|
+ for(QuestionDifficultRelation relation : selectedList){
|
|
|
+ targetIds.add(relation.getId());
|
|
|
+ }
|
|
|
+ info.put("ids", targetIds);
|
|
|
+ info.put("level", level);
|
|
|
+ return info;
|
|
|
+ }
|
|
|
|
|
|
- Transform.combine(p, reportList, UserExercisePaperRelation.class, "id", "report", UserReport.class, "paperId");
|
|
|
+ /**
|
|
|
+ * 随机生成对应的题目列表
|
|
|
+ * @param all
|
|
|
+ * @param size
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<QuestionDifficultRelation> randomList(List<QuestionDifficultRelation> all, Integer size){
|
|
|
+ List<QuestionDifficultRelation> list = new ArrayList<>(size);
|
|
|
+ do{
|
|
|
+ int index = rand.nextInt(all.size());
|
|
|
+ QuestionDifficultRelation target = all.get(index);
|
|
|
+ list.add(target);
|
|
|
+ }while(list.size() == size);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
|
|
|
- return new PageResult<>(pr, p.getTotal());
|
|
|
+ /**
|
|
|
+ * 随机生成对应的题目
|
|
|
+ * @param all
|
|
|
+ * @param ids
|
|
|
+ * @param level
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public QuestionDifficultRelation random(List<QuestionDifficultRelation> all, Collection ids, Integer level){
|
|
|
+ String difficult = levelToDifficult(level);
|
|
|
+ do{
|
|
|
+ int index = rand.nextInt(all.size());
|
|
|
+ QuestionDifficultRelation target = all.get(index);
|
|
|
+ if (ids == null || !ids.contains(target.getId())){
|
|
|
+ if (difficult == null || target.getDifficult().equals(difficult)) return target;
|
|
|
+ }
|
|
|
+ }while(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算考题对应的难度分
|
|
|
+ * @param relationList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Integer computeNoLevel(List<QuestionNoRelation> relationList){
|
|
|
+ int level = 0;
|
|
|
+ for(QuestionNoRelation relation : relationList){
|
|
|
+ level += difficultToLevel(relation.getQuestion().getDifficult());
|
|
|
+ }
|
|
|
+ return level;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer computeDifficultLevel(List<QuestionDifficultRelation> relationList){
|
|
|
+ int level = 0;
|
|
|
+ for(QuestionDifficultRelation relation : relationList){
|
|
|
+ level += difficultToLevel(relation.getDifficult());
|
|
|
+ }
|
|
|
+ return level;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer difficultToLevel(String difficult){
|
|
|
+ switch(difficult){
|
|
|
+ case "easy":
|
|
|
+ return 1;
|
|
|
+ case "medium":
|
|
|
+ return 2;
|
|
|
+ case "hard":
|
|
|
+ return 3;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String levelToDifficult(Integer level){
|
|
|
+ switch(level){
|
|
|
+ case 1:
|
|
|
+ return "easy";
|
|
|
+ case 2:
|
|
|
+ return "medium";
|
|
|
+ case 3:
|
|
|
+ return "hard";
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer[] generateLevel(Integer level, Integer number){
|
|
|
+ Integer[] levels = new Integer[number];
|
|
|
+ int no = 0;
|
|
|
+ // 至少有一个可以3分
|
|
|
+ if (level > number + 1){
|
|
|
+ for(int i = 0; i < number; i++){
|
|
|
+ int n = rand.nextInt(2) + 2; // 随机2-3
|
|
|
+ levels[i] = n;
|
|
|
+ level -= n;
|
|
|
+ no += 1;
|
|
|
+
|
|
|
+ // 只能还有一个2分
|
|
|
+ if (level == number - no + 1){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ number -= no;
|
|
|
+ }
|
|
|
+ if(level == number + 1){
|
|
|
+ // 只有一个2分
|
|
|
+ levels[no] = 2;
|
|
|
+ no += 1;
|
|
|
+ }
|
|
|
+ // 剩余都是1分
|
|
|
+ for (int i = 0; i < number; i++) {
|
|
|
+ levels[i+no] = 1;
|
|
|
+ }
|
|
|
+ shuffle(levels);
|
|
|
+ return levels;
|
|
|
}
|
|
|
|
|
|
+ public static <T> void swap(T[] a, int i, int j){
|
|
|
+ T temp = a[i];
|
|
|
+ a[i] = a[j];
|
|
|
+ a[j] = temp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> void shuffle(T[] arr) {
|
|
|
+ int length = arr.length;
|
|
|
+ for ( int i = length; i > 0; i-- ){
|
|
|
+ int randInd = rand.nextInt(i);
|
|
|
+ swap(arr, randInd, i - 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|