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