示例#1
0
def recount(request):    
    standingAllTours = []
    standingDelta = []
    PointTour.objects.all().delete();
    allPlayers = Player.objects.all()
    
    for curPlayer in allPlayers: 
        for last_id_tour in range(settingsNUM_TOUR()): 
            name_player = User.objects.get(username=curPlayer.name).first_name + " " + User.objects.get(username=curPlayer.name).last_name
            his_words = list(set(CurrentGame.objects.filter(name = curPlayer, id_tour = last_id_tour)))
            #CurrentGame.objects.filter(name = curPlayer, id_tour = last_id_tour).delete()
            #for word in his_words:
            #    p = CurrentGame(name = Player.objects.get(name = name_player), word = word, id_tour = last_id_tour)
            #    p.save()   
            
    for curPlayer in allPlayers: 
        player_points = 0
        for last_id_tour in range(settingsNUM_TOUR()): 
            #обновление баллов для данного игрока
            his_words = CurrentGame.objects.filter(name = curPlayer, id_tour = last_id_tour)
            name_player = User.objects.get(username=curPlayer.name).first_name + " " + User.objects.get(username=curPlayer.name).last_name
            if last_id_tour == id_tour:
                standingDelta.append([name_player, player_points]);
            for cur_word in his_words:
                player_points += len(CurrentGame.objects.filter(word = cur_word.word, id_tour = last_id_tour)) - 1
            standingAllTours.append([name_player, player_points])
            standingDelta[-1][1] = player_points - standingDelta[-1][1]
            Player.objects.filter(name = curPlayer.name).update(point = player_points)
            newPointTours = PointTour(user = User.objects.get(username = curPlayer.name), id_tour = id_tour, point = standingDelta[-1][1])
            newPointTours.save()    
    return
示例#2
0
def results(request):
    id_tour = (cur_time() - time_start())/(length_game())
    timeStartResult = time_start() + settings('SEGMENT_GAME') * (id_tour + 1) + settings('SEGMENT_WAITING')*(id_tour + 1) + settings('WATCHING_RESULT') * id_tour
    if id_tour >= settingsNUM_TOUR() or id_tour < 0 or cur_time() < timeStartResult or is_watch_game() != 1:
        return HttpResponseRedirect('/home/')

    standingAllTours = []
    standingDelta = []
    allPlayers = Player.objects.all()
    isCount = False        
    countResults.acquire()
    print u"in results " + request.user.username + " " + str(id_tour)
    try:
        if(len(PointTour.objects.filter(id_tour = id_tour)) < len(allPlayers)):
            isCount = True
            for curPlayer in allPlayers: 
                player_points = 0
                for last_id_tour in range(id_tour + 1): 
                    #обновление баллов для данного игрока
                    his_words = CurrentGame.objects.filter(name = curPlayer, id_tour = last_id_tour, validity = 1)
                    name_player = User.objects.get(username=curPlayer.name).first_name + " " + User.objects.get(username=curPlayer.name).last_name
                    if last_id_tour == id_tour:
                        standingDelta.append([name_player, player_points]);
                    for cur_word in his_words:
                        player_points += len(CurrentGame.objects.filter(word = cur_word.word, id_tour = last_id_tour, validity = 1)) - 1
                standingAllTours.append([name_player, player_points])
                standingDelta[-1][1] = player_points - standingDelta[-1][1]
                Player.objects.filter(name = curPlayer.name).update(point = player_points)
                print str(User.objects.get(username = curPlayer.name).id) + " " + str(standingDelta[-1][1]) + " " + str(player_points)
                if(len(PointTour.objects.filter(user = User.objects.get(username = curPlayer.name), id_tour = id_tour)) == 0):
                    newPointTours = PointTour(user = User.objects.get(username = curPlayer.name), id_tour = id_tour, point = standingDelta[-1][1])
                    newPointTours.save()
    finally:
        print u"out results " + request.user.username + " " + str(id_tour)
        if countResults.locked() :
            countResults.release()

    if isCount == False :
        for curPlayer in allPlayers:
            name_player = User.objects.get(username=curPlayer.name).first_name + " " + User.objects.get(username=curPlayer.name).last_name
            resultPlayer = PointTour.objects.filter(user = User.objects.get(username=curPlayer.name))
            sumAll = 0
            delta = 0
            for result in resultPlayer:
                if(result.id_tour <= id_tour):
                    sumAll += result.point
                if(result.id_tour < id_tour):
                    delta += result.point
            standingDelta.append([name_player, sumAll - delta])
            standingAllTours.append([name_player, sumAll])

    
    standingDelta.sort(key = lambda element: element[1], reverse = True)
    standingAllTours.sort(key = lambda element: element[1], reverse = True)

    #количество одинаковых слов
    statistics_words = CurrentGame.objects.filter(id_tour = id_tour)
    dict_words = {}
    for it in statistics_words:
        dict_words[it.word] = dict_words.get(it.word, 0)
        dict_words[it.word] += 1

    BanWordsTemp = CurrentGame.objects.filter(validity = 0)
    BanWords = []
    for word in BanWordsTemp:
        BanWords.append(word.word)

    #списки участник - его слова
    allPlayers = Player.objects.all()
    listWords = []
    for name in allPlayers:
        name_player = User.objects.get(username=name.name).first_name + " " + User.objects.get(username=name.name).last_name
        listWords.append([name_player, CurrentGame.objects.filter(name = name, id_tour = id_tour)])

    st_words = dict_words.items()
    st_words.sort(key = lambda element: element[1], reverse = True)

    title = settingsString('TITLE').split(",")
    messageForTitle = u"Ситуация в игре после " + str(id_tour + 1)  + u" тура. Тема " + title[id_tour] + "."
    cur_tour = id_tour + 1
    num_tour = settingsNUM_TOUR()
    if id_tour >= settingsNUM_TOUR() - 1:
        return render_to_response('results.html', dict({"standings": Player.objects.all(), "list_words": listWords}, **locals()))
    else:
        time = time_start() + settings('SEGMENT_GAME') * (id_tour + 1) + settings('SEGMENT_WAITING')*(id_tour + 1) + settings('WATCHING_RESULT')*(id_tour + 1)
        NUMBER_SEC_REDIRECT = time - cur_time()
        return render_to_response('results.html', dict({"standings": Player.objects.all(), "list_words": listWords, 'message': "До следующего тура осталось ", 'next': 'yes',  'NEXT_PAGE': '/write.words/'}, **locals()))