コード例 #1
0
ファイル: interact.py プロジェクト: gamblegear/djmatt
def add_update_game_interact(request):
    
    game_id = request.POST.get('game_id')
    name = request.POST.get('name')
    rows = request.POST.get('rows')
    columns = request.POST.get('columns')
    groups_num = request.POST.get('groups_num')
    panels_per_group = request.POST.get('panels_per_group')
    wait_time = request.POST.get('wait_time')
    total_time = request.POST.get('total_time')
    warning_time = request.POST.get('warning_time')
    switch_cost = request.POST.get('switch_cost')
    
    #parse to int
    try:
        rows = int(rows)
        columns = int(columns)
        groups_num = int(groups_num)
        panels_per_group = int(panels_per_group)
        wait_time = int(wait_time)
    except:
        return HttpResponse('Error')

    try:
        game = Game.objects.get(pk = game_id)
    except:
        game = Game()
        game.create_time = datetime.now()
    
    needInitGroupsPanels = True
    if game.rows and game.columns and game.groups_num \
            and game.panels_per_group \
            and game.rows * game.columns==rows*columns \
            and game.groups_num==groups_num and game.panels_per_group==panels_per_group:
        needInitGroupsPanels = False

    game.name = name
    game.rows = rows
    game.columns = columns
    game.groups_num = groups_num
    game.panels_per_group = panels_per_group
    game.wait_time = wait_time
    game.total_time = total_time
    game.warning_time = warning_time
    game.switch_cost = switch_cost
    
    game.save()


    #initial Groups and Panels
    if needInitGroupsPanels:
        Group.objects.filter(game = game).delete()
        for i in range(groups_num):
            group = Group()
            group.game = game
            group.name = "Group %d" % (i+1) 
            group.save()
            for j in range(panels_per_group):
                panel = Panel()
                panel.group = group
                panel.game = game
                panel.name = "Panel %d" % (j+1)
                panel.progress = '0' * rows * columns
                panel.save()



    nextUrl = reverse('mine.views.mine_admin_view')
    return HttpResponseRedirect(nextUrl)