Mercurial > maze-src
comparison database.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 require 'utils' | |
| 2 require 'gs_logger' | |
| 3 | |
| 4 # | |
| 5 # This class is responsible for handling all database related requests | |
| 6 # | |
| 7 | |
| 8 class Database | |
| 9 | |
| 10 private_class_method :new | |
| 11 | |
| 12 def Database.initialize | |
| 13 | |
| 14 @@conn = PG::Connection.new(:dbname => 'mazedb', :user => 'mazedb', :password => 's321851gper') | |
| 15 | |
| 16 @@conn.prepare('ins_prep_stmt', 'insert into games (level_id, level_number,level_type) values ($1,$2,$3)') | |
| 17 @@conn.prepare('sel_for_game_id_prep_stmt', 'select * from games where level_id=$1') | |
| 18 @@conn.prepare('sel_level_no_prep_stmt', 'select level_number from games where level_id=$1') | |
| 19 @@conn.prepare('upd_prep_stmt', 'update games set level_number=$1 where level_id=$2') | |
| 20 | |
| 21 $LOG.info 'Database initialized' | |
| 22 rescue => ex | |
| 23 $LOG.fatal "Exception caught while preparing the statements: #{ex.class.to_s}\n" | |
| 24 exit 1 | |
| 25 end | |
| 26 | |
| 27 # | |
| 28 # Inserts a game ID into the database | |
| 29 # | |
| 30 def Database.insert_game_id_to_db(game_id, lev_no, gam_type) | |
| 31 @@conn.exec_prepared 'ins_prep_stmt', ["#{game_id}", lev_no, gam_type] | |
| 32 rescue => ex | |
| 33 $LOG.error "Exception caught: #{ex.class.to_s}\n" | |
| 34 raise | |
| 35 end | |
| 36 | |
| 37 # | |
| 38 # Creates a valid game ID by assuring no duplicates in the database | |
| 39 # | |
| 40 def Database.create_valid_game_id (game_type) | |
| 41 game_id = game_type + Utils.random_string(8) | |
| 42 # Create a unique ID for the current level | |
| 43 loop do | |
| 44 begin | |
| 45 res = @@conn.exec_prepared 'sel_for_game_id_prep_stmt', ["#{game_id}"] | |
| 46 rescue => ex | |
| 47 $LOG.error "Exception caught: #{ex.class.to_s}\n" | |
| 48 raise | |
| 49 end | |
| 50 | |
| 51 break if res.cmd_tuples == 0 | |
| 52 res.clear | |
| 53 game_id = 'S' + Utils.random_string(8) | |
| 54 | |
| 55 end | |
| 56 game_id | |
| 57 end | |
| 58 | |
| 59 # | |
| 60 # Returns the level to which this gid is associated | |
| 61 # | |
| 62 def Database.get_db_level_for_gid(gid) | |
| 63 level_no = 1 | |
| 64 loop do | |
| 65 begin | |
| 66 res = @@conn.exec_prepared 'sel_level_no_prep_stmt', ["#{gid}"] | |
| 67 rescue => ex | |
| 68 $LOG.error "Exception caught: #{ex.class.to_s}\n" | |
| 69 raise | |
| 70 end | |
| 71 return -1 if res.cmd_tuples == 0 | |
| 72 level_no = res[0] | |
| 73 res.clear | |
| 74 break | |
| 75 end | |
| 76 level_no['level_number'].to_i | |
| 77 end | |
| 78 | |
| 79 # | |
| 80 # Updates the level number for the given game ID to reflect th current state | |
| 81 # | |
| 82 def Database.update_level_number_for_gid(level_number, gid) | |
| 83 loop do | |
| 84 begin | |
| 85 res = @@conn.exec_prepared 'upd_prep_stmt', [level_number, "#{gid}"] | |
| 86 rescue => ex | |
| 87 $LOG.error "Exception caught: #{ex.class.to_s}\n" | |
| 88 raise | |
| 89 end | |
| 90 break if res.cmd_tuples == 0 | |
| 91 res.clear | |
| 92 break | |
| 93 end | |
| 94 end | |
| 95 | |
| 96 end |
