簡體   English   中英

在 flutter 和 SQL 中創建商店/物品系統,如何正確構建游戲內/應用程序物品?

[英]Creating a shop/ item system in flutter with SQL, how to structure in-game/app items properly?

SQL 和數據庫的新功能,創建一個包含不同項目的應用程序內商店。

  void _createTableItems(Batch batch) {
    batch.execute('DROP TABLE IF EXISTS Items');
    batch.execute('''CREATE TABLE boughtItems ( 
    id INTEGER PRIMARY KEY,
    price INTEGER
  )''');
  }

class Item {
  Item({required this.id, required this.price, required this.title});
  String title;
  int id;
  int price;
}


List<Item> availableProducts = [WeaponItem(id: 0, strength: 5), WeaponItem(id: 1, strength: 7), StableItem(id: 2, speed: 4), FoodItem(id: 3, rev: 7)]

我現在幾乎擁有最基本的結構。 當我需要獲取產品時,我所做的就是在數據庫查詢中搜索可用產品列表中的 ID。

  Future<List<Item>> getBought() async {
await database;

List products = await _database!.query("Items");

List<Item> result = [];

for (var element in products) {
  result.add(availableProducts.where((e) => e.id == element["id"]).first);
}

return result;

}

這是一種可以接受的方法嗎?

我擔心的是項目類型的混合。

我有點迷路,因為我可以做很多事情。 我應該創建一個不同的表來保存每個單獨項目的所有屬性嗎? 我應該在表中添加一個類型字符串來區分不同的項目嗎?

我會為示例產品創建一個表。 此表包含有關所有產品共有的產品的一般信息(id、名稱、product_number、info)。 如果您想要每種產品類型的附加和不同信息,我會添加一個 fk_type,它引用存儲不同產品類型的 product_type(id, name) 表。 然后我會為每種產品類型創建一個附加表。 例如:product_weapon(id, size, ammunition, reload_time ), product_chair(id, high, depth, weight)。 但您也可以只使用簡單的產品表並添加可以存儲 jsons、xmls、css 文件的一般描述字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM