annotate game.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
rev   line source
ferencd@0 1 #
ferencd@0 2 # A class to encapsulate methods for generating a game
ferencd@0 3 #
ferencd@0 4 class Game
ferencd@0 5
ferencd@0 6
ferencd@0 7 GAME_STRING_STORY = 'S'
ferencd@0 8 GAME_STRING_ADVENTURE = 'A'
ferencd@0 9 GAME_STRING_TIMERUN = 'T'
ferencd@0 10
ferencd@0 11 #
ferencd@0 12 # Will generate a URL for the given string
ferencd@0 13 # The first 6 characters are the encryption key of the type
ferencd@0 14 # What comes after is the encrypted game type (SG, AG, TG)
ferencd@0 15 #
ferencd@0 16 def Game.gen_game_url (type)
ferencd@0 17 key = Utils.random_hex_string 6
ferencd@0 18 encrypted_game_type = type.encrypt(key)
ferencd@0 19 return key + encrypted_game_type
ferencd@0 20 end
ferencd@0 21
ferencd@0 22 #
ferencd@0 23 # Will create a new sotry game, generate ID and put it in the database
ferencd@0 24 #
ferencd@0 25 def Game.create_new_story_game
ferencd@0 26 # Story game IDs always start with 'S'
ferencd@0 27 game_id = Database.create_valid_game_id(GAME_STRING_STORY)
ferencd@0 28
ferencd@0 29 # Insert the ID in the DB
ferencd@0 30 Database.insert_game_id_to_db(game_id, 1, 'S')
ferencd@0 31
ferencd@0 32 handle_maze_with_number(1, game_id)
ferencd@0 33 end
ferencd@0 34
ferencd@0 35 #
ferencd@0 36 # Will create a new sotry game, generate ID and put it in the database
ferencd@0 37 #
ferencd@0 38 def Game.create_new_adventure_game
ferencd@0 39 # Adventure game IDs always start with 'A'
ferencd@0 40 game_id = Database.create_valid_game_id(GAME_STRING_ADVENTURE)
ferencd@0 41 level_number = rand(1..Integer::MAX) % 2147483647
ferencd@0 42
ferencd@0 43 # Insert the ID in the DB and associate it with the given level
ferencd@0 44 Database.insert_game_id_to_db(game_id, level_number, 'A')
ferencd@0 45 Database.update_level_number_for_gid(level_number, game_id)
ferencd@0 46
ferencd@0 47 handle_maze_with_number(level_number, game_id, MAIN_HTML, GAME_TYPE_ADVENTURE)
ferencd@0 48 end
ferencd@0 49
ferencd@0 50 #
ferencd@0 51 # Will create a new sotry game, generate ID and put it in the database
ferencd@0 52 #
ferencd@0 53 def Game.create_new_maze_game
ferencd@0 54 # Pure maze game IDs always start with 'T'
ferencd@0 55 game_id = Database.create_valid_game_id(GAME_STRING_TIMERUN)
ferencd@0 56 level_number = rand(1..Integer::MAX) % 2147483647
ferencd@0 57
ferencd@0 58 # Insert the ID in the DB and associate it with the given level
ferencd@0 59 Database.insert_game_id_to_db(game_id, level_number, 'T')
ferencd@0 60 Database.update_level_number_for_gid(level_number, game_id)
ferencd@0 61
ferencd@0 62 handle_maze_with_number(level_number, game_id, MAIN_HTML, GAME_TYPE_MAZE)
ferencd@0 63 end
ferencd@0 64
ferencd@0 65 #
ferencd@0 66 # Will create a game, with the specified type
ferencd@0 67 #
ferencd@0 68 def Game.create_game(game_type)
ferencd@0 69
ferencd@0 70 code, retv = create_new_story_game if game_type == 'SG'
ferencd@0 71 code, retv = create_new_adventure_game if game_type == 'AG'
ferencd@0 72 code, retv = create_new_maze_game if game_type == 'TG'
ferencd@0 73
ferencd@0 74 return code, retv
ferencd@0 75 end
ferencd@0 76
ferencd@0 77 end