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

99 lines
2.3 KiB

package restclientsupport
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
6 years ago
"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")
key := r.FormValue("key")
newLevel := r.FormValue("level")
6 years ago
sequence := r.FormValue("sequence")
newLevelInt, err := strconv.Atoi(newLevel)
fmt.Println("Payout: key", key, "new_level", newLevelInt)
res := &Response{
Success: false,
}
if s.CreateTransactionForPayoutMethod == nil {
json.NewEncoder(w).Encode(res)
return
}
txID, err := s.CreateTransactionForPayoutMethod(key, 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")
key := r.FormValue("key")
6 years ago
fmt.Println("endgame: key", key)
6 years ago
res := &Response{
Success: false,
}
if s.CreateTransactionForPayoutMethod == nil {
json.NewEncoder(w).Encode(res)
return
}
txID, err := s.CreateTransactionForEndMethod(key)
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)
}