def gameTurn(self, user=None, diff=None, disp=None , minYr=1926, maxYr=2010): l.sLogger.debug("game:gameTurn<s> - create a randomly generated turn for the game") l.sLogger.debug("variables passed: ") l.sLogger.debug("user="******"diff="+str(diff)) l.sLogger.debug("disp="+str(disp)) l.sLogger.debug("minYr="+str(minYr)) l.sLogger.debug("maxYr="+str(maxYr)) title = None artist = None album = None self.conn = sqlite3.connect("subset_track_metadata.db") self.cursor = self.conn.cursor() preview = None tries = 0 cherrypy.session['diff'] = diff #guarentee we get a preveiw while preview == None and tries <100: l.sLogger.debug("generating a random song") tries += 1 self.songID, self.compGuess, self.correct = self.g.getRandSong() #make sure the song is in the year range specified if self.correct >= int(minYr) and self.correct <= int(maxYr): cherrypy.session['songID'] = self.songID cherrypy.session['compGuess'] = self.compGuess cherrypy.session['correct'] = self.correct cherrypy.session['disp'] = disp cherrypy.session['minYr'] = minYr cherrypy.session['maxYr'] = maxYr cherrypy.session['session'] = "" preview = song_preview.getPreview(self.songID) self.cursor.execute("SELECT title, artist_name, release FROM songs WHERE song_id == '{}'".format(self.songID)) data = self.cursor.fetchone() if diff != None: if diff == "H" or diff == "M" or diff == "E": try: title = data[0] except: l.sLogger.debug("couldn't get track title") if diff == "M" or diff == "E": try: artist = data[1] except: l.sLogger.debug("couldn't get artist name") if diff == "E": try: album = data[2] except: l.sLogger.debug("couldn't get album name") #custom game if diff == "C": if 't' in disp: try: title = data[0] except: l.sLogger.debug("couldn't get track title") if 'A' in disp: try: artist = data[1] except: l.sLogger.debug("couldn't get artist name") if 'a' in disp: try: album = data[2] except: l.sLogger.debug("couldn't get album name") if tries == 100: return "<font size='7'>Sorry, could not find a song of that type. Please try again with different settings.</font>" l.sLogger.debug("preview url="+preview) #this is a html file that is dynamically added to, depending on parameters passed in the url html = """ <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="/css/layout.css" type="text/css" /> <style> .center{ margin: auto; width: 60%; padding: 10px; } input[type=submit] { width: 10em; height: 2em; } </style> <script type="text/javascript"> window.history.forward(); function noBack(){window.history.forward();} </script> </head> <body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload=""> <br><br><br><br><br>""" #Title, artist, and album parameters. If nothing is provided in url, nothing is shown if (title != None): html +=""" <div class="center" id="header"> <font size="10">Title: """+title+"""</font> </div> <br>""" if (artist != None): html +=""" <div class="center" id="header"> <font size="10">Artist: """+artist+"""</font> </div> <br>""" if (album != None): html +=""" <div class="center" id="header"> <font size="10">Album: """+album+"""</font> </div>""" #NOTE THE INCLUSION OF THE URL HERE!!! html +=""" <div class="center"> <br><br><br><br> <font size="5">Listen:</font> <br> <audio controls> <source src="""+preview+""" type="audio/mpeg" align="center"> Your browser doesn't support playing audio. C'mon...update the browser. </audio> </div> <div class="center"> <br><br><br><br> <font size='5'>I think it came out in: </font> <form method="get", action="checkAnswer?"> <select id="guess" name="guess"> <option value="0">----</option>""" #if the maxYr or minYr are provided, they come as strings. Convert to ints just in case maxYr = int(maxYr) minYr = int(minYr) #generate the year spinner for i in range(maxYr, minYr-1, -1): html += '<option value ="' + str(i) + '">' + str(i) + '</option>' #finish up the remaining html code html +=""" </select> <br><br><br> <input type="submit" value="Guess"> <input type="hidden" name="user" value='"""+str(user)+"""'/> <input type="hidden" name="session" value='"""+self.randomword(20)+"""'/> </form> </div> </body> </html> """ # #prevent cheating self.session = None l.sLogger.info("Showing gameTurn for songid:"+str(self.songID)) l.sLogger.debug("game:gameTurn<e>") #return the html to be displayed return html
def sample(self, gen=True, user=None, url=DEATH_OR_GLORY, diff=None, title=None, artist=None, album=None, minYr=1926, maxYr=2010): l.sLogger.debug("game:sample<s> - generates a webpage where everything can be specified. Used for samples") l.sLogger.debug("parameters:") l.sLogger.debug("gen="+str(gen)) l.sLogger.debug("user="******"url="+str(url)) l.sLogger.debug("diff="+str(diff)) l.sLogger.debug("title="+str(title)) l.sLogger.debug("artist="+str(artist)) l.sLogger.debug("album="+str(album)) l.sLogger.debug("minYr="+str(minYr)) l.sLogger.debug("maxYr="+str(maxYr)) #disables the guess button #generate song randomly if gen == "F": generate = False preview = url else: generate = True self.conn = sqlite3.connect("subset_track_metadata.db") self.cursor = self.conn.cursor() if generate: preview = None cherrypy.session['diff'] = diff #guarentee we get a preveiw while preview == None: self.songID, self.compGuess, self.correct = self.g.getRandSong() cherrypy.session['songID'] = self.songID cherrypy.session['compGuess'] = self.compGuess cherrypy.session['correct'] = self.correct preview = song_preview.getPreview(self.songID) self.cursor.execute("SELECT title, artist_name, release FROM songs WHERE song_id == '{}'".format(self.songID)) data = self.cursor.fetchone() if diff != None: if diff == "H" or diff == "M" or diff == "E": try: title = data[0] except: l.sLogger.debug("couldn't get track title") if diff == "M" or diff == "E": try: artist = data[1] except: l.sLogger.debug("couldn't get artist name") if diff == "E": try: album = data[2] except: l.sLogger.debug("couldn't get album name") #this is a html file that is dynamically added to, depending on parameters passed in the url html = """ <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="/css/layout.css" type="text/css" /> <style> .center{ margin: auto; width: 60%; padding: 10px; } </style> <script type="text/javascript"> window.history.forward(); function noBack(){window.history.forward();} </script> </head> <body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload=""> <br><br><br><br><br>""" #Title, artist, and album parameters. If nothing is provided in url, nothing is shown if (title != None): html +=""" <div class="center" id="header"> <font size="10">Title: """+title+"""</font> </div> <br>""" if (artist != None): html +=""" <div class="center" id="header"> <font size="10">Artist: """+artist+"""</font> </div> <br>""" if (album != None): html +=""" <div class="center" id="header"> <font size="10">Album: """+album+"""</font> </div>""" #NOTE THE INCLUSION OF THE URL HERE!!! html +=""" <div class="center"> <br><br><br><br> <font size="5">Listen:</font> <br> <audio controls> <source src="""+preview+""" type="audio/mpeg" align="center"> Your browser doesn't support playing audio. C'mon...update the browser. </audio> </div> <div class="center"> <br><br><br><br> <font size='5'>I think it came out in: </font> <select id="guess" name="guess"> <option value="0">----</option>""" #if the maxYr or minYr are provided, they come as strings. Convert to ints just in case maxYr = int(maxYr) minYr = int(minYr) #generate the year spinner for i in range(maxYr, minYr-1, -1): html += '<option value ="' + str(i) + '">' + str(i) + '</option>' #finish up the remaining html code html +=""" </select> <br><br><br> <input type="submit" value="Guess"> <input type="hidden" name="user" value='"""+str(user)+"""'/> </div> </body> </html> """ l.sLogger.info("Generating webpage for song: "+ str(title)) l.sLogger.debug("game:sample<e>") #return the html to be displayed return html