def check_connection(request):
    user = check_user(request)
    if user != None:
        data_json = {'answer': True, 'error': ""}
    else:
        data_json = {'answer': False, 'error': "Mdp/login invalide"}
    return JsonResponse(data_json, safe=False)
def new_friend_answer(request):
    user = check_user(request)
    data_json = {}
    if user != None:
        try:
            answer = request.POST['answer']
            id_info = int(request.POST['id'])
            answ = answer == "true"
            demander = user.info.friend_demands.filter(id=id_info).first()
            if demander != None:
                if answ:
                    game = Game()
                    game.save()
                    user.info.games.add(game)
                    demander.games.add(game)

                user.info.friend_demands.remove(demander)
                if answ:
                    demander.save()
                user.info.save()

        except:
            pass

    return JsonResponse(data_json, safe=False)
Exemple #3
0
def quit_game(request):
    user = check_user(request)
    if user != None:
        try:
            id_game = int(request.POST['id_game'])
            game = Game.objects.filter(id = id_game).first()
            if game != None and game.number_of_letters >=50:
             
                if game.number_of_quit == 0:
                    game.number_of_quit = 1
                    game.userleft = user.info
                    game.save()
                    
                else:
                    if game.userleft.id != user.info.id:
                    
                        game.isStarted = False
                        game.save()
              
            else:
                
                data_json = {'error' : True}
        except:
           
            data_json = {'error' : True}
    else:
       
        data_json = {'error' : True}
    print(data_json)
    return JsonResponse(data_json, safe = False)
Exemple #4
0
def send_letter(request):
    user = check_user(request)
    if user != None:
        user_info_obj = user.info
        try:
            letter = request.POST['letter']
            if len(letter) == 1 and letter.lower() in "abcdefghijklmnopqrstuvwxyz":
                id_game = int(request.POST['id_game'])
                game = Game.objects.filter(id = id_game).first()
                if game != None:
                    if game.letter_choosed == "" or game.letter_choosed == None:
                        game.letter_choosed = letter
                        game.save()
                        data_json = send_game_info(game, user_info_obj)
                    else:
                        data_json = {'error' : True}
                else:
                    data_json = {'error' : True}
            else:
                    data_json = {'error' : True}
        except:
            data_json = {'error' : True}
    else:
        data_json = {'error' : True}
    return JsonResponse(data_json, safe = False)
Exemple #5
0
def send_letter_grid(request):
    user = check_user(request)
    if user != None:
        user_info_obj = user.info
        try:
            letter = request.POST['letter']
            id_game = int(request.POST['id_game'])
            i = int(request.POST['i'])
            j = int(request.POST['j'])
            game = Game.objects.filter(id = id_game).first()
            if game != None and game.number_of_letters < 50 and i>= 0 and i < 5 and j >= 0 and j < 5:
                
                already_ex = game.cells.filter(Q(user__id = user_info_obj.id)  & \
                                  Q(row = i) & Q(col = j)).first()
                if already_ex == None:
                    cell = Cell()
                    cell.game = game
                    cell.row = i
                    cell.col = j
                    cell.user = user_info_obj
                    cell.letter = letter.upper()
                    cell.save()
                    game.number_of_letters += 1
                    if game.userplayed != None:
                        game.userplayed = None
                        game.letter_choosed = ""
                        
                        if game.userturn.id == user_info_obj.id:
                            others = game.users.all()
                            other_f = None
                            for other in others:
                                if other.id != user_info_obj.id:
                                    other_f = other
                                    break
                            if other_f != None:
                                game.userturn = other_f
                        else:
                            game.userturn = user_info_obj
                    else:
                        game.userplayed = user_info_obj
               
                    game.save()
                    data_json = send_game_info(game, user_info_obj)
                else:
                    data_json = {'error' : True}
            else:
                data_json = {'error' : True}
        except:
            data_json = {'error' : True}
    else:
        data_json = {'error' : True}
    return JsonResponse(data_json, safe = False)
def add_friend(request):
    user = check_user(request)
    if user != None:

        try:
            login_val = request.POST['friend']

            new_frien = User.objects.filter(username=login_val).first()

            if new_frien != None and new_frien.id != user.id:
                try:

                    games = user.info.games.all()
                    check_is_friend = True
                    for g in games:

                        if g.users.filter(
                                id=new_frien.info.id).first() != None:
                            check_is_friend = False
                            break

                    if check_is_friend:
                        user.info.friends_asked.add(new_frien.info)
                        user.info.save()
                        data_json = {
                            'error':
                            False,
                            'message':
                            "Veuillez attendre la réponse de votre contacte"
                        }
                    else:
                        data_json = {
                            'error': True,
                            'message': "C'est déjà votre amis"
                        }

                except:
                    data_json = {'error': True, 'message': "Internal error"}

            else:
                data_json = {'error': True, 'message': "Contacte non trouvé"}
        except:
            data_json = {'error': True, 'message': "Connection error"}

    else:
        data_json = {'error': True, 'message': "Login error"}
    return JsonResponse(data_json, safe=False)
Exemple #7
0
def read_game(request):
    user = check_user(request)
    if user != None:
        user_info_obj = user.info
        try:
            id_game = int(request.POST['id_game'])
            
            game = Game.objects.filter(id = id_game).first()
            if game != None:
                data_json = send_game_info(game, user_info_obj)
            else:
                data_json = {'error' : True}
        except:
            data_json = {'error' : True}
    else:
        data_json = {'error' : True}
    return JsonResponse(data_json, safe = False)
def get_new_friend_list(request):
    user = check_user(request)
    if user != None:
        user_info_obj = user.info
        list_new_friend = []

        for friend_request in user_info_obj.friend_demands.all():
            list_new_friend.append({
                "id": friend_request.id,
                "name": friend_request.user.username,
            })
        list_friend = []
        for game_found in user_info_obj.games.all():
            for user_aux in game_found.users.all():
                if user_aux.id != user_info_obj.id:
                    letter_choose = game_found.letter_choosed != None
                    if letter_choose != None and game_found.letter_choosed != "":
                        letter = game_found.letter_choosed
                    else:
                        letter = ""
                    yourturn = game_found.userturn.id == user_info_obj.id
                    waiting_for_other = (\
                    (letter != "" and game_found.userplayed != None and \
                     game_found.userplayed.id == user_info_obj.id) or \
                    (not yourturn and letter == ""))
                    gamefinished = game_found.number_of_letters >= 50
                    list_friend.append({"id": game_found.id,
                                            "name" : user_aux.user.username,
                                            "mmr" : user_aux.mmr,
                                            "game_started" : game_found.isStarted,
                                            "can_play" : not waiting_for_other,
                                            "game_finished" : gamefinished,
                                            "wait_quit" : game_found.userleft != None\
                                             and game_found.userleft.id == user_info_obj.id,
                                            })

        data_json = {'error' : False, \
                     'new_friend_list' : list_new_friend, \
                     'list_friend' : list_friend, "elo" : int(user_info_obj.mmr)}

    else:
        data_json = {'error': True, 'new_friend_list': [], 'list_friend': []}

    return JsonResponse(data_json, safe=False)