diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index 3cf0bfc..25029f8 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -26,16 +26,17 @@ import be.jeffcheasey88.peeratcode.routes.ChapterElement; import be.jeffcheasey88.peeratcode.routes.ChapterList; import be.jeffcheasey88.peeratcode.routes.DynamicLeaderboard; import be.jeffcheasey88.peeratcode.routes.Leaderboard; -import be.jeffcheasey88.peeratcode.routes.Login; import be.jeffcheasey88.peeratcode.routes.PlayerDetails; import be.jeffcheasey88.peeratcode.routes.PuzzleElement; import be.jeffcheasey88.peeratcode.routes.PuzzleResponse; -import be.jeffcheasey88.peeratcode.routes.Register; import be.jeffcheasey88.peeratcode.routes.Result; import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate; import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin; import be.jeffcheasey88.peeratcode.routes.groups.GroupList; import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit; +import be.jeffcheasey88.peeratcode.routes.users.Login; +import be.jeffcheasey88.peeratcode.routes.users.ProfileSettings; +import be.jeffcheasey88.peeratcode.routes.users.Register; public class Main{ public static void main(String[] args) throws Exception{ @@ -72,30 +73,33 @@ public class Main{ } private static void initRoutes(Router router, Configuration config){ - router.register(new ChapterElement(router.getDataBase())); - router.register(new ChapterList(router.getDataBase())); - router.register(new PuzzleElement(router.getDataBase())); - router.register(new Register(router.getDataBase(), router, config.getUsersFiles())); - router.register(new Login(router.getDataBase(), router)); - router.register(new Result(router.getDataBase())); - router.register(new Leaderboard(router.getDataBase())); - router.register(new PlayerDetails(router.getDataBase())); - router.register(new BadgeDetails(router.getDataBase())); + DatabaseRepository repo = router.getDataBase(); + router.register(new Register(repo, router, config.getUsersFiles())); + router.register(new Login(repo, router)); + router.register(new ProfileSettings(repo)); + + router.register(new ChapterElement(repo)); + router.register(new ChapterList(repo)); + router.register(new PuzzleElement(repo)); + router.register(new Result(repo)); + router.register(new Leaderboard(repo)); + router.register(new PlayerDetails(repo)); + router.register(new BadgeDetails(repo)); Locker groupLock = new Locker<>(); - router.register(new GroupCreate(router.getDataBase(), groupLock, config.getGroupJoinMinutes())); + router.register(new GroupCreate(repo, groupLock, config.getGroupJoinMinutes())); - DynamicLeaderboard dlb = new DynamicLeaderboard(router.getDataBase()); + DynamicLeaderboard dlb = new DynamicLeaderboard(repo); router.register(dlb); Locker leaderboard = dlb.getLocker(); - router.register(new PuzzleResponse(router.getDataBase(), config.getUsersFiles(), leaderboard)); - router.register(new GroupList(router.getDataBase())); - router.register(new GroupJoin(router.getDataBase(), config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard)); - router.register(new GroupQuit(router.getDataBase(), config.getGroupJoinMinutes(), leaderboard)); + router.register(new PuzzleResponse(repo, config.getUsersFiles(), leaderboard)); + router.register(new GroupList(repo)); + router.register(new GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard)); + router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard)); -// Bot bot = new Bot(config, router.getDataBase(), groupLock); +// Bot bot = new Bot(config, repo, groupLock); // bot.start(); } diff --git a/src/be/jeffcheasey88/peeratcode/model/Player.java b/src/be/jeffcheasey88/peeratcode/model/Player.java index 6e491d2..2eb6ea2 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Player.java +++ b/src/be/jeffcheasey88/peeratcode/model/Player.java @@ -36,6 +36,10 @@ public class Player implements Comparable { this.completions.add(new Completion(tries, score)); email = ""; // TO make compareTo and equals works as usual } + + public void setPseudo(String pseudo){ + this.pseudo = pseudo; + } public String getPseudo() { return this.pseudo; diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java deleted file mode 100644 index 65ef1a3..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ /dev/null @@ -1,110 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Class { - - private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$"); - - private int modifier; - private String name; - - private List vars; - private List functions; - - public Class(){} - - public int parse(String content) throws Exception{ - Matcher matcher = PATTERN.matcher(content); - matcher.matches(); - - String[] split = matcher.group(2).split("\\s+"); - for(int i = 0; i < split.length-1; i++){ - this.modifier+=JavaParser.getModifier(split[i]); - } - this.name = split[split.length-1]; - - this.vars = new ArrayList<>(); - this.functions = new ArrayList<>(); - - content = matcher.group(3); - Pattern empty = Pattern.compile("^\\s*$"); - while(!(empty.matcher(content).matches())){ - int quotes = indexOf(content,";"); - int braces = indexOf(content,"\\{"); - int equals = indexOf(content,"="); - if(quotes < braces && quotes < equals){ - boolean quote = false; - Variable last = null; - do { - Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType()); - int index = variable.parse(content); - this.vars.add(variable); - content = content.substring(index); - quote = content.startsWith(","); - if(quote) { - content = content.substring(1); - last = variable; - } - }while(quote); - }else if(equals < braces){ - //variable with value - boolean quote = false; - Variable last = null; - do { - Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType()); - int index = variable.parse(content); - this.vars.add(variable); - content = content.substring(index); - quote = content.startsWith(","); - if(quote) { - content = content.substring(1); - last = variable; - }else if(indexOf(content, "=") < indexOf(content, ";")){ - Operation operation = new Operation(); - index = operation.parse(content); - content = content.substring(index); - break; - } - }while(quote); - }else{ - System.out.println("Function "+content); - Function func = new Function(); - int index = func.parse(content); - this.functions.add(func); - content = content.substring(index); - System.out.println("End "+content); - } - } - - return matcher.group(1).length(); - } - - private int indexOf(String value, String target){ - return value.split(target)[0].length(); - } - - public int getModifier(){ - return this.modifier; - } - - public String getName(){ - return this.name; - } - - public List getVariables(){ - return this.vars; - } - - public void show(){ - System.out.println(Modifier.toString(modifier)+" "+this.name+"{"); - for(Variable v : this.vars) v.show(1); - System.out.println(); - for(Function f : this.functions) f.show(1); - System.out.println("}"); - } -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java b/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java deleted file mode 100644 index 36b50f0..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/CleanerPool.java +++ /dev/null @@ -1,94 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -public class CleanerPool { - - private static String CONSTANT_REPLACER_STRING = "$STRING_STATEMENT_CONSTANT_"; - private static String CONSTANT_REPLACER_CHAR = "$CHAR_STATEMENT_CONSTANT_"; - private static String CONSTANT_REPLACER_GENERIC = "$GENERIC_STATEMENT_CONSTANT_"; - - private List constants; - - private CleanerPool(){ - this.constants = new ArrayList<>(); - } - - public String clean(String statement){ - char[] chars = statement.toCharArray(); - StringBuilder builder = new StringBuilder(); - for(int i = 0; i < chars.length; i++){ - char current = chars[i]; - if(current== '"'){ - int constantPos = this.constants.size(); - String constant = cutConstant(chars, i); - i+=constant.length()+1; - builder.append(CONSTANT_REPLACER_STRING+constantPos); - this.constants.add(constant); - }else{ - builder.append(current); - } - } - - for(String s : constants){ - System.out.println("CONSTANT="+s); - } - return builder.toString(); - } - - public boolean isConstant(String region){ - return region.startsWith(CONSTANT_REPLACER_STRING); - } - - public String getConstant(String replacer){ - if(!replacer.startsWith(CONSTANT_REPLACER_STRING)) return null; - return this.constants.get(Integer.parseInt(replacer.replace(CONSTANT_REPLACER_STRING,""))); - } - - public List getConstants(){ - return this.constants; - } - - private static Pattern parenthesisPattern = Pattern.compile("^\\$SQL_STATEMENT_PARENTHESIS_([0-9]*$)"); - - public boolean isParenthesis(String region){ - return parenthesisPattern.matcher(region).matches(); - } - - private String cutConstant(char[] chars, int pos){ - StringBuilder builder = new StringBuilder(); - for(int i = pos+1; i < chars.length; i++){ - char current = chars[i]; - if(current == '"'){ - if(current == '\\'){ //toChange - builder.append(current); - }else break; - }else{ - builder.append(current); - } - } - return builder.toString(); - } - - public static interface Cutter{ - - String execute(String value, List constants, String pattern); - - default String cutOpenable(String value, List constants, String pattern, char open, char close){ - StringBuilder builder = new StringBuilder(); - - for(char c : value.toCharArray()){ - if(c == open){ - - }else if(c == close){ - - } - } - - return builder.toString(); - } - - } -} \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java b/src/be/jeffcheasey88/peeratcode/parser/java/Function.java deleted file mode 100644 index f849436..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Function.java +++ /dev/null @@ -1,85 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Function { - - private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$"); - - private int modifier; - private String name; - private String exceptions; - private String parameters; - - private List functions; - private List operations; - - public Function(){ - this.functions = new ArrayList<>(); - this.operations = new ArrayList<>(); - } - - public int parse(String content) throws Exception{ - Matcher matcher = PATTERN.matcher(content); - matcher.matches(); - - String[] split = matcher.group(2).split("\\s+"); - for(int i = 0; i < split.length-2; i++){ - this.modifier+=JavaParser.getModifier(split[i]); - } - this.name = split[split.length-1]; - this.parameters = matcher.group(3); - this.exceptions = matcher.group(4); - - String body = matcher.group(5); - int offset = 0; - int index = 0; - do { - int end = body.indexOf('}'); - int braces = body.indexOf('{'); - int quotes = body.indexOf(';'); - - if((end < 0) || (end < braces && end < quotes)){ - if(end > 0) offset+=end; - break; - } - - if(braces < 0 && quotes < 0){ - if(end > 0) offset+=end; - break; - } - - if(braces >= 0 && braces < quotes){ - Function func = new Function(); - index = func.parse(body.substring(0, end+1)); - this.functions.add(func); - }else{ - Operation op = new Operation(); - index = op.parse(body.substring(0, end+1)); - this.operations.add(op); - } - offset+=index+1; - body = body.substring(index); - }while(offset > -1); - return matcher.group(1).length()+offset; - } - - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+" {"); - for(Operation o : this.operations) o.show(tab+1); - System.out.println(); - for(Function f : this.functions) f.show(tab+1); - System.out.println(start+"}"); - } - - @Override - public String toString(){ - return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]"; - } -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Import.java b/src/be/jeffcheasey88/peeratcode/parser/java/Import.java deleted file mode 100644 index 283accd..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Import.java +++ /dev/null @@ -1,28 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Import { - - private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);).*$"); - - public static boolean isImport(String content){ - return PATTERN.matcher(content).matches(); - } - - private String name; - - public Import(){} - - public int parse(String content) throws Exception{ - Matcher matcher = PATTERN.matcher(content); - matcher.matches(); - this.name = matcher.group(2); - return matcher.group(1).length(); - } - - public String getName(){ - return this.name; - } -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java deleted file mode 100644 index 12043c9..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ /dev/null @@ -1,96 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - -public class JavaParser { - - public static void main(String[] args) throws Exception { - File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java"); - BufferedReader reader = new BufferedReader(new FileReader(file)); - - JavaParser parser = new JavaParser(reader); - parser.parse(); - System.out.println("SHOW-----------------"); - parser.show(); - } - - private Package pack; - private List imports; - private Class clazz; - - private BufferedReader reader; - - public JavaParser(BufferedReader reader){ - this.reader = reader; - } - - public void parse() throws Exception{ - String content = ""; - int index; - - String line; - while((line = reader.readLine()) != null) content+=line; - -// content = CleanerPool.getterToDelete.clean(content); - - this.pack = new Package(); - index = this.pack.parse(content); - content = content.substring(index); - - this.imports = new ArrayList<>(); - while(Import.isImport(content)){ - Import imp = new Import(); - index = imp.parse(content); - this.imports.add(imp); - content = content.substring(index); - } - - this.clazz = new Class(); - index = this.clazz.parse(content); - content = content.substring(index); - } - - public Package getPackage(){ - return this.pack; - } - - public List getImports(){ - return this.imports; - } - - public Class getClazz(){ - return this.clazz; - } - - public void show(){ - System.out.println("package "+this.pack.getName()+";"); - System.out.println(); - for(Import i : this.imports) System.out.println("import "+i.getName()+";"); - System.out.println(); - this.clazz.show(); - } - - public static int getModifier(String modifier){ - switch(modifier){ - case "public": return Modifier.PUBLIC; - case "private": return Modifier.PRIVATE; - case "protected": return Modifier.PROTECTED; - case "static": return Modifier.STATIC; - case "final": return Modifier.FINAL; - case "synchronized": return Modifier.SYNCHRONIZED; - case "volatile": return Modifier.VOLATILE; - case "transient": return Modifier.TRANSIENT; - case "native": return Modifier.NATIVE; - case "abstract": return Modifier.ABSTRACT; - case "strictfp": return Modifier.STRICT; - default: break; - } - return 0; - } - -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java b/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java deleted file mode 100644 index dd03a90..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Operation.java +++ /dev/null @@ -1,29 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Operation { - - private static Pattern VARIABLE_PATTERN = Pattern.compile("^(\\s*([^;]*)).*$"); - - private String tmp; - - public Operation(){} - - public int parse(String content) throws Exception{ - Matcher matcher = VARIABLE_PATTERN.matcher(content); - if(matcher.matches()){ - this.tmp = matcher.group(2); - return matcher.group(1).length()+1; - } - return 0; - } - - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+tmp+";"); - } - -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Package.java b/src/be/jeffcheasey88/peeratcode/parser/java/Package.java deleted file mode 100644 index b160919..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Package.java +++ /dev/null @@ -1,24 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Package { - - private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$"); - - private String name; - - public Package(){} - - public int parse(String content) throws Exception{ - Matcher matcher = PATTERN.matcher(content); - matcher.matches(); - this.name = matcher.group(2); - return matcher.group(1).length(); - } - - public String getName(){ - return this.name; - } -} diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java b/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java deleted file mode 100644 index c41fa29..0000000 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Variable.java +++ /dev/null @@ -1,127 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Variable { - - private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$"); - - private int modifier; - private String name; - private String type; - private Variable value; - - public Variable(){} - - public Variable(int modifier, String type){ - this.modifier = modifier; - this.type = type; - } - - public int parse(String content) throws Exception{ - Matcher matcher = PATTERN.matcher(content); - matcher.matches(); - - int offset = matcher.group(1).length(); - - String body = matcher.group(2); - int equals = indexOf(body, "="); - int quote = indexOf(body,","); - int quotes = indexOf(body, ";"); - int min = Math.min(quote, quotes); - body = body.substring(0, min); - - if(equals < quote && equals < quotes){ - assigment(body); - }else{ - onlyDefine(body); - } - - return offset+min; - } - - private void assigment(String content){ - } - - private void onlyDefine(String content){ - content = generiqueTypes(content); - System.out.println(content); - String[] values = content.split("\\s+"); - for(String value : values){ - int modifier = JavaParser.getModifier(value); - if(modifier > 0){ - this.modifier+=modifier; - continue; - } - if(this.type == null){ - this.type = value; - continue; - } - this.name = value; - } - } - - private String generiqueTypes(String content){ - System.out.println(content); - String result = ""; - int opened = 0; - for(char c : content.toCharArray()){ - if(c == '<') opened++; - else if(c == '>') opened--; - - if(opened > 0){ - if(Character.isWhitespace(c)) continue; - } - result+=c; - } - result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>"); - return result; - } - - private int indexOf(String value, String target){ - return value.split(target)[0].length(); - } - - public int getModifier(){ - return this.modifier; - } - - public String getName(){ - return this.name; - } - - public String getType(){ - return this.type; - } - - public Variable getValue(){ - return this.value; - } - - public void show(int tab){ - String start = ""; - for(int i = 0; i < tab; i++) start+="\t"; - System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":"="+value+";")); - } - - public static class Value extends Variable{ - - private String value; - - public Value(String value){ - this.value = value; - } - - public String value(){ - return this.value; - } - - @Override - public String toString(){ - return this.value; - } - } -} diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java index 999ea50..3aec837 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java @@ -69,6 +69,7 @@ public enum DatabaseQuery { // PLAYERS GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"), + GET_PLAYER_PSEUDO("SELECT * FROM players WHERE pseudo = ?"), GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n" + "FROM players p\r\n" + "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n" @@ -80,6 +81,8 @@ public enum DatabaseQuery { GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"), GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN players p ON p.id_player = c.fk_player GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"), + UPDATE_PLAYE_INFO("UPDATE players SET pseudo = ?, email = ?, first_name = ?, last_name = ? WHERE id_player = ?"), + // BADGES GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER( "SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"), diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 1f6ad5c..6c0f61b 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -32,68 +32,6 @@ public class DatabaseRepository { public DatabaseRepository(Configuration config) { this.config = config; } -// testTrigger(); -// } -// -// private void testTrigger(){ -// try { -// ensureConnection(); -// }catch(Exception e){ -// e.printStackTrace(); -// } -// System.out.println("connection ensured"); -// -// try { -// PreparedStatement log = this.con.prepareStatement("DROP TABLE mycustomlog;"); -// log.execute(); -// }catch(Exception e){ -// e.printStackTrace(); -// } -// System.out.println("log dropped"); -// -// try { -// PreparedStatement log = this.con.prepareStatement("CREATE TABLE mycustomlog(\r\n" -// + " message VARCHAR(255),\r\n" -// + " primary key (message)\r\n" -// + ");"); -// log.execute(); -// }catch(Exception e){ -// e.printStackTrace(); -// } -// System.out.println("log created"); -// -// try { -// System.out.println(DatabaseQuery.FIRST_TRY.toString()); -// DatabaseQuery.FIRST_TRY.prepare(this.con).execute(); -// }catch(Exception e){ -// e.printStackTrace(); -// } -// -// System.out.println("trigger inserted"); -// -// try { -// insertCompletion(new Completion(1, 1, 1, null, 1)); -// } catch (SQLException e1) { -// e1.printStackTrace(); -// } -// -// try { -// showLog(); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// System.out.println("------------------------------"); -// } -// -// private void showLog() throws Exception{ -// ensureConnection(); -// -// PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM mycustomlog"); -// ResultSet result = stmt.executeQuery(); -// while(result.next()){ -// System.out.println("[LOG] "+result.getString("message")); -// } -// } private void ensureConnection() throws SQLException { if (con == null || (!con.isValid(5))) { @@ -278,6 +216,39 @@ public class DatabaseRepository { } return null; } + + public boolean updatePseudo(int id, Player player, String pseudo){ + try{ + PreparedStatement statment = DatabaseQuery.GET_PLAYER_PSEUDO.prepare(this.con); + statment.setString(1, pseudo); + ResultSet result = statment.executeQuery(); + if(result.next()) return false; + statment = DatabaseQuery.UPDATE_PLAYE_INFO.prepare(this.con); + statment.setString(1, player.getPseudo()); + statment.setString(2, player.getEmail()); + statment.setString(3, player.getFirstname()); + statment.setString(4, player.getLastname()); + statment.setInt(5, id); + return statment.executeUpdate() > 0; + }catch(Exception e){ + e.printStackTrace(); + } + return false; + } + + public void updateProfile(int id, Player player, String lastname, String firstname){ + try{ + PreparedStatement statment = DatabaseQuery.UPDATE_PLAYE_INFO.prepare(this.con); + statment.setString(1, player.getPseudo()); + statment.setString(2, player.getEmail()); + statment.setString(3, firstname); + statment.setString(4, lastname); + statment.setInt(5, id); + statment.executeUpdate(); + }catch(Exception e){ + e.printStackTrace(); + } + } public Player getPlayerDetails(int idPlayer) { return getPlayerDetails(idPlayer, null); @@ -569,7 +540,7 @@ public class DatabaseRepository { * @param password The password of the user * @return id the id of the user, -1 if not login successefuly */ - public int login(String username, String password) { + public int login(String username, String password){ try { ensureConnection(); PreparedStatement statement = con.prepareStatement(DatabaseQuery.CHECK_PASSWORD.toString()); diff --git a/src/be/jeffcheasey88/peeratcode/routes/Login.java b/src/be/jeffcheasey88/peeratcode/routes/users/Login.java similarity index 97% rename from src/be/jeffcheasey88/peeratcode/routes/Login.java rename to src/be/jeffcheasey88/peeratcode/routes/users/Login.java index 378cf3a..265e3be 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Login.java +++ b/src/be/jeffcheasey88/peeratcode/routes/users/Login.java @@ -1,4 +1,4 @@ -package be.jeffcheasey88.peeratcode.routes; +package be.jeffcheasey88.peeratcode.routes.users; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; diff --git a/src/be/jeffcheasey88/peeratcode/routes/users/ProfileSettings.java b/src/be/jeffcheasey88/peeratcode/routes/users/ProfileSettings.java new file mode 100644 index 0000000..47333ba --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/routes/users/ProfileSettings.java @@ -0,0 +1,57 @@ +package be.jeffcheasey88.peeratcode.routes.users; + +import java.util.regex.Matcher; + +import org.json.simple.JSONObject; + +import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc; +import be.jeffcheasey88.peeratcode.framework.HttpReader; +import be.jeffcheasey88.peeratcode.framework.HttpWriter; +import be.jeffcheasey88.peeratcode.framework.RequestType; +import be.jeffcheasey88.peeratcode.framework.Response; +import be.jeffcheasey88.peeratcode.framework.Route; +import be.jeffcheasey88.peeratcode.framework.User; +import be.jeffcheasey88.peeratcode.model.Player; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; + +public class ProfileSettings implements Response{ + + private DatabaseRepository repo; + + public ProfileSettings(DatabaseRepository repo){ + this.repo = repo; + } + + @RouteDoc(path = "/settings", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont profile") + @RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer une donnée unique, déjà utilisée") + + @Route(path = "^/settings$", type = RequestType.POST, needLogin = true) + public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { + JSONObject json = reader.readJson(); + + String pseudo = (String) json.get("pseudo"); + String email = (String) json.get("email"); + String firstname = (String) json.get("firstname"); + String lastname = (String) json.get("lastname"); + Player player = repo.getPlayer(user.getId()); + + if(!player.getPseudo().equals(pseudo)){ + if(!repo.updatePseudo(user.getId(), player, pseudo)){ + writer.response(400, "Access-Control-Allow-Origin: *"); + return; + } + player.setPseudo(pseudo); + } + + if(!player.getEmail().equals(email)){ + + } + + if((!player.getFirstname().equals(firstname)) || (!player.getLastname().equals(lastname))){ + repo.updateProfile(user.getId(), player, lastname, firstname); + } + + writer.response(200, "Access-Control-Allow-Origin: *"); + } + +} diff --git a/src/be/jeffcheasey88/peeratcode/routes/Register.java b/src/be/jeffcheasey88/peeratcode/routes/users/Register.java similarity index 98% rename from src/be/jeffcheasey88/peeratcode/routes/Register.java rename to src/be/jeffcheasey88/peeratcode/routes/users/Register.java index 2f3007c..bd3faa9 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Register.java +++ b/src/be/jeffcheasey88/peeratcode/routes/users/Register.java @@ -1,4 +1,4 @@ -package be.jeffcheasey88.peeratcode.routes; +package be.jeffcheasey88.peeratcode.routes.users; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java deleted file mode 100644 index 5e0bcbd..0000000 --- a/test/be/jeffcheasey88/peeratcode/parser/java/VariableTest.java +++ /dev/null @@ -1,142 +0,0 @@ -package be.jeffcheasey88.peeratcode.parser.java; - -import static org.junit.Assert.assertNull; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import java.util.List; - -import org.junit.jupiter.api.Test; - -import be.jeffcheasey88.peeratcode.parser.java.Variable.Value; - -class VariableTest { - - //int i = 4; - //int i,j,k,l=1; - //int lm ; - //public static int l; - //Testt; - //Test j = new Test().schedule(p -> { return true;}); - //int i =j=k=l=4; - - @Test - void case1(){ - try { - Variable variable = new Variable(); - variable.parse(" int i = 4 ; "); - - assertEquals(0, variable.getModifier()); - assertEquals("int", variable.getType()); - assertEquals("i", variable.getName()); - assertEquals("4", ((Value)variable.getValue()).value()); - }catch(Exception e){ - fail(e); - } - } - - @Test - void case2(){ - try { - Variable variable = new Variable(); - variable.parse("public static int l ; "); - - assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier()); - assertEquals("int", variable.getType()); - assertEquals("l", variable.getName()); - assertNull(variable.getValue()); - }catch(Exception e){ - fail(e); - } - } - - @Test - void case3(){ - try { - Variable variable = new Variable(); - variable.parse(" int lm ; "); - - assertEquals(0, variable.getModifier()); - assertEquals("int", variable.getType()); - assertEquals("lm", variable.getName()); - assertNull(variable.getValue()); - }catch(Exception e){ - fail(e); - } - } - - @Test - void case4(){ - try { - Variable variable = new Variable(); - variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; "); - - assertEquals(0, variable.getModifier()); - assertEquals("Testas,Test>>>", variable.getType()); - assertEquals("t", variable.getName()); - assertNull(variable.getValue()); - }catch(Exception e){ - fail(e); - } - } - - @Test - void case5(){ - try { - Variable variable = new Variable(); - variable.parse(" int i,j,k,l=1; "); - - assertEquals(0, variable.getModifier()); - assertEquals("int", variable.getType()); - assertEquals("i", variable.getName()); - assertNull(variable.getValue()); - }catch(Exception e){ - fail(e); - } - } - - @Test - void case6(){ - try { - Class clazz = new Class(); - clazz.parse("public class Test{ int i ,j,k,l=1; } "); - - List vars = clazz.getVariables(); - assertEquals(vars.size(), 4); - for(int i = 0; i < 3; i++){ - Variable v = vars.get(i); - assertEquals(0, v.getModifier()); - assertEquals("int", v.getType()); - assertEquals((char)('i'+i), v.getName().charAt(0)); - assertNull(v.getValue()); - } - Variable v = vars.get(3); - assertEquals(0, v.getModifier()); - assertEquals("int", v.getType()); - assertEquals('l', v.getName().charAt(0)); - assertEquals("1", ((Value)v.getValue()).value()); - }catch(Exception e){ - fail(e); - } - } - - @Test - void case7(){ - try { - Class clazz = new Class(); - clazz.parse("public class Test{ int i ,j,k; int l=i=k=l=4; } "); - - List vars = clazz.getVariables(); - assertEquals(vars.size(), 4); - for(int i = 0; i < 4; i++){ - Variable v = vars.get(i); - assertEquals(0, v.getModifier()); - assertEquals("int", v.getType()); - assertEquals((char)('i'+i), v.getName().charAt(0)); - } - }catch(Exception e){ - fail(e); - } - } - -}