Skip to content

Commit

Permalink
removed v1 folders
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 21, 2024
1 parent 6c255f9 commit 77ab717
Show file tree
Hide file tree
Showing 26 changed files with 89 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
package all

import (
"github.com/ardanlabs/liarsdice/app/services/engine/v1/handlers/checkgrp"
"github.com/ardanlabs/liarsdice/app/services/engine/v1/handlers/gamegrp"
"github.com/ardanlabs/liarsdice/business/web/v1/mux"
"github.com/ardanlabs/liarsdice/app/services/engine/handlers/checkgrp"
"github.com/ardanlabs/liarsdice/app/services/engine/handlers/gamegrp"
"github.com/ardanlabs/liarsdice/business/web/mux"
"github.com/ardanlabs/liarsdice/foundation/web"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"sync"

"github.com/ardanlabs/liarsdice/business/web/v1/mid"
"github.com/ardanlabs/liarsdice/business/web/mid"
"github.com/google/uuid"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"github.com/ardanlabs/ethereum/currency"
"github.com/ardanlabs/liarsdice/business/core/bank"
"github.com/ardanlabs/liarsdice/business/core/game"
v1 "github.com/ardanlabs/liarsdice/business/web/v1"
"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/v1/mid"
"github.com/ardanlabs/liarsdice/business/web/auth"
"github.com/ardanlabs/liarsdice/business/web/errs"
"github.com/ardanlabs/liarsdice/business/web/mid"
"github.com/ardanlabs/liarsdice/foundation/logger"
"github.com/ardanlabs/liarsdice/foundation/web"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -44,12 +44,12 @@ type handlers struct {
func (h *handlers) connect(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
address, err := validateSignature(ctx, h.log, r, h.connectTimeout, h.bank.Client().ChainID())
if err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

token, err := generateToken(h.auth, h.activeKID, address)
if err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

data := struct {
Expand Down Expand Up @@ -182,7 +182,7 @@ func (h *handlers) configuration(ctx context.Context, w http.ResponseWriter, r *
func (h *handlers) usd2Wei(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
usd, err := strconv.ParseFloat(web.Param(r, "usd"), 64)
if err != nil {
return v1.NewTrustedError(fmt.Errorf("converting usd: %s", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("converting usd: %s", err), http.StatusBadRequest)
}

wei := h.converter.USD2Wei(big.NewFloat(usd))
Expand Down Expand Up @@ -215,7 +215,7 @@ func (h *handlers) state(ctx context.Context, w http.ResponseWriter, r *http.Req

gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
Expand All @@ -235,13 +235,13 @@ func (h *handlers) state(ctx context.Context, w http.ResponseWriter, r *http.Req
func (h *handlers) newGame(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
g, err := game.New(ctx, h.log, h.converter, h.storer, h.bank, mid.GetSubject(ctx), h.anteUSD)
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to create game: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to create game: %w", err), http.StatusBadRequest)
}

subjectID := mid.GetSubject(ctx).String()

if err := evts.addPlayerToGame(g.ID(), subjectID); err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
}

web.SetParam(r, "id", g.ID().String())
Expand All @@ -253,12 +253,12 @@ func (h *handlers) newGame(ctx context.Context, w http.ResponseWriter, r *http.R
func (h *handlers) join(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

n, err := evts.numberOfPlayers(g.ID())
Expand All @@ -267,17 +267,17 @@ func (h *handlers) join(ctx context.Context, w http.ResponseWriter, r *http.Requ
}

if n == 5 {
return v1.NewTrustedError(errors.New("max players sitting"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("max players sitting"), http.StatusBadRequest)
}

subjectID := mid.GetSubject(ctx)

if err := g.AddAccount(ctx, subjectID); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

if err := evts.addPlayerToGame(g.ID(), subjectID.String()); err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to add player %q to game: %w", subjectID, err), http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "join")
Expand All @@ -289,16 +289,16 @@ func (h *handlers) join(ctx context.Context, w http.ResponseWriter, r *http.Requ
func (h *handlers) startGame(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

if err := g.StartGame(ctx); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "start")
Expand All @@ -310,16 +310,16 @@ func (h *handlers) startGame(ctx context.Context, w http.ResponseWriter, r *http
func (h *handlers) rollDice(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

if err := g.RollDice(ctx, mid.GetSubject(ctx)); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "rolldice")
Expand All @@ -331,28 +331,28 @@ func (h *handlers) rollDice(ctx context.Context, w http.ResponseWriter, r *http.
func (h *handlers) bet(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

number, err := strconv.Atoi(web.Param(r, "number"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("converting number: %s", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("converting number: %s", err), http.StatusBadRequest)
}

suit, err := strconv.Atoi(web.Param(r, "suit"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("converting suit: %s", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("converting suit: %s", err), http.StatusBadRequest)
}

address := mid.GetSubject(ctx)

if err := g.Bet(ctx, address, number, suit); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "bet", "index", g.State().Cups[address].OrderIdx)
Expand All @@ -364,20 +364,20 @@ func (h *handlers) bet(ctx context.Context, w http.ResponseWriter, r *http.Reque
func (h *handlers) callLiar(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

if _, _, err := g.CallLiar(ctx, mid.GetSubject(ctx)); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

if _, err := g.NextRound(ctx); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "callliar")
Expand All @@ -389,19 +389,19 @@ func (h *handlers) callLiar(ctx context.Context, w http.ResponseWriter, r *http.
func (h *handlers) reconcile(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

ctx, cancel := context.WithTimeout(ctx, h.bankTimeout)
defer cancel()

if _, _, err := g.Reconcile(ctx); err != nil {
return v1.NewTrustedError(err, http.StatusInternalServerError)
return errs.NewTrustedError(err, http.StatusInternalServerError)
}

evts.send(ctx, g.ID(), "reconcile")
Expand All @@ -418,7 +418,7 @@ func (h *handlers) balance(ctx context.Context, w http.ResponseWriter, r *http.R

balanceGWei, err := h.bank.AccountBalance(ctx, mid.GetSubject(ctx))
if err != nil {
return v1.NewTrustedError(err, http.StatusInternalServerError)
return errs.NewTrustedError(err, http.StatusInternalServerError)
}

resp := struct {
Expand All @@ -434,16 +434,16 @@ func (h *handlers) balance(ctx context.Context, w http.ResponseWriter, r *http.R
func (h *handlers) nextTurn(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

if err := g.NextTurn(ctx); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "nextturn")
Expand All @@ -457,23 +457,23 @@ func (h *handlers) nextTurn(ctx context.Context, w http.ResponseWriter, r *http.
func (h *handlers) updateOut(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
gameID, err := uuid.Parse(web.Param(r, "id"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("unable to parse game id: %w", err), http.StatusBadRequest)
}

g, err := game.Tables.Retrieve(gameID)
if err != nil {
return v1.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
return errs.NewTrustedError(errors.New("no game exists"), http.StatusBadRequest)
}

outs, err := strconv.Atoi(web.Param(r, "outs"))
if err != nil {
return v1.NewTrustedError(fmt.Errorf("converting outs: %s", err), http.StatusBadRequest)
return errs.NewTrustedError(fmt.Errorf("converting outs: %s", err), http.StatusBadRequest)
}

address := mid.GetSubject(ctx)

if err := g.ApplyOut(ctx, address, outs); err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
return errs.NewTrustedError(err, http.StatusBadRequest)
}

evts.send(ctx, g.ID(), "outs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/ardanlabs/ethereum/currency"
"github.com/ardanlabs/liarsdice/business/core/bank"
"github.com/ardanlabs/liarsdice/business/core/game/stores/gamedb"
"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/v1/mid"
"github.com/ardanlabs/liarsdice/business/web/auth"
"github.com/ardanlabs/liarsdice/business/web/mid"
"github.com/ardanlabs/liarsdice/foundation/logger"
"github.com/ardanlabs/liarsdice/foundation/web"
"github.com/gorilla/websocket"
Expand Down
8 changes: 4 additions & 4 deletions app/services/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"github.com/ardanlabs/conf/v3"
"github.com/ardanlabs/ethereum"
"github.com/ardanlabs/ethereum/currency"
"github.com/ardanlabs/liarsdice/app/services/engine/v1/build/all"
"github.com/ardanlabs/liarsdice/app/services/engine/build/all"
scbank "github.com/ardanlabs/liarsdice/business/contract/go/bank"
"github.com/ardanlabs/liarsdice/business/core/bank"
"github.com/ardanlabs/liarsdice/business/data/sqldb"
"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/v1/debug"
"github.com/ardanlabs/liarsdice/business/web/v1/mux"
"github.com/ardanlabs/liarsdice/business/web/auth"
"github.com/ardanlabs/liarsdice/business/web/debug"
"github.com/ardanlabs/liarsdice/business/web/mux"
"github.com/ardanlabs/liarsdice/foundation/keystore"
"github.com/ardanlabs/liarsdice/foundation/logger"
"github.com/ardanlabs/liarsdice/foundation/web"
Expand Down
2 changes: 1 addition & 1 deletion business/data/dbtest/dbtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/ardanlabs/liarsdice/business/data/migrate"
"github.com/ardanlabs/liarsdice/business/data/sqldb"
"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/auth"
"github.com/ardanlabs/liarsdice/foundation/docker"
"github.com/ardanlabs/liarsdice/foundation/logger"
"github.com/ardanlabs/liarsdice/foundation/web"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/auth"
"github.com/ardanlabs/liarsdice/foundation/logger"
"github.com/golang-jwt/jwt/v4"
"github.com/jmoiron/sqlx"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 2 additions & 20 deletions business/web/v1/v1.go → business/web/errs/errs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package v1 provides types and support related to web v1 functionality.
package v1
// Package errs provides types and support related to web v1 functionality.
package errs

import "errors"

Expand Down Expand Up @@ -42,21 +42,3 @@ func GetTrustedError(err error) *TrustedError {
}
return te
}

// PageDocument is the form used for API responses from query API calls.
type PageDocument[T any] struct {
Items []T `json:"items"`
Total int `json:"total"`
Page int `json:"page"`
RowsPerPage int `json:"rowsPerPage"`
}

// NewPageDocument constructs a response value for a web paging trusted.
func NewPageDocument[T any](items []T, total int, page int, rowsPerPage int) PageDocument[T] {
return PageDocument[T]{
Items: items,
Total: total,
Page: page,
RowsPerPage: rowsPerPage,
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion business/web/v1/mid/auth.go → business/web/mid/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"net/http"

"github.com/ardanlabs/liarsdice/business/web/v1/auth"
"github.com/ardanlabs/liarsdice/business/web/auth"
"github.com/ardanlabs/liarsdice/foundation/web"
)

Expand Down
File renamed without changes.
Loading

0 comments on commit 77ab717

Please sign in to comment.