Skip to content

Commit

Permalink
refactor: move types to websocket types
Browse files Browse the repository at this point in the history
  • Loading branch information
Emyr298 committed Jun 29, 2024
1 parent 37100c1 commit 2f84096
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 79 deletions.
44 changes: 22 additions & 22 deletions src/handlers/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"os"

"pixeltactics.com/match/src/exceptions"
"pixeltactics.com/match/src/types"
"pixeltactics.com/match/src/utils"
ws_types "pixeltactics.com/match/src/websocket/types"
)

type AuthMessageBody struct {
PlayerToken string `json:"playerToken"`
}

type AuthHandler struct {
Interaction chan *types.Interaction
successResponses chan *types.Interaction
errorResponses chan *types.Interaction
Interaction chan *ws_types.Interaction
successResponses chan *ws_types.Interaction
errorResponses chan *ws_types.Interaction
}

func (handler *AuthHandler) AuthenticateClient(req *types.Request, res *types.Response) {
func (handler *AuthHandler) AuthenticateClient(req *ws_types.Request, res *ws_types.Response) {
var body AuthMessageBody
err := utils.MapToObject(req.Message.Body, &body)
if err != nil {
Expand All @@ -33,8 +33,8 @@ func (handler *AuthHandler) AuthenticateClient(req *types.Request, res *types.Re

playerId := body.PlayerToken
if len(playerId) == 0 {
res.SendToClient(&types.Message{
Action: types.ACTION_ERROR,
res.SendToClient(&ws_types.Message{
Action: ws_types.ACTION_ERROR,
Body: map[string]interface{}{
"status": "failed",
"message": "invalid player token",
Expand All @@ -46,7 +46,7 @@ func (handler *AuthHandler) AuthenticateClient(req *types.Request, res *types.Re
handler.sendAuthRequest(body.PlayerToken, res)
}

func (handler *AuthHandler) handleSuccess(req *types.Request, res *types.Response) {
func (handler *AuthHandler) handleSuccess(req *ws_types.Request, res *ws_types.Response) {
playerIdRaw, ok := req.Message.Body["playerId"]
if !ok {
log.Println(exceptions.InvalidJsonError())
Expand All @@ -60,16 +60,16 @@ func (handler *AuthHandler) handleSuccess(req *types.Request, res *types.Respons
}

res.RegisterPlayer(playerId)
res.SendToClient(&types.Message{
Action: types.ACTION_FEEDBACK,
res.SendToClient(&ws_types.Message{
Action: ws_types.ACTION_FEEDBACK,
Body: map[string]interface{}{
"status": "success",
"message": "user registered on session",
},
})
}

func (handler *AuthHandler) handleError(req *types.Request, res *types.Response) {
func (handler *AuthHandler) handleError(req *ws_types.Request, res *ws_types.Response) {
msgRaw, ok := req.Message.Body["message"]
if !ok {
log.Println(exceptions.InvalidJsonError())
Expand All @@ -85,12 +85,12 @@ func (handler *AuthHandler) handleError(req *types.Request, res *types.Response)
res.SendToClient(utils.ErrorMessage(errors.New(msg)))
}

func (handler *AuthHandler) sendAuthRequest(playerToken string, res *types.Response) {
func (handler *AuthHandler) sendAuthRequest(playerToken string, res *ws_types.Response) {
go func() {
sendError := func() {
handler.errorResponses <- &types.Interaction{
Request: &types.Request{
Message: &types.Message{
handler.errorResponses <- &ws_types.Interaction{
Request: &ws_types.Request{
Message: &ws_types.Message{
Body: map[string]interface{}{
"message": "invalid token",
},
Expand Down Expand Up @@ -128,9 +128,9 @@ func (handler *AuthHandler) sendAuthRequest(playerToken string, res *types.Respo
sendError()
}

handler.successResponses <- &types.Interaction{
Request: &types.Request{
Message: &types.Message{
handler.successResponses <- &ws_types.Interaction{
Request: &ws_types.Request{
Message: &ws_types.Message{
Body: map[string]interface{}{
"playerId": playerId,
},
Expand All @@ -146,17 +146,17 @@ func (handler *AuthHandler) sendAuthRequest(playerToken string, res *types.Respo

func NewAuthHandler() *AuthHandler {
return &AuthHandler{
Interaction: make(chan *types.Interaction, 256),
successResponses: make(chan *types.Interaction, 256),
errorResponses: make(chan *types.Interaction, 256),
Interaction: make(chan *ws_types.Interaction, 256),
successResponses: make(chan *ws_types.Interaction, 256),
errorResponses: make(chan *ws_types.Interaction, 256),
}
}

func (handler *AuthHandler) Run() {
for {
select {
case interaction := <-handler.Interaction:
if interaction.Request.Message.Action == types.ACTION_AUTH {
if interaction.Request.Message.Action == ws_types.ACTION_AUTH {
handler.AuthenticateClient(interaction.Request, interaction.Response)
}
case successResp := <-handler.successResponses:
Expand Down
60 changes: 30 additions & 30 deletions src/handlers/session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (

"pixeltactics.com/match/src/notifiers"
"pixeltactics.com/match/src/services"
"pixeltactics.com/match/src/types"
"pixeltactics.com/match/src/utils"
ws_types "pixeltactics.com/match/src/websocket/types"
)

type SessionHandler struct {
matchService services.MatchService
Interaction chan *types.Interaction
Interaction chan *ws_types.Interaction
}

func (handler *SessionHandler) GetSession(req *types.Request, res *types.Response) {
func (handler *SessionHandler) GetSession(req *ws_types.Request, res *ws_types.Response) {
var body services.GetSessionRequestDTO
err := utils.MapToObject(req.Message.Body, &body)
if err != nil {
Expand All @@ -27,14 +27,14 @@ func (handler *SessionHandler) GetSession(req *types.Request, res *types.Respons
return
}

res.SendToClient(&types.Message{
Action: types.ACTION_FEEDBACK,
res.SendToClient(&ws_types.Message{
Action: ws_types.ACTION_FEEDBACK,
Identifier: req.Message.Identifier,
Body: session,
})
}

func (handler *SessionHandler) CreateSession(req *types.Request, res *types.Response) {
func (handler *SessionHandler) CreateSession(req *ws_types.Request, res *ws_types.Response) {
var body services.CreateSessionRequestDTO
err := utils.MapToObject(req.Message.Body, &body)
if err != nil {
Expand All @@ -53,31 +53,31 @@ func (handler *SessionHandler) CreateSession(req *types.Request, res *types.Resp
return
}

res.SendToClient(&types.Message{
Action: types.ACTION_FEEDBACK,
res.SendToClient(&ws_types.Message{
Action: ws_types.ACTION_FEEDBACK,
Body: map[string]interface{}{
"success": true,
},
})

if !session.GetRunningSync() {
res.NotifyOtherClient(body.OpponentId, &types.Message{
Action: types.ACTION_INVITE_SESSION,
res.NotifyOtherClient(body.OpponentId, &ws_types.Message{
Action: ws_types.ACTION_INVITE_SESSION,
Body: map[string]interface{}{
"playerId": body.PlayerId,
},
})
} else {
sessionMap := session.GetDataSync()
res.NotifyOtherClient(body.OpponentId, &types.Message{
Action: types.ACTION_START_SESSION,
res.NotifyOtherClient(body.OpponentId, &ws_types.Message{
Action: ws_types.ACTION_START_SESSION,
Body: map[string]interface{}{
"opponentId": body.PlayerId,
"session": sessionMap,
},
})
res.NotifyClient(&types.Message{
Action: types.ACTION_START_SESSION,
res.NotifyClient(&ws_types.Message{
Action: ws_types.ACTION_START_SESSION,
Body: map[string]interface{}{
"opponentId": body.OpponentId,
"session": sessionMap,
Expand All @@ -86,7 +86,7 @@ func (handler *SessionHandler) CreateSession(req *types.Request, res *types.Resp
}
}

func (handler *SessionHandler) PreparePlayer(req *types.Request, res *types.Response) {
func (handler *SessionHandler) PreparePlayer(req *ws_types.Request, res *ws_types.Response) {
var body services.PreparePlayerRequestDTO
err := utils.MapToObject(req.Message.Body, &body)
if err != nil {
Expand All @@ -100,15 +100,15 @@ func (handler *SessionHandler) PreparePlayer(req *types.Request, res *types.Resp
return
}

res.SendToClient(&types.Message{
Action: types.ACTION_FEEDBACK,
res.SendToClient(&ws_types.Message{
Action: ws_types.ACTION_FEEDBACK,
Body: map[string]interface{}{
"success": true,
},
})
}

func (handler *SessionHandler) ExecuteAction(req *types.Request, res *types.Response) {
func (handler *SessionHandler) ExecuteAction(req *ws_types.Request, res *ws_types.Response) {
var body services.ExecuteActionRequestDTO
err := utils.MapToObject(req.Message.Body, &body)
if err != nil {
Expand All @@ -123,7 +123,7 @@ func (handler *SessionHandler) ExecuteAction(req *types.Request, res *types.Resp
}
}

func (handler *SessionHandler) EndTurn(req *types.Request, res *types.Response) {
func (handler *SessionHandler) EndTurn(req *ws_types.Request, res *ws_types.Response) {
playerIdInterface, ok := req.Message.Body["playerId"]
if !ok {
res.SendToClient(utils.ErrorMessage(errors.New("no player id")))
Expand All @@ -142,11 +142,11 @@ func (handler *SessionHandler) EndTurn(req *types.Request, res *types.Response)
}
}

func (handler *SessionHandler) GetServerTime(req *types.Request, res *types.Response) {
func (handler *SessionHandler) GetServerTime(req *ws_types.Request, res *ws_types.Response) {
curTime := float64(handler.matchService.GetServerTime().UnixMilli())
resTime := curTime / 1000.0
res.SendToClient(&types.Message{
Action: types.ACTION_FEEDBACK,
res.SendToClient(&ws_types.Message{
Action: ws_types.ACTION_FEEDBACK,
Body: map[string]interface{}{
"localTime": req.Message.Body["localTime"],
"serverTime": resTime,
Expand All @@ -160,25 +160,25 @@ func (handler *SessionHandler) Run() {
req := interaction.Request
res := interaction.Response
if ok {
if req.Message.Action == types.ACTION_GET_SESSION {
if req.Message.Action == ws_types.ACTION_GET_SESSION {
handler.GetSession(req, res)
} else if req.Message.Action == types.ACTION_CREATE_SESSION {
} else if req.Message.Action == ws_types.ACTION_CREATE_SESSION {
handler.CreateSession(req, res)
} else if req.Message.Action == types.ACTION_SERVER_TIME {
} else if req.Message.Action == ws_types.ACTION_SERVER_TIME {
handler.GetServerTime(req, res)
} else if req.Message.Action == types.ACTION_PREPARE_PLAYER {
} else if req.Message.Action == ws_types.ACTION_PREPARE_PLAYER {
handler.PreparePlayer(req, res)
} else if req.Message.Action == types.ACTION_EXECUTE_ACTION {
} else if req.Message.Action == ws_types.ACTION_EXECUTE_ACTION {
handler.ExecuteAction(req, res)
} else if req.Message.Action == types.ACTION_END_TURN {
} else if req.Message.Action == ws_types.ACTION_END_TURN {
handler.EndTurn(req, res)
}
handler.handleNotifierChannel(res)
}
}
}

func (handler *SessionHandler) handleNotifierChannel(res *types.Response) {
func (handler *SessionHandler) handleNotifierChannel(res *ws_types.Response) {
notifier := notifiers.GetSessionNotifier()
for {
isBreak := false
Expand All @@ -202,6 +202,6 @@ func (handler *SessionHandler) handleNotifierChannel(res *types.Response) {
func NewSessionHandler() *SessionHandler {
return &SessionHandler{
matchService: *services.NewMatchService(),
Interaction: make(chan *types.Interaction, 256),
Interaction: make(chan *ws_types.Interaction, 256),
}
}
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"

"pixeltactics.com/match/src/utils"
ws "pixeltactics.com/match/src/websocket"
ws "pixeltactics.com/match/src/websocket/core"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
Expand Down
4 changes: 2 additions & 2 deletions src/notifiers/notifier_data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package notifiers

import "pixeltactics.com/match/src/types"
import ws_types "pixeltactics.com/match/src/websocket/types"

type NotifierData struct {
PlayerId string
Message types.Message
Message ws_types.Message
}
8 changes: 4 additions & 4 deletions src/notifiers/session_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package notifiers
import (
"sync"

"pixeltactics.com/match/src/types"
ws_types "pixeltactics.com/match/src/websocket/types"
)

type SessionNotifier struct {
Expand All @@ -13,7 +13,7 @@ type SessionNotifier struct {
func (notifier *SessionNotifier) NotifyChangeState(playerId string, sessionData map[string]interface{}) {
notifier.SendChannel <- &NotifierData{
PlayerId: playerId,
Message: types.Message{
Message: ws_types.Message{
Action: "STATE_CHANGE",
Body: map[string]interface{}{
"session": sessionData,
Expand All @@ -25,8 +25,8 @@ func (notifier *SessionNotifier) NotifyChangeState(playerId string, sessionData
func (notifier *SessionNotifier) NotifyAction(playerId string, actionName string, actionData map[string]interface{}) {
notifier.SendChannel <- &NotifierData{
PlayerId: playerId,
Message: types.Message{
Action: types.ACTION_APPLY_ACTION,
Message: ws_types.Message{
Action: ws_types.ACTION_APPLY_ACTION,
Body: map[string]interface{}{
"actionName": actionName,
"actionSpecific": actionData,
Expand Down
10 changes: 6 additions & 4 deletions src/utils/errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package utils

import "pixeltactics.com/match/src/types"
import (
ws_types "pixeltactics.com/match/src/websocket/types"
)

func ErrorMessage(err error) *types.Message {
return &types.Message{
Action: types.ACTION_ERROR,
func ErrorMessage(err error) *ws_types.Message {
return &ws_types.Message{
Action: ws_types.ACTION_ERROR,
Body: map[string]interface{}{
"error": err.Error(),
},
Expand Down
Loading

0 comments on commit 2f84096

Please sign in to comment.