Skip to content

Instantly share code, notes, and snippets.

@harry830622
Created February 27, 2022 06:25
Show Gist options
  • Save harry830622/fb9495975fa20fd641df28555bda97fc to your computer and use it in GitHub Desktop.
Save harry830622/fb9495975fa20fd641df28555bda97fc to your computer and use it in GitHub Desktop.
// Admin 是管理者的 resource,
// 讓管理者可以創造新的 play、set、series
pub resource Admin {
// 創造新的 play,
// 並發出創造新 play 的 event
pub fun createPlay(metadata: {String: String}): UInt32 {
// Create the new Play
var newPlay = Play(metadata: metadata)
let newID = newPlay.playID
// Increment the ID so that it isn't used again
TopShot.nextPlayID = TopShot.nextPlayID + UInt32(1)
emit PlayCreated(id: newPlay.playID, metadata: metadata)
// Store it in the contract storage
TopShot.playDatas[newID] = newPlay
return newID
}
// 創造新的 set,
// 並發出創造新 set 的 event
pub fun createSet(name: String): UInt32 {
// Create the new Set
var newSet <- create Set(name: name)
// Increment the setID so that it isn't used again
TopShot.nextSetID = TopShot.nextSetID + UInt32(1)
let newID = newSet.setID
emit SetCreated(setID: newSet.setID, series: TopShot.currentSeries)
// Store it in the sets mapping field
TopShot.sets[newID] <-! newSet
return newID
}
// 借用某個 set 的讀寫權限,
// 管理者就可以透過 set 做各種操作像是加入新 play 到 set 中
pub fun borrowSet(setID: UInt32): &Set {
pre {
TopShot.sets[setID] != nil: "Cannot borrow Set: The Set doesn't exist"
}
// Get a reference to the Set and return it
// use `&` to indicate the reference to the object and type
return &TopShot.sets[setID] as &Set
}
// 開始新的 series,
// 並發出開始新 series 的 event
// 需要注意的是這會直接結束目前的 series
pub fun startNewSeries(): UInt32 {
// End the current series and start a new one
// by incrementing the TopShot series number
TopShot.currentSeries = TopShot.currentSeries + UInt32(1)
emit NewSeriesStarted(newCurrentSeries: TopShot.currentSeries)
return TopShot.currentSeries
}
pub fun createNewAdmin(): @Admin {
return <-create Admin()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment