if fs.exists(".installed") then
	print("Game is already installed! Stopping script.")
	return false
end

local start = microtime(true)
local db = sqlite("dk")

db:begin()

db:create_table("babble",
	"id      INTEGER PRIMARY KEY AUTOINCREMENT",
	"user_id INTEGER NOT NULL",
	"visible INTEGER NOT NULL DEFAULT 1",
	"content TEXT NOT NULL",
	"posted  INTEGER NOT NULL DEFAULT (unixepoch())"
)

db:create_table("item_blueprints",
	"id            INTEGER PRIMARY KEY AUTOINCREMENT",
	"type          INTEGER NOT NULL DEFAULT 0",
	"tier          INTEGER NOT NULL DEFAULT 0",
	"rarity        INTEGER NOT NULL DEFAULT 0",
	"base_value    INTEGER NOT NULL DEFAULT 0",
	"attributes_id INTEGER NOT NULL DEFAULT 0",
	"stackable     INTEGER NOT NULL DEFAULT 0",
	"max_stack     INTEGER NOT NULL DEFAULT 1",
	"name          TEXT UNIQUE NOT NULL",
	"lore          TEXT",
	"special       TEXT NOT NULL DEFAULT ''"
)

db:create_table("items",
	"id            INTEGER PRIMARY KEY AUTOINCREMENT",
	"type          INTEGER NOT NULL DEFAULT 0",
	"tier          INTEGER NOT NULL DEFAULT 0",
	"rarity        INTEGER NOT NULL DEFAULT 0",
	"crafted       INTEGER NOT NULL DEFAULT 0",
	"crafter_id    INTEGER NOT NULL DEFAULT 0",
	"base_value    INTEGER NOT NULL DEFAULT 0",
	"attributes_id INTEGER NOT NULL DEFAULT 0",
	"stackable     INTEGER NOT NULL DEFAULT 0",
	"max_stack     INTEGER NOT NULL DEFAULT 1",
	"name          TEXT UNIQUE NOT NULL",
	"lore          TEXT",
	"special       TEXT NOT NULL DEFAULT ''"
)

db:create_table("user_attributes",
	"id          INTEGER PRIMARY KEY AUTOINCREMENT",
	"power       INTEGER NOT NULL DEFAULT 0",
	"toughness   INTEGER NOT NULL DEFAULT 0",
	"agility     INTEGER NOT NULL DEFAULT 0",
	"dexterity   INTEGER NOT NULL DEFAULT 0",
	"magic       INTEGER NOT NULL DEFAULT 0",
	"luck        INTEGER NOT NULL DEFAULT 0",
	"perception  INTEGER NOT NULL DEFAULT 0",
	"uber_power  INTEGER NOT NULL DEFAULT 0",
	"uber_resist INTEGER NOT NULL DEFAULT 0",
	"hp          INTEGER NOT NULL DEFAULT 0",
	"mp          INTEGER NOT NULL DEFAULT 0"
)

db:create_table("item_blueprint_attributes",
	"id          INTEGER PRIMARY KEY AUTOINCREMENT",
	"power       INTEGER NOT NULL DEFAULT 0",
	"toughness   INTEGER NOT NULL DEFAULT 0",
	"agility     INTEGER NOT NULL DEFAULT 0",
	"dexterity   INTEGER NOT NULL DEFAULT 0",
	"magic       INTEGER NOT NULL DEFAULT 0",
	"luck        INTEGER NOT NULL DEFAULT 0",
	"perception  INTEGER NOT NULL DEFAULT 0",
	"uber_power  INTEGER NOT NULL DEFAULT 0",
	"uber_resist INTEGER NOT NULL DEFAULT 0",
	"hp          INTEGER NOT NULL DEFAULT 0",
	"mp          INTEGER NOT NULL DEFAULT 0"
)

db:create_table("item_attributes",
	"id          INTEGER PRIMARY KEY AUTOINCREMENT",
	"power       INTEGER NOT NULL DEFAULT 0",
	"toughness   INTEGER NOT NULL DEFAULT 0",
	"agility     INTEGER NOT NULL DEFAULT 0",
	"dexterity   INTEGER NOT NULL DEFAULT 0",
	"magic       INTEGER NOT NULL DEFAULT 0",
	"luck        INTEGER NOT NULL DEFAULT 0",
	"perception  INTEGER NOT NULL DEFAULT 0",
	"uber_power  INTEGER NOT NULL DEFAULT 0",
	"uber_resist INTEGER NOT NULL DEFAULT 0",
	"hp          INTEGER NOT NULL DEFAULT 0",
	"mp          INTEGER NOT NULL DEFAULT 0"
)

