diff --git a/SQL/Database Model.xlsx b/SQL/Database Model.xlsx new file mode 100644 index 0000000..fc35f73 Binary files /dev/null and b/SQL/Database Model.xlsx differ diff --git a/SQL/adventure.sql b/SQL/adventure.sql new file mode 100644 index 0000000..17181e2 --- /dev/null +++ b/SQL/adventure.sql @@ -0,0 +1,160 @@ +-- branch shallin +DROP DATABASE IF EXISTS adventure; +CREATE DATABASE adventure; +USE adventure; + +-- +-- Table structure for table `users` +-- + +-- DROP TABLE IF EXISTS `users`; +CREATE TABLE `users`( + `user_id` int(11) auto_increment, + `user_name` varchar(200) default NULL, + PRIMARY KEY (`user_id`) +); -- ENGINE=MyISAM DEFAULT CHARSET=utf8; + +-- +-- Dumping data for table `users` +-- + +-- /*!40000 ALTER TABLE `users` DISABLE KEYS */; +INSERT INTO `users` VALUES +(0,'Example User'), +(0,'Shallin Ris'); + +-- LOCK TABLES `games` WRITE; +-- /*!40000 ALTER TABLE `games` DISABLE KEYS */; + +-- /*!40000 ALTER TABLE `games` ENABLE KEYS */; +-- UNLOCK TABLES; +CREATE TABLE `images` ( + `image_name` varchar(500) default NULL, + `story_id` int(11), + `adventure_id` int(11), + PRIMARY KEY (`story_id`) + ); + + +INSERT INTO `images` VALUES +('inbed.jpg', 1, 1), +('clock.jpg', 2, 1), +('stairs.jpg', 3, 1), +('coffee_and_computer.jpeg', 4, 1), +('phone_interview.jpg', 5, 2), +('interview.jpg', 6, 2), +('salary.jpg', 7, 2), +('work.jpeg', 8, 2), +('victory.jpg', 0, 0); + +-- +-- Table structure for table `story` +-- + +-- DROP TABLE IF EXISTS `story`; +CREATE TABLE `story` ( + `id` int(11), + `adventure_id` int(11) default NULL, + `story_id` int(11) default NULL, + `question_type` int(11) default NULL, + `content` varchar(1000) default NULL, + `life_unit` int(11) default NULL, + `wealth_unit` int(11) default NULL, + PRIMARY KEY (`id`), + FOREIGN KEY (story_id) REFERENCES images(story_id) + ); + +-- KEY `idx_actor_id` (`actor_id`), +-- KEY `idx_movie_id` (`movie_id`) + -- ENGINE=MyISAM DEFAULT CHARSET=utf8; + +-- +-- Dumping data for table `story` +-- + +-- LOCK TABLES `story` WRITE; +-- /*!40000 ALTER TABLE `story` DISABLE KEYS */; +INSERT INTO `story` VALUES +(1, 1, 1, 0, 'You wake up late. You:', 0, 0), +(2, 1, 1, 1, 'get ready in a hurry, put on your running shoes and sprint to campus', 30, 2), +(3, 1, 1, 2, 'text Shy that youre running late', 10, 0), +(4, 1, 1, 3, 'dont text anyone and hope that no one notices when you walk in late', 90, 4), +(5, 1, 1, 4, 'throw caution to the wind and decide to skip the entire day', 100, 9), +(6, 1, 2, 0, 'You get to school in the nick of time. You:', 0, 0), +(7, 1, 2, 1, 'walk into class and quietly sit down', 10, 0), +(8, 1, 2, 2, 'hang out in the kitchen talking to your friends and making coffee, despite the fact that Nathalie has told you lecture is starting', 50, 4), +(9, 1, 2, 3, 'stop by cofix because its ok to be a few minutes late', 70, 5), +(10, 1, 2, 4, 'at the sight of 16 Herzl you decide you dont feel like coding today so you turn around and go home', 100, 10), +(11, 1, 3, 0, 'You encounter Shy as you run up the stairs. He asks why you didnt text anyone that you were going to be late.', 0, 0), +(12, 1, 3, 1, 'You apologize and say it wont happen again', 30, 1), +(13, 1, 3, 2, 'You start to cry and then run away', 70, 6), +(14, 1, 3, 3, 'You voluntarily offer to pay the $200 late fee', 50, 4), +(15, 1, 3, 4, 'You tell him that you lost your phone', 40, 6), +(16, 1, 4, 0, 'Youre sitting in class, sipping on coffee, and working on your computer. Someone gets up and knocks your coffee over onto your computer:', 0, 0), +(17, 1, 4, 1, 'You grab another cup of coffee and spill it on their computer', 70, 10), +(18, 1, 4, 2, 'You throw the coffee cup and your ruined computer in the trash', 60, 10), +(19, 1, 4, 3, 'You tell Shy its the other persons fault and that they should pay for the computer damage', 60, 5), +(20, 1, 4, 4, 'You realize that maybe you shouldnt have been drinking coffee in the classroom', 10, 4), +(21, 2, 5, 0, 'You finally got your first interview and you get a question you have no idea how to do', 0, 0), +(22, 2, 5, 1, 'You totally break down and tell them you cant do it', 100, 10), +(23, 2, 5, 2, 'You struggle a lot but ask them questions and sort of get the problem right', 50, 4), +(24, 2, 5, 3, 'You struggle a lot but dont talk and dont ask them anything', 80, 6), +(25, 2, 5, 4, 'You tell them why the question they asked is stupid and then you answer your own question', 100, 10), +(26, 2, 6, 0, 'You make it to the second round of interviews', 0, 0), +(27, 2, 6, 1, 'Youre so excited that you spend all night partying and getting wasted', 100, 10), +(28, 2, 6, 2, 'You get to the interview late but then do a pretty good job', 50, 5), +(29, 2, 6, 3, 'You get to the interview early and are not sure how you did, but you did everything ITC trained you to do', 30, 2), +(30, 2, 6, 4, 'You freak out mid interview', 100, 8), +(31, 2, 7, 0, 'You get an offer and need to negotiate salary', 0, 0), +(32, 2, 7, 1, 'You give them a number way below your worth', 50, 10), +(33, 2, 7, 2, 'You tell them to pay you whatever they think is best', 30, 5), +(34, 2, 7, 3, 'You engage in a back and forth with them about a salary you feel is fair', 10, 1), +(35, 2, 7, 4, 'You hire someone to negotiate for you', 100, 10), +(36, 2, 8, 0, 'You got the job! You have your first day of work', 0, 0), +(37, 2, 8, 1, 'You pick an appropriate outfit and get there early', 0, 1), +(38, 2, 8, 2, 'You dont leave much extra time to get there and you show up a little late', 20, 4), +(39, 2, 8, 3, 'You ask if you can work from home because youre erally nervous', 50, 8), +(40, 2, 8, 4, 'You take everyone in your new office out to breakfast', 30, 10) +; +-- /*!40000 ALTER TABLE `story` ENABLE KEYS */; +-- UNLOCK TABLES; + +-- Table structure for table `adventure` + +-- DROP TABLE IF EXISTS `adventure`; +CREATE TABLE `adventure` ( + `adventure_id` int(11), + `adventure_name` varchar(250) default NULL, + PRIMARY KEY (`adventure_id`) +-- KEY `idx_actor_id` (`actor_id`), +-- KEY `idx_movie_id` (`movie_id`) +); -- ENGINE=MyISAM DEFAULT CHARSET=utf8; + +-- Dumping data for table `adventure` + +-- LOCK TABLES `adventure` WRITE; +-- /*!40000 ALTER TABLE `adventure` DISABLE KEYS */; +INSERT INTO `adventure` VALUES +(1,'Surviving ITC'), +(2, 'Getting a Job After ITC'); +-- /*!40000 ALTER TABLE `adventure` ENABLE KEYS */; +-- UNLOCK TABLES; + +-- DROP TABLE IF EXISTS `games`; +CREATE TABLE `games` ( + `game_id` int(11) auto_increment, + `user_id` int(11), + `adventure_id` int(11) default NULL, + `user_life` int(11) default NULL, + `user_money` int(11) default NULL, + `current_story_id` int(11) default NULL, + `game_completed` int(11) default NULL, + PRIMARY KEY (`game_id`), + FOREIGN KEY (user_id) REFERENCES users(user_id), + FOREIGN KEY (adventure_id) REFERENCES adventure(adventure_id) +-- should this be the primary key?? +); -- ENGINE=MyISAM DEFAULT CHARSET=utf8; + +INSERT INTO `games` VALUES +(0, 1, 1, 100, 10, 4, 1), +(0, 2, 1, 55, 4, 5, 1); \ No newline at end of file diff --git a/SQL/test.py b/SQL/test.py new file mode 100644 index 0000000..bec4815 --- /dev/null +++ b/SQL/test.py @@ -0,0 +1,103 @@ +import random +import json + +import json, pymysql + +# add database connection here + + + +# ================ DATABASE INTERFACE ====================== + + +# defining database connection + +connection = pymysql.connect(host='localhost', + port=3306, + user='root', + password='talmap', + db='adventure', + charset='utf8', + cursorclass=pymysql.cursors.DictCursor) + + +# check if user exists +def check_user(userName): + # check if user exists + with connection.cursor() as cursor: + sql = "SELECT * FROM users WHERE user_name = '{0}'".format(userName) + cursor.execute(sql) + result = cursor.fetchall() + + if(len(result) == 0): + print("User does not exist") + return False + + elif(len(result) > 0): + print("Matching user found") + return True + +# function to add user +def add_user(userName): + with connection.cursor() as cursor: + sql = "INSERT INTO users VALUES (default,'{0}');".format(userName) + cursor.execute(sql) + connection.commit() + +# check if active game for specific user +def get_active_game(userName): + with connection.cursor() as cursor: + sql = 'SELECT * FROM games JOIN users ON games.user_id = users.user_id WHERE users.user_name = "{0}" AND games.game_completed = 1;'.format(userName) + cursor.execute(sql) + result = cursor.fetchall() + + if (len(result) == 0): + print("No active game for user") + return False + else: + return result + +# create new game (return id of new game) +def create_game(userID, adventureID): + with connection.cursor() as cursor: + sql = 'INSERT INTO games VALUES (default,{0},{1},{2},{3},{4},{5});'.format(userID,adventureID,100,10,1,0) + cursor.execute(sql) + connection.commit() + + sql= 'SELECT max(game_id) AS current_game FROM games;' + cursor.execute(sql) + + result = cursor.fetchall() + game_id = result[0]['current_game'] + + return game_id + +# update life and step +def update_game(gameID, lifeUnit, wealthUnit, storyId): + with connection.cursor() as cursor: + sql = 'UPDATE games SET user_life= {0}, user_money = {1}, current_story_id = {2} WHERE game_id = {3};'.format(lifeUnit,wealthUnit,storyId,gameID) + cursor.execute(sql) + connection.commit() + +# game completed +def complete_game(gameID, lifeUnit, wealthUnit): + with connection.cursor() as cursor: + sql = 'UPDATE games SET user_life= {0}, user_money = {1}, game_completed = {2} WHERE game_id = {3};'.format(lifeUnit, wealthUnit,1, gameID) + cursor.execute(sql) + connection.commit() + +# get new story +def new_story(adventureID, storyID): + with connection.cursor() as cursor: + sql = 'SELECT adventure_id, story_id, question_type, content FROM story WHERE adventure_id = {0} AND story_id = {1} ORDER BY question_type ;'.format(adventureID, storyID) + cursor.execute(sql) + result = cursor.fetchall() + return result + + + + +print(not True) + + + diff --git a/adventure.html b/adventure.html index e8563de..978d131 100644 --- a/adventure.html +++ b/adventure.html @@ -4,75 +4,101 @@ + Create your own adventure + + + + + + + + + + + -
-

-
-
- -
-
-

- Hello and welcome to the "pick an adventure" game! -

+
+

+
+ + +
+ + +
+
+

+ Hello and welcome to the "pick an adventure" game! +

+ +
-
- -

What is your name?

-
-
- + +

What is your name?

+
+
+ +
+
-
- -
-

What game would you like to play?

- - -
-
-
- -
-
-

- Welcome to the adventure -

+ +
+

What game would you like to play?

+ + +
- -
- -

-
- -
- - - - + + + +
+ +
+
+

+ Welcome to the adventure +

+ +
+
+ +
+ +

+
+ +
+ + + + + +
+
-
+

-

-
- + + +
+ +
-
\ No newline at end of file diff --git a/images/clock.jpg b/images/clock.jpg new file mode 100644 index 0000000..35af53f Binary files /dev/null and b/images/clock.jpg differ diff --git a/images/coffee_and_bed.jpeg b/images/coffee_and_bed.jpeg new file mode 100644 index 0000000..8e714d0 Binary files /dev/null and b/images/coffee_and_bed.jpeg differ diff --git a/images/coffee_and_computer.jpeg b/images/coffee_and_computer.jpeg new file mode 100644 index 0000000..8d49fb2 Binary files /dev/null and b/images/coffee_and_computer.jpeg differ diff --git a/images/inbed.jpg b/images/inbed.jpg new file mode 100644 index 0000000..a03aa07 Binary files /dev/null and b/images/inbed.jpg differ diff --git a/images/interview.jpg b/images/interview.jpg new file mode 100644 index 0000000..1549c1b Binary files /dev/null and b/images/interview.jpg differ diff --git a/images/phone_interview.jpg b/images/phone_interview.jpg new file mode 100644 index 0000000..e09b0ac Binary files /dev/null and b/images/phone_interview.jpg differ diff --git a/images/salary.jpg b/images/salary.jpg new file mode 100644 index 0000000..1e03992 Binary files /dev/null and b/images/salary.jpg differ diff --git a/images/stairs.jpg b/images/stairs.jpg new file mode 100644 index 0000000..1d76056 Binary files /dev/null and b/images/stairs.jpg differ diff --git a/images/work.jpeg b/images/work.jpeg new file mode 100644 index 0000000..30e4c36 Binary files /dev/null and b/images/work.jpeg differ diff --git a/js/adventure.js b/js/adventure.js index 770d7b3..32c56b9 100644 --- a/js/adventure.js +++ b/js/adventure.js @@ -1,9 +1,10 @@ var Adventures = {}; //currentAdventure is used for the adventure we're currently on (id). This should be determined at the beginning of the program -Adventures.currentAdventure = 0; //todo keep track from db +Adventures.currentAdventure = 0; //currentStep is used for the step we're currently on (id). This should be determined at every crossroad, depending on what the user chose -Adventures.currentStep = 0;//todo keep track from db -Adventures.currentUser = 0;//todo keep track from db +Adventures.currentStory = 0; +Adventures.currentUser = 1; +Adventures.current_story_answer = 0; //TODO: remove for production @@ -28,25 +29,41 @@ Adventures.bindErrorHandlers = function () { //The core function of the app, sends the user's choice and then parses the results to the server and handling the response Adventures.chooseOption = function(){ - Adventures.currentStep = $(this).val(); + Adventures.current_story_answer = $(this).val(); + $.ajax("/story",{ type: "POST", data: {"user": Adventures.currentUser, "adventure": Adventures.currentAdventure, - "next": Adventures.currentStep}, + "current_story": Adventures.currentStory, + "current_story_answer": Adventures.current_story_answer}, dataType: "json", contentType: "application/json", success: function (data) { console.log(data); + Adventures.currentStory = data["story_id"]; $(".greeting-text").hide(); - Adventures.write(data); + + if(data["complete"] == 0){ + Adventures.write(data)} + else if(data["complete"] == 1) { + alert("You won") + } + else if(data["complete"] == -1) { + alert("You're dead and/or broke") + } + else { + alert("Error") + }; } }); }; +//Update the questions Adventures.write = function (message) { //Writing new choices and image to screen $(".situation-text").text(message["text"]).show(); + for(var i=0;i 0): + print("Matching user found") + return True + +# function to add user +def add_user(userName): + with connection.cursor() as cursor: + sql = "INSERT INTO users VALUES (default,'{0}');".format(userName) + cursor.execute(sql) + connection.commit() + + # get user id of newly added user + sql = "SELECT user_id FROM users WHERE user_name = '{0}';".format(userName) + cursor.execute(sql) + result = cursor.fetchall() + + return result[0]['user_id'] + +def get_user_id(userName): + with connection.cursor() as cursor: + # get user id of newly added user + sql = "SELECT user_id FROM users WHERE user_name = '{0}';".format(userName) + cursor.execute(sql) + result = cursor.fetchall() + + return result[0]['user_id'] + +# check if active game for specific user +def get_active_game_by_id(userID): + with connection.cursor() as cursor: + sql = 'SELECT * FROM games JOIN users ON games.user_id = users.user_id WHERE users.user_id = "{0}" AND games.game_completed = 0;'.format(userID) + cursor.execute(sql) + result = cursor.fetchall() + + if (len(result) == 0): + print("No active game for user") + return False + else: + return result + +# check if active game for specific user +def get_active_game(userName): + with connection.cursor() as cursor: + sql = 'SELECT * FROM games JOIN users ON games.user_id = users.user_id WHERE users.user_name = "{0}" AND games.game_completed = 0;'.format(userName) + cursor.execute(sql) + result = cursor.fetchall() + + if (len(result) == 0): + print("No active game for user") + return False + else: + return result + +# create new game (return id of new game) +def create_game(userID, adventureID): + with connection.cursor() as cursor: + sql = 'INSERT INTO games VALUES (default,{0},{1},{2},{3},{4},{5});'.format(userID,adventureID,100,10,1,0) + cursor.execute(sql) + connection.commit() + + sql= 'SELECT max(game_id) AS current_game FROM games;' + cursor.execute(sql) + + result = cursor.fetchall() + game_id = result[0]['current_game'] + + return game_id + +# update life and step +def update_game(gameID, lifeUnit, wealthUnit, storyId): + with connection.cursor() as cursor: + sql = 'UPDATE games SET user_life= {0}, user_money = {1}, current_story_id = {2} WHERE game_id = {3};'.format(lifeUnit,wealthUnit,storyId,gameID) + cursor.execute(sql) + connection.commit() + +# game completed +def complete_game(gameID, lifeUnit, wealthUnit): + with connection.cursor() as cursor: + sql = 'UPDATE games SET user_life= {0}, user_money = {1}, game_completed = {2} WHERE game_id = {3};'.format(lifeUnit, wealthUnit,1, gameID) + cursor.execute(sql) + connection.commit() + +# get new story +def new_story(adventureID, storyID): + with connection.cursor() as cursor: + sql = 'SELECT adventure_id, story_id, question_type, content FROM story WHERE adventure_id = {0} AND story_id = {1} ORDER BY question_type ;'.format(adventureID, storyID) + cursor.execute(sql) + result = cursor.fetchall() + return result + +# get the amount of stories in an adventure +def max_story(adventureID): + with connection.cursor() as cursor: + sql = 'SELECT max(story_id) AS "max" FROM story WHERE adventure_id = {0};'.format(adventureID) + cursor.execute(sql) + result = cursor.fetchall() + return result + +# funtion to get story data +def get_story_data(adventure_id, story_id, question_type): + with connection.cursor() as cursor: + sql = 'SELECT * FROM story WHERE adventure_id = {0} AND story_id = {1} AND question_type = {2};'.format(adventure_id, story_id, question_type) + cursor.execute(sql) + result = cursor.fetchall() + return result + +#funtion to update current state of game (health, game complete, coins, stage in game) +def update_database(game_id, health, wealth, complete_status): + with connection.cursor() as cursor: + sql = 'UPDATE games SET game_completed = {0}, user_life = {1}, user_money = {2} WHERE game_id = {3};'.format(complete_status, health, wealth, game_id) + cursor.execute(sql) + connection.commit() + +# funtion to update current state of game (story id) +def update_story_id(game_id, story_id): + with connection.cursor() as cursor: + sql = 'UPDATE games SET current_story_id = {0} WHERE game_id = {1};'.format(story_id, game_id) + cursor.execute(sql) + connection.commit() + +# get new image for story +def get_story_image(adventure_id, story_id): + with connection.cursor() as cursor: + sql = 'SELECT * FROM images WHERE adventure_id = {0} AND story_id = {1};'.format(adventure_id, story_id) + cursor.execute(sql) + try: + result = (cursor.fetchall())[0] + except: + result = {"image_name" : " "} + + return result + + +# ======================= GAME LOGIC =============================== + +def check_answer(adventure_id, story_id, answer, user_id): + + # get current game id from database + game_data = (get_active_game_by_id(user_id))[0] + + # get user health and coins from database + user_health = game_data["user_life"] + user_wealth = game_data["user_money"] + game_status = game_data["game_completed"] + game_id = game_data["game_id"] + + + print("current player status, wealth: {0}, health: {1}, game Status: {2}".format(user_wealth, user_health,game_status)) + + + # get information regarding specific story (current answer, damage, coins) + + print("Test for sql query, adventure id: {0}, story_id: {1}, user answer: {2}".format(adventure_id,story_id,answer)) + story_data = (get_story_data(adventure_id,story_id,answer))[0] + + story_life_damage = story_data["life_unit"] + story_wealth_damage = story_data["wealth_unit"] + + print("Specific story info story life {0} and story wealth {1}".format(story_life_damage,story_wealth_damage)) + + + # update health and wealth + user_health = user_health - story_life_damage + user_wealth = user_wealth - story_wealth_damage + + print("Updated player status, wealth: {0}, health: {1}, game Status: {2}".format(user_wealth, user_health, game_status)) + + # check if user is dead + database_game_status = 0 + + if(user_health > 0 and user_wealth > 0): + # user is dead + game_status = 0 + else: + game_status = -1 + database_game_status = 1 + + + # update databse with new health, coins and game status + update_database(game_id,user_health,user_wealth,database_game_status) + + + # if game_status is -1, user died, if 0 game still contunues + return game_status + + + +# ======================= SERVER =============================== @route("/", method="GET") def index(): return template("adventure.html") - @route("/start", method="POST") def start(): - username = request.POST.get("name") + username = request.POST.get("user") current_adv_id = request.POST.get("adventure_id") + user_id = 0 + current_story_id = 0 + image="" + + + + # check if user exists + if(not check_user(username)): + # if does not exist create new user and game + user_id = add_user(username) + current_story_id = 1 + create_game(user_id,current_adv_id) + + + else: + # if user exists but all games are completed + + user_id = get_user_id(username) + + if(not get_active_game_by_id(user_id)): + + current_story_id = 1 + create_game(user_id, current_adv_id) + else: + # upload uncompleted game + print("active game found") + game_info = (get_active_game_by_id(user_id))[0] + current_story_id = game_info["current_story_id"] + current_adv_id = game_info["adventure_id"] + + + # amount of stories in current adventure + number_of_stories = (max_story(current_adv_id))[0]["max"] + + + if ((current_story_id) > number_of_stories): + print("Game complete") + + # get new story from database + new_story_object = (new_story(current_adv_id, current_story_id)) - user_id = 0 #todo check if exists and if not create it - current_story_id = 0 #todo change next_steps_results = [ - {"id": 1, "option_text": "I fight it"}, - {"id": 2, "option_text": "I give him 10 coins"}, - {"id": 3, "option_text": "I tell it that I just want to go home"}, - {"id": 4, "option_text": "I run away quickly"} + {"id": 1, "option_text": new_story_object[1]["content"]}, + {"id": 2, "option_text": new_story_object[2]["content"]}, + {"id": 3, "option_text": new_story_object[3]["content"]}, + {"id": 4, "option_text": new_story_object[4]["content"]} ] + + image = str((get_story_image(current_adv_id,current_story_id))["image_name"]) + + #todo add the next step based on db return json.dumps({"user": user_id, "adventure": current_adv_id, "current": current_story_id, - "text": "You meet a mysterious creature in the woods, what do you do?", - "image": "troll.png", + "text": new_story_object[0]["content"] , + "image": image, "options": next_steps_results }) - @route("/story", method="POST") def story(): - user_id = request.POST.get("user") - current_adv_id = request.POST.get("adventure") - next_story_id = request.POST.get("next") #this is what the user chose - use it! - next_steps_results = [ - {"id": 1, "option_text": "I run!"}, - {"id": 2, "option_text": "I hide!"}, - {"id": 3, "option_text": "I sleep!"}, - {"id": 4, "option_text": "I fight!"} + + # Get information from the AJAX request + user_id = int(float(request.POST.get("user"))) + current_adv_id = int(float(request.POST.get("adventure"))) + current_story_id = int(float(request.POST.get("current_story"))) #this is what the user chose - use it! + current_story_answer = int(float(request.POST.get("current_story_answer"))) + + + print("user answer from frontend: " + str(current_story_answer)) + + # check user answer and update life and coins + game_complete = check_answer(current_adv_id, current_story_id, current_story_answer, user_id) + + + + print("Game status after check answer: " + str(game_complete)) + + + # For testing purposes + print("User ID: {0}, Adventure: {1}, Story: {2}, Answer: {3}".format(user_id,current_adv_id,current_story_id,current_story_answer)) + + + # amount of stories in current adventure + number_of_stories = (max_story(current_adv_id))[0]["max"] + + # declare needed variables + next_story = current_story_id + 1 + story_question = "" + next_steps_results = [] + + + # condition is user is alive or dead + if(game_complete == 0): + + # if user is alive and no stories left + if((next_story) > number_of_stories): + game_complete = 1 + + next_steps_results = [ + {"id": 1, "option_text": ""}, + {"id": 2, "option_text": ""}, + {"id": 3, "option_text": ""}, + {"id": 4, "option_text": ""} + ] + else: + # get new story from database + new_story_object = (new_story(current_adv_id,next_story)) + + # update database with story number + game_id = (get_active_game_by_id(user_id))[0]["game_id"] + update_story_id(game_id, next_story) + + + + next_steps_results = [ + {"id": 1, "option_text": new_story_object[1]["content"]}, + {"id": 2, "option_text": new_story_object[2]["content"]}, + {"id": 3, "option_text": new_story_object[3]["content"]}, + {"id": 4, "option_text": new_story_object[4]["content"]} + ] + + story_question = new_story_object[0]['content'] + + # if user is dead + else: + game_complete = -1 + + next_steps_results = [ + {"id": 1, "option_text": ""}, + {"id": 2, "option_text": ""}, + {"id": 3, "option_text": ""}, + {"id": 4, "option_text": ""} ] + + print("Game status: " + str(game_complete)) + random.shuffle(next_steps_results) #todo change - used only for demonstration purpouses + image = (get_story_image(current_adv_id, next_story))["image_name"] + + #todo add the next step based on db return json.dumps({"user": user_id, "adventure": current_adv_id, - "text": "New scenario! What would you do?", - "image": "choice.jpg", - "options": next_steps_results + "text": story_question, + "image": image, + "options": next_steps_results, + "story_id": next_story, + "complete": game_complete }) -@route('/js/', method='GET') +@route("/js/") def javascripts(filename): - return static_file(filename, root='js') - + response = static_file(filename, root="js") + response.set_header("Cache-Control", "public, max-age=2") + return response @route('/css/', method='GET') def stylesheets(filename): return static_file(filename, root='css') - -@route('/images/', method='GET') +@route('/images/', method='GET') def images(filename): return static_file(filename, root='images')