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