From 1680afda75e5addf5e594a026d97f2d3c5b77661 Mon Sep 17 00:00:00 2001 From: Emyr298 Date: Sat, 29 Jun 2024 10:09:16 +0700 Subject: [PATCH] refactor: separate notifier logic from sessions --- src/handlers/session_handler.go | 23 ++++++++++++++++++++++- src/matches/session.go | 12 ++++-------- src/services/match_service.go | 4 ++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/handlers/session_handler.go b/src/handlers/session_handler.go index 87efc4e..cf7aa17 100644 --- a/src/handlers/session_handler.go +++ b/src/handlers/session_handler.go @@ -116,11 +116,32 @@ func (handler *SessionHandler) ExecuteAction(req *ws_types.Request, res *ws_type return } - err = handler.matchService.ExecuteAction(body) + opponentId, err := handler.matchService.GetOpponentId(body.PlayerId) if err != nil { res.SendToClient(utils.ErrorMessage(err)) return } + + action, err := handler.matchService.ExecuteAction(body) + if err != nil { + res.SendToClient(utils.ErrorMessage(err)) + return + } + + res.NotifyClient(&ws_types.Message{ + Action: ws_types.ACTION_APPLY_ACTION, + Body: map[string]interface{}{ + "actionName": body.ActionName, + "actionSpecific": action, + }, + }) + res.NotifyOtherClient(opponentId, &ws_types.Message{ + Action: ws_types.ACTION_APPLY_ACTION, + Body: map[string]interface{}{ + "actionName": body.ActionName, + "actionSpecific": action, + }, + }) } func (handler *SessionHandler) EndTurn(req *ws_types.Request, res *ws_types.Response) { diff --git a/src/matches/session.go b/src/matches/session.go index ce1b8b0..6c06d1d 100644 --- a/src/matches/session.go +++ b/src/matches/session.go @@ -304,22 +304,18 @@ func (session *Session) StartBattleSync() error { return session.state.startBattle() } -func (session *Session) ExecuteActionSync(actionName string, actionBody map[string]interface{}) error { +func (session *Session) ExecuteActionSync(actionName string, actionBody map[string]interface{}) (map[string]interface{}, error) { session.lock.Lock() defer session.lock.Unlock() action, err := session.createActionLog(actionName, actionBody) if err != nil { - return err + return nil, err } err = session.state.executeAction(action) if err != nil { - return err + return nil, err } - - notifier := notifiers.GetSessionNotifier() - notifier.NotifyAction(session.player1.Id, action.GetName(), action.GetData()) - notifier.NotifyAction(session.player2.Id, action.GetName(), action.GetData()) - return nil + return action.GetData(), nil } func (session *Session) EndTurnSync(playerId string) error { diff --git a/src/services/match_service.go b/src/services/match_service.go index 5cd35ac..85b1ec7 100644 --- a/src/services/match_service.go +++ b/src/services/match_service.go @@ -68,10 +68,10 @@ func (service *MatchService) PreparePlayer(data PreparePlayerRequestDTO) (bool, } } -func (service *MatchService) ExecuteAction(data ExecuteActionRequestDTO) error { +func (service *MatchService) ExecuteAction(data ExecuteActionRequestDTO) (map[string]interface{}, error) { session := service.sessionRepository.GetSessionByPlayerId(data.PlayerId) if session == nil { - return exceptions.SessionNotFound() + return nil, exceptions.SessionNotFound() } data.ActionSpecific["playerId"] = data.PlayerId