Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Database Models & Schema

Sabbir Ahmed edited this page Dec 15, 2024 · 3 revisions

Table of Contents


Content Model

Table Name: contents

Schema

Column Type Constraints
id integer Primary key
story_text text
image_url string
created_at datetime
updated_at datetime

Associations

  • has_many :grid_cells

Game Model

Table Name: games

Schema

Column Type Constraints
id integer Primary key
name string
server_id integer null: false
created_at datetime
updated_at datetime

Associations

  • belongs_to :server
  • has_many :messages

User Model

Table Name: users

Schema

Column Type Constraints
id integer Primary key
username string null: false, unique: true
email string null: false, unique: true
encrypted_password string null: false (managed by Devise)
reset_password_token string unique: true (managed by Devise)
reset_password_sent_at datetime (managed by Devise)
remember_created_at datetime (managed by Devise)
created_at datetime
updated_at datetime

Associations

  • has_one :wallet, dependent: :destroy
  • has_many :server_users, dependent: :destroy
  • has_many :servers, through: :server_users
  • has_many :transactions, dependent: :destroy
  • has_many :inventories, dependent: :destroy
  • has_many :items, through: :inventories
  • has_many :scores, dependent: :destroy
  • has_many :treasure_finds, dependent: :destroy
  • has_many :leaderboard_entries, dependent: :destroy
  • has_many :leaderboards, through: :leaderboard_entries

Wallet Model

Table Name: wallets

Schema

Column Type Constraints
id integer Primary key
user_id integer null: false
balance decimal precision: 10, scale: 2
created_at datetime
updated_at datetime

Associations

  • belongs_to :user

Transaction Model

Table Name: transactions

Schema

Column Type Constraints
id integer Primary key
user_id integer null: false
transaction_type string
amount decimal precision: 10, scale: 2
currency string
payment_method string
item_id integer
quantity integer
description text
created_at datetime
updated_at datetime

Associations

  • belongs_to :user
  • belongs_to :item, optional: true

Item Model

Table Name: items

Schema

Column Type Constraints
id integer Primary key
name string
description text
price decimal precision: 10, scale: 2
category string
required_level integer
created_at datetime
updated_at datetime
image_url string

Associations

  • has_many :inventories
  • has_many :transactions

Inventory Model

Table Name: inventories

Schema

Column Type Constraints
id integer Primary key
user_id integer null: false
item_id integer null: false
quantity integer
created_at datetime
updated_at datetime
item_name string
server_user_id integer

Associations

  • belongs_to :user
  • belongs_to :item

Server Model

Table Name: servers

Schema

Column Type Constraints
id integer Primary key
name string
max_players integer
created_by integer null: false
created_at datetime
updated_at datetime
status string Default: 'pending'
current_turn_server_user_id integer
background_image_url string
role string
turn_count integer

Associations

  • belongs_to :user, foreign_key: 'created_by'
  • has_many :server_users
  • has_many :games
  • has_many :leaderboards
  • has_many :scores

ServerUser Model

Table Name: server_users

Schema

Column Type Constraints
id integer Primary key
user_id integer null: false
server_id integer null: false
current_position_x integer
current_position_y integer
created_at datetime
updated_at datetime
total_ap integer Default: 200
turn_ap integer Default: 2
shard_balance integer Default: 0
symbol string
turn_order integer
can_move_diagonally boolean
diagonal_moves_left integer
mirror_shield boolean
turns_skipped integer
cable_token string
role integer null: false

Associations

  • belongs_to :user
  • belongs_to :server

GridCell Model

Table Name: grid_cells

Schema

Column Type Constraints
id integer Primary key
server_id integer null: false
x integer
y integer
content_id integer
treasure_id integer
created_at datetime
updated_at datetime
owner_id integer
obstacle boolean Default: false
fortified integer

Associations

  • belongs_to :server
  • belongs_to :content
  • belongs_to :treasure

Content Model

Table Name: contents

Schema

Column Type Constraints
id integer Primary key
story_text text
image_url string
created_at datetime
updated_at datetime

Associations

  • has_many :grid_cells

Treasure Model

Table Name: treasures

Schema

Column Type Constraints
id integer Primary key
name string
description text
points integer
item_id integer null: false
unlock_criteria string
created_at datetime
updated_at datetime

Associations

  • belongs_to :item
  • has_many :treasure_finds

TreasureFind Model

Table Name: treasure_finds

Schema

Column Type Constraints
id integer Primary key
user_id integer null: false
treasure_id integer null: false
server_id integer null: false
found_at datetime
created_at datetime
updated_at datetime

Associations

  • belongs_to :user
  • belongs_to :treasure
  • belongs_to :server

Score Model

Table Name: scores

Schema

Column Type Constraints
id integer Primary key
user_id integer null: false
server_id integer null: false
points integer
level integer
created_at datetime
updated_at datetime

Associations

  • belongs_to :user
  • belongs_to :server

Leaderboard Model

Table Name: leaderboards

Schema

Column Type Constraints
id integer Primary key
name string
scope string
server_id integer null: false
created_at datetime
updated_at datetime

Associations

  • belongs_to :server
  • has_many :leaderboard_entries

LeaderboardEntry Model

Table Name: leaderboard_entries

Schema

Column Type Constraints
id integer Primary key
leaderboard_id integer null: false
user_id integer null: false
points integer
rank integer
remaining_ap integer Default: 0
cells_occupied integer Default: 0
created_at datetime
updated_at datetime

Associations

  • belongs_to :leaderboard
  • belongs_to :user

Associations Summary

  • User: has one wallet, has many server_users, has many transactions, has many inventories, has many scores, has many treasure_finds, has many leaderboard_entries, has many leaderboards through leaderboard_entries.
  • Server: has many server_users, has many games, has many leaderboards, has many scores.
  • ServerUser: belongs to user and server.
  • GridCell: belongs to server, content, and treasure.
  • Content: has many grid_cells.
  • Treasure: belongs to item, has many treasure_finds.
  • TreasureFind: belongs to user, treasure, and server.
  • Item: has many inventories, has many transactions.
  • Inventory: belongs to user and item.
  • Transaction: belongs to user and item (optional).
  • Leaderboard: belongs to server, has many leaderboard_entries.
  • LeaderboardEntry: belongs to leaderboard and user.

Notes and Next Steps

  • Review associations to ensure consistency across models.
  • Implement necessary validations where applicable.
  • Add any needed callbacks, scopes, or custom methods for business logic.
  • Continue refining database queries and optimizing performance.