def makerobogame(request): fb_id = ROBOT1 request.session['fb_id'] = fb_id # make robot player 1 try: m_from = Member.objects.get( fb_id=ROBOT1 ) except Member.DoesNotExist: m_from = Member(fb_id=ROBOT1) m_from.user_name = 'Robo1' m_from.fb_first_name = 'Robo1' m_from.fb_last_name = 'Robo1' m_from.save() # make robot player 2 try: m_to = Member.objects.get( fb_id=ROBOT2 ) except Member.DoesNotExist: m_to = Member(fb_id=ROBOT2) m_to.user_name = 'Robo2' m_to.fb_first_name = 'Robo2' m_to.fb_last_name = 'Robo2' m_to.save() # name is set request_from = 'Robo1' request_to = 'Robo2' gamename = "%s_%s_%s" % ( request_from, request_to, time.asctime(time.gmtime()) ) r = Room(name=gamename) r.fb_request_id = ROBOREQUEST r.save() g = Game(room=r) # game name and room name duplicated g.name = gamename g.save() p = Player(member=m_from) # player in game, link to member p.game=g # game p.turn=0 # static order of player in game p.save() request.session['player_id'] = p.turn # TODO assumes two player game - generalize p = Player( member = m_to ) p.game=g p.turn=1 p.save() return HttpResponse( simplejson.dumps( { "gamename": gamename } ), mimetype='application/javascript' )
def handlefbrequestcallback(request): fp = open( 'tmp.3.log', 'w' ) print >> fp, request request_str = ( request.GET.get('request') ) request_to = ( request.GET.get('to') ) request_id = ( request.GET.get('id') ) request_from = request.session['fb_id'] print >> fp, "request id", request_id print >> fp, "request string", request_str print >> fp, "request to", request_to print >> fp, "request from", request_from print >> fp, "request fb id", request.session['fb_id'] instance = UserSocialAuth.objects.filter(provider='facebook',user=request.user.id).order_by('-id')[0] graph = facebook.GraphAPI(instance.tokens["access_token"]) try: profile = graph.get_object("me") except: logout(request) return HttpResponseRedirect( reverse('lupalo.views.lupalo') ) # Redirect after POST try: m_to = Member.objects.get(fb_id=request_to) except: # need to make member m_to = Member(fb_id=request_to) # play against robot if request_to == 'auto': m_to.fb_first_name = 'auto' m_to.fb_last_name = 'auto' else: profile = graph.get_object( str(request_to) ) m_to.fb_first_name = profile['first_name'] m_to.fb_last_name = profile['last_name'] m_to.save() if m_to.fb_first_name == None or m_to.fb_last_name == None: profile = graph.get_object( str(request_to) ) m_to.fb_first_name = profile['first_name'] m_to.fb_last_name = profile['last_name'] m_to.save() try: m_from = Member.objects.get(fb_id=request_from) except: # need to make member m_from = Member(fb_id=request_from) profile = graph.get_object( str(request_from) ) m_from.fb_first_name = profile['first_name'] m_from.fb_last_name = profile['last_name'] m_from.save() if m_from.fb_first_name == None or m_from.fb_last_name == None: profile = graph.get_object( str(request_from) ) m_from.fb_first_name = profile['first_name'] m_from.fb_last_name = profile['last_name'] m_from.save() # name is set gamename = "%s_%s_%s" % ( request_from, request_to, time.asctime(time.gmtime()) ) print >> fp, "gamename", gamename r = Room(name=gamename) r.fb_request_id = request_id r.save() g = Game(room=r) # game name and room name duplicated g.name = gamename g.save() p = Player(member=m_from) p.game=g p.turn=0 p.save() # TODO assumes two player game - generalize p = Player( member = m_to ) p.game=g p.turn=1 p.save() #return HttpResponseRedirect('requestpage.html') return HttpResponseRedirect( reverse('lupalo.views.lupalo') ) # Redirect after POST