-- Game DB (та же БД): минимум для экрана выбора/создания персонажа и входа в мир.
-- Полный дамп datapack ставьте поверх, если сборка Samurai Crow требует больше таблиц.

CREATE TABLE IF NOT EXISTS global_data (
  var VARCHAR(255) NOT NULL PRIMARY KEY,
  value TEXT
);

CREATE TABLE IF NOT EXISTS characters (
  account_name VARCHAR(45),
  charId INTEGER NOT NULL PRIMARY KEY,
  char_name VARCHAR(35) NOT NULL,
  level SMALLINT DEFAULT 1,
  maxHp INTEGER DEFAULT 100,
  curHp INTEGER DEFAULT 100,
  maxCp INTEGER DEFAULT 100,
  curCp INTEGER DEFAULT 100,
  maxMp INTEGER DEFAULT 100,
  curMp INTEGER DEFAULT 100,
  face SMALLINT DEFAULT 0,
  hairStyle SMALLINT DEFAULT 0,
  hairColor SMALLINT DEFAULT 0,
  sex SMALLINT DEFAULT 0,
  reputation INTEGER DEFAULT 0,
  fame INTEGER DEFAULT 0,
  raidbossPoints INTEGER DEFAULT 0,
  pvpkills INTEGER DEFAULT 0,
  pkkills INTEGER DEFAULT 0,
  clanid INTEGER DEFAULT 0,
  race SMALLINT DEFAULT 0,
  classid INTEGER DEFAULT 0,
  deletetime BIGINT DEFAULT 0,
  title VARCHAR(21) DEFAULT '',
  accesslevel INTEGER DEFAULT 0,
  online SMALLINT DEFAULT 0,
  onlinetime INTEGER DEFAULT 0,
  char_slot SMALLINT DEFAULT 0,
  lastAccess BIGINT DEFAULT 0,
  clan_privs INTEGER DEFAULT 0,
  wantspeace SMALLINT DEFAULT 0,
  base_class INTEGER DEFAULT 0,
  x INTEGER DEFAULT -114520,
  y INTEGER DEFAULT -249704,
  z INTEGER DEFAULT -2984,
  heading INTEGER DEFAULT 0,
  createDate DATE DEFAULT CURRENT_DATE,
  isIn7sDungeon SMALLINT DEFAULT 0,
  in_jail SMALLINT DEFAULT 0,
  jail_timer INTEGER DEFAULT 0,
  powerGrade SMALLINT DEFAULT 0,
  apprentice INTEGER DEFAULT 0,
  sponsor INTEGER DEFAULT 0,
  clan_join_expiry_time TIMESTAMP NULL,
  clan_create_expiry_time TIMESTAMP NULL,
  bookmarkslot SMALLINT DEFAULT 0,
  vitality_points INTEGER DEFAULT 35000,
  language VARCHAR(5) DEFAULT 'en'
);

CREATE INDEX IF NOT EXISTS idx_characters_account ON characters (account_name);
CREATE UNIQUE INDEX IF NOT EXISTS idx_characters_name ON characters (char_name);

CREATE TABLE IF NOT EXISTS character_subclasses (
  charId INTEGER NOT NULL,
  class_id INTEGER NOT NULL DEFAULT 0,
  exp BIGINT DEFAULT 0,
  sp BIGINT DEFAULT 0,
  level INTEGER DEFAULT 1,
  class_index INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (charId, class_index),
  CONSTRAINT fk_subclasses_char FOREIGN KEY (charId)
    REFERENCES characters (charId) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS character_skills (
  charId INTEGER NOT NULL,
  skill_id INTEGER NOT NULL,
  skill_level INTEGER NOT NULL DEFAULT 1,
  class_index INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (charId, skill_id, class_index),
  CONSTRAINT fk_skills_char FOREIGN KEY (charId)
    REFERENCES characters (charId) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS character_shortcuts (
  charId INTEGER NOT NULL,
  slot INTEGER NOT NULL,
  page INTEGER NOT NULL,
  type INTEGER DEFAULT 0,
  shortcut_id INTEGER DEFAULT 0,
  level INTEGER DEFAULT 0,
  class_index INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (charId, slot, page, class_index),
  CONSTRAINT fk_shortcuts_char FOREIGN KEY (charId)
    REFERENCES characters (charId) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS character_variables (
  charId INTEGER NOT NULL,
  var VARCHAR(255) NOT NULL,
  val TEXT,
  PRIMARY KEY (charId, var),
  CONSTRAINT fk_variables_char FOREIGN KEY (charId)
    REFERENCES characters (charId) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS items (
  object_id INTEGER NOT NULL PRIMARY KEY,
  owner_id INTEGER NOT NULL,
  item_id INTEGER NOT NULL,
  count BIGINT NOT NULL DEFAULT 1,
  enchant_level INTEGER DEFAULT 0,
  loc VARCHAR(10) NOT NULL DEFAULT 'INVENTORY',
  loc_data INTEGER DEFAULT 0
);

CREATE INDEX IF NOT EXISTS idx_items_owner ON items (owner_id);

CREATE TABLE IF NOT EXISTS clan_data (
  clan_id INTEGER NOT NULL PRIMARY KEY,
  clan_name VARCHAR(45) NOT NULL,
  clan_level INTEGER DEFAULT 0,
  reputation_score INTEGER DEFAULT 0,
  hasCastle INTEGER DEFAULT 0,
  blood_alliance INTEGER DEFAULT 0,
  blood_oath INTEGER DEFAULT 0,
  ally_id INTEGER DEFAULT 0,
  ally_name VARCHAR(45) DEFAULT '',
  leader_id INTEGER DEFAULT 0,
  crest_id INTEGER DEFAULT 0,
  crest_large_id INTEGER DEFAULT 0,
  ally_crest_id INTEGER DEFAULT 0,
  auction_bid_at INTEGER DEFAULT 0,
  ally_penalty_expiry_time BIGINT DEFAULT 0,
  ally_penalty_type SMALLINT DEFAULT 0,
  char_penalty_expiry_time BIGINT DEFAULT 0,
  dissolving_expiry_time BIGINT DEFAULT 0,
  new_leader_id INTEGER DEFAULT 0
);
