From 8f5ff23fa4e0487e21852673c20aa15cf00ed7a6 Mon Sep 17 00:00:00 2001
From: ivarnm <36505347+ivarnm@users.noreply.github.com>
Date: Tue, 17 Mar 2020 14:49:08 +0100
Subject: [PATCH] #10 Add create and join new tournament QI

---
 backend/api/tournament.js                     | 11 +++--
 .../com/gameware/game/QueryIntermediate.java  | 40 ++++++++++++++++++-
 .../com/gameware/game/models/Tournament.java  | 15 +++++++
 3 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/backend/api/tournament.js b/backend/api/tournament.js
index efb981d..c28b572 100644
--- a/backend/api/tournament.js
+++ b/backend/api/tournament.js
@@ -20,10 +20,15 @@ router.post("/new", (req, res) => {
       let tournamentid;
       const tournamentdate = new Date();
       let tournyresult;
+      const games = req.body.games
+        .substring(1, req.body.games.length - 1)
+        .replace(" ", "")
+        .split(",");
+      console.log(games);
       db.collection(collection).insertOne(
         {
           players: [mongo.ObjectID(req.body.userid)],
-          games: req.body.games,
+          games: games,
           name: req.body.name,
           timePerRound: parseInt(req.body.timePerRound),
           maxPlayers: parseInt(req.body.maxPlayers),
@@ -52,7 +57,7 @@ router.post("/new", (req, res) => {
           {
             tournamentId: mongo.ObjectID(tournyid),
             playerId: mongo.ObjectID(req.body.userid),
-            gameId: mongo.ObjectID(req.body.games[0]),
+            gameId: mongo.ObjectID(games[0]),
             scoreValue: 0,
             roundNr: 1,
             hasPlayed: false,
@@ -63,7 +68,7 @@ router.post("/new", (req, res) => {
               res.sendStatus(500); // Internal server error
               return;
             }
-            res.json(tournyresult);
+            res.json(tournyresult.ops[0]);
           }
         );
       }
diff --git a/frontend/core/src/com/gameware/game/QueryIntermediate.java b/frontend/core/src/com/gameware/game/QueryIntermediate.java
index bb6d5ab..47b8f12 100644
--- a/frontend/core/src/com/gameware/game/QueryIntermediate.java
+++ b/frontend/core/src/com/gameware/game/QueryIntermediate.java
@@ -14,8 +14,10 @@ import com.gameware.game.models.Tournament;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.HashMap;
 import java.util.NoSuchElementException;
 
 public class QueryIntermediate {
@@ -86,6 +88,12 @@ public class QueryIntermediate {
         }
     }
 
+    private static void checkIteratorNotEmpty(JsonValue.JsonIterator iterator) throws NoSuchElementException {
+        if (!iterator.hasNext()) {
+            throw new NoSuchElementException("Response empty");
+        }
+    }
+
     // ---------------- Games methods ----------------
     public static Game getGameById(String gameId) throws IOException, NoSuchElementException {
         String route = "games/gamename/" + gameId;
@@ -103,6 +111,7 @@ public class QueryIntermediate {
         checkStatusCode(response);
         JsonValue base = jsonReader.parse(response[1]);
         JsonValue.JsonIterator iterator = base.iterator();
+        checkIteratorNotEmpty(iterator);
         while (iterator.hasNext()) {
             Game game = json.fromJson(Game.class, iterator.next().toString());
             games.add(game);
@@ -119,6 +128,7 @@ public class QueryIntermediate {
         checkStatusCode(response);
         JsonValue base = jsonReader.parse(response[1]);
         JsonValue.JsonIterator iterator = base.iterator();
+        checkIteratorNotEmpty(iterator);
         while (iterator.hasNext()) {
             Tournament tournament = json.fromJson(Tournament.class, iterator.next().toString());
             tournaments.add(tournament);
@@ -126,7 +136,33 @@ public class QueryIntermediate {
         return tournaments;
     }
 
-    
-
+    public static Tournament joinATournament(String playerId) throws IOException, NoSuchElementException {
+        String route = "tournament/join/";
+        Map<String, String> params = new HashMap<>();
+        params.put("userid", playerId);
+        String[] response = sendPostRequest(route, params);
+        checkStatusCode(response);
+        Tournament tournament = json.fromJson(Tournament.class, response[1]);
+        checkObjectNotNull(tournament);
+        return tournament;
+    }
 
+    public static Tournament createNewTournament(Tournament tournament) throws IOException, NoSuchElementException{
+        //createNewTournament(String playerId, String[] gameIds, String name,
+        // double timePerRound, int maxPlayers, int roundsPerGame)
+        String route = "tournament/new";
+        Map<String, String> params = new HashMap<>();
+        params.put("userid", tournament.getPlayers().get(0));
+        params.put("games", tournament.getGames().toString());
+        params.put("name", tournament.getName());
+        params.put("timePerRound", Double.toString(tournament.getTimePerRound()));
+        params.put("maxPlayers", Integer.toString(tournament.getMaxPlayers()));
+        params.put("roundsPerGame", Integer.toString(tournament.getRoundsPerGame()));
+        //System.out.println(params);
+        String[] response = sendPostRequest(route, params);
+        checkStatusCode(response);
+        Tournament createdTournament = json.fromJson(Tournament.class, response[1]);
+        checkObjectNotNull(tournament);
+        return createdTournament;
+    }
 }
diff --git a/frontend/core/src/com/gameware/game/models/Tournament.java b/frontend/core/src/com/gameware/game/models/Tournament.java
index d179c52..c6040f8 100644
--- a/frontend/core/src/com/gameware/game/models/Tournament.java
+++ b/frontend/core/src/com/gameware/game/models/Tournament.java
@@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.Json;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.TimeZone;
@@ -24,6 +25,20 @@ public class Tournament {
     public Tournament() {
     }
 
+    //createNewTournament(String playerId, String[] gameIds, String name,
+    // double timePerRound, int maxPlayers, int roundsPerGame)
+
+
+    public Tournament(String playerId, List<String> games, String name, double timePerRound, int maxPlayers, int roundsPerGame) {
+        this.players = new ArrayList<>();
+        this.players.add(playerId);
+        this.games = games;
+        this.name = name;
+        this.timePerRound = timePerRound;
+        this.maxPlayers = maxPlayers;
+        this.roundsPerGame = roundsPerGame;
+    }
+
     public String get_id() {
         return _id;
     }
-- 
GitLab