package org.xs.dntown.wx.idiom.modules.web; import java.util.Date; import java.util.List; import java.util.Random; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.xs.dntown.wx.common.utils.MessageUtil; import org.xs.dntown.wx.common.web.BaseController; import org.xs.dntown.wx.core.modules.entity.UserInfo; import org.xs.dntown.wx.core.modules.entity.enums.ModuleEnum; import org.xs.dntown.wx.core.modules.entity.req.BaseMsgReq; import org.xs.dntown.wx.core.modules.service.LogService; import org.xs.dntown.wx.core.modules.service.UserService; import org.xs.dntown.wx.idiom.modules.entity.IdiomListInfo; import org.xs.dntown.wx.idiom.modules.entity.IdiomUserInfo; import org.xs.dntown.wx.idiom.modules.entity.enums.StepEnum; import org.xs.dntown.wx.idiom.modules.service.IdiomListService; import org.xs.dntown.wx.idiom.modules.service.IdiomUserService; /** * 核心Controller */ @Controller(value = "idiomCoreController") @RequestMapping("idiom") public class CoreController extends BaseController { @Autowired private IdiomUserService idiomUserService; @Autowired private IdiomListService idiomListService; @Autowired private UserService userService; @Autowired private LogService logService; /** * 文本消息 */ @ResponseBody @RequestMapping(value = "msg", produces = { "text/plain;charset=utf-8" }) public String textMsg(HttpServletRequest request) throws Exception { String result = ""; String openId = ""; String userName = ""; String content = ""; try { //得到接收消息實體 BaseMsgReq msgReq = (BaseMsgReq)request.getAttribute("msgReq"); openId = msgReq.getFromUserName(); content = msgReq.getContent(); //得到用戶信息實體 UserInfo userInfo = (UserInfo)request.getAttribute("userInfo"); userName = userInfo.getUserName(); log.info("收到消息:" + msgReq.getContent()); //更新模塊 userService.setModule(msgReq.getFromUserName(), ModuleEnum.idiom.getValue()); //得到接龍用戶信息實體 IdiomUserInfo iUserInfo = idiomUserService.getByOpenId(userInfo.getOpenId()); if(iUserInfo == null) { log.info("新增接龍用戶"); //如果沒有就新增 iUserInfo = new IdiomUserInfo(); iUserInfo.setOpenId(userInfo.getOpenId()); iUserInfo.setModuleStep(StepEnum.normal.getValue()); iUserInfo.setStepTime(new Date()); iUserInfo.setIsAnswered(false); idiomUserService.add(iUserInfo); } //刷新模塊(1小時) long timeDiff = new Date().getTime() - userInfo.getModuleTime().getTime(); long hoursDiff = timeDiff / (1000 * 60 * 60); if(hoursDiff >= 1) { idiomUserService.setStep(userInfo.getOpenId(), StepEnum.normal.getValue()); iUserInfo.setModuleStep(StepEnum.normal.getValue()); } log.info("開始轉入步驟"); //出題 if(msgReq.getContent().equals("開始")) { //完成上一個成語 if(!StringUtils.isEmpty(iUserInfo.getIdiomId()) && iUserInfo.getModuleStep().equals(StepEnum.answer.getValue())) { //處理答題結果 disposeAnswer(iUserInfo, msgReq); result += finishAnswer(iUserInfo, msgReq, false) + "\n"; } log.info("開始下一題"); //下一題 result = nextIdiom(iUserInfo, result); logService.addInfoLog(openId, userName, content, result, ModuleEnum.idiom.getValue(), StepEnum.answer.getValue()); result += MessageUtil.msgReqToXml(msgReq, result); log.info(result); return result; } //答案 else if(msgReq.getContent().equals("答案") && iUserInfo.getModuleStep().equals(StepEnum.answer.getValue())) { //返回答案 if(!StringUtils.isEmpty(iUserInfo.getIdiomId())) { log.info("返回答案"); //完成答題 result += finishAnswer(iUserInfo, msgReq, true); //設置步驟 iUserInfo.setModuleStep(StepEnum.normal.getValue()); iUserInfo.setStepTime(new Date()); idiomUserService.update(iUserInfo); logService.addInfoLog(openId, userName, content, result, ModuleEnum.game24.getValue(), StepEnum.answer.getValue()); result = MessageUtil.msgReqToXml(msgReq, result); log.info(result); return result; } } //排行榜 else if(msgReq.getContent().equals("排行") || msgReq.getContent().equals("排行榜")) { log.info("返回排行榜"); result += "【排行榜】\n"; boolean flag = false; List<IdiomUserInfo> list = idiomUserService.getTopScope(1000); if(list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { if(list.get(i).getOpenId().equals(iUserInfo.getOpenId())) { flag = true; } //TODO 優化 UserInfo sUserInfo = userService.getByOpenId(list.get(i).getOpenId()); if(sUserInfo != null) { result += "No." + (i + 1) + " 『" + sUserInfo.getUserName() + "』(" + list.get(i).getScore() + "分)\n"; } } } if(!flag) { result += "No." + (Integer.parseInt(idiomUserService.getRankScope(iUserInfo.getScore()))) + " 『" + userInfo.getUserName() + "』(" + iUserInfo.getScore() + "分)"; } else { result = result.substring(0, result.length() - 1); } logService.addInfoLog(openId, userName, content, result, ModuleEnum.idiom.getValue(), StepEnum.normal.getValue()); result = MessageUtil.msgReqToXml(msgReq, result); log.info(result); return result; } //如果是新進來就刷新狀態 if(msgReq.getContent().equals("接龍")) { //完成答題 finishAnswer(iUserInfo, msgReq, true); //設置步驟 iUserInfo.setModuleStep(StepEnum.normal.getValue()); iUserInfo.setStepTime(new Date()); idiomUserService.update(iUserInfo); } //如果是答題狀態 if(iUserInfo.getModuleStep().equals(StepEnum.answer.getValue())) { //返回處理答題結果 result = disposeAnswer(iUserInfo, msgReq); logService.addInfoLog(openId, userName, content, result, ModuleEnum.idiom.getValue(), StepEnum.answer.getValue()); result = MessageUtil.msgReqToXml(msgReq, result); log.info(result); return result; } //返回接龍公告 result += "『成語接龍』\n"; result += "【規則】\n"; result += "有3種接龍方式:\n"; result += "與尾字相同接龍,積分+3\n"; result += "與尾字諧音接龍,積分+1\n"; result += "包含尾字接龍,積分+1\n"; result += "連勝有積分獎勵\n"; result += "【說明】\n"; result += "1、輸入“開始”:開始答題\n"; result += "2、輸入“答案”:完成答題\n"; result += "3、輸入“排行”:查看排行\n"; result += "4、輸入“公告”:返回首頁"; logService.addInfoLog(openId, userName, content, result, ModuleEnum.idiom.getValue(), StepEnum.normal.getValue()); result = MessageUtil.msgReqToXml(msgReq, result); } catch (Exception e) { log.debug("執行失敗"); logService.addErrorLog(openId, userName, content, "", ModuleEnum.idiom.getValue(), StepEnum.normal.getValue(), e); e.printStackTrace(); } //返回消息 log.info(result); return result; } /** * 獲得下一題 */ private String nextIdiom(IdiomUserInfo iUserInfo, String result) { //下一題 Random r = new Random(); String questionId = String.valueOf(r.nextInt(29543) + 1); iUserInfo.setIdiomId(questionId); iUserInfo.setModuleStep(StepEnum.answer.getValue()); iUserInfo.setStepTime(new Date()); iUserInfo.setIsAnswered(false); //更新模塊 idiomUserService.update(iUserInfo); //返回題目 IdiomListInfo idiomListInfo = idiomListService.getById(questionId); if(!StringUtils.isEmpty(result)) { result += "\n"; } result += idiomListInfo.getWord(); return result; } /** * 處理答題結果 */ private String disposeAnswer(IdiomUserInfo iUserInfo, BaseMsgReq msgReq) { log.info("處理答題結果"); String result = ""; boolean flag = true; int score = 0; //上一個成語 IdiomListInfo idiomListInfo = idiomListService.getById(iUserInfo.getIdiomId()); //獲得回答的成語 IdiomListInfo idiomAnswerInfo = idiomListService.getByWord(msgReq.getContent()); //沒有這個成語 if(idiomAnswerInfo == null) { result = "這不是成語吧"; flag = false; } else { //判斷是否匹配 //如果是相同匹配 if(idiomListInfo.getWordEnd().equals(idiomAnswerInfo.getWordBegin())) { result = "答對了,積分+3\n"; flag = true; score = 3; } //如果是諧音匹配 else if(idiomListInfo.getWordEndSpell().equals(idiomAnswerInfo.getWordBeginSpell())) { result = "答對了,諧音,積分+1\n"; flag = true; score = 1; } //如果是包含匹配 else if(idiomAnswerInfo.getWord().indexOf(idiomListInfo.getWordEnd()) > -1) { if(idiomListInfo.getId().equals(idiomAnswerInfo.getId())) { //不能是同一個成語 result = "不能是同一個成語"; flag = false; } else { result = "答對了,包含,積分+1\n"; flag = true; score = 1; } } //不匹配 else { result = "成語沒接上"; flag = false; } } //回答正確 if(flag) { log.info("回答正確"); //下一題 idiomListInfo = idiomListService.getNextIdiom(idiomAnswerInfo.getWordEnd()); if(idiomListInfo != null) { iUserInfo.setIdiomId(idiomListInfo.getId()); iUserInfo.setStepTime(new Date()); iUserInfo.setIsAnswered(false); //返回題目 if(!StringUtils.isEmpty(result)) { result += "\n"; } result += idiomListInfo.getWord(); } else { result += "我接不下去了,積分+5\n"; score += 5; result = nextIdiom(iUserInfo, result); } //更新用戶積分、次數、答對次數、連勝次數、最長連勝 iUserInfo.setScore(iUserInfo.getScore() + score); iUserInfo.setRightTimes(iUserInfo.getRightTimes() + 1); iUserInfo.setTimes(iUserInfo.getTimes() + 1); iUserInfo.setComboTimes(iUserInfo.getComboTimes() + 1); if(iUserInfo.getComboTimes() > iUserInfo.getBestTimes()) { iUserInfo.setBestTimes(iUserInfo.getComboTimes()); } iUserInfo.setStepTime(new Date()); iUserInfo.setIsAnswered(false); } //回答錯誤 else { log.info("回答錯誤"); //如果第一次回答 if(!iUserInfo.getIsAnswered()) { //更新用戶積分、次數、答對次數、連勝次數、最長連勝 iUserInfo.setTimes(iUserInfo.getTimes() + 1); iUserInfo.setIsAnswered(true); } } //更新接龍用戶 idiomUserService.update(iUserInfo); return result; } /** * 完成答題 */ private String finishAnswer(IdiomUserInfo iUserInfo, BaseMsgReq msgReq, boolean flag) { log.info("完成答題"); String result = ""; //上一個成語 IdiomListInfo idiomListInfo = idiomListService.getById(iUserInfo.getIdiomId()); //下一題 if(flag) { idiomListInfo = idiomListService.getNextIdiom(idiomListInfo.getWordEnd()); if(idiomListInfo != null) { result += "答案:" + idiomListInfo.getWord() + "\n\n"; } else { result += "答案我也不知道\n\n"; } } int score = 0; int comboTimes = iUserInfo.getComboTimes(); if(comboTimes > 1) { int rank = comboTimes / 5 + 1; score += Math.pow(2, rank); } //更新用戶積分、次數、答對次數、連勝次數、最長連勝 iUserInfo.setScore(iUserInfo.getScore() + score); iUserInfo.setStepTime(new Date()); iUserInfo.setIsAnswered(false); iUserInfo.setComboTimes(0); //更新接龍用戶 idiomUserService.update(iUserInfo); result += "答對" + comboTimes + "題,連勝獎勵積分+" + score; return result; } }
源碼地址:https://github.com/ctxsdhy/dntown