Created
February 27, 2022 06:24
-
-
Save harry830622/dc4b9ee912793c0146d046b477c1c393 to your computer and use it in GitHub Desktop.
This file contains 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
// Set resource 主要掌控一個系列要加入什麼 play 的卡片, | |
// 以及要 retire 哪張卡片或是 lock 整個系列, | |
// 不過當然只能由 Admin 操作這些動作 | |
pub resource Set { | |
// 每個 set 有一個獨特的 ID 表示 | |
pub let setID: UInt32 | |
// 紀錄這個系列中有哪些 play | |
access(contract) var plays: [UInt32] | |
// 紀錄某個 play 是否已經被 retire 了 | |
// 是一個 play ID -> 是否 retire 的 mapping | |
access(contract) var retired: {UInt32: Bool} | |
// 表示這個系列是否鎖住, | |
// 鎖住就代表這個系列不能再加入任何新的 play, | |
// 但已經加入的 play 還是可以 mint | |
pub var locked: Bool | |
// 紀錄一個 play 到目前為止已經 mint 了幾張卡片 | |
// 是一個 play ID -> 卡片數量的 mapping | |
access(contract) var numberMintedPerPlay: {UInt32: UInt32} | |
init(name: String) { | |
self.setID = TopShot.nextSetID | |
self.plays = [] | |
self.retired = {} | |
self.locked = false | |
self.numberMintedPerPlay = {} | |
// Create a new SetData for this Set and store it in contract storage | |
TopShot.setDatas[self.setID] = SetData(name: name) | |
} | |
// 加入某個新的 play 到 set 中, | |
// 並發出加入新 play 的 event | |
pub fun addPlay(playID: UInt32) { | |
pre { | |
TopShot.playDatas[playID] != nil: "Cannot add the Play to Set: Play doesn't exist." | |
!self.locked: "Cannot add the play to the Set after the set has been locked." | |
self.numberMintedPerPlay[playID] == nil: "The play has already beed added to the set." | |
} | |
// Add the Play to the array of Plays | |
self.plays.append(playID) | |
// Open the Play up for minting | |
self.retired[playID] = false | |
// Initialize the Moment count to zero | |
self.numberMintedPerPlay[playID] = 0 | |
emit PlayAddedToSet(setID: self.setID, playID: playID) | |
} | |
// 加入多個新的 play 到 set 中 | |
pub fun addPlays(playIDs: [UInt32]) { | |
for play in playIDs { | |
self.addPlay(playID: play) | |
} | |
} | |
// retire 某個 play, | |
// 並發出 retire 舊 play 的 event | |
pub fun retirePlay(playID: UInt32) { | |
pre { | |
self.retired[playID] != nil: "Cannot retire the Play: Play doesn't exist in this set!" | |
} | |
if !self.retired[playID]! { | |
self.retired[playID] = true | |
emit PlayRetiredFromSet(setID: self.setID, playID: playID, numMoments: self.numberMintedPerPlay[playID]!) | |
} | |
} | |
// retire 所有 play | |
pub fun retireAll() { | |
for play in self.plays { | |
self.retirePlay(playID: play) | |
} | |
} | |
// 將這個 set 鎖起來 | |
pub fun lock() { | |
if !self.locked { | |
self.locked = true | |
emit SetLocked(setID: self.setID) | |
} | |
} | |
// mint 新卡片,並回傳新卡片 resource | |
pub fun mintMoment(playID: UInt32): @NFT { | |
pre { | |
self.retired[playID] != nil: "Cannot mint the moment: This play doesn't exist." | |
!self.retired[playID]!: "Cannot mint the moment from this play: This play has been retired." | |
} | |
// Gets the number of Moments that have been minted for this Play | |
// to use as this Moment's serial number | |
let numInPlay = self.numberMintedPerPlay[playID]! | |
// Mint the new moment | |
let newMoment: @NFT <- create NFT(serialNumber: numInPlay + UInt32(1), | |
playID: playID, | |
setID: self.setID) | |
// Increment the count of Moments minted for this Play | |
self.numberMintedPerPlay[playID] = numInPlay + UInt32(1) | |
return <-newMoment | |
} | |
// mint 多個新卡片,並回傳包含這些卡片的 collection resource | |
pub fun batchMintMoment(playID: UInt32, quantity: UInt64): @Collection { | |
let newCollection <- create Collection() | |
var i: UInt64 = 0 | |
while i < quantity { | |
newCollection.deposit(token: <-self.mintMoment(playID: playID)) | |
i = i + UInt64(1) | |
} | |
return <-newCollection | |
} | |
pub fun getPlays(): [UInt32] { | |
return self.plays | |
} | |
pub fun getRetired(): {UInt32: Bool} { | |
return self.retired | |
} | |
pub fun getNumMintedPerPlay(): {UInt32: UInt32} { | |
return self.numberMintedPerPlay | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment