def get(self): user = users.get_current_user() if user: write_header(self) self.response.out.write(""" <h1 style="color: #AA4422; margin:10px 0"><b>Color Quest</b></h1> <h2 style="font: bold 18px Arial; margin-top:5px"><i>A simple bargaining game.</i></h2> <p><b><u>The Game</u></b>: <blockquote><i>Try to reach the goal and accumulate as many points as possible along the way! Each square in the path has a different color. In order to cross a square, you must give up a chip of the same color as the square. You will probably not have all the chips you need to reach the goal, so to get them you must <u>choose among the proposals offered by the your computer opponent</u>. Be quick, however, because after the first few rounds, the trail will begin to burn. If the fire catches up to you, GAME OVER!!</i></blockquote> </p> <p><b><u>Scoring</u></b>: <ul> <li><b>Only your first game</b> will considered for the Hall of Fame - do your best!</li> <li><i>+5</i> points for each square crossed</li> <li><b><u>If you finish the game</u></b>, you also get: <ul> <li><i>+5</i> points for every non-black chip you have left</li> <li><i>+1</i> point for every black chip you have left - there will be no blacks in your trail.</li> <li><i>+50</i> points for reaching the end of the trail</li> </ul> </ul> </p> <center> <FORM METHOD="LINK" ACTION="/gameplay_start.html"> <button style="height:50px; width:400px; font:bold 24px Arial;">Play now!</button> </FORM>""") self.response.out.write(get_high_scores_html()) self.response.out.write(""" <p style="font: 10px Arial; text-align: center;">By continuing to use this application, you acknowledge that gameplay data will be stored for research purposes.</p> </center> """) write_footer(self) else: self.redirect(users.create_login_url(self.request.uri))
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)