auction/RPS/rps.go

111 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-01-12 18:22:38 +00:00
package rps
2023-12-19 15:19:32 +00:00
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"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
const (
zaCoinHashKey = "zaCoinHash"
)
func _deploy(data interface{}, isUpdate bool) {
if isUpdate {
return
}
args := data.(struct {
zaCoinHash interop.Hash160
})
if len(args.zaCoinHash) != interop.Hash160Len {
2024-01-13 07:42:52 +00:00
panic("Invalid hash of zaCoin contract")
2024-01-08 22:08:11 +00:00
}
2023-12-19 15:19:32 +00:00
ctx := storage.GetContext()
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
}
2024-01-16 13:59:15 +00:00
func PlayRPS(playerChoice int, bet int) {
2024-01-12 18:22:38 +00:00
ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender
2024-01-16 13:59:15 +00:00
playerContract := runtime.GetExecutingScriptHash()
2024-01-12 18:22:38 +00:00
if bet <= 0 {
panic("Invalid bet amount")
}
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerBalance := contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
if playerBalance < bet {
panic("Insufficient funds")
}
2024-01-08 22:08:11 +00:00
2024-01-16 13:59:15 +00:00
if playerChoice <= 0 || playerChoice > 3 {
2024-01-08 22:08:11 +00:00
panic("invalid player choice")
}
2023-12-19 15:19:32 +00:00
computerChoice := (runtime.GetRandom() % 3) + 1
2024-01-16 13:59:15 +00:00
runtime.Notify("computerChoice", computerChoice)
result := isWinner(playerChoice, computerChoice)
if result == 1 {
changePlayerBalance(playerContract, playerOwner, bet)
runtime.Notify("gameResult", result)
} else if result == 0 {
changePlayerBalance(playerOwner, playerContract, bet)
runtime.Notify("gameResult", result)
} else {
runtime.Log("game tied")
runtime.Notify("gameResult", result)
2024-01-08 22:08:11 +00:00
}
2024-01-12 18:22:38 +00:00
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
2024-01-16 13:59:15 +00:00
runtime.Notify("playerBalance", playerBalance)
2023-12-19 15:19:32 +00:00
}
2024-01-16 13:59:15 +00:00
func isWinner(playerChoice int, computerChoice int) int {
2023-12-19 15:19:32 +00:00
if playerChoice == computerChoice {
2024-01-16 13:59:15 +00:00
return 2
2024-01-08 22:08:11 +00:00
}
2023-12-19 15:19:32 +00:00
2024-01-16 13:59:15 +00:00
if playerChoice == 1 && computerChoice == 3 {
return 1
2024-01-08 22:08:11 +00:00
}
2023-12-19 15:19:32 +00:00
2024-01-16 13:59:15 +00:00
if playerChoice == 3 && computerChoice == 2 {
return 1
2024-01-08 22:08:11 +00:00
}
2023-12-19 15:19:32 +00:00
2024-01-16 13:59:15 +00:00
if playerChoice == 2 && computerChoice == 1 {
return 1
2024-01-08 22:08:11 +00:00
}
2023-12-19 15:19:32 +00:00
2024-01-16 13:59:15 +00:00
return 0
2023-12-19 15:19:32 +00:00
}
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
ctx := storage.GetContext()
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(zaCoinHash) {
panic("only ZC is accepted")
}
}
2024-01-16 13:59:15 +00:00
func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
ctx := storage.GetContext()
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
2023-12-19 15:19:32 +00:00
2024-01-16 13:59:15 +00:00
transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
if !transferred {
panic("failed to transfer zaCoins")
}
2023-12-19 15:19:32 +00:00
}