Contract con_crafter_v5


Contract Code


  
1 I = importlib
2 random.seed()
3
4 nft_collection_contract = Variable()
5 owner = Variable()
6 craftable_things = Hash(default_value=0)
7 amount_crafted = Hash(default_value=0)
8 currency_contract = Variable()
9
10 @construct
11 def seed():
12 nft_collection_contract.set("con_lamden_realm_v2")
13 currency_contract.set("con_trt")
14 owner.set(ctx.caller)
15
16 @export
17 def craft(item_type: str):
18 assert craftable_things[item_type] != 0, "This item type is not craftable"
19 cost = craftable_things[item_type]["cost"]
20 I.import_module(currency_contract.get()).transfer_from(amount=cost, to="Burned", main_account=ctx.caller)
21
22 attributes = craftable_things[item_type]["attributes"]
23 final_metadata = {}
24 for attribute in attributes:
25 if isinstance(attributes[attribute], dict):
26 # take into account the preferred attribute and weight it based on if the min or max is preferred without using uniform
27 min_value = attributes[attribute]["min"]
28 max_value = attributes[attribute]["max"]
29 preferred = attributes[attribute]["preferred"]
30 # generate a random int between min and max
31 # preferred is what is better for the attribute and has a lower chance of being generated
32 # so we weight the random number based on the preferred using linear interpolation
33 if preferred == "min":
34 weight = 0.5
35 sample = random.random() ** weight
36 else:
37 weight = 1.5
38 sample = 1 - (1 - random.random()) ** weight
39
40 # Scale and shift the random number to the range of min_value to max_value
41 final_metadata[attribute] = round((1 - sample) * min_value + sample * max_value)
42
43
44 elif isinstance(attributes[attribute], list):
45 final_metadata[attribute] = random.choice(attributes[attribute])
46 image = random.choice(craftable_things[item_type]["images"])
47 final_metadata["type"] = item_type
48 amount_crafted[item_type] += 1
49 # Name will be the item type, but uppercase first letter + the amount crafted
50 name = item_type.capitalize() + " " + str(amount_crafted[item_type])
51 I.import_module(nft_collection_contract.get()).mint_nft(name=name, description="Item crafted by {}".format(ctx.caller), ipfs_image_url=image, metadata=final_metadata, amount=1)
52 I.import_module(nft_collection_contract.get()).transfer(amount=1, to=ctx.caller, name=name)
53
54 @export
55 def set_nft_collection_contract(contract: str):
56 assert ctx.caller == owner.get(), "Only the owner can set the NFT collection contract"
57 nft_collection_contract.set(contract)
58
59 @export
60 def modify_craftable_things(item_type: str, cost: float, attributes: dict, images: list):
61 assert ctx.caller == owner.get(), "Only the owner can modify the craftable things"
62 # ex item_type = "weapon"
63 # ex attributes = {
64 # "cooldown": {"min": 0.1, "max": 2, "preferred": "min"},
65 # "damage" : {"min": 1, "max": 80, "preferred": "max"},
66 # "effect" : ["cosmicball", "natureball", "fireball"],
67 # "max_distance" : {"min": 100, "max": 1000, "preferred": "max"}
68 # }
69 # ex images = ["https://nftstorage.link/ipfs/bafybeihpmoirvpgnt7m6lytm5xarhxncbugorg43pvmvgf5o7z3vegelbi/file", "https://nftstorage.link/ipfs/bafybeiaputppri6gm676y75kjtgd2we4sbm2dl2rmcshafc5shmfpgekmy/file", "https://nftstorage.link/ipfs/bafybeiez5v3zs2fcctzytchlwcgryu2k342sz2txypwunzgb7jdqtykwx4/file"]
70 craftable_things[item_type] = {"attributes": attributes, "cost": cost, "images": images}

Byte Code

