Skip to content

Commit

Permalink
Merge pull request #88 from CS196Illinois/specialstuff
Browse files Browse the repository at this point in the history
finding matches works
  • Loading branch information
VINERAJ authored Dec 8, 2024
2 parents 881f026 + a3af6e6 commit b402345
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.springframework.http.ResponseEntity;

Expand Down Expand Up @@ -76,23 +74,40 @@ public ResponseEntity<ReturnValue> login(@RequestBody String code)
}
}

@PostMapping(path = "/api/v1/matching")
public ResponseEntity<List<User>> matching(@RequestParam int minAge, @RequestParam int maxAge, @RequestParam String[] ranks, @RequestParam String[] roles) throws IOException {
Matching matcher = new Matching();
boolean success = matcher.fillMatches(minAge, maxAge, ranks, roles);
if (success) {
List<User> matches = matcher.getMatchList();
return ResponseEntity.ok(matches);
} else {
return ResponseEntity.ok(new ArrayList<>());
public class matchUpdate {
private String sessionToken;
private String matchedId;

public matchUpdate(String sessionToken, String matchedId) {
this.sessionToken = sessionToken;
this.matchedId = matchedId;
}

public String getMatchedId() {
return matchedId;
}

public void setMatchedId(String matchedId) {
this.matchedId = matchedId;
}

public String getSessionToken() {
return sessionToken;
}

public void setSessionToken(String sessionToken) {
this.sessionToken = sessionToken;
}
}

@PostMapping(path = "/api/v1/update_matches")
public ResponseEntity<ReturnValue> updateMatches(@RequestParam String userId, @RequestParam String matchedId) {
public ResponseEntity<Boolean> updateMatches(@RequestBody matchUpdate matchUpdate) {
OneAndTwoWayMatches oneAndTwoWayMatches = new OneAndTwoWayMatches();
boolean success = oneAndTwoWayMatches.updateMatches(userId, matchedId);
return ResponseEntity.ok(new ReturnValue(success, ""));
PostgreSQLController pgController = new PostgreSQLController();
boolean success =
oneAndTwoWayMatches.updateMatches(
pgController.getDiscordId(matchUpdate.getSessionToken()),
pgController.getDiscordIdfromRiot(matchUpdate.getMatchedId()));
return ResponseEntity.ok(success);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

public class Matching {
private List<User> matchList = new ArrayList<>();
private String sessionToken;

public Matching(String sessionToken) {
this.sessionToken = sessionToken;
}


public void newFillMatchList() {
matchList.clear();
PostgreSQLController pgController = new PostgreSQLController();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.server;

import com.google.gson.JsonObject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;

@RestController
Expand Down Expand Up @@ -60,22 +59,76 @@ public ResponseEntity<UserOutput> nextUser(@RequestBody JsonObject token) {
PostgreSQLController pgController = new PostgreSQLController();
String sessionToken = token.get("session_token").getAsString();
String discordId = pgController.getDiscordId(sessionToken);
pgController.resetSeen(discordId);
if (pgController.hasUserBeenCreated(discordId)) {
try {
Matching matching = new Matching(sessionToken);
matching.newFillMatchList();
List<User> matchList = matching.getMatchList();
if (matchList.isEmpty()) {
pgController.resetSeen(discordId);
matching.newFillMatchList();
matchList = matching.getMatchList();
if (matchList.isEmpty()) {
return ResponseEntity.ok().build();
}
return ResponseEntity.ok(new UserOutput(matchList.getFirst()));
Random rand = new Random();
return ResponseEntity.ok(new UserOutput(matchList.get(rand.nextInt(matchList.size()))));
}
pgController.addUserToSeen(discordId, matchList.getFirst().getPrivateUser().getDiscordId());
return ResponseEntity.ok(new UserOutput(matchList.getFirst()));
Random rand = new Random();
return ResponseEntity.ok(new UserOutput(matchList.get(rand.nextInt(matchList.size()))));
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
return ResponseEntity.notFound().build();
}

public class MatchResponse {
private String name;
private String discordId;

public MatchResponse(String name, String discordId) {
this.name = name;
this.discordId = discordId;
}

public String getDiscordId() {
return discordId;
}

public void setDiscordId(String discordId) {
this.discordId = discordId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

@PostMapping(path = "/api/v1/get_matches")
public ResponseEntity<List<MatchResponse>> getMatches(@RequestBody JsonObject token) {
PostgreSQLController pgController = new PostgreSQLController();
String sessionToken = token.get("session_token").getAsString();
String discordId = pgController.getDiscordId(sessionToken);
if (pgController.hasUserBeenCreated(discordId)) {
try {
List<String> outputList = pgController.getTwoWayMatched(discordId);
List<MatchResponse> outputFinal = new ArrayList<>();
DiscordOps discordOps = new DiscordOps();
for (String user : outputList) {
outputFinal.add(
new MatchResponse(
pgController.getUser(user).getPublicUser().getFirstName()
+ " "
+ pgController.getUser(user).getPublicUser().getLastName(),
discordOps.getUsername(user)));
}
return ResponseEntity.ok(outputFinal);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@

public class OneAndTwoWayMatches {

public boolean updateMatches(String discordId, String matchedId) {
PostgreSQLController pgController = new PostgreSQLController();
boolean exists = pgController.existsInOneWay(discordId, matchedId);
if (exists) {
boolean deleted = pgController.deleteOneWayMatched(discordId, matchedId);
if (!deleted) {
return false;
}
boolean added_to_two_way = pgController.updateTwoWayMatched(discordId, matchedId);
if (!added_to_two_way) {
return false;
}
} else {
boolean added = pgController.updateOneWayMatched(discordId, matchedId);
if (!added) {
return false;
}
public boolean updateMatches(String discordId, String matchedId) {
PostgreSQLController pgController = new PostgreSQLController();
boolean exists = pgController.existsInOneWay(matchedId, discordId);
if (pgController.containsTwoWayMatched(discordId, matchedId)) {
return false;
}
boolean wasAddedToTwoWay = false;
if (exists) {
boolean deleted = pgController.deleteOneWayMatched(matchedId, discordId);
if (!deleted) {
return false;
}
boolean added_to_two_way = pgController.updateTwoWayMatched(discordId, matchedId);
if (added_to_two_way) {
wasAddedToTwoWay = true;
}
} else {
boolean existsInOneWay = pgController.existsInOneWay(matchedId, discordId);
if (!existsInOneWay) {
boolean added = pgController.updateOneWayMatched(discordId, matchedId);
if (!added) {
return wasAddedToTwoWay;
}
return true;
} else {
return false;
}
}
return wasAddedToTwoWay;
}
}
Loading

0 comments on commit b402345

Please sign in to comment.