Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import openfl.Lib;
- import sys.db.Connection;
- import sys.db.Sqlite;
- /**
- * ...
- * @author Reka
- */
- class Database
- {
- static var questionID:Int = 1;
- static var myAnswerContainer:Answer;
- public static function resetQuestionID()
- {
- questionID = 1;
- }
- /**
- * Load the information of the next question, answer options and correct index.
- * Also resets the timer variables.
- */
- public static function loadQuestion()
- {
- var questionText:String = "";
- var answer1Text:String = "";
- var answer2Text:String = "";
- var answer3Text:String = "";
- var correctAnswer:Int = 1;
- var db = Sqlite.open("img/databass.db");
- var result = db.request("SELECT * FROM QnA WHERE QuestionID==" + questionID);
- for (row in result)
- {
- questionText = row.Question;
- answer1Text = row.Answer1;
- answer2Text = row.Answer2;
- if (row.Answer3 != null){
- answer3Text = row.Answer3;
- }
- correctAnswer = row.CorrectAnswer;
- }
- db.close();
- myAnswerContainer = new Answer(questionText, answer1Text, answer2Text, answer3Text, correctAnswer);
- return myAnswerContainer;
- }
- /**
- * Check the given answer against the correct answer.
- * If correct answer, add points. Otherwise subtract.
- * Whichever: update the score field and load the next question.
- *
- * @param fieldIndex The index of the answer given.
- */
- public static function checkAnswer(fieldIndex:Int)
- {
- var newQuestionID = 0;
- var db = Sqlite.open("img/databass.db");
- var result = db.request("SELECT * FROM QnA WHERE QuestionID==" + questionID);
- for (row in result)
- {
- if (fieldIndex == 1)
- newQuestionID = row.A1Next;
- else if (fieldIndex == 2)
- newQuestionID = row.A2Next;
- else if (fieldIndex == 3){
- if (row.A3Next != null){
- newQuestionID = row.A3Next;
- } else {
- return;
- }
- }
- }
- db.close();
- //scoreDisplay.text = Std.string(score);
- questionID = newQuestionID;
- }
- public static function PostScore(playerName:String, playerScore:Int)
- {
- var db = Sqlite.open("img/hallofame.db");
- db.request("INSERT INTO hallofame (Player,Score) VALUES ('" + playerName + "'," + Std.string(playerScore) + ")");
- db.close();
- }
- // Get the 10 highest scoring players from the leaderboard table in the database
- public static function GetScores()
- {
- var db = Sqlite.open("img/hallofame.db");
- var results = db.request("SELECT * FROM hallofame ORDER BY Score DESC limit 10");
- var entries:Array<Leaderboard> = new Array<Leaderboard>();
- for (row in results)
- {
- var entry:Leaderboard = new Leaderboard(row.Player, row.Score);
- entries.push( entry );
- }
- db.close();
- return entries;
- }
- }
Add Comment
Please, Sign In to add comment