mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-17 08:39:06 +00:00
Added web server and partly integrated frontend
This commit is contained in:
@@ -2,8 +2,15 @@ package net.simon987.server;
|
||||
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.webserver.SocketServer;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import spark.ModelAndView;
|
||||
import spark.Spark;
|
||||
import spark.template.velocity.VelocityTemplateEngine;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
public class Main {
|
||||
@@ -22,5 +29,36 @@ public class Main {
|
||||
|
||||
(new Thread(socketServer)).start();
|
||||
(new Thread(GameServer.INSTANCE)).start();
|
||||
|
||||
//TEST ---------------------------
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("file.resource.loader.path", "templates/");
|
||||
VelocityTemplateEngine templateEngine = new VelocityTemplateEngine(new VelocityEngine(properties));
|
||||
//--
|
||||
|
||||
Spark.staticFiles.externalLocation("static");
|
||||
|
||||
Spark.get("/", (request, response) -> {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
System.out.println((String) request.session().attribute("user"));
|
||||
|
||||
return new ModelAndView(model, "home.vm");
|
||||
}, templateEngine);
|
||||
|
||||
Spark.get("/leaderboard", (request, response) -> {
|
||||
return new ModelAndView(new HashMap<>(), "leaderboard.vm");
|
||||
}, templateEngine);
|
||||
|
||||
Spark.get("/play", (request, response) -> {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
return new ModelAndView(model, "play.vm");
|
||||
}, templateEngine);
|
||||
|
||||
Spark.get("/account", (request, response) -> {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
return new ModelAndView(model, "account.vm");
|
||||
}, templateEngine);
|
||||
|
||||
Spark.after((request, response) -> response.header("Content-Encoding", "gzip"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package net.simon987.server.game;
|
||||
|
||||
//Alpha: ±5cm
|
||||
//Beta: 10-20 feet
|
||||
//Gamma: 100+ feet
|
||||
|
||||
public interface Radioactive {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package net.simon987.server.user;
|
||||
|
||||
import com.mongodb.*;
|
||||
import net.simon987.server.assembly.exception.CancelledException;
|
||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class UserManager {
|
||||
|
||||
private MongoClient mongo;
|
||||
private DBCollection userCollection;
|
||||
|
||||
public UserManager(MongoClient mongo) {
|
||||
|
||||
this.mongo = mongo;
|
||||
DB db = mongo.getDB("mar");
|
||||
userCollection = db.getCollection("user");
|
||||
}
|
||||
|
||||
public ArrayList<User> getUsers() {
|
||||
|
||||
ArrayList<User> userList = new ArrayList<>();
|
||||
|
||||
DBCursor cursor = userCollection.find();
|
||||
while (cursor.hasNext()) {
|
||||
try {
|
||||
userList.add(User.deserialize(cursor.next()));
|
||||
} catch (CancelledException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return userList;
|
||||
}
|
||||
|
||||
public void registerUser(User user, String password) {
|
||||
|
||||
DBObject dbUser = user.mongoSerialise();
|
||||
|
||||
String salt = BCrypt.gensalt(12);
|
||||
String hashedPassword = BCrypt.hashpw(password, salt);
|
||||
|
||||
dbUser.put("password", hashedPassword);
|
||||
|
||||
userCollection.save(dbUser);
|
||||
}
|
||||
|
||||
public boolean validateUser(String username, String password) {
|
||||
|
||||
DBObject where = new BasicDBObject();
|
||||
where.put("_id", username);
|
||||
|
||||
DBObject user = userCollection.findOne(where);
|
||||
|
||||
return user != null && BCrypt.checkpw(password, (String) user.get("password"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user