db:create_table("monster_attributes",
	"id          INTEGER PRIMARY KEY AUTOINCREMENT",
	"power       INTEGER NOT NULL DEFAULT 0",
	"toughness   INTEGER NOT NULL DEFAULT 0",
	"agility     INTEGER NOT NULL DEFAULT 0",
	"dexterity   INTEGER NOT NULL DEFAULT 0",
	"magic       INTEGER NOT NULL DEFAULT 0",
	"luck        INTEGER NOT NULL DEFAULT 0",
	"perception  INTEGER NOT NULL DEFAULT 0",
	"uber_power  INTEGER NOT NULL DEFAULT 0",
	"uber_resist INTEGER NOT NULL DEFAULT 0",
	"hp          INTEGER NOT NULL DEFAULT 0",
	"mp          INTEGER NOT NULL DEFAULT 0"
)

db:create_table("forum",
	"id        INTEGER PRIMARY KEY AUTOINCREMENT",
	"user_id   INTEGER NOT NULL",
	"parent_id INTEGER NOT NULL DEFAULT 0",
	"replies   INTEGER NOT NULL DEFAULT 0",
	"locked    INTEGER NOT NULL DEFAULT 0",
	"visible   INTEGER NOT NULL DEFAULT 1",
	"pinned    INTEGER NOT NULL DEFAULT 0",
	"posted    INTEGER NOT NULL DEFAULT (unixepoch())",
	"latest    INTEGER NOT NULL DEFAULT (unixepoch())",
	"title     TEXT NOT NULL",
	"content   TEXT NOT NULL"
)

db:create_table("monsters",
	"id               INTEGER PRIMARY KEY AUTOINCREMENT",
	"tier             INTEGER NOT NULL DEFAULT 0",
	"min_level        INTEGER NOT NULL DEFAULT 1",
	"max_level        INTEGER NOT NULL DEFAULT 1",
	"min_hp           INTEGER NOT NULL DEFAULT 1",
	"max_hp           INTEGER NOT NULL DEFAULT 1",
	"attributes_id    INTEGER NOT NULL",
	"attribute_spread INTEGER NOT NULL DEFAULT 0",
	"exp_base         INTEGER NOT NULL DEFAULT 0",
	"exp_spread       INTEGER NOT NULL DEFAULT 0",
	"gold_base        INTEGER NOT NULL DEFAULT 0",
	"gold_spread      INTEGER NOT NULL DEFAULT 0",
	"immunity         INTEGER NOT NULL DEFAULT 0",
	"name             TEXT UNIQUE NOT NULL"
)

db:create_table("news",
	"id      INTEGER PRIMARY KEY AUTOINCREMENT",
	"posted  INTEGER NOT NULL DEFAULT (unixepoch())",
	"author  TEXT NOT NULL DEFAULT 'Guild Master'",
	"content TEXT NOT NULL"
)

db:create_table("spells",
	"id    INTEGER PRIMARY KEY AUTOINCREMENT",
	"mp    INTEGER NOT NULL DEFAULT 0",
	"power INTEGER NOT NULL DEFAULT 0",
	"type  INTEGER NOT NULL DEFAULT 0",
	"name  TEXT UNIQUE NOT NULL",
	"lore  TEXT",
	"info  TEXT NOT NULL"
)

db:create_table("pois",
	"id INTEGER PRIMARY KEY AUTOINCREMENT",
	"lat INTEGER NOT NULL",
	"lon INTEGER NOT NULL",
	"type INTEGER NOT NULL",
	"poi_id INTEGER UNIQUE NOT NULL",
	"UNIQUE INDEX:pois_location_idx(lat, lon)"
)

db:create_table("towns",
	"id        INTEGER PRIMARY KEY AUTOINCREMENT",
	"poi_id    INTEGER UNIQUE NOT NULL",
	"inn_price INTEGER NOT NULL DEFAULT 0",
	"map_price INTEGER NOT NULL DEFAULT 0",
	"tp_cost   INTEGER NOT NULL DEFAULT 0",
	"name      TEXT UNIQUE NOT NULL"
)

db:create_table("town_shop_items",
	"town_id    INTEGER NOT NULL",
	"item_id    INTEGER NOT NULL",
	"buy_value  INTEGER NOT NULL DEFAULT 1",
	"sell_value INTEGER NOT NULL DEFAULT 1"
)

db:create_table("fights",
	"id                    INTEGER PRIMARY KEY AUTOINCREMENT",
	"user_id               INTEGER NOT NULL",
	"user_attributes_id    INTEGER NOT NULL",
	"user_uber_power       INTEGER NOT NULL",
	"user_uber_resist      INTEGER NOT NULL",
	"monster_id            INTEGER NOT NULL",
	"monster_attributes_id INTEGER NOT NULL",
	"monster_hp            INTEGER NOT NULL",
	"monster_max_hp        INTEGER NOT NULL",
	"monster_mp            INTEGER NOT NULL",
	"monster_max_mp        INTEGER NOT NULL",
	"monster_sleep         INTEGER NOT NULL DEFAULT 0",
	"monster_uber_power    INTEGER NOT NULL",
	"monster_uber_resist   INTEGER NOT NULL",
	"first                 INTEGER NOT NULL DEFAULT 0",
	"turn                  INTEGER NOT NULL DEFAULT 0",
	"won                   INTEGER NOT NULL DEFAULT 0",
	"log                   TEXT"
)

db:create_table("users",
	"id             INTEGER PRIMARY KEY AUTOINCREMENT",
	"username       TEXT UNIQUE NOT NULL",
	"password       TEXT NOT NULL",
	"email          TEXT UNIQUE NOT NULL",
	"verified       INTEGER NOT NULL DEFAULT 0",
	"verify_token   TEXT NOT NULL DEFAULT ''",
	"registered     INTEGER NOT NULL DEFAULT (unixepoch())",
	"last_login     INTEGER NOT NULL DEFAULT (unixepoch())",
	"auth_level     INTEGER NOT NULL DEFAULT 0",
	"lat            INTEGER NOT NULL DEFAULT 0",
	"lon            INTEGER NOT NULL DEFAULT 0",
	"class_id       INTEGER NOT NULL DEFAULT 1",
	"attributes_id  INTEGER NOT NULL",
	"currently      INTEGER NOT NULL DEFAULT 0",
	"fight_id       INTEGER NOT NULL DEFAULT 0",
	"hp             INTEGER NOT NULL default 15",
	"max_hp         INTEGER NOT NULL default 15",
	"mp             INTEGER NOT NULL default 0",
	"max_mp         INTEGER NOT NULL default 0",
	"tp             INTEGER NOT NULL default 10",
	"max_tp         INTEGER NOT NULL default 10",
	"level          INTEGER NOT NULL default 1",
	"gold           INTEGER NOT NULL default 100",
	"exp            INTEGER NOT NULL default 0",
	"drop_code      INTEGER NOT NULL default 0",
	"spells         TEXT",
	"maps           TEXT"
)

db:create_table("user_equipped",
	"user_id      INTEGER NOT NULL",
	"weapon_id    INTEGER NOT NULL DEFAULT 0",
	"weapon_name  TEXT",
	"armor_id     INTEGER NOT NULL DEFAULT 0",
	"armor_name   TEXT",
	"shield_id    INTEGER NOT NULL DEFAULT 0",
	"shield_name  TEXT",
	"relic_1_id   INTEGER NOT NULL DEFAULT 0",
	"relic_1_name TEXT",
	"relic_2_id   INTEGER NOT NULL DEFAULT 0",
	"relic_2_name TEXT",
	"relic_3_id   INTEGER NOT NULL DEFAULT 0",
	"relic_3_name TEXT"
)

db:create_table("user_inventories",
	"user_id INTEGER NOT NULL",
	"item_id INTEGER NOT NULL",
	"stack   INTEGER NOT NULL DEFAULT 1"
)

db:commit()

local time = math.roundto(microtime(true) - start, 4)
print(iparse("Database setup in <!> seconds.", {time}))
return true
