Commit e96d9917 authored by Tobias Ingebrigt Ørstad's avatar Tobias Ingebrigt Ørstad
Browse files

Merge branch 'dev' into 'master'

Dev

See merge request tobiasio/progark-gruppe-3!104
parents de7e4ee1 f204cbbb
......@@ -220,6 +220,8 @@ router.post("/join", (req, res) => {
active: true,
// the array of players cannot already contain the player
players: { $nin: [playerId] },
// we dont want tournaments that have gone past their starttime
startTime: { $gte: new Date() },
},
// Add the player to the array
{
......@@ -500,6 +502,17 @@ function roundcheck(client, tournament) {
//if there are less then two people left in the first round, or zero people left in the first round
//the tournament ends
}
if (rounds.length > 0) {
// If there is only one player in the tournaments first round and the starttime
// has expired, we end the tournament
if (
left.length < 2 &&
active == 1 &&
new Date(start) < new Date()
) {
end = true;
}
}
functions
.timeOut(client, tournament, timedOut, timedOutRounds)
.then(
......
......@@ -16,7 +16,7 @@ import com.gameware.game.models.Player;
import com.gameware.game.states.GameStateManager;
import com.gameware.game.states.LoginState;
import com.gameware.game.states.MenuState;
import com.gameware.game.states.PlayStateComposite;
import com.gameware.game.states.PlayStateTemplate;
import java.io.IOException;
import java.lang.reflect.Constructor;
......@@ -44,7 +44,7 @@ public class GameWare extends ApplicationAdapter {
private Player player;
private List<Game> games = new ArrayList<>();
private Map<String, PlayStateComposite> gameIdToPlayState = new HashMap<>();
private Map<String, PlayStateTemplate> gameIdToPlayState = new HashMap<>();
//Singleton (lazy initialization)
private GameWare(){ }
......@@ -60,10 +60,6 @@ public class GameWare extends ApplicationAdapter {
@Override
public void create () {
if(instance == null){
instance = this;
}
gsm = GameStateManager.getInstance();
loadGameConfigs();
......@@ -121,11 +117,11 @@ public class GameWare extends ApplicationAdapter {
return games;
}
public Map<String, PlayStateComposite> getGameIdToPlayState(){
public Map<String, PlayStateTemplate> getGameIdToPlayState(){
return gameIdToPlayState;
}
public void updateGameMap(String id, PlayStateComposite state){
public void updateGameMap(String id, PlayStateTemplate state){
gameIdToPlayState.put(id, state);
}
......@@ -201,7 +197,7 @@ public class GameWare extends ApplicationAdapter {
try {
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(GameStateManager.class);
PlayStateComposite state = (PlayStateComposite) con.newInstance(gsm);
PlayStateTemplate state = (PlayStateTemplate) con.newInstance(gsm);
gameIdToPlayState.put(id, state);
} catch (Exception e) {
e.printStackTrace();
......
......@@ -31,13 +31,15 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TimeZone;
public class QueryIntermediate {
public final class QueryIntermediate {
/*
QueryIntermediate works as connector between frontend and backend. It takes care of all
communications and requests with backend.
*/
private QueryIntermediate(){}
private static String baseUrl = GameWare.apiBaseUrl;
private static Json json = new Json();
......@@ -160,6 +162,15 @@ public class QueryIntermediate {
return tournaments;
}
public static Tournament getSpecificTournament(String tournamentID) throws IOException, NoSuchElementException {
String route = "tournament/specific/" + tournamentID;
String[] response = sendGetRequest(route);
checkStatusCode(response);
Tournament tournament = json.fromJson(Tournament.class, response[1]);
checkObjectNotNull(tournament);
return tournament;
}
public static Tournament joinATournament(String playerId) throws IOException, NoSuchElementException {
String route = "tournament/join/";
Map<String, String> params = new HashMap<>();
......
package com.gameware.game.models;
public class LocalStorage {
import com.badlogic.gdx.utils.Json;
public class LocalStorage implements ModelInterface {
private Player player;
private Boolean musicOn;
private Boolean soundEffects;
......@@ -31,4 +33,24 @@ public class LocalStorage {
public Boolean getIncludeFin() {
return includeFin;
}
@Override
public void reset() {
player = null;
musicOn = true; // default value
soundEffects = true; // default value
includeFin = false; // default value
}
@Override
public String report() {
Json json = new Json();
json.setUsePrototypes(false);
return json.toJson(this);
}
@Override
public String toString() {
return report();
}
}
......@@ -7,7 +7,7 @@ import com.gameware.game.sprites.Bubble;
import java.util.ArrayList;
public class BubbleWrapState extends PlayStateComposite {
public class BubbleWrapState extends PlayStateTemplate {
private Texture background;
private Texture unpopped;
private Texture popped1;
......
......@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ColorRushState extends PlayStateComposite {
public class ColorRushState extends PlayStateTemplate {
private List<ColorRushTarget> activeTargets;
private List<ColorRushTarget> previousTargets;
......
......@@ -26,7 +26,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateJoinTournamentState extends State {
public class CreateJoinTournamentState extends MenuStateTemplate {
// Data
private List<Tournament> tournaments = new ArrayList<>();
......@@ -107,7 +107,7 @@ public class CreateJoinTournamentState extends State {
}
}
private void makeStage(){
protected void makeStage(){
Table rootTable = makeRootTable();
titleLabel.setFontScale(titleFontBigScale);
......@@ -250,7 +250,7 @@ public class CreateJoinTournamentState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
......@@ -27,7 +27,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateNewTournamentState extends State {
public class CreateNewTournamentState extends MenuStateTemplate {
// Data
private List<Game> games;
......@@ -46,18 +46,16 @@ public class CreateNewTournamentState extends State {
private final Label timePerRoundLabel = new Label("Days per round", skin);
private final Label maxPlayersLabel = new Label("Max players", skin);
private final Label roundsPerGameLabel = new Label("Rounds per game", skin);
private final Label startDelayLabel = new Label("Days before start", skin);
private final Label nameLabelQuestion = new Label("Name of the tournament. This will be shown to other players", skin);
private final Label timePerRoundLabelQuestion = new Label("Number of days that players have to finish their own turn in the tournament. If they don't finish within the dealine they are kicked out of the tournament.", skin);
private final Label timePerRoundLabelQuestion = new Label("Number of days that players have to finish their own turn in the tournament. If they don't finish within the dealine they are kicked out of the tournament.\n\nEven if max number of players has not been reached, the tournament still starts after specified number of days.", skin);
private final Label maxPlayersLabelQuestion = new Label("Max number of players able to enter the tournament. If max number of players enter and play their round, then the tournament continues.", skin);
private final Label roundsPerGameLabelQuestion = new Label("Number of rounds per game. For example if you choose 2 games, and have 3 rounds per game then you will play each game 3 times.", skin);
private final Label startDelayLabelQuestion = new Label("Max days before tournament starts. Even if max number of players has not been reached, the tournament still starts after specified number of days.", skin);
// Texts
private final String backBtnText = "Back";
private final String createBtnText = "Create";
private final String nextBtnText = "Next";
private final String nextBtnText = "Select games";
private final String noNameText = "No name given";
private final String noGamesSelectedText = "No games selected";
......@@ -69,14 +67,12 @@ public class CreateNewTournamentState extends State {
private SelectBox timePerRoundSelectBox = new SelectBox(skin);
private SelectBox maxPlayersSelectBox = new SelectBox(skin);
private SelectBox roundsPerGameSelectBox = new SelectBox(skin);
private SelectBox startDelaySelectBox = new SelectBox(skin);
// User inputs
private String nameUserInput = "";
private int timePerRoundUserInput = 1;
private int maxPlayersUserInput = 2;
private int roundsPerGameUserInput = 1;
private int startDelayUserInput = 1;
// Variables
private int page = 1;
......@@ -94,7 +90,6 @@ public class CreateNewTournamentState extends State {
private Dialog timePerRoundDialog;
private Dialog maxPlayersDialog;
private Dialog roundsPerGameDialog;
private Dialog startDelayDialog;
// Loading text
private LoadingText loadingText = new LoadingText();
......@@ -113,18 +108,16 @@ public class CreateNewTournamentState extends State {
timePerRoundSelectBox.setItems(1,2,3,4,5);
maxPlayersSelectBox.setItems(2,3,4,5);
roundsPerGameSelectBox.setItems(1,2,3);
startDelaySelectBox.setItems(1,2,3);
nameDialog = makeDialog(nameLabelQuestion);
timePerRoundDialog = makeDialog(timePerRoundLabelQuestion);
maxPlayersDialog = makeDialog(maxPlayersLabelQuestion);
roundsPerGameDialog = makeDialog(roundsPerGameLabelQuestion);
startDelayDialog = makeDialog(startDelayLabelQuestion);
nameDialog = makeQuestionIconDialog(nameLabelQuestion);
timePerRoundDialog = makeQuestionIconDialog(timePerRoundLabelQuestion);
maxPlayersDialog = makeQuestionIconDialog(maxPlayersLabelQuestion);
roundsPerGameDialog = makeQuestionIconDialog(roundsPerGameLabelQuestion);
makeStage();
}
private void makeStage(){
protected void makeStage(){
rootTable = makeRootTable();
titleLabel.setFontScale(titleFontBigScale);
......@@ -146,22 +139,19 @@ public class CreateNewTournamentState extends State {
innerTable.pad(spacingLittle);
innerTable.setBackground(backgroundTableBlueRounded);
innerTable.add(makeRow(nameLabel, nameDialog));
nameInputField.setMaxLength(10);
innerTable.add(makeTableWithLabelAndQuestionIcon(nameLabel, nameDialog));
nameInputField.setMaxLength(9);
innerTable.add(nameInputField).size(nameFieldWidth, inputFieldHeight);
innerTable.row();
removeKeyPadAtTouch();
innerTable.add(makeRow(timePerRoundLabel, timePerRoundDialog));
innerTable.add(timePerRoundSelectBox);
innerTable.row();
innerTable.add(makeRow(maxPlayersLabel, maxPlayersDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(maxPlayersLabel, maxPlayersDialog));
innerTable.add(maxPlayersSelectBox);
innerTable.row();
innerTable.add(makeRow(roundsPerGameLabel, roundsPerGameDialog));
innerTable.add(makeTableWithLabelAndQuestionIcon(roundsPerGameLabel, roundsPerGameDialog));
innerTable.add(roundsPerGameSelectBox);
innerTable.row();
innerTable.add(makeRow(startDelayLabel, startDelayDialog));
innerTable.add(startDelaySelectBox);
innerTable.add(makeTableWithLabelAndQuestionIcon(timePerRoundLabel, timePerRoundDialog));
innerTable.add(timePerRoundSelectBox);
rootTable.add(innerTable).colspan(2);
rootTable.row();
rootTable.add(errorLabel).colspan(2);
......@@ -169,7 +159,7 @@ public class CreateNewTournamentState extends State {
rootTable.add(makeBackBtn()).expand().bottom().left();
TextButton nextBtn = makeNextBtn();
rootTable.add(nextBtn).size(buttonWidth, buttonHeight).expand().bottom().right();
rootTable.add(nextBtn).size(buttonWidth*1.2f, buttonHeight).expand().bottom().right();
}
private void makePageTwo(){
......@@ -186,11 +176,11 @@ public class CreateNewTournamentState extends State {
continue;
}
innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).screenshot)).width(imageWidthAndHeigh).height(imageWidthAndHeigh);
innerTable.add(new Image(GameWare.getInstance().getGameIdToPlayState().get(g.getId()).getScreenshot())).width(imageWidthAndHeigh).height(imageWidthAndHeigh);
Table innerInnerTable = new Table();
innerInnerTable.defaults().space(spacingLittle);
innerInnerTable.add(makeRow(new Label(g.getName(), skin), makeDialog(new Label(g.getExplanation().replaceAll("\\\\n", "\n"), skin)))).spaceBottom(spacingLittle);
innerInnerTable.add(makeTableWithLabelAndQuestionIcon(new Label(g.getName(), skin), makeQuestionIconDialog(new Label(g.getExplanation().replaceAll("\\\\n", "\n"), skin)))).spaceBottom(spacingLittle);
innerInnerTable.row();
CheckBox gameCB = new CheckBox("",skin);
......@@ -235,7 +225,7 @@ public class CreateNewTournamentState extends State {
}
private TextButton makeCreateBtn() {
TextButton createBtn = new TextButton(createBtnText, skin);
TextButton createBtn = new TextButton(createBtnText, makeTextButtonStyle(greenColor));
createBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......@@ -264,7 +254,6 @@ public class CreateNewTournamentState extends State {
timePerRoundUserInput = getTimePerRound();
maxPlayersUserInput = getMaxPlayers();
roundsPerGameUserInput = getRoundsPerGame();
startDelayUserInput = getStartDelay();
stage.clear();
makeStage();
......@@ -274,7 +263,7 @@ public class CreateNewTournamentState extends State {
}
private TextButton makeBackBtn() {
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......@@ -339,7 +328,6 @@ public class CreateNewTournamentState extends State {
timePerRoundSelectBox.setSelected(timePerRoundUserInput);
maxPlayersSelectBox.setSelected(maxPlayersUserInput);
roundsPerGameSelectBox.setSelected(roundsPerGameUserInput);
startDelaySelectBox.setSelected(startDelayUserInput);
}
}
......@@ -350,7 +338,7 @@ public class CreateNewTournamentState extends State {
return;
}
try {
tournament = new Tournament(GameWare.getInstance().getPlayer().getId(),chosenGames,nameUserInput, timePerRoundUserInput, maxPlayersUserInput, roundsPerGameUserInput,1,chosenGames.size(),startDelayUserInput);
tournament = new Tournament(GameWare.getInstance().getPlayer().getId(),chosenGames,nameUserInput, timePerRoundUserInput, maxPlayersUserInput, roundsPerGameUserInput,1,chosenGames.size(),timePerRoundUserInput);
tournamentFromDB = QueryIntermediate.createNewTournament(tournament);
round = QueryIntermediate.getRoundFromTournament(tournamentFromDB.get_id(), GameWare.getInstance().getPlayer().getId(), tournamentFromDB.getCurrentRound());
gsm.set(new ViewTournamentState(gsm,tournamentFromDB,round));
......@@ -376,7 +364,6 @@ public class CreateNewTournamentState extends State {
return (int) roundsPerGameSelectBox.getSelected();
}
private int getStartDelay(){ return (int) startDelaySelectBox.getSelected(); }
private void removeKeyPadAtTouch(){
stage.getRoot().addCaptureListener(new InputListener() {
......@@ -404,12 +391,10 @@ public class CreateNewTournamentState extends State {
timePerRoundUserInput = 1;
maxPlayersUserInput = 2;
roundsPerGameUserInput = 1;
startDelayUserInput = 1;
timePerRoundSelectBox.setSelected(timePerRoundUserInput);
maxPlayersSelectBox.setSelected(maxPlayersUserInput);
roundsPerGameSelectBox.setSelected(roundsPerGameUserInput);
startDelaySelectBox.setSelected(startDelayUserInput);
for(CheckBox cb : checkBoxes){
cb.setChecked(false);
......
......@@ -18,7 +18,7 @@ import com.gameware.game.models.Tournament;
import java.util.ArrayList;
import java.util.List;
public class FinishedTournamentState extends State {
public class FinishedTournamentState extends MenuStateTemplate {
// Data
private Tournament tournament;
......@@ -56,7 +56,7 @@ public class FinishedTournamentState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = makeRootTable(backgroundFinTourn);
rootTable.defaults().spaceBottom(spacingMedium);
......@@ -135,7 +135,7 @@ public class FinishedTournamentState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backBtnText, skin);
TextButton backBtn = new TextButton(backBtnText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
......@@ -12,7 +12,7 @@ import com.gameware.game.sprites.SlicingCircle;
import java.util.ArrayList;
import java.util.List;
public class FruitSlicerState extends PlayStateComposite {
public class FruitSlicerState extends PlayStateTemplate {
private int totalFruitsCut = 0;
private float startingEmitFrequency = 1f;
private float endingEmitFrequency = 0.2f;
......
package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
......@@ -17,7 +19,7 @@ import com.gameware.game.sprites.LoadingText;
import java.io.IOException;
import java.util.NoSuchElementException;
public class LoginState extends State {
public class LoginState extends MenuStateTemplate {
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......@@ -63,7 +65,7 @@ public class LoginState extends State {
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = super.makeRootTable();
// Add widgets
......@@ -196,14 +198,11 @@ public class LoginState extends State {
}
private TextButton makeBackBtn(){
TextButton backBtn = new TextButton(backText, skin);
TextButton backBtn = new TextButton(backText, makeTextButtonStyle(backColor));
backBtn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
page --;
errorLabel.setText("");
stage.clear();
makeStage();
handleBackBtnClicked();
}
});
return backBtn;
......@@ -216,6 +215,13 @@ public class LoginState extends State {
String username = usernameInputField.getText();
String password = passwordInputField.getText();
//Check if username = Username
if(username.equals("Username")){
errorLabel.setText(wrongLoginText);
return;
}
try {
Player player = QueryIntermediate.loginPlayer(username, password);
......@@ -267,6 +273,13 @@ public class LoginState extends State {
}
}
private void handleBackBtnClicked(){
page --;
errorLabel.setText("");
stage.clear();
makeStage();
}
// Adds listener to stage that removes keyboard on touch
private void removeKeyPadAtTouch(){
stage.getRoot().addCaptureListener(new InputListener() {
......@@ -320,6 +333,13 @@ public class LoginState extends State {
this.loadingText.setLoading();
}
@Override
protected void handleInput() {
if (Gdx.input.isKeyJustPressed(Input.Keys.BACK) && page == 1){
handleBackBtnClicked();
}
}
@Override
public void dispose() {
stage.dispose();
......
......@@ -2,6 +2,7 @@ package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
......@@ -15,7 +16,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.gameware.game.GameWare;
import com.gameware.game.sprites.LoadingText;
public class MenuState extends State{
public class MenuState extends MenuStateTemplate{
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......@@ -44,14 +45,13 @@ public class MenuState extends State{
private boolean multiBtnClicked = false;
private LoadingText loadingText = new LoadingText();
public MenuState(GameStateManager gsm) {
super(gsm);
makeStage();
}
private void makeStage(){
protected void makeStage(){
Table rootTable = super.makeRootTable();
// Add widgets
......@@ -140,7 +140,7 @@ public class MenuState extends State{
Table table = new Table();
table.add(new Image(logOut)).width(logOutWidth).height(buttonHeight/3f).spaceRight(spacingLittle/2);
table.add(new Label(logOutBtnText, skin, "big")).spaceRight(spacingLittle/2);
Button btn = new Button(table, skin);
Button btn = new Button(table, makeTextButtonStyle(backColor));
btn.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y){
......
package com.gameware.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.gameware.game.GameWare;
public abstract class MenuStateTemplate extends State{
// Font
protected final float fontScale = 3f;
protected final float tinierTitleFontBigScale = 1.5f;
protected final float titleFontBigScale = 2.5f;
// Spacing
protected final float spacingLittle = Gdx.graphics.getHeight()/50;
protected final float spacingMedium = Gdx.graphics.getHeight()/15;
protected final float spacingLarge = spacingMedium*2;
// Padding
protected final int rootTablePadding = Gdx.graphics.getWidth()/15;
protected final int rootTablePaddingTop = Gdx.graphics.getWidth()/10;
protected final int padding = 50;
// Width & Height
protected final int buttonWidth = Gdx.graphics.getWidth()/3;
protected final int buttonHeight = Gdx.graphics.getHeight()/12;
protected final float questionMarkWidthAndHeight = Gdx.graphics.getWidth()/25;