Skip to content
Snippets Groups Projects
Commit 8f5ff23f authored by ivarnm's avatar ivarnm
Browse files

#10 Add create and join new tournament QI

parent 99969276
No related branches found
No related tags found
1 merge request!34Resolve "SinglePlayerSelectGameState"
...@@ -20,10 +20,15 @@ router.post("/new", (req, res) => { ...@@ -20,10 +20,15 @@ router.post("/new", (req, res) => {
let tournamentid; let tournamentid;
const tournamentdate = new Date(); const tournamentdate = new Date();
let tournyresult; let tournyresult;
const games = req.body.games
.substring(1, req.body.games.length - 1)
.replace(" ", "")
.split(",");
console.log(games);
db.collection(collection).insertOne( db.collection(collection).insertOne(
{ {
players: [mongo.ObjectID(req.body.userid)], players: [mongo.ObjectID(req.body.userid)],
games: req.body.games, games: games,
name: req.body.name, name: req.body.name,
timePerRound: parseInt(req.body.timePerRound), timePerRound: parseInt(req.body.timePerRound),
maxPlayers: parseInt(req.body.maxPlayers), maxPlayers: parseInt(req.body.maxPlayers),
...@@ -52,7 +57,7 @@ router.post("/new", (req, res) => { ...@@ -52,7 +57,7 @@ router.post("/new", (req, res) => {
{ {
tournamentId: mongo.ObjectID(tournyid), tournamentId: mongo.ObjectID(tournyid),
playerId: mongo.ObjectID(req.body.userid), playerId: mongo.ObjectID(req.body.userid),
gameId: mongo.ObjectID(req.body.games[0]), gameId: mongo.ObjectID(games[0]),
scoreValue: 0, scoreValue: 0,
roundNr: 1, roundNr: 1,
hasPlayed: false, hasPlayed: false,
...@@ -63,7 +68,7 @@ router.post("/new", (req, res) => { ...@@ -63,7 +68,7 @@ router.post("/new", (req, res) => {
res.sendStatus(500); // Internal server error res.sendStatus(500); // Internal server error
return; return;
} }
res.json(tournyresult); res.json(tournyresult.ops[0]);
} }
); );
} }
......
...@@ -14,8 +14,10 @@ import com.gameware.game.models.Tournament; ...@@ -14,8 +14,10 @@ import com.gameware.game.models.Tournament;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.HashMap;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
public class QueryIntermediate { public class QueryIntermediate {
...@@ -86,6 +88,12 @@ 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 ---------------- // ---------------- Games methods ----------------
public static Game getGameById(String gameId) throws IOException, NoSuchElementException { public static Game getGameById(String gameId) throws IOException, NoSuchElementException {
String route = "games/gamename/" + gameId; String route = "games/gamename/" + gameId;
...@@ -103,6 +111,7 @@ public class QueryIntermediate { ...@@ -103,6 +111,7 @@ public class QueryIntermediate {
checkStatusCode(response); checkStatusCode(response);
JsonValue base = jsonReader.parse(response[1]); JsonValue base = jsonReader.parse(response[1]);
JsonValue.JsonIterator iterator = base.iterator(); JsonValue.JsonIterator iterator = base.iterator();
checkIteratorNotEmpty(iterator);
while (iterator.hasNext()) { while (iterator.hasNext()) {
Game game = json.fromJson(Game.class, iterator.next().toString()); Game game = json.fromJson(Game.class, iterator.next().toString());
games.add(game); games.add(game);
...@@ -119,6 +128,7 @@ public class QueryIntermediate { ...@@ -119,6 +128,7 @@ public class QueryIntermediate {
checkStatusCode(response); checkStatusCode(response);
JsonValue base = jsonReader.parse(response[1]); JsonValue base = jsonReader.parse(response[1]);
JsonValue.JsonIterator iterator = base.iterator(); JsonValue.JsonIterator iterator = base.iterator();
checkIteratorNotEmpty(iterator);
while (iterator.hasNext()) { while (iterator.hasNext()) {
Tournament tournament = json.fromJson(Tournament.class, iterator.next().toString()); Tournament tournament = json.fromJson(Tournament.class, iterator.next().toString());
tournaments.add(tournament); tournaments.add(tournament);
...@@ -126,7 +136,33 @@ public class QueryIntermediate { ...@@ -126,7 +136,33 @@ public class QueryIntermediate {
return tournaments; 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;
}
} }
...@@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.Json; ...@@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.Json;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
...@@ -24,6 +25,20 @@ public class Tournament { ...@@ -24,6 +25,20 @@ public class Tournament {
public 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() { public String get_id() {
return _id; return _id;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment