Skip to content

Commit

Permalink
refactor: separate notifier logic from sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Emyr298 committed Jun 29, 2024
1 parent 2f84096 commit 1680afd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
23 changes: 22 additions & 1 deletion src/handlers/session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 4 additions & 8 deletions src/matches/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/services/match_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1680afd

Please sign in to comment.