def move_service(request): if request.method == 'POST': move_form = MoveForm(data=request.POST) if move_form.is_valid(): user = request.user if 'game_selected' in request.session: game = Game.objects.get(id=request.session['game_selected']) else: raise Http404("ERROR 404") try: move = Move(origin=request.POST.get('origin'), target=request.POST.get('target'), game=game, player=user) move.save() return redirect(reverse('show_game')) except ValidationError: return redirect(reverse('show_game')) else: print(move_form.errors) return redirect(reverse('landing')) raise Http404("ERROR 404")
def test4(self): """ Los movimientos que dan una situación de \ victoria cambian el estado a FINISHED """ games = [] games.append(Game.objects.create(cat_user=self.users[0], mouse_user=self.users[1], cat1=25, cat2=43, cat3=38, cat4=47, mouse=31, status=GameStatus.ACTIVE)) games.append(Game.objects.create(cat_user=self.users[0], mouse_user=self.users[1], cat1=63, cat2=61, cat3=54, cat4=38, mouse=45, status=GameStatus.ACTIVE)) games.append(Game.objects.create(cat_user=self.users[0], mouse_user=self.users[1], cat1=27, cat2=22, cat3=43, cat4=45, mouse=36, status=GameStatus.ACTIVE)) moves = [] moves.append(Move(game=games[0], origin=25, target=32, player=self.users[0])) moves.append(Move(game=games[1], origin=38, target=47, player=self.users[0])) moves.append(Move(game=games[2], origin=22, target=29, player=self.users[0])) for game in games: game.full_clean() game.save() self.assertEqual(game.status, GameStatus.ACTIVE) for move in moves: move.full_clean() move.save() for game in games: self.assertEqual(game.status, GameStatus.FINISHED)
def move_service(request): if 'game_selected' not in request.session: return HttpResponseNotFound('No se ha seleccionado una partida a la que jugar') game_id = request.session['game_selected'] games = Game.objects.filter(id=game_id) if len(games) == 0: return HttpResponseNotFound('La partida seleccionada no es válida') game = games[0] if game.status != GameStatus.ACTIVE: return HttpResponseNotFound('La partida no está activa') if game.cat_user != request.user and game.mouse_user != request.user: return HttpResponseNotFound('No eres jugador de la partida seleccionada') player = request.user origin = int(request.POST.get('origin')) target = int(request.POST.get('target')) try: move = Move(origin=origin, target=target, game=game, player=player) move.save() except: pass return redirect(reverse('show_game'))
def test0(self): """ Validación de la actualización del juego al ganar el gato""" game = Game(cat_user=self.user1) game.save() game.cat1 = 41 game.cat2 = 50 game.mouse = 48 game.mouse_user = self.user2 game.save() move = Move(origin=50, target=57, player=self.user1, game=game) move.save() self.assertEqual(game.status, GameStatus.FINISHED)
def test2(self): ''' main author: Alejandro Santorum ''' """ Conversiones a string """ move = Move(game=self.game, player=self.game.cat_user, origin=0, target=9) self.assertEqual(str(move), "[cat_user_test] - Origen: 0 - Destino: 9")
def is_valid(self): move = Move() if self.game == None: if move.valid_nogame(self.data['origin'], self.data['target']): return super(MoveForm, self).is_valid() return False else: if self.game.cat_turn: if move.valid(self.game, self.game.cat_user, self.data['origin'], self.data['target']): return super(MoveForm, self).is_valid() else: if move.valid(self.game, self.game.mouse_user, self.data['origin'], self.data['target']): return super(MoveForm, self).is_valid() return False
def test1(self): """ Validación de la actualización del juego al ganar el raton""" game = Game(cat_user=self.user1) game.save() game.cat1 = 57 game.cat2 = 59 game.cat3 = 61 game.cat4 = 63 game.mouse = 18 game.mouse_user = self.user2 game.cat_turn = False game.save() move = Move(origin=18, target=11, player=self.user2, game=game) move.save() self.assertEqual(game.status, GameStatus.FINISHED)
def move(request): """ move (main author: Alejandro Santorum) ---------- Input parameters: request: received request. It also contains player, selected game information and origin and target of the movement. Player and game information are included in session variable. Origin and target can be found at the POST body parameters. ---------- Returns: It renders "mouse_cat/show_game.html" template; or Error 404 if an invalid method is used. ---------- Raises: None ---------- Description: It develops a movement of a given player in the selected game. User is required to be logged. """ if request.method == 'GET': return HttpResponse('Invalid method.', status=404) # POST if not request.session.get(constants.GAME_SELECTED_SESSION_ID): return HttpResponse('Invalid method.', status=404) game_id = request.session.get(constants.GAME_SELECTED_SESSION_ID) game = Game.objects.get(id=game_id) origin = int(request.POST.get('origin')) target = int(request.POST.get('target')) move_form = MoveForm(data=request.POST) move = Move(origin=origin, target=target, game=game, player=request.user) try: move.save() except ValidationError as err: move_form.add_error('origin', err.messages[0]) board = [0] * constants.BOARD_SIZE board[game.mouse] = -1 for i in game._get_cat_places(): board[i] = 1 context_dict = {'board': board, 'game': game, 'move_form': move_form} return render(request, "mouse_cat/game.html", context_dict) return redirect(reverse('show_game'))
def test1(self): game_t = Game( cat_user=self.users[0], mouse_user=self.users[1], cat1=47, mouse=63) game_t.save() move = Move( game=game_t, player=self.users[0], origin=47, target=54 ) Move.save(move) self.assertEqual(GameStatus.FINISHED, game_t.status)
def move(request): if 'game_id' in request.session.keys(): game_id = request.session['game_id'] if request.method == 'POST': game = Game.objects.get(id=game_id) if game.status == GameStatus.FINISHED: redirect(reverse('index')) move_form = MoveForm(game=game, data=request.POST) if move_form.is_valid(): move = Move( game=game, player=request.user, origin=int( move_form.data['origin']), target=int(move_form.data['target'])) move.save() if game.status == GameStatus.FINISHED: return HttpResponse('<h1>You won</h1> <p><a href="\">Return to homepage</a></p>') return redirect(reverse('show_game')) return HttpResponseNotFound('<h1>Page Not Found</h1>')
def move_service(request): player = request.user if 'game_id' in request.session.keys(): game_id = request.session['game_id'] game = Game.objects.get(id=game_id) if request.method == 'POST': if not player: redirect(reverse('login')) move_form = MoveForm(data=request.POST) if move_form.is_valid(): move = Move(game=Game.objects.get(id=game_id), player=player, origin=move_form.data['origin'], target=move_form.data['target']) move.save() return redirect(reverse('show_game')) return HttpResponseNotFound('<h1>Page Not Found</h1>')
def move_service(request): if 'game_selected' not in request.session: return errorHTTP(request, exception='You have not selected a game', status=404) game_id = request.session['game_selected'] games = Game.objects.filter(id=game_id) if len(games) == 0: return errorHTTP(request, exception='The selected game is not valid', status=404) game = games[0] if game.status != GameStatus.ACTIVE: return errorHTTP(request, exception='The selected game is not active', status=404) if game.cat_user != request.user and game.mouse_user != request.user: return errorHTTP(request, exception='You are not a player of the selected game', status=404) player = request.user origin = int(request.POST.get('origin')) target = int(request.POST.get('target')) try: move = Move(origin=origin, target=target, game=game, player=player) move.save() except Exception: pass return redirect(reverse('show_game'))
def test_query(): # Check if there is an user with id=10 and if it does not exists, creat it u10 = User.objects.get_or_create(id=10, username="******")[0] u10.save() # Check if there is an user with id=11 and if it does not exists, creat it u11 = User.objects.get_or_create(id=11, username="******")[0] u11.save() # Create a game and assign it to the user with id=10 game = Game(cat_user=u10) game.save() # Search for all games with only one user assigned # We sort it at the same time by id g_1u = Game.objects.filter(mouse_user=None).order_by('id') for g in g_1u: print(g) # Assign a second user to the game and start it curr_game = g_1u[0] curr_game.mouse_user = u11 curr_game.save() print(curr_game) # Add moves to the game move1 = Move(game=curr_game, origin=2, target=11, player=curr_game.cat_user) move1.save() # curr_game.cat2 = 11 # curr_game.cat_turn = False # curr_game.save() print(curr_game) move2 = Move(game=curr_game, origin=59, target=52, player=curr_game.mouse_user) move2.save() # curr_game.mouse = 52 # curr_game.cat_turn = True # curr_game.save() print(curr_game)
def main(): user10 = User.objects.get_or_create(username='******', id=10)[0] user10.save() user11 = User.objects.get_or_create(username='******', id=11)[0] user11.save() game1 = Game(cat_user=user10) game1.save() game_query = Game.objects.filter(cat_user__username__contains='', mouse_user=None) print('Juegos donde solo hay un usuario: ') print(game_query) print('') game2 = game_query[0] game2.mouse_user = User.objects.get(id=11) game2.save() print('Juego con menor ID de los anteriores con el ratón añadido: ') print(game2) print('') move = Move(origin=2, target=11, player=user10, game=game2) move.save() print('Juego después del primer movimiento del gato: ') print(game2) print('') move = Move(origin=59, target=52, player=user11, game=game2) move.save() print('Juego después del primer movimiento del ratón: ') print(game2) print('')
from datamodel.models import Game, Move from django.contrib.auth.models import User user_10 = User.objects.get_or_create(id=10)[0] user_10.username = "******" user_10.save() user_11 = User.objects.get_or_create(id=11)[0] user_11.username = "******" user_11.save() game = Game(cat_user=user_10) game.save() games_one_player = Game.objects.filter(mouse_user=None).order_by("id") print("Partidas con un único jugador:") for game_one_player in games_one_player: print(" -> " + str(game_one_player)) game = games_one_player[0] game.mouse_user = user_11 game.save() print("Partida comenzada - " + str(game)) move = Move(game=game, player=user_10, origin=2, target=11) move.save() print("Gato movido a 11 - " + str(game)) move = Move(game=game, player=user_11, origin=59, target=52) move.save() print("Ratón movido a 52 - " + str(game))