Public platform development-Idiom solitaire, public platform idiom Solitaire
I. Source Code
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. w Eb. 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. ser Vice. 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;/*** core Controller */@ Controller (value = "idiomCoreController") @ RequestMapping ("Idiom") public class CoreController extends BaseController {@ Autowiredprivate IdiomUserService idiomUserService; @ Autowiredprivate IdiomListService idiomListService; @ Autowiredprivate UserService userService; @ Autowiredprivate LogService logService; /*** text message */@ ResponseBody @ RequestMapping (value = "msg", produces = {"text/plain; charset = UTF-8"}) public String textMsg (HttpServletRequest request) throw S Exception {String result = ""; String openId = ""; String userName = ""; String content = ""; try {// get the receiving message entity BaseMsgReq msgReq = (BaseMsgReq) request. getAttribute ("msgReq"); openId = msgReq. getFromUserName (); content = msgReq. getContent (); // obtain the User Information Entity UserInfo userInfo = (UserInfo) request. getAttribute ("userInfo"); userName = userInfo. getUserName (); log.info ("received message:" + msgReq. getContent (); // update the userService module. s EtModule (msgReq. getFromUserName (), ModuleEnum. idiom. getValue (); // The IdiomUserInfo iUserInfo = idiomUserService. getByOpenId (userInfo. getOpenId (); if (iUserInfo = null) {log.info ("new solitaire user"); // if not, iUserInfo = new IdiomUserInfo (); iUserInfo. setOpenId (userInfo. getOpenId (); iUserInfo. setModuleStep (StepEnum. normal. getValue (); iUserInfo. setStepTime (new Date (); iUserInfo. setIsAnswered (false); idiom UserService. add (iUserInfo);} // refresh module (1 hour) 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 ("START transfer step"); // if (msgReq. getContent (). equals ("START") {// complete the previous idiom if (! StringUtils. isEmpty (iUserInfo. getIdiomId () & iUserInfo. getModuleStep (). equals (StepEnum. answer. getValue () {// process the answer result disposeAnswer (iUserInfo, msgReq); result + = finishAnswer (iUserInfo, msgReq, false) + "\ n ";} log.info ("Start next question"); // result = nextIdiom (iUserInfo, result); logService. addInfoLog (openId, userName, content, result, ModuleEnum. idiom. getValue (), StepEnum. answer. getValue (); result + = MessageU Til. msgReqToXml (msgReq, result); log.info (result); return result;} // Answer else if (msgReq. getContent (). equals ("Answer") & iUserInfo. getModuleStep (). equals (StepEnum. answer. getValue () {// return the answer if (! StringUtils. isEmpty (iUserInfo. getIdiomId () {log.info ("Return answer"); // complete the answer result + = finishAnswer (iUserInfo, msgReq, true); // set step 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, r Esult); log.info (result); return result ;}// rank else if (msgReq. getContent (). equals ("ranking") | 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 optimize UserInfo sUserInfo = userService. getByOpenId (list. get (I ). getOpenId (); if (sUserInfo! = Null) {result + = "No. "+ (I + 1) +" "+ sUserInfo. getUserName () + "" ("+ list. get (I ). getScore () + "points) \ n" ;}} if (! Flag) {result + = "No. "+ (Integer. parseInt (idiomUserService. getRankScope (iUserInfo. getScore () + "" + userInfo. getUserName () + "" ("+ iUserInfo. getScore () + "points)";} 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 (resu Lt); return result;} // refresh the status if (msgReq. getContent (). equals ("") {// finishAnswer (iUserInfo, msgReq, true); // set step iUserInfo. setModuleStep (StepEnum. normal. getValue (); iUserInfo. setStepTime (new Date (); idiomUserService. update (iUserInfo);} // if the answer status is if (iUserInfo. getModuleStep (). equals (StepEnum. answer. getValue () {// return the result of processing the answer result = disposeAnswer (iUserInfo, msgReq); logService. addInfoLog (openId, u SerName, content, result, ModuleEnum. idiom. getValue (), StepEnum. answer. getValue (); result = MessageUtil. msgReqToXml (msgReq, result); log.info (result); return result;} // return result + = "" \ n "; result + = "[Rule] \ n"; result + = "There are three ways to connect a Dragon: \ n"; result + = "the same as the last character, points + 3 \ n "; result + =" homophone connected to the last character, points + 1 \ n "; result + =" includes the last character connected to the dragon, points + 1 \ n "; result + = "bonus for winning streak \ n"; result + = "[description] \ n"; result + = "1. Enter" start ": Start answer \ n "; result + = "2. Enter" Answer ": complete answer \ n"; result + = "3. Enter" ranking ": View ranking \ n"; result + = "4. Enter" Announcement ": back to homepage "; logService. addInfoLog (openId, userName, content, result, ModuleEnum. idiom. getValue (), StepEnum. normal. getValue (); result = MessageUtil. msgReqToXml (msgReq, result);} catch (Exception e) {log. debug ("failed to execute"); logService. addErrorLog (openId, userName, content, "", ModuleEnum. idiom. getValue (), StepEnum. normal. getValue (), e); e. printStackTrac E () ;}// return message log.info (result); return result ;}/ *** get the next question */private String nextIdiom (IdiomUserInfo iUserInfo, String result) {// next question 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); // update the idiomUserService module. update (iUserInfo ); // Return question IdiomListInfo idiomListInfo = idiomListService. getById (questionId); if (! StringUtils. isEmpty (result) {result + = "\ n";} result + = idiomListInfo. getWord (); return result;}/*** process answer result */private String disposeAnswer (IdiomUserInfo iUserInfo, BaseMsgReq msgReq) {log.info ("process answer result "); string result = ""; boolean flag = true; int score = 0; // The previous idiom IdiomListInfo idiomListInfo = idiomListService. getById (iUserInfo. getIdiomId (); // The IdiomListInfo idiomAnswerInfo = idiomListService. g EtByWord (msgReq. getContent (); // no such idiom if (idiomAnswerInfo = null) {result = "this is not an idiom"; flag = false ;} else {// determine whether or not to match // if it is the same match if (idiomListInfo. getWordEnd (). equals (idiomAnswerInfo. getWordBegin () {result = "correct, points + 3 \ n"; flag = true; score = 3 ;}// if the homophone matches else if (idiomListInfo. getWordEndSpell (). equals (idiomAnswerInfo. getWordBeginSpell () {result = "correct, homophonic, points + 1 \ n"; flag = true; score = 1 ;}// if the match else if (I DiomAnswerInfo. getWord (). indexOf (idiomListInfo. getWordEnd ()>-1) {if (idiomListInfo. getId (). equals (idiomAnswerInfo. getId () {// cannot be the same idiom result = "cannot be the same idiom"; flag = false;} else {result = "correct, including, points + 1 \ n "; flag = true; score = 1 ;}// unmatched else {result =" idiom not connected "; flag = false ;}} // correct answer if (flag) {log.info ("correct answer"); // The next question idiomListInfo = idiomListService. getNextIdiom (idiomAnswerInfo. getWordEnd (); if (idiomListI Nfo! = Null) {iUserInfo. setIdiomId (idiomListInfo. getId (); iUserInfo. setStepTime (new Date (); iUserInfo. setIsAnswered (false); // return question if (! StringUtils. isEmpty (result) {result + = "\ n";} result + = idiomListInfo. getWord () ;}else {result + = "I cannot continue, points + 5 \ n"; score + = 5; result = nextIdiom (iUserInfo, result );} // update user points, number of times, number of correct answers, number of winning streak, and maximum winning streak 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);} // response error else {log.info ("Answer error"); // if you reply if (! IUserInfo. getIsAnswered () {// updates user points, times, number of correct answers, number of winning streak, and maximum winning streak iUserInfo. setTimes (iUserInfo. getTimes () + 1); iUserInfo. setIsAnswered (true) ;}// update the idiomUserService. update (iUserInfo); return result;}/*** completed answer */private String finishAnswer (IdiomUserInfo iUserInfo, BaseMsgReq msgReq, boolean flag) {log.info ("completed answer "); string result = ""; // The previous idiom IdiomListInfo idiomListInfo = idiomListService. getById (iUserInfo. GetIdiomId (); // if (flag) {idiomListInfo = idiomListService. getNextIdiom (idiomListInfo. getWordEnd (); if (idiomListInfo! = Null) {result + = "Answer:" + idiomListInfo. getWord () + "\ n";} else {result + = "I do not know the answer \ n" ;}} int score = 0; int comboTimes = iUserInfo. getComboTimes (); if (comboTimes> 1) {int rank = comboTimes/5 + 1; score + = Math. pow (2, rank);} // updates user points, times, number of correct answers, number of winning streak, Maximum Winning iUserInfo. setScore (iUserInfo. getScore () + score); iUserInfo. setStepTime (new Date (); iUserInfo. setIsAnswered (false); iUserInfo. setComboTimes (0); // update the idiomUserService. update (iUserInfo); result + = "correct" + comboTimes + ", winning bonus points +" + score; return result ;}}
Source Code address: https://github.com/ctxsdhy/dntown
Ii. Demonstration