def create_game(request): game_name = request.POST.get('game_name') default_x = request.POST.get('default_x') default_y = request.POST.get('default_y') goal_x = request.POST.get('goal_x') goal_y = request.POST.get('goal_y') if not Game.get_game_by_game_name(game_name): game = Game.create_game(game_name, [], goal_x, goal_y) flag = Flag.create_flag(game_name, default_x, default_y) return HttpResponse(simplejson.dumps({}), mimetype="application/json")
def update_location(self, x, y): """Update x, y location.""" self.location_x = x self.location_y = y self.save() for game_name in self.flag_list: flag = Flag.get_flag_by_game_name(game_name) flag.update_location(x, y) if flag.is_in_goal(): game = Game.get_game_by_game_name(game_name) game.end_game(self)
def get_flag(request, game_name): flag = Flag.get_flag_by_game_name(game_name) if not flag: raise Http404 response_data = { 'x_pos': flag.location_x, 'y_pos': flag.location_y, 'is_held': flag.is_held } return HttpResponse(simplejson.dumps(response_data), mimetype="application/json")
def pick_up_flag(request, player_name): player = Player.get_player_by_player_name(player_name) if not player: raise Http404 game_name = request.POST.get('game_name') flag = Flag.get_flag_by_game_name(game_name) if abs(flag.location_x - player.location_x) < 1 and abs(flag.location_y - player.location_y) < 1: player.pick_up_flag(flag) return HttpResponse(simplejson.dumps({}), mimetype="application/json") raise HttpResponseBadRequest('can\'t reach')
def pick_up_flag(request, player_name): player = Player.get_player_by_player_name(player_name) if not player: raise Http404 game_name = request.POST.get('game_name') flag = Flag.get_flag_by_game_name(game_name) if abs(flag.location_x - player.location_x) < 1 and abs( flag.location_y - player.location_y) < 1: player.pick_up_flag(flag) return HttpResponse(simplejson.dumps({}), mimetype="application/json") raise HttpResponseBadRequest('can\'t reach')
def get_game(request, game_name): game = Game.get_game_by_game_name(game_name) flag = Flag.get_flag_by_game_name(game_name) if not game or not flag: raise Http404 response_data = { 'game_name': game.game_name, 'goal_x': game.goal_x, 'goal_y': game.goal_y, 'is_active': game.is_active, 'players': game.player_list, 'flag_x': flag.location_x, 'flag_y': flag.location_y, 'flag_held_by': flag.held_by } return HttpResponse(simplejson.dumps(response_data), mimetype="application/json")
def drop_flag(self, game_name): """Drop flag""" flag = Flag.get_flag_by_game_name(game_name) flag.reset() self.flag_list.remove(game_name)