Skip to content

Commit

Permalink
Merge pull request #4 from Pixel-Tactics/fix/second-match
Browse files Browse the repository at this point in the history
fix: cannot send match invitation after match
  • Loading branch information
Emyr298 authored Jun 26, 2024
2 parents 1b9b94b + 6943b82 commit 069044c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/matches/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ func (session *Session) GetRunningSync() bool {
return session.running
}

func (session *Session) GetEndedSync() bool {
session.lock.Lock()
defer session.lock.Unlock()
_, ok := session.state.(*EndState)
return ok
}

func (session *Session) GetMatchMapSync() matches_maps.MatchMap {
session.lock.Lock()
defer session.lock.Unlock()
Expand Down
81 changes: 60 additions & 21 deletions src/repositories/session_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repositories

import (
"errors"
"log"

"github.com/google/uuid"
"pixeltactics.com/match/src/data_structures"
Expand Down Expand Up @@ -32,30 +33,25 @@ func (repo *SessionRepository) GetSessionByPlayerId(playerId string) *matches.Se
}

func (repo *SessionRepository) CreateSession(playerId string, opponentId string) (*matches.Session, error) {
playerSession, isPlayerInSession := repo.playerSession.Load(playerId)
if isPlayerInSession {
sessionOpponent, _ := playerSession.GetOpponentPlayerSync(playerId)
if sessionOpponent.Id != opponentId {
if playerSession.GetRunningSync() {
return nil, errors.New("player is already on a session with another player")
} else {
repo.DeleteSession(playerSession.GetIdSync())
}
} else {
return nil, errors.New("session is already created")
}
err := repo.checkPlayerSession(playerId, opponentId)
if err != nil {
return nil, err
}

opponentSession, isOpponentInSession := repo.playerSession.Load(opponentId)
if isOpponentInSession {
sessionOpponent, _ := opponentSession.GetOpponentPlayerSync(opponentId)
if sessionOpponent.Id != playerId {
return nil, errors.New("opponent is already on a session with another player")
} else {
repo.playerSession.Store(playerId, opponentSession)
opponentSession.RunSessionSync()
return opponentSession, nil
isStart, err := repo.checkOpponentSession(playerId, opponentId)
if err != nil {
return nil, err
}

if isStart {
opponentSession, isOpponentInSession := repo.playerSession.Load(opponentId)
if !isOpponentInSession {
log.Fatalln("session found before but now not")
return nil, errors.New("server cannot get opponent session")
}
repo.playerSession.Store(playerId, opponentSession)
opponentSession.RunSessionSync()
return opponentSession, nil
}

newSessionId := uuid.New().String()
Expand Down Expand Up @@ -100,6 +96,49 @@ func (repo *SessionRepository) DeleteSession(sessionId string) {
repo.sessions.Delete(sessionId)
}

func (repo *SessionRepository) checkPlayerSession(playerId string, opponentId string) error {
playerSession, isPlayerInSession := repo.playerSession.Load(playerId)
if isPlayerInSession {
sessionOpponent, _ := playerSession.GetOpponentPlayerSync(playerId)
isRunning := playerSession.GetRunningSync()

if !isRunning {
repo.DeleteSession(playerSession.GetIdSync())
return nil
} else {
if sessionOpponent.Id != opponentId {
return errors.New("player is already on a session with another player")
} else {
return errors.New("session is already created")
}
}
}
return nil
}

func (repo *SessionRepository) checkOpponentSession(playerId string, opponentId string) (bool, error) {
opponentSession, isOpponentInSession := repo.playerSession.Load(opponentId)
if isOpponentInSession {
sessionOpponent, _ := opponentSession.GetOpponentPlayerSync(opponentId)
isRunning := opponentSession.GetRunningSync()
isEnded := opponentSession.GetEndedSync()

if isEnded {
repo.DeleteSession(opponentSession.GetIdSync())
return false, nil
} else if isRunning {
if sessionOpponent.Id != playerId {
return false, errors.New("opponent is already on a session with another player")
} else {
return false, errors.New("session is already created")
}
} else {
return sessionOpponent.Id == playerId, nil
}
}
return false, nil
}

var sessionRepository *SessionRepository = nil

func GetSessionRepository() *SessionRepository {
Expand Down

0 comments on commit 069044c

Please sign in to comment.