From 0d7ac8226b7dc588adc4d23f789df94bad8c9eb7 Mon Sep 17 00:00:00 2001 From: blockchainer_cr Date: Tue, 3 Dec 2024 11:28:04 -0600 Subject: [PATCH] [feat] Create an evolution PoC model (#97) --- .gitignore | 2 +- .tool-versions | 2 +- .../models/bytebeasts-Evolution-52744a50.json | 419 ++++++++++++++++++ .../models/bytebeasts-Evolution-52744a50.toml | 31 ++ src/lib.cairo | 1 + src/models/evolution.cairo | 51 +++ 6 files changed, 504 insertions(+), 2 deletions(-) create mode 100644 manifests/dev/base/abis/models/bytebeasts-Evolution-52744a50.json create mode 100644 manifests/dev/base/models/bytebeasts-Evolution-52744a50.toml create mode 100644 src/models/evolution.cairo diff --git a/.gitignore b/.gitignore index 1de5659..eb5a316 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -target \ No newline at end of file +target diff --git a/.tool-versions b/.tool-versions index 366991b..342a21f 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -scarb 2.7.0 dojo 1.0.0-alpha.5 +scarb 2.7.0 diff --git a/manifests/dev/base/abis/models/bytebeasts-Evolution-52744a50.json b/manifests/dev/base/abis/models/bytebeasts-Evolution-52744a50.json new file mode 100644 index 0000000..4fc097d --- /dev/null +++ b/manifests/dev/base/abis/models/bytebeasts-Evolution-52744a50.json @@ -0,0 +1,419 @@ +[ + { + "type": "impl", + "name": "DojoModelImpl", + "interface_name": "dojo::model::model::IModel" + }, + { + "type": "struct", + "name": "core::byte_array::ByteArray", + "members": [ + { + "name": "data", + "type": "core::array::Array::" + }, + { + "name": "pending_word", + "type": "core::felt252" + }, + { + "name": "pending_word_len", + "type": "core::integer::u32" + } + ] + }, + { + "type": "enum", + "name": "core::option::Option::", + "variants": [ + { + "name": "Some", + "type": "core::integer::u32" + }, + { + "name": "None", + "type": "()" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::" + } + ] + }, + { + "type": "struct", + "name": "dojo::model::layout::FieldLayout", + "members": [ + { + "name": "selector", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::model::layout::Layout" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::" + } + ] + }, + { + "type": "enum", + "name": "dojo::model::layout::Layout", + "variants": [ + { + "name": "Fixed", + "type": "core::array::Span::" + }, + { + "name": "Struct", + "type": "core::array::Span::" + }, + { + "name": "Tuple", + "type": "core::array::Span::" + }, + { + "name": "Array", + "type": "core::array::Span::" + }, + { + "name": "ByteArray", + "type": "()" + }, + { + "name": "Enum", + "type": "core::array::Span::" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::" + } + ] + }, + { + "type": "struct", + "name": "dojo::model::introspect::Member", + "members": [ + { + "name": "name", + "type": "core::felt252" + }, + { + "name": "attrs", + "type": "core::array::Span::" + }, + { + "name": "ty", + "type": "dojo::model::introspect::Ty" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::" + } + ] + }, + { + "type": "struct", + "name": "dojo::model::introspect::Struct", + "members": [ + { + "name": "name", + "type": "core::felt252" + }, + { + "name": "attrs", + "type": "core::array::Span::" + }, + { + "name": "children", + "type": "core::array::Span::" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::<(core::felt252, dojo::model::introspect::Ty)>", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::<(core::felt252, dojo::model::introspect::Ty)>" + } + ] + }, + { + "type": "struct", + "name": "dojo::model::introspect::Enum", + "members": [ + { + "name": "name", + "type": "core::felt252" + }, + { + "name": "attrs", + "type": "core::array::Span::" + }, + { + "name": "children", + "type": "core::array::Span::<(core::felt252, dojo::model::introspect::Ty)>" + } + ] + }, + { + "type": "struct", + "name": "core::array::Span::", + "members": [ + { + "name": "snapshot", + "type": "@core::array::Array::" + } + ] + }, + { + "type": "enum", + "name": "dojo::model::introspect::Ty", + "variants": [ + { + "name": "Primitive", + "type": "core::felt252" + }, + { + "name": "Struct", + "type": "dojo::model::introspect::Struct" + }, + { + "name": "Enum", + "type": "dojo::model::introspect::Enum" + }, + { + "name": "Tuple", + "type": "core::array::Span::" + }, + { + "name": "Array", + "type": "core::array::Span::" + }, + { + "name": "ByteArray", + "type": "()" + } + ] + }, + { + "type": "interface", + "name": "dojo::model::model::IModel", + "items": [ + { + "type": "function", + "name": "name", + "inputs": [], + "outputs": [ + { + "type": "core::byte_array::ByteArray" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "namespace", + "inputs": [], + "outputs": [ + { + "type": "core::byte_array::ByteArray" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "tag", + "inputs": [], + "outputs": [ + { + "type": "core::byte_array::ByteArray" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "version", + "inputs": [], + "outputs": [ + { + "type": "core::integer::u8" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "selector", + "inputs": [], + "outputs": [ + { + "type": "core::felt252" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "name_hash", + "inputs": [], + "outputs": [ + { + "type": "core::felt252" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "namespace_hash", + "inputs": [], + "outputs": [ + { + "type": "core::felt252" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "unpacked_size", + "inputs": [], + "outputs": [ + { + "type": "core::option::Option::" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "packed_size", + "inputs": [], + "outputs": [ + { + "type": "core::option::Option::" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "layout", + "inputs": [], + "outputs": [ + { + "type": "dojo::model::layout::Layout" + } + ], + "state_mutability": "view" + }, + { + "type": "function", + "name": "schema", + "inputs": [], + "outputs": [ + { + "type": "dojo::model::introspect::Ty" + } + ], + "state_mutability": "view" + } + ] + }, + { + "type": "impl", + "name": "evolutionImpl", + "interface_name": "bytebeasts::models::evolution::Ievolution" + }, + { + "type": "struct", + "name": "bytebeasts::models::evolution::Evolution", + "members": [ + { + "name": "base_beast_id", + "type": "core::integer::u32" + }, + { + "name": "evolved_beast_id", + "type": "core::integer::u32" + }, + { + "name": "level_requirement", + "type": "core::integer::u32" + }, + { + "name": "required_battles", + "type": "core::integer::u32" + }, + { + "name": "required_item", + "type": "core::option::Option::" + } + ] + }, + { + "type": "interface", + "name": "bytebeasts::models::evolution::Ievolution", + "items": [ + { + "type": "function", + "name": "ensure_abi", + "inputs": [ + { + "name": "model", + "type": "bytebeasts::models::evolution::Evolution" + } + ], + "outputs": [], + "state_mutability": "view" + } + ] + }, + { + "type": "event", + "name": "bytebeasts::models::evolution::evolution::Event", + "kind": "enum", + "variants": [] + } +] \ No newline at end of file diff --git a/manifests/dev/base/models/bytebeasts-Evolution-52744a50.toml b/manifests/dev/base/models/bytebeasts-Evolution-52744a50.toml new file mode 100644 index 0000000..646038c --- /dev/null +++ b/manifests/dev/base/models/bytebeasts-Evolution-52744a50.toml @@ -0,0 +1,31 @@ +kind = "DojoModel" +class_hash = "0x2d40a7cd64e9c72b855b55fddaa9de2d4c80aadce9f8a4276bf3d859cb18a9c" +original_class_hash = "0x2d40a7cd64e9c72b855b55fddaa9de2d4c80aadce9f8a4276bf3d859cb18a9c" +abi = "manifests/dev/base/abis/models/bytebeasts-Evolution-52744a50.json" +tag = "bytebeasts-Evolution" +manifest_name = "bytebeasts-Evolution-52744a50" + +[[members]] +name = "base_beast_id" +type = "u32" +key = true + +[[members]] +name = "evolved_beast_id" +type = "u32" +key = false + +[[members]] +name = "level_requirement" +type = "u32" +key = false + +[[members]] +name = "required_battles" +type = "u32" +key = false + +[[members]] +name = "required_item" +type = "Option" +key = false diff --git a/src/lib.cairo b/src/lib.cairo index ae61d22..aa41f96 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -30,6 +30,7 @@ mod models { mod tournament; mod season; mod leaderboard; + mod evolution; } mod tests { diff --git a/src/models/evolution.cairo b/src/models/evolution.cairo new file mode 100644 index 0000000..c9a6241 --- /dev/null +++ b/src/models/evolution.cairo @@ -0,0 +1,51 @@ +use core::result::Result; + +#[derive(Drop, Copy, Serde)] +#[dojo::model] +struct Evolution { + #[key] + pub base_beast_id: u32, + pub evolved_beast_id: u32, + pub level_requirement: u32, + pub required_battles: u32, + pub required_item: Option +} + +mod rules { + /// Minimum rules for beast evolution + + /// Minimum level required before evolution is possible + const LEVEL_REQUIREMENT: u32 = 10; + /// Minimum number of battles required before evolution + const REQUIRED_BATTLES: u32 = 5; + /// Item ID required for evolution (special evolution stone) + const REQUIRED_ITEM: u32 = 1001; + +} + +#[generate_trait] +impl EvolutionImpl of EvolutionTrait { + fn can_evolve(self: Evolution) -> bool { + let is_valid_item: bool = match self.required_item { + Option::Some(val) => val == rules::REQUIRED_ITEM, + Option::None => true, + }; + + is_valid_item && + self.level_requirement > rules::LEVEL_REQUIREMENT && + self.required_battles > rules::REQUIRED_BATTLES + } + + fn evolve(ref self: Evolution) -> Result { + assert(self.base_beast_id != 0, 'Invalid base beast'); + + match self.can_evolve() { + true => { + // TODO: investigate how can we make It random + self.evolved_beast_id = self.base_beast_id * 10_u32; + Result::Ok(self.evolved_beast_id) + }, + false => Result::Err('Evolution requirements not met'), + } + } +}