-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from CS3219-AY2425S1/titus/matching-service-cl…
…eanups refactor: clean up files in matching-service
- Loading branch information
Showing
9 changed files
with
231 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package models | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
) | ||
|
||
// Get the highest common difficulty (aka complexity) between two users, | ||
// If no common difficulty found, choose the min of the 2 arrays. | ||
func GetCommonDifficulty(userArr []string, matchedUserArr []string) string { | ||
commonDifficulties := make([]int, 3) | ||
for i := range commonDifficulties { | ||
commonDifficulties[i] = 0 | ||
} | ||
|
||
for _, difficulty := range userArr { | ||
formattedDifficulty := strings.ToLower(difficulty) | ||
switch formattedDifficulty { | ||
case "easy": | ||
commonDifficulties[0]++ | ||
case "medium": | ||
commonDifficulties[1]++ | ||
case "hard": | ||
commonDifficulties[2]++ | ||
default: | ||
log.Println("Unknown difficulty specified: " + difficulty) | ||
} | ||
} | ||
|
||
for _, difficulty := range matchedUserArr { | ||
formattedDifficulty := strings.ToLower(difficulty) | ||
switch formattedDifficulty { | ||
case "easy": | ||
commonDifficulties[0]++ | ||
case "medium": | ||
commonDifficulties[1]++ | ||
case "hard": | ||
commonDifficulties[2]++ | ||
default: | ||
log.Println("Unknown difficulty specified: " + difficulty) | ||
} | ||
} | ||
|
||
lowest := "Hard" | ||
for i := 2; i >= 0; i-- { | ||
if commonDifficulties[i] == 2 { | ||
switch i { | ||
case 0: | ||
return "Easy" | ||
case 1: | ||
return "Medium" | ||
case 2: | ||
return "Hard" | ||
} | ||
} else if commonDifficulties[i] > 0 { | ||
switch i { | ||
case 0: | ||
lowest = "Easy" | ||
case 1: | ||
lowest = "Medium" | ||
case 2: | ||
lowest = "Hard" | ||
} | ||
} | ||
} | ||
return lowest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package processes | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
const matchmakingQueueRedisKey = "matchmaking_queue" | ||
|
||
var ( | ||
redisClient *redis.Client | ||
matchingRoutineMutex sync.Mutex // Mutex to ensure only one matchmaking goroutine is running | ||
redisAccessMutex sync.Mutex // Mutex for Redis access for concurrency safety | ||
ctx = context.Background() | ||
) |
Oops, something went wrong.