def pick_category(request): context = RequestContext(request, {}) #TODO(leifos): filter this #cats = Category.objects.filter(is_shown=True) import random cats = sorted(Category.objects.filter(is_shown=True)[:10], key=lambda x: random.random()) for c in cats: if request.user.is_authenticated(): hs = HighScore.objects.filter(user=request.user, category=c) if len(hs) > 0: c.score = hs[0].highest_score else: c.score = 0 else: c.score = 0 c.url = encode_string_to_url(c.name) avatar = GameAvatar('CategoryPage') if request.user.is_authenticated(): avatar.update(user=request.user) msg = avatar.get() response = render_to_response('pagefetch/cat_picker.html', { 'cats': cats, 'avatar': msg }, context) response.delete_cookie('game_id') return response
def pick_category(request): context = RequestContext(request, {}) #TODO(leifos): filter this #cats = Category.objects.filter(is_shown=True) import random cats = sorted(Category.objects.filter(is_shown=True)[:10], key=lambda x: random.random()) for c in cats: if request.user.is_authenticated(): hs = HighScore.objects.filter(user=request.user ,category=c) if len(hs) >0: c.score = hs[0].highest_score else: c.score = 0 else: c.score = 0 c.url = encode_string_to_url(c.name) avatar = GameAvatar('CategoryPage') if request.user.is_authenticated(): avatar.update(user=request.user) msg = avatar.get() response = render_to_response('pagefetch/cat_picker.html', {'cats': cats, 'avatar': msg}, context) response.delete_cookie('game_id') return response
def display_next_page(request): user = request.user avatar = GameAvatar('Skip') if request.user.is_authenticated(): avatar.update(user=user) if request.COOKIES.has_key('game_id'): ds = EngineFactory("bing", api_key=BING_API_KEY) gm = RMIYCMechanic(ds) game_id = request.COOKIES.get('game_id') gm.retrieve_game(user, game_id) gm.take_points() gm.update_game() p = gm.get_current_page() current_score = gm.get_current_score() quoted_screenshot = str(p.screenshot) msg = avatar.get() if gm.is_game_over(): gm.handle_game_over() objects = { "screenshot": quoted_screenshot, "is_game_over": 1, "no_round": gm.get_round_no(), "no_successful_round": gm.get_no_rounds_completed(), "no_of_queries_issued_for_current_page": gm.get_no_of_queries_issued_for_current_page(), "no_remaining_rounds": gm.get_remaining_rounds(), "current_score": current_score, "avatar": msg } else: gm.set_next_page() gm.update_game() p = gm.get_current_page() current_score = gm.get_current_score() quoted_screenshot = str(p.screenshot) objects = { "screenshot": quoted_screenshot, "is_game_over": 0, "no_round": gm.get_round_no(), "no_successful_round": gm.get_no_rounds_completed(), "no_of_queries_issued_for_current_page": gm.get_no_of_queries_issued_for_current_page(), "no_remaining_rounds": gm.get_remaining_rounds(), "current_score": current_score, "avatar": msg } data = json.dumps(objects) return HttpResponse(data, mimetype='application/json') else: # the game has not been created yet # redirect to play view print 'the game has not been created yet' return HttpResponseRedirect('/pagefetch/cat_picker/')
def search(request): #get current user user = request.user result_list = [] #create game avatar avatar = GameAvatar('Search') if request.user.is_authenticated(): avatar.update(user=user) if request.COOKIES.has_key('game_id'): ds = EngineFactory("bing", api_key=BING_API_KEY) gm = GameMechanic(ds) game_id = request.COOKIES.get('game_id') gm.retrieve_game(user, game_id) if request.method == 'GET': query = request.GET['query'].strip() #Augement query #this limits querying to www.gla.ac.uk #query += ' site:gla.ac.uk ' if query: result_list = gm.get_search_results(query,top=10) gm.handle_query(query) gm.update_game() avatar.update(current_game=gm.game) # get the last query score objects = [] for item in result_list: objects.append({"title": item.title, "link": item.url, "summary": item.summary}) json_objects = json.dumps(objects) last_score = gm.get_last_query_score() current_score = gm.get_current_score() msg = avatar.get() if gm.is_game_over(): gm.handle_game_over() Json_results = { "results": json_objects, "score": last_score, "is_game_over": 1, "url_to_find":gm.get_current_page().url, "no_round": gm.get_round_no(), "no_successful_round": gm.get_no_rounds_completed(), "no_of_queries_issued_for_current_page": gm.get_no_of_queries_issued_for_current_page(), "no_remaining_rounds": gm.get_remaining_rounds(), "current_score": current_score, "avatar": msg } else: Json_results = { "results": json_objects, "score": last_score, "is_game_over": 0, "url_to_find":gm.get_current_page().url, "no_round": gm.get_round_no(), "no_successful_round": gm.get_no_rounds_completed(), "no_of_queries_issued_for_current_page": gm.get_no_of_queries_issued_for_current_page(), "no_remaining_rounds": gm.get_remaining_rounds(), "current_score": current_score, "avatar": msg } data = json.dumps(Json_results) return HttpResponse(data, mimetype='application/json') else: # the game has not been created yet # redirect to play view return HttpResponseRedirect('/pagefetch/cat_picker/')
def play(request, category_name): context = RequestContext(request, {}) #Create game avatar avatar = GameAvatar('GamePage') #Get the current user u = request.user # If the user is anonymous, then the user anon (added to db in population scripts) will be retrieved if not u.is_authenticated(): u = User.objects.get(username='******') else: avatar.update(request.user) #decode the category name to replace '_' with ' ' decoded_category_name = decode_url_to_string(category_name) #query the database for the provided category name c = Category.objects.get(name=decoded_category_name) #create game mechanics gm = GameMechanic() # This view shall be called when a new game is to start # Thus, there should be no cookie containing a game_id if request.COOKIES.has_key('game_id'): # redirect the player to the page where they can pick a category and start a new game response = HttpResponseRedirect('/pagefetch/pick_category/') # delete the cookie response.delete_cookie('game_id') return response else: # create a new game gm.create_game(u, c, 0) # get the game_id to assign it later to a cookie game_id = gm.get_game_id() # get the current page that is going to be displayed first to the user p = gm.get_current_page() avatar.update(current_game=gm.game) msg = avatar.get() response = render_to_response( 'pagefetch/game.html', { 'page': p.screenshot, 'avatar': msg, 'game_running': True, 'category': decoded_category_name, 'cat': c }, context) response.set_cookie('game_id', game_id) # terminate the session whenever the browser closes #response.cookies.set_expiry(0) return response
def play(request, category_name): context = RequestContext(request, {}) #Create game avatar avatar = GameAvatar('GamePage') #Get the current user u = request.user # If the user is anonymous, then the user anon (added to db in population scripts) will be retrieved if not u.is_authenticated(): u = User.objects.get(username='******') else: avatar.update(request.user) #decode the category name to replace '_' with ' ' decoded_category_name= decode_url_to_string(category_name) #query the database for the provided category name c = Category.objects.get(name= decoded_category_name) #create game mechanics gm = GameMechanic() # This view shall be called when a new game is to start # Thus, there should be no cookie containing a game_id if request.COOKIES.has_key('game_id'): # redirect the player to the page where they can pick a category and start a new game response = HttpResponseRedirect('/pagefetch/pick_category/') # delete the cookie response.delete_cookie('game_id') return response else: # create a new game gm.create_game(u, c, 0) # get the game_id to assign it later to a cookie game_id = gm.get_game_id() # get the current page that is going to be displayed first to the user p = gm.get_current_page() avatar.update(current_game=gm.game) msg = avatar.get() response = render_to_response('pagefetch/game.html', {'page': p.screenshot, 'avatar':msg ,'game_running':True, 'category':decoded_category_name, 'cat':c}, context) response.set_cookie('game_id', game_id) # terminate the session whenever the browser closes #response.cookies.set_expiry(0) return response
def search(request): #get current user user = request.user result_list = [] #create game avatar avatar = GameAvatar('Search') if request.user.is_authenticated(): avatar.update(user=user) if request.COOKIES.has_key('game_id'): ds = EngineFactory("bing", api_key=BING_API_KEY) gm = GameMechanic(ds) game_id = request.COOKIES.get('game_id') gm.retrieve_game(user, game_id) if request.method == 'GET': query = request.GET['query'].strip() #Augement query #this limits querying to www.gla.ac.uk #query += ' site:gla.ac.uk ' if query: result_list = gm.get_search_results(query, top=10) gm.handle_query(query) gm.update_game() avatar.update(current_game=gm.game) # get the last query score objects = [] for item in result_list: objects.append({ "title": item.title, "link": item.url, "summary": item.summary }) json_objects = json.dumps(objects) last_score = gm.get_last_query_score() current_score = gm.get_current_score() msg = avatar.get() if gm.is_game_over(): gm.handle_game_over() Json_results = { "results": json_objects, "score": last_score, "is_game_over": 1, "url_to_find": gm.get_current_page().url, "no_round": gm.get_round_no(), "no_successful_round": gm.get_no_rounds_completed(), "no_of_queries_issued_for_current_page": gm.get_no_of_queries_issued_for_current_page(), "no_remaining_rounds": gm.get_remaining_rounds(), "current_score": current_score, "avatar": msg } else: Json_results = { "results": json_objects, "score": last_score, "is_game_over": 0, "url_to_find": gm.get_current_page().url, "no_round": gm.get_round_no(), "no_successful_round": gm.get_no_rounds_completed(), "no_of_queries_issued_for_current_page": gm.get_no_of_queries_issued_for_current_page(), "no_remaining_rounds": gm.get_remaining_rounds(), "current_score": current_score, "avatar": msg } data = json.dumps(Json_results) return HttpResponse(data, mimetype='application/json') else: # the game has not been created yet # redirect to play view return HttpResponseRedirect('/pagefetch/cat_picker/')