Пример #1
0
def set_hard_chosen_commands(request, s_id):
    spectacle = get_spectacle(s_id)

    scene = get_open_scene(spectacle)

    actors = []
    for a in spectacle.actors.all():
        commands = a.hardmode_set.filter(spectacle=spectacle, scene=scene)
        if commands:
            commands = commands.values('command__pk', 'command__name')
            commands = commands.annotate(total=Count('command__pk'))

            # FIXME
            max_value = 0
            command_id = None
            for c in commands:
                if c['total'] > max_value:
                    max_value = c['total']
                    command_id = c['command__pk']

            command = Command.objects.get(pk = command_id)

            cc = ChosenCommand(spectacle = spectacle,
                               scene = scene,
                               command = command,
                               mode = spectacle.mode,
                               actor = a)
            cc.save()

            actors.append ({ 'actor': { 'name':a.name,
                                         'pk': a.pk },
                             'command': { 'name':command.name,
                                          'pk': command.pk } })

        else:
            # FIXME
            actors.append ({ 'actor': { 'name':a.name,
                                        'pk': a.pk },
                             'command': { 'name': '',
                                          'pk': '' } })

    # FIXME Counter is only in pyhton2.7
    # from django.db.models import Counter
    # total_cc = len(Counter(actor['command']['pk'] for actor in actors))

    total = defaultdict(int)
    for actor in actors:
        total[actor['command']['pk']] += 1
    total_cc = len(total)

    if total_cc == 3:
        spectacle.hard_happiness_meter += 15
    elif total_cc == 2:
        spectacle.hard_happiness_meter -= 5
    elif total_cc == 1:
        spectacle.hard_happiness_meter -= 15
    spectacle.save()

    message = simplejson.dumps( { 'error': 0, 'actors':actors })
    return HttpResponse(message, mimetype="application/json")
Пример #2
0
def easy_add(request, s_id):
    user = request.user

    post = request.POST.copy()
    post['player'] = user.id

    spectacle = get_spectacle(s_id)
    post['spectacle'] = spectacle.id

    scene = get_open_scene(spectacle)
    if scene:
        post['scene'] = scene.id

    form = EasyModeForm(post or None)

    if request.POST and form.is_valid():
        instance = form.save(commit=False)
        command = instance.command
        total_cc = ChosenCommand.objects.filter(spectacle = spectacle,
                                                command = command,
                                                mode = spectacle.mode).count()

        if total_cc < MAX_SAME_COMMAND and spectacle.mobile_interaction:
            instance.save()
            total_scene_command = EasyMode.objects.filter(spectacle = spectacle,
                                                          scene = scene,
                                                          command = command)
            total_scene_command = total_scene_command.count()

            has_cc = ChosenCommand.objects.filter(spectacle = spectacle,
                                                  scene = scene).count()

            minimum = get_minimum(spectacle)

            if total_scene_command >= minimum and not has_cc:
                cc = ChosenCommand(spectacle = spectacle,
                                   scene = scene,
                                   command = command,
                                   mode = spectacle.mode)
                cc.save()

                if total_cc == 0:
                    spectacle.easy_happiness_meter += cc.command.value_1
                elif total_cc == 1:
                    spectacle.easy_happiness_meter += cc.command.value_2
                elif total_cc == 2:
                    spectacle.easy_happiness_meter += cc.command.value_3

                if spectacle.easy_happiness_meter < 0:
                    spectacle.easy_happiness_meter = 0;
                elif spectacle.easy_happiness_meter > 100:
                    spectacle.easy_happiness_meter = 100;

                spectacle.save()

            message = simplejson.dumps( { 'error': 0 } )
            return HttpResponse(message, mimetype="application/json")

        else:
            message = simplejson.dumps( { 'error': 1,
                                          'error-msg': 'max-same-command',
                                          'command': [ {'name': command.name,
                                                        'pk': command.pk } ] } )
            return HttpResponse(message, mimetype="application/json")
    else:
        message = simplejson.dumps( { 'error': 1 } )
        return HttpResponse(message, mimetype="application/json")