The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/api/service/restclientsupport/puzzle_service.go

100 lines
2.4 KiB

package restclientsupport
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
6 years ago
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/internal/utils"
)
// Play triggers play method of puzzle smart contract.
func (s *Service) Play(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
key := r.FormValue("key")
amount := r.FormValue("amount")
amountInt, err := strconv.ParseInt(amount, 10, 64)
fmt.Println("Play: key", key, "amount", amountInt)
res := &Response{
Success: false,
}
if err != nil {
json.NewEncoder(w).Encode(res)
return
}
if s.CreateTransactionForPlayMethod == nil {
fmt.Println("puzzle-play no method", key)
json.NewEncoder(w).Encode(res)
return
}
txID, err := s.CreateTransactionForPlayMethod(key, amountInt)
if err != nil {
utils.GetLogInstance().Error("puzzle-play, error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
res.TxID = txID
json.NewEncoder(w).Encode(res)
}
// Payout triggers play payout of puzzle smart contract.
func (s *Service) Payout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
address := r.FormValue("address")
newLevel := r.FormValue("new_level")
6 years ago
sequence := r.FormValue("sequence")
newLevelInt, err := strconv.Atoi(newLevel)
fmt.Println("Payout: address", address, "new_level", newLevelInt)
res := &Response{
Success: false,
}
if s.CreateTransactionForPayoutMethod == nil {
json.NewEncoder(w).Encode(res)
return
}
txID, err := s.CreateTransactionForPayoutMethod(common.HexToAddress(address), newLevelInt, sequence)
if err != nil {
utils.GetLogInstance().Error("Payout error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
res.TxID = txID
json.NewEncoder(w).Encode(res)
}
6 years ago
// End triggers endGame of puzzle smart contract.
func (s *Service) End(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
address := r.FormValue("address")
fmt.Println("Payout: address", address)
res := &Response{
Success: false,
}
if s.CreateTransactionForPayoutMethod == nil {
json.NewEncoder(w).Encode(res)
return
}
txID, err := s.CreateTransactionForEndMethod(common.HexToAddress(address))
if err != nil {
6 years ago
utils.GetLogInstance().Error("Payout error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
res.TxID = txID
6 years ago
json.NewEncoder(w).Encode(res)
}