RingOfRaces/Storage/Database.gd

109 lines
3.1 KiB
GDScript3
Raw Normal View History

2021-04-08 19:43:34 +00:00
extends Node
2023-03-25 20:42:54 +00:00
#const SQLite
2021-04-08 19:43:34 +00:00
2021-04-15 20:14:31 +00:00
var path = "res://Savegames/"
2021-04-08 19:43:34 +00:00
var db_name = "RingOfRaces"
var db = null
var verbose = true
var host = null
# Called when the node enters the scene tree for the first time.
func _ready():
host = OS.get_name()
2021-04-08 20:58:04 +00:00
class Inventory_structure:
var id = 0
var item_id = 0
var item_name = "item_name"
var amount = 0
var shortdesc = "shortdesc"
2023-03-25 20:42:54 +00:00
func _init(id,item_id,item_name,amount,shortdesc):
2021-04-08 20:58:04 +00:00
self.id = id
self.item_id = item_id
self.item_name = item_name
self.amount = amount
self.shortdesc = shortdesc
2021-04-08 19:43:34 +00:00
func CreateWorldDatabase():
print("Creating new database")
2021-04-08 20:58:04 +00:00
#Inventory
2021-04-08 19:43:34 +00:00
var player_inventory : Dictionary = Dictionary()
player_inventory["id"] = {"data_type":"int", "primary_key": true, "not_null": true} #slot id
player_inventory["item_id"] = {"data_type":"int", "not_null": true} #item id
player_inventory["item_name"] = {"data_type":"text", "not_null": true} #item name
player_inventory["amount"] = {"data_type":"int", "not_null": true} #amount
player_inventory["shortdesc"] = {"data_type":"char(80)", "not_null": true} #short description
2021-04-08 20:58:04 +00:00
self.db.create_table("player_inventory", player_inventory)
2021-04-08 19:43:34 +00:00
var items : Dictionary = Dictionary()
for i in range(40):
items["id"] = i
items["item_id"] = 0
items["item_name"] = "No Item"
items["amount"] = 0
items["shortdesc"] = "No item here"
# Insert a new row in the table
2021-04-08 20:58:04 +00:00
self.db.insert_row("player_inventory", items)
2021-04-08 19:43:34 +00:00
items.clear()
2021-04-23 20:16:43 +00:00
#interaction_map
2021-04-08 20:58:04 +00:00
#Other world data
2021-04-08 19:43:34 +00:00
func OpenConnection():
2021-04-08 20:58:04 +00:00
if(str(OS.get_name()) == "Android"):
2023-03-25 20:42:54 +00:00
DirAccess.make_dir_absolute("user://Savegames/")
2021-04-15 20:14:31 +00:00
path += Global.dbname
2021-04-08 19:43:34 +00:00
self.db = SQLite.new()
self.db.path = path
var create = false
print(path)
# This does not seem to work. The file is in the right place, but being recreated everytime. The file is findable in Res:// and C:/ .. But not after the user folder
2023-03-25 20:42:54 +00:00
if ResourceLoader.exists(path):
print("Database does not exist. Creating a new one.")
2021-04-08 19:43:34 +00:00
create = true
self.db.open_db()
if create:
CreateWorldDatabase()
func OpenConnectionIfClosed():
if self.db == null:
OpenConnection()
2021-04-08 20:58:04 +00:00
func CloseConnection():
if self.db != null:
self.db.commit()
self.db.close()
self.db = null
2021-04-08 19:43:34 +00:00
func GetInventoryItems():
OpenConnectionIfClosed()
2021-04-08 20:58:04 +00:00
return db.select_rows("player_inventory", "",["*"])
2021-04-08 19:43:34 +00:00
func SaveInventory(player_inventory_items):
print("Now on inventory save file")
if(player_inventory_items == null or len(player_inventory_items) != 40):
Global.Log("Bad inventory save!", 3)
OpenConnectionIfClosed()
2021-04-08 20:58:04 +00:00
var items : Dictionary = Dictionary()
for item in player_inventory_items:
if(item['item_id'] == 0):
continue
items["id"] = item["id"]
items["item_id"] = item["item_id"]
items["item_name"] = item["item_name"]
items["amount"] = item["amount"]
items["shortdesc"] = item["shortdesc"]
#self.db.query("select * from player_inventory")
self.db.update_rows("player_inventory", "id == " + str(items['id']),items)
#items.clear()
Global.Log("Inventory save succeeded", 1)
2021-04-08 19:43:34 +00:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass