$three_years,]); $ROOT=''; //save root of web directory TODO somewhere behind public www $MAX_WORD_INDEX = 3088; $db = new SQLite3($ROOT.'word_v5.sqlite3');//pdo create sqlite table $db->busyTimeout(60000);//60 seconds $db->query('PRAGMA journal_mode = WAL;'); //multiple readers, https://sqlite.org/wal.html $db->query('PRAGMA busy_timeout = 60000;');//PRAGMA busy_timeout = milliseconds before retry; $db->query('PRAGMA cell_size_check = ON;'); //database corruption is detected earlier $db->query("PRAGMA encoding = 'UTF-8';"); //watch php WHERE clause will not work since ISO encoding vs UTF 8, $db->query("PRAGMA foreign_keys = ON;"); //off by default , each connection must call this $cookie_username = ""; if(!isset($_COOKIE['username'])) { //create random username, add ability to edit later $random_name = 'GUEST_'.bin2hex(random_bytes(7)); //setcookie, namecookie, valuecookie, expires unix timestamp $timestamp_expired = time()+60*60*24*365; //1 year setcookie('username',$random_name, $timestamp_expired,'/','localhost'); //change when online localhost .domain.com TODO $cookie_username=$random_name; //http cookie so since no response yet, cant call later //store username in database $insert_new_user = $db->prepare( 'INSERT INTO accounts ( username,last_session_hashid,word_number,number_tried,calc_score,last_word_id,currently_playing) VALUES (?,?,?,?,?,?,?)'); $insert_new_user->bindValue(1, $random_name, SQLITE3_TEXT); //change later $insert_new_user->bindValue(2, $random_name, SQLITE3_TEXT); //everyone gets random number beginning $insert_new_user->bindValue(3, 1, SQLITE3_INTEGER); //1st word $insert_new_user->bindValue(4, 0, SQLITE3_INTEGER); //0 tries $insert_new_user->bindValue(5, 0, SQLITE3_FLOAT); //0 score $insert_new_user->bindValue(6, mt_rand(1, $MAX_WORD_INDEX), SQLITE3_INTEGER); // assume database count $insert_new_user->bindValue(7, 1, SQLITE3_INTEGER); //1= true, 0 false $insert_new_user->execute(); //error_log("here ".$cookie_username,4); } else { //NOTE developer cookie needs to be wiped out if database wiped out!!TODO if db wipred $cookie_username =$_COOKIE['username']; //retrieve username from database //error_log("ther ".$cookie_username,4); } $mutable_template_html = str_replace("%username%", htmlspecialchars($cookie_username) ,$mutable_template_html); //if blank input if ($input_text_filtered == "") { $mutable_template_html = str_replace("%terminal_output%", "Error a-z 0-9 only", $mutable_template_html); } else { //CHECK IF WORD IN DATABASE $check_if_word_in_db = $db->prepare("SELECT * FROM words WHERE word_text=?;"); $check_if_word_in_db->bindValue(1, $input_text_filtered, SQLITE3_TEXT); $check_if_word_in_db_result= $check_if_word_in_db->execute(); $word_found = false; if( is_a($check_if_word_in_db_result, 'SQLite3Result' ) ) { $row_word = $check_if_word_in_db_result->fetchArray(SQLITE3_ASSOC); if ( is_array($row_word)) { //error_log('word found>'.$row_word['word_text'], 4); $word_found = true; } else { //error_log( $input_text_filtered.'word not found>'.$row_word, 4); } } else { //error_log('word not found error db'.print_r($check_if_word_in_db_result->fetchArray()['word_text']), 4); $mutable_template_html = str_replace("%terminal_output%", "Word not found", $mutable_template_html); } //get word for this user //cant use * since ambigous, MUST SAY blog.doc_id not doc_id //LIKE A JSON document with three branches to the right $select_user_info = $db->prepare("SELECT accounts.user_id,username, profile_image_base64 , profile_image_mimetype , last_session_hashid , word_number, number_tried , calc_score , last_word_id , currently_playing, word_text, attempt_word FROM accounts LEFT JOIN words on accounts.last_word_id = words.word_id LEFT JOIN attempts on accounts.user_id = attempts.user_id AND accounts.last_word_id = attempts.word_id WHERE accounts.username = ? ORDER BY attempt_id ;"); $select_user_info->bindValue(1, $cookie_username , SQLITE3_TEXT); $select_user_inforesult = $select_user_info->execute(); if( is_a($select_user_inforesult, 'SQLite3Result' ) && $word_found==true ) { //error_log('is_a($select_user_inforesult, \'SQLite3Result\' ) && $word_found==true ',4); $arrayreturn = array(); while($arrayrow = $select_user_inforesult->fetchArray(SQLITE3_ASSOC) ){ //error_log(print_r($arrayrow),4); $arrayreturn[]=$arrayrow; } //error_log('count'.count($arrayreturn),4); //error_log(print_r($arrayreturn), 4); //TODO delete error logs production //got a word //split into characters //$unicode_chars = mb_str_split($input_text_filtered, 1, "UTF-8"); //error_log(print_r($arrayreturn[0]),4); $input_text_filtered_padded = str_pad($input_text_filtered, strlen($arrayreturn[0]['word_text'])); //word spurred, 7 characters, so pad string 7 characters //error_log(''.$arrayreturn[0]['word_text'].''.$input_text_filtered_padded.'<>', 4); $input_chars = str_split($input_text_filtered_padded, 1); $string_letters = ""; $correct_word = $arrayreturn[0]['word_text']; $correct_word_chars = str_split($arrayreturn[0]['word_text'],1); //user input cat, match, string //word spurred, 7 characters, so pad string 7 characters //need to pad str str_pad($input, 10) //correct word == comma //guesses = omar_ , crane, //highlite correct position and letter green //count number of letters //comma vs apple vs mocko //c1,o1,m1,m2,a1 a1,p1,p2,l1,e1, m1 o1 c1 k1 o2 //mocko o highlite first instance but not second //can now check if a1 there , but not in position put yellow //for each letter in usersub //if position and char same in correctword //green //if not check what character number instance is usersub and correct word //if both equal put yellow //if not make black $attempt_array = array(); //TODO watch if input len > correr word for($x = 0; $x < strlen($correct_word); $x++) { $the_char = $input_chars[$x]; if ($input_chars[$x] == $correct_word_chars[$x]) { $attempt_array[] = array($the_char, 'green'); } else { $input_char_num_instance = 0; $correct_word_char_num_instance = 0; for($y = 0; $y <= $x; $y++) { if($the_char == $input_chars[$y]) { $input_char_num_instance+=1; } } $found_char_same_instance = false; for($z = 0; $z < strlen($correct_word); $z++) { if($the_char == $correct_word_chars[$z]) { $correct_word_char_num_instance+=1; } if($input_char_num_instance != 0 && $input_char_num_instance == $correct_word_char_num_instance ) { $found_char_same_instance = true; } } if($found_char_same_instance) { $attempt_array[] = array($the_char, 'yellow'); } else { $attempt_array[] = array($the_char, 'black'); } } } $character_state = ""; foreach($attempt_array as $an_attempt) { if($an_attempt[1] == 'green'){ $character_state = $character_state."G"; }elseif($an_attempt[1] == 'yellow'){ $character_state = $character_state."P"; }else{ $character_state = $character_state."I"; } } //update db $insert_attempt = $db->prepare( 'INSERT INTO attempts ( user_id, word_id , attempt_word, character_state, attempt_sequence) VALUES (?,?,?,?,?)'); $insert_attempt->bindValue(1, $arrayreturn[0]['user_id'], SQLITE3_INTEGER); //change later $insert_attempt->bindValue(2, $arrayreturn[0]['last_word_id'], SQLITE3_INTEGER); //change later $insert_attempt->bindValue(3, $input_text_filtered_padded, SQLITE3_TEXT); //coded letter good, bad, possible $insert_attempt->bindValue(4, $character_state, SQLITE3_TEXT); //coded letter good, bad, possible $insert_attempt->bindValue(5, 999, SQLITE3_INTEGER); //change later $insert_attempt->execute(); //view attempts mvc even one just inserted | attempts.get_filter_user_id_attempt_id() //TODO orm idea, using dynamic string, dynamic arguments "get_all_filter_user_id_attempt_id" //output all | input user_id attempt_id $select_attempts = $db->prepare("SELECT * from attempts where word_id = ? and user_id = ? ORDER BY attempt_id;"); $select_attempts->bindValue(1, $arrayreturn[0]['last_word_id'] , SQLITE3_TEXT); $select_attempts->bindValue(2, $arrayreturn[0]['user_id'] , SQLITE3_TEXT); $select_attempts = $select_attempts->execute(); $string_letters = $string_letters."
"; //$track_alphabet_array=array("a" => 0, "b" => 1); $track_alphabet_array=str_split("abcdefghijklmnopqrstuvwxyz",1); $track_alphabet=array(); foreach($track_alphabet_array as $alpha_value){ $track_alphabet[$alpha_value]='?'; //question mark null state out of I,G,P } $share_text_progress=""; if( is_a($select_attempts, 'SQLite3Result' ) ) { $array_attempts = array(); while($arrayrow = $select_attempts->fetchArray(SQLITE3_ASSOC) ){ $array_attempts[]=$arrayrow; } //GPI class = G = correct_letter, I = incorrect_letter, P=possible_letter foreach($array_attempts as $attempt_i) { $attempt_chars = str_split($attempt_i['attempt_word'], 1); $attempt_chars_state = str_split($attempt_i['character_state'], 1); $string_letters = $string_letters."
"; //new line \n $share_text_progress=$share_text_progress.$attempt_i['attempt_word']."%0A"; foreach($attempt_chars as $attempt_char_key => $attempt_char_value) { $string_letters = $string_letters."".$attempt_char_value.""; if ($attempt_chars_state[$attempt_char_key] == 'G') { if( $track_alphabet[$attempt_char_value] !== 'G') { $track_alphabet[$attempt_char_value]='G'; } } elseif ($attempt_chars_state[$attempt_char_key] == 'P') { if( $track_alphabet[$attempt_char_value] !== 'G') { $track_alphabet[$attempt_char_value]='P'; } } elseif ($attempt_chars_state[$attempt_char_key] == 'I') { if( $track_alphabet[$attempt_char_value] !== 'G' || $track_alphabet[$attempt_char_value] !== 'P') { $track_alphabet[$attempt_char_value]='I'; } } } $string_letters = $string_letters."
"; } } $string_letters = $string_letters."
"; //draw BANNER $string_letters = $string_letters."
"; $string_letters = $string_letters."
"; $break_counter=0; foreach($track_alphabet as $key_alpha => $alpha_value){ $break_counter++; $string_letters = $string_letters."".$key_alpha.""; //if($break_counter%6 == 0) { //$string_letters = $string_letters."
"; //} } $string_letters = $string_letters."
"; //if correct if($input_text_filtered == $correct_word) { $string_letters = $string_letters."
Congrats! You won!
"; //update new word user.update_word $update_statement = $db->prepare('UPDATE accounts SET last_word_id = ? WHERE user_id = ?;'); $update_statement->bindValue(1, mt_rand(1, $MAX_WORD_INDEX), SQLITE3_INTEGER); $update_statement->bindValue(2, $arrayreturn[0]['user_id'], SQLITE3_INTEGER); $update_result = $update_statement->execute(); } //retrieve last session information //display profile pic | score // display last attempts , success or failure //prevent user from enter more data if successful $mutable_template_html = str_replace("%terminal_output%", $string_letters, $mutable_template_html); $mutable_template_html = str_replace("%share_text_progress%", $share_text_progress, $mutable_template_html); } else { $mutable_template_html = str_replace("%terminal_output%", "User Info not found", $mutable_template_html); } } //filter %% as input $mutable_template_html = str_replace("%title%", "6ixword - Processed", $mutable_template_html); $db->close(); echo $mutable_template_html; ?>