Skip to content

Commit

Permalink
Fast Break - Address Mapping (#242)
Browse files Browse the repository at this point in the history
* added helper mapping for third parties & oracle

* assets.go
  • Loading branch information
HouseOfHufflepuff authored Feb 7, 2024
1 parent b5e889a commit 4ae49a4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
9 changes: 9 additions & 0 deletions contracts/FastBreak.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub contract FastBreak: NonFungibleToken {
access(self) let fastBreakGameByID: {String: FastBreakGame}
access(self) let fastBreakPlayerByID: {UInt64: PlayerData}
access(self) let playerAccountMapping: {UInt64: Address}
access(self) let accountPlayerMapping: {Address: UInt64}

/// A top-level Fast Break Run, the container for Fast Break Games
/// A Fast Break Run contains many Fast Break games & is a mini-season.
Expand Down Expand Up @@ -388,6 +389,7 @@ pub contract FastBreak: NonFungibleToken {
/// Update player address mapping
if let ownerAddress = self.owner?.address {
FastBreak.playerAccountMapping[self.id] = ownerAddress
FastBreak.accountPlayerMapping[ownerAddress] = self.id
}

/// Validate Top Shots
Expand Down Expand Up @@ -455,6 +457,12 @@ pub contract FastBreak: NonFungibleToken {
}
}

/// Get a Fast Break Run by Id
///
pub fun getPlayerIdByAccount(accountAddress: Address): UInt64 {
return FastBreak.accountPlayerMapping[accountAddress]!
}

/// Validate Fast Break Submission topShots
///
pub fun validatePlaySubmission(fastBreakGame: FastBreakGame, topShots: [UInt64]): Bool {
Expand Down Expand Up @@ -797,6 +805,7 @@ pub contract FastBreak: NonFungibleToken {
self.fastBreakGameByID = {}
self.fastBreakPlayerByID = {}
self.playerAccountMapping = {}
self.accountPlayerMapping = {}

let oracle <- create FastBreakDaemon()
self.account.save(<-oracle, to: self.OracleStoragePath)
Expand Down
6 changes: 3 additions & 3 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/go/test/fast_break_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func TestFastBreak(t *testing.T) {
arg1Err := tx.AddArgument(cdcState)
assert.Nil(t, arg1Err)

// winner
arg2Err := tx.AddArgument(cadence.NewUInt64(playerId))
assert.Nil(t, arg2Err)

Expand All @@ -435,7 +436,7 @@ func TestFastBreak(t *testing.T) {
arg0Err := tx.AddArgument(cdcId)
assert.Nil(t, arg0Err)

arg1Err := tx.AddArgument(cadence.NewUInt64(playerId))
arg1Err := tx.AddArgument(cadence.NewAddress(jerAddress))
assert.Nil(t, arg1Err)

arg2Err := tx.AddArgument(cadence.NewUInt64(100))
Expand All @@ -454,7 +455,7 @@ func TestFastBreak(t *testing.T) {
t,
b,
templates.GenerateGetPlayerScoreScript(env),
[][]byte{jsoncdc.MustEncode(cdcId), jsoncdc.MustEncode(cadence.UInt64(playerId))},
[][]byte{jsoncdc.MustEncode(cdcId), jsoncdc.MustEncode(cadence.NewAddress(jerAddress))},
)
assert.Equal(t, cadence.NewUInt64(100), result)
})
Expand Down
7 changes: 5 additions & 2 deletions transactions/fastbreak/oracle/score_fast_break_submission.cdc
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import FastBreak from 0xFASTBREAKADDRESS

transaction(fastBreakGameID: String, playerId: UInt64, points: UInt64, win: Bool) {
transaction(fastBreakGameID: String, playerAddress: Address, points: UInt64, win: Bool) {

let oracleRef: &FastBreak.FastBreakDaemon
let playerId: UInt64

prepare(acct: AuthAccount) {
self.oracleRef = acct.borrow<&FastBreak.FastBreakDaemon>(from: FastBreak.OracleStoragePath)
?? panic("could not borrow a reference to the oracle resource")

self.playerId = FastBreak.getPlayerIdByAccount(accountAddress: playerAddress)
}

execute {

self.oracleRef.updateFastBreakScore(
fastBreakGameID: fastBreakGameID,
playerId: playerId,
playerId: self.playerId,
points: points,
win: win
)
Expand Down
3 changes: 2 additions & 1 deletion transactions/fastbreak/scripts/get_player_score.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FastBreak from 0xFASTBREAKADDRESS

pub fun main(id: String, playerId: UInt64): UInt64 {
pub fun main(id: String, playerAddress: Address): UInt64 {
let playerId = FastBreak.getPlayerIdByAccount(accountAddress: playerAddress)
let fastBreak = FastBreak.getFastBreakGame(id: id)!
let submission : FastBreak.FastBreakSubmission = fastBreak.getFastBreakSubmissionByPlayerId(playerId: playerId)!

Expand Down

0 comments on commit 4ae49a4

Please sign in to comment.