diff --git a/l6/lottery/config.yml b/l6/lottery/config.yml deleted file mode 100644 index f7834a0..0000000 --- a/l6/lottery/config.yml +++ /dev/null @@ -1,5 +0,0 @@ -name: "Lottery" -supportedstandards: [] -events: -permissions: - - methods: [] diff --git a/l6/lottery/lottery_contract.go b/l6/lottery/lottery_contract.go deleted file mode 100644 index d975d20..0000000 --- a/l6/lottery/lottery_contract.go +++ /dev/null @@ -1,13 +0,0 @@ -package lottery - -import ( - "github.com/nspcc-dev/neo-go/pkg/interop/runtime" -) - -func IsWinner(playerName string) bool { - if playerName == "demo" { - return true - } - random := runtime.GetRandom() - return random < 100 -} diff --git a/l6/player/player_contract.go b/l6/player/player_contract.go index 0f36e34..d3f90b2 100644 --- a/l6/player/player_contract.go +++ b/l6/player/player_contract.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" "github.com/nspcc-dev/neo-go/pkg/interop/native/std" + "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/util" @@ -56,19 +57,20 @@ func Items(playerName string) []string { } func BuyItem(playerName string, itemName string) { - itemPrice := itemPrices[itemName] - ctx := storage.GetContext() player := getPlayer(ctx, playerName) if !runtime.CheckWitness(player.owner) { - panic("not authorized") + panic("player not witnessed") } + + itemPrice := itemPrices[itemName] if player.balance < itemPrice { panic("insufficient balance") } - player.balance -= itemPrice addItemToInventory(player, itemName) + player.balance -= itemPrice + savePlayer(ctx, playerName, player) } @@ -76,15 +78,12 @@ func SellItem(playerName string, itemName string) { ctx := storage.GetContext() player := getPlayer(ctx, playerName) if !runtime.CheckWitness(player.owner) { - panic("not authorized") + panic("player not witnessed") } - if !hasItemInInventory(player, itemName) { - panic("player has no specified item") - } - - player.balance += itemPrices[itemName] removeItemFromInventory(player, itemName) + player.balance += itemPrices[itemName] + savePlayer(ctx, playerName, player) } @@ -92,7 +91,7 @@ func BuyItemForGas(playerName string, lotID int, itemPrice int, merchantHash int ctx := storage.GetContext() player := getPlayer(ctx, playerName) if !runtime.CheckWitness(player.owner) { - panic("not authorized") + panic("player not witnessed") } itemName := contract.Call(merchantHash, "getItemName", contract.ReadOnly, lotID).(string) @@ -111,12 +110,9 @@ func SellItemForGas(playerName string, itemName string, itemPrice int, merchantH ctx := storage.GetContext() player := getPlayer(ctx, playerName) if !runtime.CheckWitness(player.owner) { - panic("not authorized") + panic("player not witnessed") } - if !hasItemInInventory(player, itemName) { - panic("player has no specified item") - } removeItemFromInventory(player, itemName) balanceBefore := gas.BalanceOf(runtime.GetScriptContainer().Sender) @@ -146,14 +142,13 @@ func savePlayer(ctx storage.Context, playerName string, player Player) { } func hasItemInInventory(player Player, itemName string) bool { - // Ugly loop, because the following standard syntax does not work: - // _, exists := player.itemCount[itemName] - for ownedName, count := range player.itemCount { - if ownedName == itemName { - return count > 0 - } + // Standard syntax for go map does not work: _, exists := player.itemCount[itemName] + // So, we use HASKEY command to do this: + hasKey := neogointernal.Opcode2("HASKEY", player.itemCount, itemName).(bool) + if !hasKey { + return false } - return false + return player.itemCount[itemName] > 0 } func addItemToInventory(player Player, itemName string) { @@ -164,6 +159,9 @@ func addItemToInventory(player Player, itemName string) { } func removeItemFromInventory(player Player, itemName string) { + if !hasItemInInventory(player, itemName) { + panic("player has no specified item") + } player.itemCount[itemName] -= 1 if player.itemCount[itemName] == 0 { delete(player.itemCount, itemName)