
ПРИЛОЖЕНИЕ Б.
Код контроллера GameController. java:
package ru. kazan_street_games. controller;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. security. core. Authentication;
import org. springframework. security. core. context. SecurityContextHolder;
import org. springframework. stereotype. Controller;
import org. springframework. ui. ModelMap;
import org. springframework. web. bind. annotation. PathVariable;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RequestMethod;
import ru. kazan_street_games. model.*;
import ru. kazan_street_games. service.*;
import javax. servlet. http. HttpServletRequest;
@Controller
public class GameController {
private InputKeyService inputKeyService;
private QuestService questService;
private UserService userService;
private UserQuestService userQuestService;
private LevelService levelService;
private UserLevelService userLevelService;
@Autowired
public void setInputKeyService(InputKeyService inputKeyService) {
this. inputKeyService = inputKeyService;
}
@Autowired
public void setQuestService(QuestService questService) {
this. questService = questService;
}
@Autowired
public void setUserService(UserService userService) {
this. userService = userService;
}
@Autowired
public void setUserQuestService(UserQuestService userQuestService) {
this. userQuestService = userQuestService;
}
@Autowired
public void setLevelService(LevelService levelService) {
this. levelService = levelService;
}
@Autowired
public void setUserLevelService(UserLevelService userLevelService) {
this. userLevelService = userLevelService;
}
//Ввод входного ключа
@RequestMapping(value = "/quest/{questId}/quest_start_key", method = RequestMethod. POST)
public String inputKey(HttpServletRequest request, String key,
@PathVariable("questId") int questId, ModelMap model) {
Quest quest = questService. findById(questId);
InputKey inputKey = inputKeyService. findByVal(key);
boolean res = inputKeyService. validate(inputKey, false);
if (res) {
Authentication auth = SecurityContextHolder. getContext().getAuthentication();
User user = userService. getUserByUsername(auth. getName());
if (user!= null) {
inputKey. setUser(user);
} else {
User newUser = userService. generate();
inputKey. setUser(newUser);
}
inputKeyService. changeKey(inputKey);
request. getSession().setAttribute("key", key);
return "redirect:/start_game";
} else {
if (inputKey. getUser() != null) {
UserQuest userQuest = userQuestService. findByKey(key);
if (userQuest!= null) {
if(userQuest. isBegin()){
Level level = userLevelService. findCurrent(userQuest);
if(level!=null) {
model. addAttribute("level", level);
return "level_game";
}
else {
model. addAttribute("userQuest", userQuest);
return "redirect:/finish";
}
}else {
request. getSession().setAttribute("key", key);
return "redirect:/start_game";
}
}
}
model. addAttribute("err", "invalid_key");
model. addAttribute("quest", quest);
return "redirect:/quest/" + questId;
}
}
//вывод страницы с кнопкой "Начать игру"
@RequestMapping(value = "/start_game", method = RequestMethod. GET)
public String startGamePage(HttpServletRequest request, ModelMap model) {
String key = (String) request. getSession().getAttribute("key");
if (key!= null) {
InputKey inputKey = inputKeyService. findByVal(key);
if (inputKey!= null) {
Quest q = questService. findById(inputKey. getQuest().getId());
UserQuest userQuest = userQuestService. findByKey(key);
if (userQuest == null) {
userQuest = new UserQuest(inputKey. getUser(), q, key);
userQuestService. save(userQuest);
}
model. addAttribute("quest", q);
return "start_game";
}
}
return "redirect:/error403";
}
//нажата кнопка "Начать игру"
@RequestMapping(value = "quest/{id}/start_game", method = RequestMethod. POST)
public String startGame(@PathVariable int id, HttpServletRequest request, ModelMap model) {
String key = (String) request. getSession().getAttribute("key");
InputKey inputKey = inputKeyService. findByVal(key);
if (inputKeyService. validate(inputKey, true) && questService. isCanPlayNow(inputKey. getQuest())) {
UserQuest userQuest = userQuestService. findByKey(key);
userQuest. setBegin(true);
userQuestService. save(userQuest);
model. addAttribute("level", levelService. findByQuestAndNumber(inputKey. getQuest(), 1));
return "level_game";
} else {
return "redirect:/error403";
}
}
}
ПРИЛОЖЕНИЕ В.
Код модуля appNewQuest для формы добавления нового квеста (new_quest. js):
обязател
var appNewQuest = angular. module('newQuest', ['yaMap']);
appNewQuest. controller("newQuestController", function ($scope, $http) {
$scope. lastTitle = "";
$scope. title = "";
$scope. isUniqTitle = true;
$scope. uniqTitle = function () {
if (!this. title.$pristine && this. title!== "" && this. title!== this. lastTitle) {
this. lastTitle = this. title;
var data = $.param({
title: $scope. title
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
};
$http. post('getQuestByTitle' , data, config)
.success(function (data) {
$scope. isUniqTitle = (data!== '1');
return data!== '1';
});
}
else {
return $scope. isUniqTitle;
}
};
var map;
$scope. afterInit = function ($map) {
map = $map;
};
$scope. myCoords = [];
$scope. mapClick = function (e) {
if (!map. balloon. isOpen()) {
$scope. myCoords = e. get('coords');
map. balloon. open($scope. myCoords, {
contentHeader: 'Здесь будет место старта',
contentBody: 'Координаты : ' + [$scope. myCoords[0].toPrecision(6), $scope. myCoords[1].toPrecision(6)].join(', ') + '\n',
contentFooter: 'Щелкните для изменения места'
});
}
else {
map. balloon. close();
}
};
$scope. isCommandCountValid = function() {
if(this. maxCommandCount){
return mandCount!==undefined;
}
else {
return true;
}
};
$scope. isConfirmValid = function(){
if(this. confirm){
return this. dateEndConfirm!==undefined;
}
else {
return true;
}
};
$scope. isAlwaysValid = function(){
if(this. always){
return true;
}
else {
return (this. dateBegin!==undefined)&&(this. dateEnd!==undefined);
}
}
});
|
Из за большого объема этот материал размещен на нескольких страницах:
1 2 3 4 5 6 |


