Commit 7fc78a72 authored by Ivar Nordvik Myrstad's avatar Ivar Nordvik Myrstad
Browse files

Merge branch 'dev' into 'master'

Dev

See merge request tobiasio/progark-gruppe-3!106
parents e96d9917 1fac56ac
const express = require("express");
const router = express.Router();
const mongo = require("mongodb");
const bcrypt = require("bcrypt");
const MongoClient = mongo.MongoClient;
const connectionUrl = process.env.MONGO_CONNECTION_STRING;
const saltRounds = 10;
router.get("/username/:playerId", (req, res) => {
// Connect to database
......@@ -67,14 +69,30 @@ router.get("/login/:username/:password", (req, res) => {
db.collection(collection)
.find({
name: req.params.username,
password: req.params.password,
})
.toArray((err, result) => {
if (err) {
res.sendStatus(500);
return;
}
res.json(result);
// Compares the given password with the encrypted password stored in the database,
// response is true on match, false else
bcrypt.compare(
req.params.password,
result[0].password,
(err, response) => {
if (err) {
res.sendStatus(500);
client.close();
return;
}
if (response) {
res.json(result);
} else {
res.json([]);
}
}
);
client.close();
});
}
......@@ -103,24 +121,30 @@ router.put("/", (req, res) => {
res.status(400).send("Invalid parameters");
return;
}
// Inserts the user. Note that the name index is unique, inserting a user with an
// already existing username will give an error.
db.collection(collection).insertOne(
{
name: req.body.username,
password: req.body.password,
dateJoined: date,
},
(err, result) => {
if (err) {
res.status(400).send("Already existing username"); // Internal server error
return;
}
res.json(result.ops[0]);
client.close();
//Hashes the password
bcrypt.hash(req.body.password, saltRounds, (err, hash) => {
if (err) {
res.sendStatus(500); // Internal server error
return;
}
);
// Inserts the user. Note that the name index is unique, inserting a user with an
// already existing username will give an error.
db.collection(collection).insertOne(
{
name: req.body.username,
password: hash,
dateJoined: date,
},
(err, result) => {
if (err) {
res.status(400).send("Already existing username");
return;
}
res.json(result.ops[0]);
client.close();
}
);
});
}
);
});
......
This diff is collapsed.
......@@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^4.0.1",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
......
......@@ -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.PlayStateTemplate;
import com.gameware.game.states.PlayStateUnion;
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, PlayStateTemplate> gameIdToPlayState = new HashMap<>();
private Map<String, PlayStateUnion> gameIdToPlayState = new HashMap<>();
//Singleton (lazy initialization)
private GameWare(){ }
......@@ -117,11 +117,11 @@ public class GameWare extends ApplicationAdapter {
return games;
}
public Map<String, PlayStateTemplate> getGameIdToPlayState(){
public Map<String, PlayStateUnion> getGameIdToPlayState(){
return gameIdToPlayState;
}
public void updateGameMap(String id, PlayStateTemplate state){
public void updateGameMap(String id, PlayStateUnion state){
gameIdToPlayState.put(id, state);
}
......@@ -197,7 +197,7 @@ public class GameWare extends ApplicationAdapter {
try {
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(GameStateManager.class);
PlayStateTemplate state = (PlayStateTemplate) con.newInstance(gsm);
PlayStateUnion state = (PlayStateUnion) con.newInstance(gsm);
gameIdToPlayState.put(id, state);
} catch (Exception e) {
e.printStackTrace();
......
......@@ -7,7 +7,7 @@ import com.gameware.game.sprites.Bubble;
import java.util.ArrayList;
public class BubbleWrapState extends PlayStateTemplate {
public class BubbleWrapState extends PlayStateUnion {
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 PlayStateTemplate {
public class ColorRushState extends PlayStateUnion {
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 MenuStateTemplate {
public class CreateJoinTournamentState extends MenuStateUnion {
// Data
private List<Tournament> tournaments = new ArrayList<>();
......
......@@ -27,7 +27,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateNewTournamentState extends MenuStateTemplate {
public class CreateNewTournamentState extends MenuStateUnion {
// Data
private List<Game> games;
......
......@@ -18,7 +18,7 @@ import com.gameware.game.models.Tournament;
import java.util.ArrayList;
import java.util.List;
public class FinishedTournamentState extends MenuStateTemplate {
public class FinishedTournamentState extends MenuStateUnion {
// Data
private Tournament tournament;
......
......@@ -12,7 +12,7 @@ import com.gameware.game.sprites.SlicingCircle;
import java.util.ArrayList;
import java.util.List;
public class FruitSlicerState extends PlayStateTemplate {
public class FruitSlicerState extends PlayStateUnion {
private int totalFruitsCut = 0;
private float startingEmitFrequency = 1f;
private float endingEmitFrequency = 0.2f;
......
......@@ -2,7 +2,6 @@ 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;
......@@ -19,7 +18,7 @@ import com.gameware.game.sprites.LoadingText;
import java.io.IOException;
import java.util.NoSuchElementException;
public class LoginState extends MenuStateTemplate {
public class LoginState extends MenuStateUnion {
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......
......@@ -2,7 +2,6 @@ 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;
......@@ -16,7 +15,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.gameware.game.GameWare;
import com.gameware.game.sprites.LoadingText;
public class MenuState extends MenuStateTemplate{
public class MenuState extends MenuStateUnion {
// Labels
private final Label titleLabel = new Label("GameWare", skin, "big");
......
......@@ -18,7 +18,7 @@ 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{
public abstract class MenuStateUnion extends State{
// Font
protected final float fontScale = 3f;
......@@ -55,7 +55,7 @@ public abstract class MenuStateTemplate extends State{
protected Sound checkBoxSound;
public MenuStateTemplate(GameStateManager gsm){
public MenuStateUnion(GameStateManager gsm){
super(gsm);
skin.getFont("font").getData().setScale(fontScale);
......
......@@ -12,7 +12,7 @@ import com.badlogic.gdx.utils.Scaling;
import com.gameware.game.GameWare;
public class OptionsState extends MenuStateTemplate {
public class OptionsState extends MenuStateUnion {
// Labels
private final Label titleLabel = new Label("Options", skin, "big");
......
......@@ -16,7 +16,7 @@ import java.util.List;
public class PauseState extends State {
// Data
private PlayStateTemplate pausedGame;
private PlayStateUnion pausedGame;
// Textures
private Texture background;
......@@ -40,7 +40,7 @@ public class PauseState extends State {
private LoadingText loadingText;
public PauseState(GameStateManager gsm, PlayStateTemplate pausedGame) {
public PauseState(GameStateManager gsm, PlayStateUnion pausedGame) {
super(gsm);
this.background = new Texture(Gdx.files.internal("pause/PauseBackground.jpg"));
......
......@@ -26,7 +26,7 @@ import java.io.IOException;
import java.util.List;
public abstract class PlayStateTemplate extends State {
public abstract class PlayStateUnion extends State {
// Data
private Round round = null;
......@@ -51,7 +51,7 @@ public abstract class PlayStateTemplate extends State {
private boolean currentScoreVisible = true;
public PlayStateTemplate(GameStateManager gsm){
public PlayStateUnion(GameStateManager gsm){
super(gsm);
// Default pause button (black color)
......
......@@ -11,7 +11,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Align;
import com.gameware.game.GameWare;
public class ScoreState extends MenuStateTemplate {
public class ScoreState extends MenuStateUnion {
// Data
private State nextState;
......
......@@ -17,7 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SinglePlayerSelectGameState extends MenuStateTemplate {
public class SinglePlayerSelectGameState extends MenuStateUnion {
// Data
private List<Game> games = new ArrayList<>();
......@@ -124,7 +124,7 @@ public class SinglePlayerSelectGameState extends MenuStateTemplate {
@Override
public void clicked(InputEvent event, float x, float y) {
Map<String, PlayStateTemplate> map = GameWare.getInstance().getGameIdToPlayState();
Map<String, PlayStateUnion> map = GameWare.getInstance().getGameIdToPlayState();
State s = map.get(game.getId());
handleGameBtnClick(s);
};
......
......@@ -10,7 +10,7 @@ import com.gameware.game.GameWare;
public abstract class State {
// Common variables between MenuStateTemplate and PlayStateTemplate
// Common variables between MenuStateUnion and PlayStateUnion
// Data
protected GameStateManager gsm;
......
......@@ -17,7 +17,7 @@ import com.gameware.game.models.Tournament;
import java.util.ArrayList;
import java.util.List;
public class TournamentHighScoreState extends MenuStateTemplate{
public class TournamentHighScoreState extends MenuStateUnion {
// Data
private Tournament tournament;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment