forked from fsobolev/BitPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Add query tool for crafting database #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sauramel
wants to merge
1
commit into
main
Choose a base branch
from
oaolqn-codex/refactor-codebase-and-extract-game-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import argparse | ||
| import json | ||
| import sqlite3 | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def build_database(json_path: Path, db_path: Path) -> None: | ||
| data = json.loads(json_path.read_text()) | ||
| conn = sqlite3.connect(db_path) | ||
| cur = conn.cursor() | ||
|
|
||
| cur.executescript( | ||
| """ | ||
| PRAGMA journal_mode = WAL; | ||
| PRAGMA synchronous = NORMAL; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS items ( | ||
| id INTEGER PRIMARY KEY, | ||
| name TEXT, | ||
| tier INTEGER, | ||
| rarity INTEGER, | ||
| icon TEXT, | ||
| extraction_skill INTEGER | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS recipes ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| item_id INTEGER, | ||
| output_quantity INTEGER, | ||
| level_requirement INTEGER, | ||
| FOREIGN KEY(item_id) REFERENCES items(id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS recipe_ingredients ( | ||
| recipe_id INTEGER, | ||
| ingredient_id INTEGER, | ||
| quantity INTEGER, | ||
| FOREIGN KEY(recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY(ingredient_id) REFERENCES items(id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS recipe_possibilities ( | ||
| recipe_id INTEGER, | ||
| quantity INTEGER, | ||
| chance REAL, | ||
| FOREIGN KEY(recipe_id) REFERENCES recipes(id) | ||
| ); | ||
| """ | ||
| ) | ||
| conn.commit() | ||
|
|
||
| cur.execute("BEGIN") | ||
| for item_id_str, item in data.items(): | ||
| item_id = int(item_id_str) | ||
| cur.execute( | ||
| "INSERT OR REPLACE INTO items (id, name, tier, rarity, icon, extraction_skill) " | ||
| "VALUES (?, ?, ?, ?, ?, ?)", | ||
| ( | ||
| item_id, | ||
| item.get("name"), | ||
| item.get("tier"), | ||
| item.get("rarity"), | ||
| item.get("icon"), | ||
| item.get("extraction_skill", -1), | ||
| ), | ||
| ) | ||
| for recipe in item.get("recipes", []): | ||
| level_req = recipe.get("level_requirements") | ||
| lvl = level_req[0] if level_req else None | ||
| cur.execute( | ||
| "INSERT INTO recipes (item_id, output_quantity, level_requirement) VALUES (?, ?, ?)", | ||
| (item_id, recipe.get("output_quantity"), lvl), | ||
| ) | ||
| recipe_id = cur.lastrowid | ||
| for ing in recipe.get("consumed_items", []): | ||
| cur.execute( | ||
| "INSERT INTO recipe_ingredients (recipe_id, ingredient_id, quantity) VALUES (?, ?, ?)", | ||
| (recipe_id, ing.get("id"), ing.get("quantity")), | ||
| ) | ||
| for qty, chance in recipe.get("possibilities", {}).items(): | ||
| cur.execute( | ||
| "INSERT INTO recipe_possibilities (recipe_id, quantity, chance) VALUES (?, ?, ?)", | ||
| (recipe_id, int(qty), chance), | ||
| ) | ||
| conn.commit() | ||
|
|
||
| cur.executescript( | ||
| """ | ||
| CREATE INDEX IF NOT EXISTS idx_items_name ON items(name); | ||
| CREATE INDEX IF NOT EXISTS idx_recipes_item ON recipes(item_id); | ||
| CREATE INDEX IF NOT EXISTS idx_ingredients_ing ON recipe_ingredients(ingredient_id); | ||
| """ | ||
| ) | ||
| conn.commit() | ||
| conn.close() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description="Convert crafting_data.json to SQLite database") | ||
| parser.add_argument("json_path", nargs="?", default="BitPlanner/crafting_data.json", help="Path to crafting_data.json") | ||
| parser.add_argument("db_path", nargs="?", default="crafting_data.db", help="Output SQLite database path") | ||
| args = parser.parse_args() | ||
|
|
||
| build_database(Path(args.json_path), Path(args.db_path)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import argparse | ||
| import sqlite3 | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def query_prerequisites(conn: sqlite3.Connection, item_name: str): | ||
| cur = conn.cursor() | ||
| cur.execute( | ||
| """ | ||
| WITH RECURSIVE deps(id, name, depth, path) AS ( | ||
| SELECT id, name, 0, ',' || id || ',' FROM items WHERE name LIKE ? | ||
| UNION ALL | ||
| SELECT ri.ingredient_id, i.name, depth + 1, path || ri.ingredient_id || ',' | ||
| FROM deps | ||
| JOIN recipes r ON r.item_id = deps.id | ||
| JOIN recipe_ingredients ri ON ri.recipe_id = r.id | ||
| JOIN items i ON i.id = ri.ingredient_id | ||
| WHERE path NOT LIKE '%,' || ri.ingredient_id || ',%' | ||
| ) | ||
| SELECT id, name, depth FROM deps ORDER BY depth, name | ||
| """, | ||
| (item_name,) | ||
| ) | ||
| for item_id, name, depth in cur.fetchall(): | ||
| print(" " * depth + f"{name} (#{item_id})") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description="Query prerequisites for an item") | ||
| parser.add_argument("item", help="Item name or substring to search") | ||
| parser.add_argument("db_path", nargs="?", default="crafting_data.db", help="Path to SQLite database") | ||
| args = parser.parse_args() | ||
|
|
||
| conn = sqlite3.connect(Path(args.db_path)) | ||
| query_prerequisites(conn, args.item) | ||
| conn.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not experienced in SQL, but as I understand
INTEGERmeans Int32. It doesn't fit, items IDs in BitPlanner scripts are expected to be in range of UInt64 (BIGINT UNSIGNED), by using Int32 you will get overflow on cargo items.