當前位置: 首頁>>代碼示例>>Python>>正文


Python Game.players方法代碼示例

本文整理匯總了Python中game.models.Game.players方法的典型用法代碼示例。如果您正苦於以下問題:Python Game.players方法的具體用法?Python Game.players怎麽用?Python Game.players使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在game.models.Game的用法示例。


在下文中一共展示了Game.players方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: request_game

# 需要導入模塊: from game.models import Game [as 別名]
# 或者: from game.models.Game import players [as 別名]
def request_game(request):
  session_id = request.session.get('_auth_user_id', -1)
  if(session_id == -1):
    return login_page(request)
  username = User.objects.get(pk=session_id)
  platform = get_platform(request.get_signed_cookie('platform', 'x86'))
  if(request.get_signed_cookie('at_index', default='False') == 'True'):
    if(len(GameRequest.objects.filter(host=username)) >= 1):
      #return HttpResponse("Can Only host one game at a time!!")
      csrf_request = {'username' : str(username)}
      return render_to_response('delete_req.html',csrf_request, context_instance=RequestContext(request))
    num_players = get_request_param(request,'num_players', 0)
    has_cpu = get_request_param(request,'has_cpu', 0)
    gamerequest = GameRequest(requested_players=num_players, accepted_players=1, has_cpu=has_cpu, host=username)
    gamerequest.save()
    gamerequest.player_names.add(username)
    gamerequest.save()
    game = Game(num_players=num_players, has_cpu=False, log = '', num_cpu_dice = 0, turn=1, pk=gamerequest.id, cpu_roll=0, num_dice = NUM_DICE*num_players, timestamp=0)
    game.save()
    #chat related?
    room = Room(id=game.id)
    room.save()
    userprofile = UserProfile.objects.get(user=username)
    userprofile.turn=1
    userprofile.num_dice = NUM_DICE 
    userprofile.save()
    csrf_request = {'message' : 'waiting'} 
    csrf_request.update(csrf(request))
    response = render_to_response('wait.html',  csrf_request, context_instance=RequestContext(request))
    response.set_signed_cookie('at_index', 'False')
    response.set_signed_cookie('gamerequest_id', str(gamerequest.id))
    #originally i stored turn in a cookie.  then i realized it was not feasible because
    #one player must know the turns of the others, so i now store in db.  I may remove the
    #cookie later
    response.set_signed_cookie('turn', 1)
    response.set_signed_cookie('playing_cpu', 0)
    return response
  else:
    int_gamerequest_id = int(request.get_signed_cookie('gamerequest_id'))
    try:
      gamerequest = GameRequest.objects.get(pk=int_gamerequest_id)
    except:
      gamerequest = -1
    if(gamerequest == -1):
      csrf_request = {} 
      csrf_request.update(csrf(request))
      return render_to_response(index_page[platform],  csrf_request, context_instance=RequestContext(request))
       
    #during development, >= will save me time.  later should make ==
    #if(gamerequest.accepted_players >= gamerequest.requested_players):
    if(gamerequest.accepted_players == gamerequest.requested_players):
      try:
        game = Game.objects.get(pk=gamerequest.id)
      except:
        game = -1
      if(game == -1):
        csrf_request = {} 
        csrf_request.update(csrf(request))
        return render_to_response(index_page[platform],  csrf_request, context_instance=RequestContext(request))
      game.players = gamerequest.player_names.all()
      game.save()
      csrf_request = {'multiplayer_begin':True, 
                      'host' : gamerequest.host, 
                      'num_players' : gamerequest.requested_players,
                      'game_fsm' : 0,
                      'playing_cpu' : 0,
                      'username' : str(username),
                      'dice_list' : [],
                      'game_id' : gamerequest.id#this doesn't seem to be working due to auto refresh 
                     }
      csrf_request.update(csrf(request))
      response = render_to_response(main_page[platform],  csrf_request, context_instance=RequestContext(request))
      response.set_signed_cookie('inagame', 'True')
      response.set_signed_cookie('game_id', gamerequest.id)
      response.set_signed_cookie('game_fsm', 0)
      response.set_signed_cookie('bullshit_timestamp', 0)
      return response
    elif(gamerequest.accepted_players < gamerequest.requested_players):
      csrf_request = {'message' : 'still waiting'} 
      csrf_request.update(csrf(request))
      response = render_to_response('wait.html',  csrf_request, context_instance=RequestContext(request))
      return response
    else:
      #if accepted_players > requested players then the user has probably hit back on browser
      #and attempted to reentered.  don't allow, redirect to index
      csrf_request = {} 
      csrf_request.update(csrf(request))
      return render_to_response(index_page[platform],  csrf_request, context_instance=RequestContext(request))
開發者ID:joshyg,項目名稱:liarsdice,代碼行數:90,代碼來源:views.py


注:本文中的game.models.Game.players方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。