2121 < link rel ="stylesheet " href ="https://bootswatch.com/cyborg/bootstrap.min.css " crossorigin ="anonymous ">
2222 <!-- Latest compiled and minified JavaScript -->
2323 < script src ="color.js "> </ script >
24- < script src ="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js " integrity =" sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa " crossorigin ="anonymous "> </ script >
24+ < script src ="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js " crossorigin ="anonymous "> </ script >
2525 < body style ="background-color: transparent; ">
2626 < center >
2727 < br >
@@ -47,87 +47,85 @@ <h1 class="win2" style="display: none">
4747< link rel ="stylesheet " type ="text/css " href ="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.8/sweetalert2.min.css ">
4848 < script >
4949 var bases = {
50- lemon : "url('lemons-base.jpg')" ,
50+ lemon : "url('lemons-base.jpg')" , // Links to fruits
5151 bannana : "url('bannana-base.jpg')" ,
5252 lime : "url('lime-base.jpg')" ,
5353 orange : "url('orange-base.jpg')"
5454 }
55- var out = [ ] ;
56- for ( key in bases ) {
57- if ( ! bases . hasOwnProperty ( key ) ) continue ;
58- out . push ( key )
55+ var out = [ ] ; // Initialise array of names of fruits
56+ for ( key in bases ) { // Go throught fruit objects
57+ if ( ! bases . hasOwnProperty ( key ) ) continue ; // If the current iteration is on a builtin property to a prototype, skip
58+ out . push ( key ) // Else, add the name of fruit to Array
5959 }
60- function choose ( choices ) {
60+ function choose ( choices ) { // Makes a random choice from Array, eg. [1,2,5] => 1 or 2 or 5
6161 var index = Math . floor ( Math . random ( ) * choices . length ) ;
6262 return choices [ index ] ;
6363 }
64- var mappings = {
65- KeyA : "lemon" ,
64+ var mappings = { // Mappings of keys to fruits
65+ KeyA : "lemon" , // TODO: Move to config?
6666 KeyW : "bannana" ,
6767 KeyS : "lime" ,
6868 KeyD : "orange"
6969 }
7070 var current = { } ;
71- function initPic ( pic , word ) {
72- $ ( "html" ) . css ( "background-image" , pic ) ;
71+
72+ function initPic ( pic , word ) { // Function that sets the background and word
73+ $ ( "html" ) . css ( "background-image" , pic ) ; // TODO: Random location for word?
7374 $ ( ".words" ) . text ( word ) ;
7475 $ ( ".words" ) . css ( "color" , color ( pic . replace ( "url('" , "" ) . replace ( "')" , "" ) ) )
7576 current . pic = pic ;
7677 current . word = word ;
7778 }
7879
79- setTimeout ( function ( ) {
80- // After xyz secs
81- $ ( "html" ) . css ( "background-image" , "" ) ;
82- $ ( "html" ) . css ( "background-color" , "black" ) ;
83- $ ( ".words" ) . fadeOut ( )
84- $ ( ".win2" ) . text ( "You scored " + rounds + " points." )
85- $ ( ".win2" ) . fadeIn ( )
86- $ ( ".sboard" ) . fadeIn ( )
87- swal ( {
80+ setTimeout ( function ( ) { // Below code runs after 60 seconds has passed
81+ $ ( "html" ) . css ( "background-image" , "" ) ; // Remove background image
82+ $ ( "html" ) . css ( "background-color" , "black" ) ; // Set the background black
83+ $ ( ".words" ) . remove ( ) // Remove the current word (ie banana)
84+ $ ( ".win2" ) . text ( "You scored " + rounds + " points." ) // Set winning text
85+ $ ( ".win2" ) . fadeIn ( ) // Fade in the winning text
86+ $ ( ".sboard" ) . fadeIn ( ) // Fade in the scoreboard
87+ swal ( { // Create a dialog box:
8888 title : 'What\'s your name?' ,
89- input : 'text' ,
89+ input : 'text' , // With an input box
9090 showCancelButton : true ,
9191 confirmButtonText : 'Submit' ,
9292 showLoaderOnConfirm : true ,
9393 allowOutsideClick : false
94- } ) . then ( function ( name ) {
94+ } ) . then ( function ( name ) { // Then when user clicks submit
9595 $ . ajax ( {
96- url : '/add-score/' ,
96+ url : '/add-score/' , // Make a web request to index.js with
9797 type : 'post' ,
98- data : {
99- access_token : 'XXXXXXXXXXXXXXXXXXX'
100- } ,
98+ data : { } ,
10199 headers : {
102- "XName" : name ,
100+ "XName" : name , // The data name and score, where name is what the user put in the box
103101 "XScore" : rounds
104102 } ,
105103 dataType : 'json' ,
106- success : function ( data ) {
104+ success : function ( data ) { // Then when that data is successfully sent
107105 console . info ( data ) ;
108- $ . get ( "/get-score/" , function ( d ) {
109- var arr = JSON . parse ( d ) ;
110- for ( var i = 0 ; i < arr . length ; i ++ ) {
111- $ ( ".leaderboard" ) . append ( "<li class='list-group-item'> " + arr [ i ] . name + " - " + arr [ i ] . score + " </li>" )
106+ $ . get ( "/get-score/" , function ( d ) { // Download the updated scoreboard
107+ var arr = JSON . parse ( d ) ; // Parse what we've been given
108+ for ( var i = 0 ; i < arr . length ; i ++ ) { // Go through all the top 20 scores
109+ $ ( ".leaderboard" ) . append ( "<li class='list-group-item'> " + arr [ i ] . name + " - " + arr [ i ] . score + " </li>" ) // And add them to the leaderboard
112110 }
113111 } )
114112 }
115113 } ) ;
116114 } )
117- document . onkeyup = function ( ) { } ;
115+ document . onkeyup = function ( ) { } ; // Finally, set the "onkeyup" function to empty so that users can't continue playing
118116 } , 60000 )
119117
120- var rounds = 0 ;
121- initPic ( bases [ choose ( out ) ] , choose ( out ) )
118+ var rounds = 0 ; // Start at 0 wins
119+ initPic ( bases [ choose ( out ) ] , choose ( out ) ) // Set a random background and text
122120
123- document . onkeyup = function ( e ) {
124- var mapped = mappings [ e . code ]
125- if ( mapped == undefined ) return ;
126- if ( mapped == current . word ) {
127- var key = choose ( out )
128- var key2 = choose ( out )
129- rounds ++ ;
130- initPic ( bases [ key2 ] , key )
121+ document . onkeyup = function ( e ) { // When someone lets go of a key
122+ var mapped = mappings [ e . code ] // Get the key from "mappings", which was defined earlier
123+ if ( mapped == undefined ) return ; // If the key isn't registered, forget about it
124+ if ( mapped == current . word ) { // If the key is the correct one TODO: Count incorrect keys
125+ var key = choose ( out ) // Then pick two random fruits
126+ var key2 = choose ( out ) // ^
127+ rounds ++ ; // Add 1 to the win counter
128+ initPic ( bases [ key2 ] , key ) // And set the background and text to those random fruits
131129 }
132130 else {
133131 }
0 commit comments