Skip to content

Commit

Permalink
refactoring constants
Browse files Browse the repository at this point in the history
  • Loading branch information
makiuchi-d committed Apr 3, 2024
1 parent dbc54c5 commit ec459c8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
3 changes: 3 additions & 0 deletions server/common/enum.go → server/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ const (
HostStatusStarting = 0
HostStatusRunning = 1
HostStatusClosing = 2

RoomIdLen = 32
RoomIdPattern = "^[0-9a-f]+$"
)
12 changes: 3 additions & 9 deletions server/game/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ import (
"golang.org/x/xerrors"
"google.golang.org/grpc/codes"

"wsnet2/common"
"wsnet2/config"
"wsnet2/log"
"wsnet2/pb"
)

const (
// RoomID文字列長
lenId = 32

idPattern = "^[0-9a-f]+$"
)

var (
roomInsertQuery string
roomUpdateQuery string
Expand All @@ -46,7 +40,7 @@ func init() {
seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
randsrc = rand.New(rand.NewSource(seed.Int64()))

rerid = regexp.MustCompile(idPattern)
rerid = regexp.MustCompile(common.RoomIdPattern)
}

func dbCols(t reflect.Type) []string {
Expand Down Expand Up @@ -309,7 +303,7 @@ func (repo *Repository) newRoomInfo(ctx context.Context, tx *sqlx.Tx, op *pb.Roo
default:
}

ri.Id = RandomHex(lenId)
ri.Id = RandomHex(common.RoomIdLen)
if op.WithNumber {
ri.Number.Number = randsrc.Int31n(maxNumber) + 1 // [1..maxNumber]
}
Expand Down
5 changes: 4 additions & 1 deletion server/game/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jmoiron/sqlx"
"golang.org/x/xerrors"

"wsnet2/common"
"wsnet2/config"
"wsnet2/pb"
)
Expand Down Expand Up @@ -60,6 +61,7 @@ func newDbMock(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock) {
}

func TestNewRoomInfo(t *testing.T) {
const lenId = common.RoomIdLen
ctx := context.Background()
db, mock := newDbMock(t)
retryCount := 3
Expand Down Expand Up @@ -132,13 +134,14 @@ func TestNewRoomInfo(t *testing.T) {
}

func TestRandomHexRoomId(t *testing.T) {
const lenId = common.RoomIdLen
rid := RandomHex(lenId)

if len(rid) != lenId {
t.Errorf("room id len = %v wants %v (%q)", len(rid), lenId, rid)
}

ok, err := regexp.MatchString(idPattern, rid)
ok, err := regexp.MatchString(common.RoomIdPattern, rid)
if err != nil || !ok {
t.Errorf("room id pattern missmatch: %v", rid)
}
Expand Down
3 changes: 2 additions & 1 deletion server/lobby/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"golang.org/x/xerrors"

"wsnet2/auth"
"wsnet2/common"
"wsnet2/lobby"
"wsnet2/log"
"wsnet2/pb"
Expand Down Expand Up @@ -248,7 +249,7 @@ func (sv *LobbyService) handleCreateRoom(w http.ResponseWriter, r *http.Request)
}

var (
idRegexp = regexp.MustCompile("^[0-9a-f]+$")
idRegexp = regexp.MustCompile(common.RoomIdPattern)
)

type JoinVars struct {
Expand Down

0 comments on commit ec459c8

Please sign in to comment.