def get(self): user = users.get_current_user() if not user: self.redirect('/') return # get the most recent game the user has been playing q = db.GqlQuery(""" SELECT * FROM Gamestate WHERE player = :1 ORDER BY date DESC LIMIT 1""", user) games = q.fetch(1) # redirect to the main page if we didn't find any games for this user if len(games)==0 or games[0].game_over: game = Gamestate() game.player = user trail_length = 30 num_chips = 20 # generate all the random numbers at once using choice() for efficiency choices = [0, 1, 2, 3, 4, 5] nums = [choice(choices) for _ in range(trail_length + num_chips)] # make the trail from the first set of numbers trail = nums[:trail_length] # the remaining numbers identify chips num_colors = 7 chips = num_colors * [0] for i in nums[trail_length:]: chips[i] += 1 game.trail = trail game.chips = chips game.iteration = 0 game.location = -1 game.score = 0 game.game_over = False game.trade1 = num_colors * [0] game.trade2 = num_colors * [0] game.round1_choices = 0 game.round1_rational = 0 game.round2_choices = 0 game.round2_rational = 0 game.chips_to_finish = False game.finalized = False # 50% of games will be the standard game, the other 50% will be evenly # divided between 50%, 75%, and 90% if ALLOW_FRAUD: game.trade_honesty = choice([0.5, 0.75, 0.9, 1.0, 1.0, 1.0]) else: game.trade_honesty = 1.0 game.trade_honoured = True put_safe(game) self.redirect('/gameplay')
def add_high_score(game): """Adds this game to the list of high scoring games and bumps the lowest game from the high score list.""" # add the new high score newhs = HighScore() newhs.name = str(game.player) newhs.score = game.score put_safe(newhs) # remove the lowest score q = db.GqlQuery(GQL_HIGH_SCORES) hscores = q.fetch(1, NUM_HIGH_SCORES) if len(hscores) > 0: db.delete(hscores)
def post(self): user = users.get_current_user() if user: q = db.GqlQuery(""" SELECT * FROM Gamestate WHERE player = :1 ORDER BY date DESC LIMIT 1 """, user) games = q.fetch(1) # redirect to the main page if we didn't find any games for this user if (len(games) == 0): self.redirect('/') return game = games[0] chips = game.chips # update the number of choices made is_round1 = (game.iteration <= HALFWAY_POINT) if is_round1: game.round1_choices = game.round1_choices + 1 else: game.round2_choices = game.round2_choices + 1 # get which trade was taken choiceh = self.request.get("choiceh", '') if choiceh == '': # only works if javascript is off (which is when choiceh fails) took_trade1 = (self.request.get("Choice_1", '') != '') else: took_trade1 = (choiceh == 'Accept Trade 1') # get the trade objects if took_trade1: trade_taken = game.trade1 trade_not_taken = game.trade2 else: trade_taken = game.trade2 trade_not_taken = game.trade1 # determine whether it was rational rational = (trade_taken[VALUE_INDEX] >= trade_not_taken[VALUE_INDEX]) if rational: if is_round1: game.round1_rational = game.round1_rational + 1 else: game.round2_rational = game.round2_rational + 1 # apply the trade? values_differ = (trade_taken[VALUE_INDEX] != trade_not_taken[VALUE_INDEX]) game.trade_honoured = (is_round1 or not values_differ or random.random() <= game.trade_honesty) if game.trade_honoured: for i in range(len(chips)): chips[i] = chips[i] + trade_taken[i] game.iteration = game.iteration + 1 #If the player is not in the last spot, update their position if (game.location < (len(game.trail) - 1 )): next_color = game.trail[game.location + 1] if (chips[next_color] > 0): chips[next_color] = chips[next_color] - 1 game.location = game.location + 1 game.chips = chips #Generate a list of chip-requirements for the trail trail_reqs = [0, 0, 0, 0, 0, 0] for i in range(game.location+1, len(game.trail)): trail_reqs[game.trail[i]] = trail_reqs[game.trail[i]] + 1 #Subtract current chips from trail requirements #Assume player has all the chips they need - if a positive number appears in need, set flag to false needs = [trail_reqs - chips for trail_reqs, chips in zip(trail_reqs, chips)] game.chips_to_finish = True for x in needs: if (x > 0): game.chips_to_finish = False redir_to = '/gameplay' if (game.location == len(game.trail)-1): game.game_over = True redir_to = '/endgame.html' if (game.location + 1 == game.iteration - 7): game.game_over = True redir_to = '/endgame.html' # this put() occasionally times out, so retry it a few times on failure put_safe(game) self.redirect(redir_to)
def get(self): user = users.get_current_user() if user: q = db.GqlQuery(""" SELECT * FROM Gamestate WHERE player = :1 ORDER BY date DESC LIMIT 1 """, user) games = q.fetch(1) # redirect to the main page if we didn't find any games for this user if (len(games) == 0): self.redirect('/') return game = games[0] # redirect back to the game if it isn't over yet! if not game.game_over: self.redirect('/gameplay') return # redirect back to the main page if this game has already been finalized if game.finalized: self.redirect('/') return finish = False if (game.location + 1 >= len(game.trail)): finish = True excess_chips = 0 for i in range(0,6): excess_chips = excess_chips + game.chips[i] write_header(self, pb=20) score = 5*game.location if finish: score = score + 50 score = score + excess_chips + game.chips[6] self.response.out.write(""" <center> <h1> <font color="#AA4422"><b>You Made it!!</b></font></h1>""") else: self.response.out.write(""" <center> <h1> <font color="#AA4422"><b>GAME OVER</b></font></h1>""") game.finalized = True game.score = score put_safe(game) self.response.out.write(""" <h2> <font color="#445566"> Your Final Score is: %s </font></h2></center>""" % game.score) # high score? self.response.out.write('<p>') self.response.out.write(check_completed_game_for_high_score(game)) self.response.out.write('</p>') write_footer(self)
def get(self): write_header(self, 0, 0) user = users.get_current_user() if user: # get the most recent game the user has been playing q = db.GqlQuery(""" SELECT * FROM Gamestate WHERE player = :1 ORDER BY date DESC LIMIT 1""", user) games = q.fetch(1) # redirect to the main page if we didn't find any games for this user if (len(games) == 0): self.redirect('/') return game = games[0] # choose a unique, deterministc random seed for each iteration so the # randomness isn't affected by simply reloading the page! random.seed(hash(str(game.key())) + game.iteration) if (game.game_over): self.redirect('/endgame.html') else: chips = game.chips trade1 = [0,0,0,0,0,0,0,0] trade2 = [0,0,0,0,0,0,0,0] #Generate Trades if they don't have the chips they need to finish if (not game.chips_to_finish): trail_temp = [] #Only send remaining trail for i in range(game.location+1, len(game.trail)): c = game.trail[i] trail_temp.append(COLORS[c]) order_var = random.random() #Testing base rationality - See if they can identify fair vs. unfair, good vs. bad trades if (game.iteration <= 15): if (order_var < 0.5): #Good trade on Left trade1 = createProposal(chips, trail_temp, False, 1) #Good trade2 = createProposal(chips, trail_temp, False, -1) #Unfair/Bad else: #Good trade on Right trade1 = createProposal(chips, trail_temp, False, -1)#Unfair trade2 = createProposal(chips, trail_temp, False, 1) #Fair else: if (order_var < 0.5): #Good trade on Left trade1 = createProposal(chips, trail_temp, False, 1) #Good trade! trade2 = createProposal(chips, trail_temp, True, -1) #zero phenomena else: #Good trade on Right trade1 = createProposal(chips, trail_temp, True, -1) #zero phenomena trade2 = createProposal(chips, trail_temp, False, 1) #Good trade! #Save trade in data structure game.trade1 = trade1 game.trade2 = trade2 put_safe(game) # Print out the table displaying the game state data write = self.response.out.write write('<div id="game">') write('<div id="pathname"><h2>The Path</h2></div>') # Print out the trail, colour in the trail as appropriate write('<div id="trail"><table><tr>') for i in (range(31)): if i == game.location + 1: idf = 'id="player"' elif i <= game.iteration - 7: idf = 'id="fire"' else: idf = '' color_num = game.trail[i-1] color = '#FFFFFF' if color_num == -1 else HTML_COLORS[color_num] self.response.out.write("""<td %s bgcolor="%s"> </td>""" % (idf, color)) write('</tr></table></div>') #Output the chip counts of each colour write('<div id="chips"><table width="100%"><tr>') write('<td style="background-color:#FFDDAA; font: bold 24px Arial; width:200px">Your Chips</td>'); for i in range(NUM_COLORS): write('<td style="background-color:%s; color:%s;">%u</td>' % (HTML_COLORS[i], HTML_COLORS_FG[i], chips[i])) write('</tr></table></div>') # Let the user know if the last trade failed (fraud!) if not game.trade_honoured: write('<div id="scam">You were scammed! The trade you tried to accept was fraudulent -- better luck next time.</div>') #Print out the two choices write('<div id="chips">') write('<div style="padding-bottom:10px;"><h2>Choose between these two trades:</h2></div>') write('<table width="100%">') trade_row_start = '''<tr> <td style="width:200px"> <FORM METHOD="POST" ACTION="/gameplay" style=" padding:0; margin:0"> <input type="hidden" id="choiceh" name="choiceh" value="nil"/> <INPUT type="submit" id="Choice_%u" name="Choice_%u" value="Accept Trade %u" style="height:%upx" onclick="document.getElementById('choiceh').value = this.value; document.getElementById('Choice_1').disabled=true; document.getElementById('Choice_2').disabled=true;">%s </FORM> </td>''' warning = ''' <div id="warning"> WARNING: There is a <u>%.0f%%</u> chance this offer is a <u>fraud</u> and won\'t have any impact! </div>''' for t in range(1, 3): if t == 2: write('<tr style="height:20px"><td colspan="%u" style="background-color:#444444;"></td></tr>' % (NUM_COLORS+1)) trade = trade1 if t == 1 else trade2 # show a warning about a dishonest trader in the second half for the rational choice rh = 50 extra = '' if game.iteration > HALFWAY_POINT and game.trade_honesty < 1.0: other_trade = trade1 if t != 1 else trade2 if trade[VALUE_INDEX] > other_trade[VALUE_INDEX]: rh = 50 extra = warning % (100.0 - 100.0*game.trade_honesty) write(trade_row_start % (t, t, t, rh, extra)) for i in range(NUM_COLORS): if HIDE_UNNEEDED_TRADE_CELLS and trade[i]==0: write('<td style="background-color:#CCCCCC"></td>') else: write('<td style="background-color:%s; color:%s;">%u</td>' % (HTML_COLORS[i], HTML_COLORS_FG[i], trade[i])) write('</tr>') write('</table></div>') if DEBUG_SHOW_VALUES: write('Trade 1 = %u<br/>Trade 2 = %u' % (trade1[VALUE_INDEX], trade2[VALUE_INDEX])) # all done write_footer(self) #Save all changes in the datastore put_safe(game)