forked from Web3N3/web3-course
Refactor player inventory management
Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
This commit is contained in:
parent
fdb6e9fa8b
commit
1fa8f126da
3 changed files with 20 additions and 40 deletions
|
@ -1,5 +0,0 @@
|
|||
name: "Lottery"
|
||||
supportedstandards: []
|
||||
events:
|
||||
permissions:
|
||||
- methods: []
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue