Keksicle

Databass

Aug 18th, 2017
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import openfl.Lib;
  4. import sys.db.Connection;
  5. import sys.db.Sqlite;
  6.  
  7. /**
  8.  * ...
  9.  * @author Reka
  10.  */
  11. class Database
  12. {
  13.     static var questionID:Int = 1;
  14.     static var myAnswerContainer:Answer;
  15.    
  16.     public static function resetQuestionID()
  17.     {
  18.         questionID = 1;
  19.     }
  20.    
  21.     /**
  22.      * Load the information of the next question, answer options and correct index.
  23.      * Also resets the timer variables.
  24.      */
  25.     public static function loadQuestion()
  26.     {
  27.         var questionText:String = "";
  28.         var answer1Text:String = "";
  29.         var answer2Text:String = "";
  30.         var answer3Text:String = "";
  31.         var correctAnswer:Int = 1;
  32.        
  33.         var db = Sqlite.open("img/databass.db");
  34.         var result = db.request("SELECT * FROM QnA WHERE QuestionID==" + questionID);
  35.         for (row in result)
  36.         {
  37.             questionText = row.Question;
  38.             answer1Text = row.Answer1;
  39.             answer2Text = row.Answer2;
  40.             if (row.Answer3 != null){
  41.                 answer3Text = row.Answer3;
  42.             }
  43.             correctAnswer = row.CorrectAnswer;
  44.         }
  45.         db.close();
  46.        
  47.         myAnswerContainer = new Answer(questionText, answer1Text, answer2Text, answer3Text, correctAnswer);    
  48.         return myAnswerContainer;
  49.     }
  50.    
  51.     /**
  52.      * Check the given answer against the correct answer.
  53.      * If correct answer, add points. Otherwise subtract.
  54.      * Whichever: update the score field and load the next question.
  55.      *
  56.      * @param   fieldIndex The index of the answer given.
  57.      */
  58.     public static function checkAnswer(fieldIndex:Int)
  59.     {
  60.         var newQuestionID = 0;
  61.         var db = Sqlite.open("img/databass.db");
  62.         var result = db.request("SELECT * FROM QnA WHERE QuestionID==" + questionID);
  63.         for (row in result)
  64.         {
  65.             if (fieldIndex == 1)
  66.                 newQuestionID = row.A1Next;
  67.             else if (fieldIndex == 2)
  68.                 newQuestionID = row.A2Next;
  69.             else if (fieldIndex == 3){
  70.                 if (row.A3Next != null){
  71.                     newQuestionID = row.A3Next;
  72.                 } else {
  73.                     return;
  74.                 }
  75.                
  76.             }
  77.         }
  78.         db.close();
  79.        
  80.         //scoreDisplay.text = Std.string(score);
  81.         questionID = newQuestionID;
  82.     }
  83.    
  84.     public static function PostScore(playerName:String, playerScore:Int)
  85.     {
  86.         var db = Sqlite.open("img/hallofame.db");
  87.         db.request("INSERT INTO hallofame (Player,Score) VALUES ('" + playerName + "'," + Std.string(playerScore) + ")");
  88.         db.close();
  89.     }
  90.    
  91.     // Get the 10 highest scoring players from the leaderboard table in the database
  92.     public static function GetScores()
  93.     {
  94.         var db = Sqlite.open("img/hallofame.db");
  95.         var results = db.request("SELECT * FROM hallofame ORDER BY Score DESC limit 10");
  96.         var entries:Array<Leaderboard> = new Array<Leaderboard>();
  97.         for (row in results)
  98.         {  
  99.             var entry:Leaderboard = new Leaderboard(row.Player, row.Score);
  100.             entries.push( entry );
  101.         }
  102.         db.close();
  103.         return entries;
  104.     }
  105. }
Add Comment
Please, Sign In to add comment