Skip to content
Snippets Groups Projects
Commit a5c9310e authored by Schuh Martin's avatar Schuh Martin
Browse files

Update: Moved Storage Classes to "Model" package

Add: Parser.java (including methods for parsing user inputs)
Add: Test for parser
parent 064823ee
No related branches found
No related tags found
4 merge requests!74V1,!73Initial commit,!71Merge DataBase into Development,!8Merge database branch into development branch
Showing
with 232 additions and 34 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/main/java/de/hdm_stuttgart/battlearena/Persistance/Scripts/DDL_Script_Oracle.sql" dialect="AZURE" />
<file url="file://$PROJECT_DIR$/src/main/java/de/hdm_stuttgart/battlearena/Model/DataStorage/Scripts/DDL_Script_Oracle.sql" dialect="AZURE" />
<file url="PROJECT" dialect="AZURE" />
</component>
</project>
\ No newline at end of file
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
public enum AccountType {
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
public class AppSettings {
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import de.hdm_stuttgart.battlearena.Exceptions.DatabaseError;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseError;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
package de.hdm_stuttgart.battlearena.Exceptions;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions;
public class CryptoException extends Exception{
public CryptoException(String message, Throwable throwable) {
......
package de.hdm_stuttgart.battlearena.Exceptions;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions;
public class DatabaseError extends Exception{
......
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions;
public class ParserError extends Exception{
public ParserError() {}
public ParserError(String message2)
{
super(message2);
}
}
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import de.hdm_stuttgart.battlearena.Exceptions.DatabaseError;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseError;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import de.hdm_stuttgart.battlearena.Exceptions.CryptoException;
import de.hdm_stuttgart.battlearena.Exceptions.DatabaseError;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseError;
import java.sql.Connection;
import java.util.ArrayList;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
public class MapData {
private String mapID;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
public class MapInfo {
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
public enum MapType {
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import de.hdm_stuttgart.battlearena.Exceptions.CryptoException;
import de.hdm_stuttgart.battlearena.Exceptions.DatabaseError;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Utilities.CryptoUtils;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.CryptoException;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseError;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import de.hdm_stuttgart.battlearena.Exceptions.DatabaseError;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Utilities.HashGenerator;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.DatabaseError;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.io.File;
import java.util.ArrayList;
public class Persistence {
......@@ -131,11 +131,16 @@ public class Persistence {
public void loadPlayerStatistics() throws DatabaseError{
try {
if (account.getAccountState() == AccountType.NONE) {
//error - user must create account
throw new DatabaseError("Must create playerAccount first");
} else if (account.getAccountState() == AccountType.LOCAL) {
statistics = gsonHandler.loadStats();
} else {
db.getStatistics(account.getPlayerName());
if(account.getPlayerName().equals(db.checkCredentials(account.getPlayerName()))){ //checks if local and remote password match to username
statistics = db.getStatistics(account.getPlayerName());
}
else{
throw new DatabaseError("Wrong Password");
}
}
}
catch(Exception e){
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
public class PlayerAccount {
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
import de.hdm_stuttgart.battlearena.Exceptions.CryptoException;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Utilities;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.CryptoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
......
package de.hdm_stuttgart.battlearena.Persistance.Classes;
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Utilities;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
package de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Utilities;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.Exceptions.ParserError;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Parser {
/*
Includes public static Parser methods for:
-username + password fields when creating account or logging in with existing credentials
-map name, when creating map
-IP-address field when joining game
-Map-name or map-ID, when searching for a map
-Parser for mapData from newly created map (length, formatting (spaces), tile-types, border-tiles etc.)
*/
public static void usernameValid(String username) throws ParserError{
if(username.length() < 4){
throw new ParserError("Username too short! Min length is 4 characters.");
}
if(username.length() > 40){
throw new ParserError("Username too long! Max length is 40 characters.");
}
Pattern pat = Pattern.compile("[^a-z0-9_-]", Pattern.CASE_INSENSITIVE);
Matcher mat = pat.matcher(username);
boolean result = mat.find();
if(result){
throw new ParserError("Forbidden characters used! Only user letters A-Z or numbers 0-9 or symbols '-' or '_' !");
}
}
public static void passwordValid(String password) throws ParserError{
if(password.length() < 4){
throw new ParserError("Password too short! Min length is 4 characters.");
}
if(password.length() > 40){
throw new ParserError("Password too long! Max length is 40 characters.");
}
Pattern pat = Pattern.compile("[^a-z0-9_-]", Pattern.CASE_INSENSITIVE);
Matcher mat = pat.matcher(password);
boolean result = mat.find();
if(result){
throw new ParserError("Forbidden characters used! Only user letters A-Z or numbers 0-9 or symbols '-' or '_' !");
}
}
public static void mapNameValid(String mapName) throws ParserError{
if(mapName.length() < 4){
throw new ParserError("Map-Name too short! Min length is 4 characters.");
}
if(mapName.length() > 40){
throw new ParserError("Map-Name too long! Max length is 30 characters.");
}
Pattern pat = Pattern.compile("[^a-z0-9_-]", Pattern.CASE_INSENSITIVE);
Matcher mat = pat.matcher(mapName);
boolean result = mat.find();
if(result){
throw new ParserError("Forbidden characters used! Only user letters A-Z or numbers 0-9 or symbols '-' or '_' !");
}
}
public static void mapIDValid(String mapID) throws ParserError{
if(!(mapID.length() == 40)){
throw new ParserError("Map-ID length not correct. Must have length of 40 characters!");
}
Pattern pat = Pattern.compile("[^a-f0-9]", Pattern.CASE_INSENSITIVE);
Matcher mat = pat.matcher(mapID);
boolean result = mat.find();
if(result){
throw new ParserError("Forbidden characters used! Map ID only consists out of letters a-f and/or numbers 0-9!");
}
}
public static void ipAddressValid(String address) throws ParserError{
if(!(address.length() == 15)){
throw new ParserError("IP-Address too long. Must have length of 15 characters including block dividers (.) !");
}
Pattern pat = Pattern.compile("[^0-9]"); //checks if fields only use numbers
int position;
for(int i = 0; i < 4; i++) {
position = (i*3) + (i);
Matcher mat = pat.matcher(address.substring(position, position+3));
boolean result = mat.find();
if(result){
throw new ParserError("IP address does not match format xxx.xxx.xxx.xxx (numbers 0-9 for x)!");
}
}
position = 0;
for(int i = 0; i < 4; i++) {
position = (i*3) + (i);
Integer.parseInt(address.substring(position, position+3));
if(Integer.parseInt(address.substring(position, position+3)) > 255){
throw new ParserError("IP address does not match format xxx.xxx.xxx.xxx (numbers 0-9 for x)!");
}
}
position = 0;
for(int i = 1; i < 4; i++) {
position = (i*3) + (i);
if(!(address.charAt(position) == '.')){
throw new ParserError("IP address does not match format xxx.xxx.xxx.xxx (wrong symbol used as separator. Must use '.')!");
}
}
}
public static void mapDataValid(String mapData) throws ParserError{
if(!(mapData.length() == 647)){
throw new ParserError("Map-Data corrupted - must have length of 647 chars including spaces!");
}
for(int i = 1; i < 647; i = i + 2){
if(!(mapData.charAt(i) == ' ')){
throw new ParserError("Map-Data corrupted - must use space every other character!");
}
}
for(int i = 0; i < 647; i = i + 2){
if((Integer.parseInt(mapData.substring(i, i+1)) > 4) | (Integer.parseInt(mapData.substring(i, i+1)) <= 0)){
throw new ParserError("Map-Data corrupted - Tile number must be between 1 and 4!");
}
}
for(int i = 0; i < 18; i = i + 2){
if(!(Integer.parseInt(mapData.substring(i, i+1)) == 3)){
throw new ParserError("Map-Data corrupted - Top-Line must be border tiles!");
}
}
for(int i = 629; i < 647; i = i + 2){
if(!(Integer.parseInt(mapData.substring(i, i+1)) == 3)){
throw new ParserError("Map-Data corrupted - Bottom-Line must be border tiles!");
}
}
for(int i = 0; i < 647; i = i + 2){
if(i % 18 == 0 && !(Integer.parseInt(mapData.substring(i, i+1)) == 3)){
throw new ParserError("Map-Data corrupted - Left Edge must be border tiles!");
}
}
for(int i = 0; i < 647; i = i + 2){
if(i % 17 == 0 && !(Integer.parseInt(mapData.substring(i, i+1)) == 3)){
throw new ParserError("Map-Data corrupted - Right Edge must be border tiles!");
}
}
if(!(Integer.parseInt(mapData.substring(38, 39)) == 1) | !(Integer.parseInt(mapData.substring(68, 69)) == 1)){
throw new ParserError("Map-Data corrupted - Player spawn must use walkable tile!");
}
if(!(Integer.parseInt(mapData.substring(578, 579)) == 1) | !(Integer.parseInt(mapData.substring(606, 607)) == 1)){
throw new ParserError("Map-Data corrupted - Player spawn must use walkable tile!");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment