Skip to content

Instantly share code, notes, and snippets.

@Mizpah
Created February 24, 2013 00:42
Show Gist options
  • Save Mizpah/5022078 to your computer and use it in GitHub Desktop.
Save Mizpah/5022078 to your computer and use it in GitHub Desktop.
Am working on an application that will play the role of a guild website, alongside the multiple systems that a guild needs. (mmo and other gaming communities). This has come about of the back of crating multiple website/system for this purpose that have to be scrapped whenever we change game, and getting tired of people needing good guild websit…
encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130222134729) do
create_table "game_classes", :force => true do |t|
t.string "name"
t.text "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "game_id", :default => 1
end
create_table "game_factions", :force => true do |t|
t.string "name"
t.text "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "game_id", :default => 1
end
create_table "game_races", :force => true do |t|
t.string "name"
t.text "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "game_id", :default => 1
end
create_table "games", :force => true do |t|
t.string "name"
t.text "description"
t.string "game_version"
t.string "data_version"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
@danneu
Copy link

danneu commented Feb 24, 2013

Serializing your data

One idea is to express your system's data in yaml files.

# world_of_warcraft.yml

name: World of Warcraft
description: Blizzard's MMO

factions:
  - 
    name: Alliance
    description: Humans and stuff
  -
    name: Horde
    description: Orcs and stuff

classes:
  - 
    name: Warrior
  - 
    name: Priest

If WoW adds a new class, you add a few lines and commit the changes. Your representation of games are now versionable.

I wrote that yaml file off the top of my head. Assuming I got the syntax right, it'll be parsed into a Ruby hash that looks like this:

wow = Yaml.load(File.read("world_of_warcraft.yml"))
wow.inspect 
#=>
{
  name: "World of Warcraft",
  description: "Blizzard's MMO",
  factions: [
    { name: "Alliance", description: "Humans and stuff" },
    { name: "Horde", description: "Orcs and stuff" }
  ],
  classes: [
    { name: "Warrior" },
    { name: "Priest" }
  ]
}

You could write a Rake task that reads all the game yaml files (one for each game) and populates the database from them.

# Might want to add a few lines that destroy the existing database records 
# so this rake task will rebuild them based on the yaml files.

game_files = Dir.entries("game_files")

game_files.each do |game_file|
  data = Yaml.load(File.read(game_file))
  game = Game.create(name: wow.name, description: wow.description)

  data[:factions].each do |faction_data|
    game.game_factions << GameFaction.new(name: faction_data[:name], description: faction_data[:description] )
  end

  data[:classes].each do |class_data|
    game.game_classes << GameClass.new(name: class_data[:name])
  end
end

Just a general idea.

Imagine if running rake seed_games brought your DB up to speed with whatever you had in game_files/**.yml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment