auction/Craps/craps.go

97 lines
2.5 KiB
Go
Raw Permalink Normal View History

2023-11-17 12:41:59 +00:00
package Craps
import (
2023-12-06 20:13:23 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
2023-11-17 12:41:59 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
2023-12-06 20:13:23 +00:00
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
const (
zaCoinHashKey = "zaCoinHash"
2023-11-17 12:41:59 +00:00
)
2023-12-06 20:13:23 +00:00
func _deploy(data interface{}, isUpdate bool) {
if isUpdate {
return
2023-11-17 12:41:59 +00:00
}
2023-12-06 20:13:23 +00:00
args := data.(struct {
zaCoinHash interop.Hash160
})
2023-11-17 12:41:59 +00:00
2023-12-06 20:13:23 +00:00
if len(args.zaCoinHash) != interop.Hash160Len {
panic("invalid hash of zaCoin contract")
}
ctx := storage.GetContext()
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
}
func PlayCraps(bet int, firstSum int, secondSum int) {
ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender
2024-01-16 13:59:15 +00:00
playerContract := runtime.GetExecutingScriptHash()
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-16 13:59:15 +00:00
2023-12-06 20:13:23 +00:00
isWin := isWinner(firstSum, secondSum)
if (isWin){
2024-01-16 13:59:15 +00:00
changePlayerBalance(playerContract, playerOwner, bet)
runtime.Notify("gameResult", int(1))
2023-12-06 20:13:23 +00:00
} else {
2024-01-16 13:59:15 +00:00
changePlayerBalance(playerOwner, playerContract, bet)
runtime.Notify("gameResult", int(0))
2023-11-17 12:41:59 +00:00
}
2024-01-07 13:58:37 +00:00
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
runtime.Notify("playerBalance", playerBalance)
2023-12-06 20:13:23 +00:00
}
func isWinner(firstSum int, secondSum int) bool {
2024-01-17 17:26:31 +00:00
if (!((firstSum >= 2 && firstSum <= 12) && (secondSum >= 2 && secondSum <= 12))){
2023-12-16 17:26:51 +00:00
panic("first and second sum should be from 2 to 12")
2023-12-11 14:37:23 +00:00
}
2023-12-06 20:13:23 +00:00
2023-12-11 14:37:23 +00:00
sum := 0
2023-12-16 17:26:51 +00:00
for i:=0; i<2; i++ {
2023-12-11 14:37:23 +00:00
crap := (runtime.GetRandom() % 6) + 1
2023-12-16 17:26:51 +00:00
runtime.Notify("Crup number", i+1)
runtime.Notify("Random number", crap)
2023-12-11 14:37:23 +00:00
sum += crap
}
2023-11-17 12:41:59 +00:00
return sum == firstSum || sum == secondSum
}
2023-12-06 20:13:23 +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-06 20:13:23 +00:00
2024-01-16 13:59:15 +00:00
transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
2023-12-06 20:13:23 +00:00
if !transferred {
panic("failed to transfer zaCoins")
}
}