rps fix
This commit is contained in:
parent
6d76d5134c
commit
170b86d4bd
1 changed files with 44 additions and 42 deletions
42
RPS/rps.go
42
RPS/rps.go
|
@ -1,7 +1,6 @@
|
|||
package RPS
|
||||
|
||||
import (
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
|
@ -9,8 +8,6 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
gasDecimals = 1_0000_0000
|
||||
initialBalance = 3000
|
||||
zaCoinHashKey = "zaCoinHash"
|
||||
)
|
||||
|
||||
|
@ -25,7 +22,6 @@ func _deploy(data interface{}, isUpdate bool) {
|
|||
return
|
||||
}
|
||||
|
||||
// Parse hash of forint contract from incoming data
|
||||
args := data.(struct {
|
||||
zaCoinHash interop.Hash160
|
||||
})
|
||||
|
@ -34,30 +30,39 @@ func _deploy(data interface{}, isUpdate bool) {
|
|||
panic("invalid hash of zaCoin contract")
|
||||
}
|
||||
|
||||
|
||||
ctx := storage.GetContext()
|
||||
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
|
||||
}
|
||||
|
||||
func PlayRPS(playerChoice string) {
|
||||
func PlayRPS(playerChoice string, ctx storage.Context, playerOwner interop.Hash160, bet int) {
|
||||
|
||||
computerChoice := (runtime.GetRandom() % 3) + 1
|
||||
if computerChoice == 1 {
|
||||
computerChoice := "rock"
|
||||
} else if computerChoice == 2 {
|
||||
computerChoice := "paper"
|
||||
} else {
|
||||
computerChoice := "scissors"
|
||||
if playerChoice != "rock" && playerChoice != "paper" && playerChoice != "scissors" {
|
||||
panic("invalid player choice")
|
||||
}
|
||||
if bet <= 0 {
|
||||
panic("bet must be positive")
|
||||
}
|
||||
|
||||
result := isWinner(playerChoice, computerChoice)
|
||||
computerChoice := (runtime.GetRandom() % 3) + 1
|
||||
|
||||
var computerChoiceString string
|
||||
switch computerChoice {
|
||||
case 0:
|
||||
computerChoiceString = "rock"
|
||||
case 1:
|
||||
computerChoiceString = "paper"
|
||||
case 2:
|
||||
computerChoiceString = "scissors"
|
||||
}
|
||||
|
||||
result := isWinner(playerChoice, computerChoiceString)
|
||||
|
||||
if result.tie {
|
||||
return
|
||||
panic("game tied: player chose " + playerChoice + ", computer chose " + computerChoiceString)
|
||||
} else if result.win {
|
||||
changePlayerBalance(ctx, playerOwner, bet)
|
||||
} else {
|
||||
changePlayerBalance(ctx, playerOwner, -bet)
|
||||
panic("player lost: player chose " + playerChoice + ", computer chose " + computerChoiceString)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +87,6 @@ func isWinner(playerChoice, computerChoice string) Result {
|
|||
return Result{lose: true}
|
||||
}
|
||||
|
||||
|
||||
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
|
||||
ctx := storage.GetContext()
|
||||
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
|
||||
|
@ -100,15 +104,13 @@ func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balan
|
|||
var from, to interop.Hash160
|
||||
var transferAmount int
|
||||
if balanceChange > 0 {
|
||||
// Transfer funds from contract to player owner
|
||||
from = playerContract
|
||||
to = playerOwner
|
||||
transferAmount = balanceChange
|
||||
} else {
|
||||
// Transfer funds from player owner to contract
|
||||
from = playerOwner
|
||||
to = playerContract
|
||||
transferAmount = -balanceChange // We flip sender/receiver, but keep amount positive
|
||||
transferAmount = -balanceChange
|
||||
}
|
||||
|
||||
transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool)
|
||||
|
|
Loading…
Reference in a new issue