diff game.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game.rb	Sun Sep 15 11:46:47 2019 +0200
@@ -0,0 +1,77 @@
+#
+# A class to encapsulate methods for generating a game
+#
+class Game
+
+
+  GAME_STRING_STORY = 'S'
+  GAME_STRING_ADVENTURE = 'A'
+  GAME_STRING_TIMERUN = 'T'
+
+  #
+  # Will generate a URL for the given string
+  # The first 6 characters are the encryption key of the type
+  # What comes after is the encrypted game type (SG, AG, TG)
+  #
+  def Game.gen_game_url (type)
+    key = Utils.random_hex_string 6
+    encrypted_game_type = type.encrypt(key)
+    return key + encrypted_game_type
+  end
+
+  #
+  # Will create a new sotry game, generate ID and put it in the database
+  #
+  def Game.create_new_story_game
+    # Story game IDs always start with 'S'
+    game_id = Database.create_valid_game_id(GAME_STRING_STORY)
+
+    # Insert the ID in the DB
+    Database.insert_game_id_to_db(game_id, 1, 'S')
+
+    handle_maze_with_number(1,  game_id)
+  end
+
+  #
+  # Will create a new sotry game, generate ID and put it in the database
+  #
+  def Game.create_new_adventure_game
+    # Adventure game IDs always start with 'A'
+    game_id = Database.create_valid_game_id(GAME_STRING_ADVENTURE)
+    level_number = rand(1..Integer::MAX) %  2147483647
+
+    # Insert the ID in the DB and associate it with the given level
+    Database.insert_game_id_to_db(game_id, level_number, 'A')
+    Database.update_level_number_for_gid(level_number, game_id)
+
+    handle_maze_with_number(level_number,  game_id, MAIN_HTML, GAME_TYPE_ADVENTURE)
+  end
+
+  #
+  # Will create a new sotry game, generate ID and put it in the database
+  #
+  def Game.create_new_maze_game
+    # Pure maze game IDs always start with 'T'
+    game_id = Database.create_valid_game_id(GAME_STRING_TIMERUN)
+    level_number = rand(1..Integer::MAX) %  2147483647
+
+    # Insert the ID in the DB and associate it with the given level
+    Database.insert_game_id_to_db(game_id, level_number, 'T')
+    Database.update_level_number_for_gid(level_number, game_id)
+
+    handle_maze_with_number(level_number,  game_id, MAIN_HTML, GAME_TYPE_MAZE)
+  end
+
+  #
+  # Will create a game, with the specified type
+  #
+  def Game.create_game(game_type)
+
+    code, retv = create_new_story_game if game_type == 'SG'
+    code, retv = create_new_adventure_game if game_type == 'AG'
+    code, retv = create_new_maze_game if game_type == 'TG'
+
+    return code, retv
+  end
+
+end
\ No newline at end of file