e3000000000000000000000000060000004000000073a000000065005a0165026a038300010065046400640164028d025a0565046400640364028d025a06650764046400640564068d035a08650764046400640764068d035a0965046400640864028d025a0a6409640a84005a0b650c64008301650d640b9c01640c640d840483015a0e650c64008301650d640e9c01640f6410840483015a0f650c64008301650d65106511651264119c0464126413840483015a13641453002915da0e636f6e5f637261667465725f7635da176e66745f636f6c6c656374696f6e5f636f6e74726163742902da08636f6e7472616374da046e616d65da056f776e6572e900000000da10637261667461626c655f7468696e67732903da0d64656661756c745f76616c756572030000007204000000da0e616d6f756e745f63726166746564da1163757272656e63795f636f6e7472616374630000000000000000000000000200000043000000732400000074006a0164018301010074026a0164028301010074036a0174046a05830101006400530029034eda13636f6e5f6c616d64656e5f7265616c6d5f7632da07636f6e5f7472742906da195f5f6e66745f636f6c6c656374696f6e5f636f6e7472616374da03736574da135f5f63757272656e63795f636f6e7472616374da075f5f6f776e6572da03637478da0663616c6c6572a90072130000007213000000da00da045f5f5f5f0e000000730600000000010a010a0172150000002901da096974656d5f747970656301000000000000000c0000000700000043000000738c01000074007c00190064016b037314740164028301820174007c001900640319007d0174026a0374046a05830083016a067c01640474076a0864058d03010074007c001900640619007d0269007d0378b67c0244005dae7d0474097c027c041900740a830272e07c027c041900640719007d057c027c041900640819007d067c027c041900640919007d077c0764076b0272a6740b640a83017d08740c6a0c83007c0813007d096e1c740b640b83017d08640c640c740c6a0c830018007c08130018007d09740d640c7c0918007c0514007c097c061400170083017c037c043c00715274097c027c041900740e83027252740c6a0f7c027c04190083017c037c043c0071525700740c6a0f74007c001900640d190083017d0a7c007c03640e3c0074107c0005001900640c370003003c007c006a118300640f1700741274107c001900830117007d0b74026a0374136a05830083016a147c0b64106a1574076a0883017c0a7c03640c64118d05010074026a0374136a05830083016a16640c74076a087c0b64128d0301006400530029134e72060000007a1f54686973206974656d2074797065206973206e6f7420637261667461626c65da04636f7374da064275726e65642903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e74da0a61747472696275746573da036d696eda036d6178da097072656665727265647a03302e357a03312e35e901000000da06696d61676573da0474797065fa01207a124974656d2063726166746564206279207b7d29057204000000da0b6465736372697074696f6eda0e697066735f696d6167655f75726cda086d65746164617461721900000029037219000000721a00000072040000002917da125f5f637261667461626c655f7468696e6773da0e417373657274696f6e4572726f72da0149da0d696d706f72745f6d6f64756c65720f000000da03676574da0d7472616e736665725f66726f6d72110000007212000000da0a6973696e7374616e6365da0464696374da07646563696d616cda0672616e646f6dda05726f756e64da046c697374da0663686f696365da105f5f616d6f756e745f63726166746564da0a6361706974616c697a65da03737472720d000000da086d696e745f6e6674da06666f726d6174da087472616e73666572290c72160000007217000000721c000000da0e66696e616c5f6d65746164617461da09617474726962757465da096d696e5f76616c7565da096d61785f76616c7565721f000000da06776569676874da0673616d706c65da05696d6167657204000000721300000072130000007214000000da05637261667414000000733c000000000206010e010c0110010c010c0104010a010e010c010c010c01080108010e02080114010c0112010e011601120108011001180110010a010c011001724100000029017203000000630100000000000000010000000200000043000000732400000074006a0174026a0383006b027316740464018301820174056a067c00830101006400530029024e7a324f6e6c7920746865206f776e65722063616e2073657420746865204e465420636f6c6c656374696f6e20636f6e74726163742907721100000072120000007210000000722b0000007228000000720d000000720e00000029017203000000721300000072130000007214000000da1b7365745f6e66745f636f6c6c656374696f6e5f636f6e74726163743700000073060000000002100106017242000000290472160000007217000000721c0000007221000000630400000000000000040000000400000043000000732a00000074006a0174026a0383006b02731674046401830182017c027c017c0364029c0374057c003c006400530029034e7a2e4f6e6c7920746865206f776e65722063616e206d6f646966792074686520637261667461626c65207468696e67732903721c000000721700000072210000002906721100000072120000007210000000722b00000072280000007227000000290472160000007217000000721c0000007221000000721300000072130000007214000000da176d6f646966795f637261667461626c655f7468696e67733e0000007308000000000310010601040172430000004e2914da09696d706f72746c696272290000007230000000da0473656564da085661726961626c65720d0000007210000000da044861736872270000007234000000720f0000007215000000da085f5f6578706f7274723600000072410000007242000000da05666c6f6174722e000000723200000072430000007213000000721300000072130000007214000000da083c6d6f64756c653e01000000732400000004010801040108010c010601080106010801040108030806060110220601100606010601