Exemple #1
0
def table(request, id):        
    print "request:", request.method
    if request.method == 'POST':
        action = request.POST['action']
        print action
        if action == "start":
            wwg = WordwallsGame()
            gameReady = wwg.startRequest(request.user, id)
            if not gameReady:
                response = HttpResponse(json.dumps({"serverMsg": request.user.username}), mimetype="application/javascript")
            else:
                quizParams = wwg.startQuiz(id)

                response = HttpResponse(json.dumps(quizParams, ensure_ascii=False), mimetype="application/javascript")
            response['Content-Type'] = 'text/plain; charset=utf-8'
            return response
        elif action == "guess":
            print request.POST['guess']
            wwg = WordwallsGame()
            state = wwg.guess(request.POST['guess'], id)
            print state

            response = HttpResponse(json.dumps({'g': state[0], 'C': state[1]}, ensure_ascii=False), 
                        mimetype="application/javascript")

            response['Content-Type'] = 'text/plain; charset=utf-8'
            return response
        elif action == "gameEnded":
            wwg = WordwallsGame()
            ret = wwg.checkGameEnded(id)
            if ret:
                response = HttpResponse(json.dumps({'g': False}, ensure_ascii=False), 
                            mimetype="application/javascript")
                response['Content-Type'] = 'text/plain; charset=utf-8'
                return response
        elif action == "giveUp":
            wwg = WordwallsGame()
            ret = wwg.giveUp(request.user, id)
            if ret:
                response = HttpResponse(json.dumps({'g': False}, ensure_ascii=False), 
                            mimetype="application/javascript")
                response['Content-Type'] = 'text/plain; charset=utf-8'
                return response
        elif action == "save":
            wwg = WordwallsGame()
            ret = wwg.save(request.user, id, request.POST['listname'])
            print "save returned: ", ret
            response = HttpResponse(json.dumps(ret, ensure_ascii=False), 
                            mimetype="application/javascript")
            response['Content-Type'] = 'text/plain; charset=utf-8'
            return response
        elif action == "giveUpAndSave":
            wwg = WordwallsGame()
            ret = wwg.giveUpAndSave(request.user, id, request.POST['listname'])
            # this shouldn't return a response, because it's not going to be caught by the javascript
            print "giveup and save returned: ", ret
            response = HttpResponse(json.dumps(ret, ensure_ascii=False), 
                            mimetype="application/javascript")
            response['Content-Type'] = 'text/plain; charset=utf-8'
            return response
        elif action == "savePrefs":
            profile = request.user.get_profile()
            profile.customWordwallsStyle = request.POST['prefs']
            print "saving custom style", profile.customWordwallsStyle
            profile.save()
            response = HttpResponse(json.dumps({'success': True}), 
                            mimetype="application/javascript")
            response['Content-Type'] = 'text/plain; charset=utf-8'
            return response
        elif action == "getDcData":
            wwg = WordwallsGame()
            dcId = wwg.getDcId(id)
            if dcId > 0:
                leaderboardData = getLeaderboardDataDcInstance(DailyChallenge.objects.get(pk=dcId))
                response = HttpResponse(json.dumps(leaderboardData, ensure_ascii=False), 
                                        mimetype="application/javascript")
                response['Content-Type'] = 'text/plain; charset=utf-8'
                return response
                
    else:   # it's a GET
        wwg = WordwallsGame()
        permitted = wwg.permit(request.user, id)
        if permitted:
            params = wwg.getAddParams(id)
            # add styling params from user's profile (for styling table tiles, backgrounds, etc)
            try:
                profile = request.user.get_profile()
                style = profile.customWordwallsStyle
                print 'style', style
                if style != "":
                    params['style'] = style
            except:
                pass
                
            return render_to_response('wordwalls/table.html', {'tablenum': id, 
                                                                'username': request.user.username,
                                                                'addParams': json.dumps(params)}, 
                                                    context_instance=RequestContext(request))
        else:
            return render_to_response('wordwalls/notPermitted.html', {'tablenum': id})