Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Added email/password length verification; made persistent database mo…
Browse files Browse the repository at this point in the history
…re flexible by adding code that checks to see how many weapons there are and adds them to the SQL database automatically rather than having to hardcode it in
  • Loading branch information
Joshua Crotts committed Dec 7, 2019
1 parent ed31415 commit 5ac1339
Show file tree
Hide file tree
Showing 55 changed files with 321 additions and 143 deletions.
25 changes: 15 additions & 10 deletions com/dsd/game/controller/AudioBoxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @author Joshua, Ronald, Rinty
*
* @updated 11/12/19
* @updated 12/7/19
*/
public abstract class AudioBoxController {

Expand All @@ -22,20 +22,25 @@ public abstract class AudioBoxController {
*/
public static void initialize(int _buffers) {
StandardAudioController.init(_buffers);

// Music initialization.
StandardAudioController.load("src/resources/audio/music/menu.mp3", 1, StandardAudioType.MUSIC);
StandardAudioController.load("src/resources/audio/music/level0.mp3", 1, StandardAudioType.MUSIC);
StandardAudioController.load("src/resources/audio/sfx/pistol.wav", 16, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/splat1.wav", 2, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/splat2.wav", 2, StandardAudioType.SFX);

// Gun sound effects.
StandardAudioController.load("src/resources/audio/sfx/empty.wav", 1, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/reload.wav", 1, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/knife.wav", 3, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/shotgun.wav", 4, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/grenade_launcher.wav", 2, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/Pistol.wav", 16, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/Shotgun.wav", 4, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/GrenadeLauncher.wav", 2, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/grenade_explosion.wav", 4, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/minigun.wav", 64, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/rifle.wav", 32, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/fast_rifle.wav", 32, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/Minigun.wav", 64, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/Rifle.wav", 32, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/FastRifle.wav", 32, StandardAudioType.SFX);

// Other miscellaneous sfx.
StandardAudioController.load("src/resources/audio/sfx/splat1.wav", 2, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/splat2.wav", 2, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/coin0.wav", 12, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/round_change.wav", 1, StandardAudioType.SFX);
StandardAudioController.load("src/resources/audio/sfx/restore_health.wav", 4, StandardAudioType.SFX);
Expand Down
27 changes: 0 additions & 27 deletions com/dsd/game/controller/BloodParticleHandler.java

This file was deleted.

2 changes: 1 addition & 1 deletion com/dsd/game/core/AccountStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
public enum AccountStatus {

DOES_NOT_EXIST, EXISTS, INCORRECT_PASS, ACCOUNT_CREATED, CORRECT, INVALID_PASS, INVALID_EMAIL;
DOES_NOT_EXIST, EXISTS, INCORRECT_PASS, ACCOUNT_CREATED, CORRECT, INVALID_EMAIL, INVALID_PASS;
}
2 changes: 1 addition & 1 deletion com/dsd/game/core/Game.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dsd.game.core;

import com.dsd.game.controller.AudioBoxController;
import com.dsd.game.controller.BloodParticleHandler;
import com.dsd.game.controller.CollisionHandlerController;
import com.dsd.game.controller.CursorController;
import com.dsd.game.controller.DebugController;
Expand All @@ -12,6 +11,7 @@
import com.dsd.game.controller.SnowController;
import com.dsd.game.controller.TimerController;
import com.dsd.game.database.TranslatorDatabase;
import com.dsd.game.handlers.BloodParticleHandler;
import com.dsd.game.levels.MetalLevel;
import com.dsd.game.objects.Player;
import com.dsd.game.userinterface.HUDScreen;
Expand Down
81 changes: 69 additions & 12 deletions com/dsd/game/database/PersistentDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* [Group Name: Data Structure Deadheads]
*
* @author Joshua, Ronald, Rinty
*
* @updated 12/7/19
*/
public class PersistentDatabase implements RemoteDatabase {

Expand All @@ -48,28 +50,32 @@ public class PersistentDatabase implements RemoteDatabase {
private static String PASSWORD;

// Number of salt iterations.
private static int SALT_ITERATIONS = 14;
private static final int SALT_ITERATIONS = 14;

public PersistentDatabase(Game _game) {
this.game = _game;
}

/**
* Write the contents of the supplied information to the db type.
*
* @return true if the save was successful, false otherwise.
*/
@Override
public boolean save() {
String playerInfo = this.game.getPlayer().createObject(SerializableType.PLAYER);
String inventoryInfo = this.game.getPlayer().getInventory().createObject(SerializableType.INVENTORY);
String levelInfo = this.game.getLevelController().createObject(SerializableType.WAVE_INFO);
String waveInfo = this.game.getDifficultyController().createObject(SerializableType.LEVEL);

// If any of these uploads fail, then we automatically reject it.
boolean player = this.uploadPlayerInfo(playerInfo.split(";"));
boolean inventory = this.uploadInventoryInfo(inventoryInfo.split(";"));
boolean level = this.uploadLevelInfo(levelInfo.split(";"));
boolean wave = this.uploadWaveInfo(waveInfo.split(";"));
boolean game = this.uploadGameInfo();
return player && inventory && level && wave && game;
boolean playerInfoBool = this.uploadPlayerInfo(playerInfo.split(";"));
boolean inventoryInfoBool = this.uploadInventoryInfo(inventoryInfo.split(";"));
boolean levelInfoBool = this.uploadLevelInfo(levelInfo.split(";"));
boolean waveInfoBool = this.uploadWaveInfo(waveInfo.split(";"));
boolean gameInfoBool = this.uploadGameInfo();

return playerInfoBool && inventoryInfoBool && levelInfoBool && waveInfoBool && gameInfoBool;
}

/**
Expand All @@ -81,9 +87,11 @@ private boolean uploadPlayerInfo(String[] _playerData) {
PreparedStatement updatePlayerQuery = null;
try {
updatePlayerQuery = this.remoteDBConnection.prepareStatement(String.format("UPDATE user_accounts SET Sex = ?, PlayerX = ?, PlayerY = ?, Money = ?, Health = ? WHERE UUID = ? ;"));

for (int i = 0; i < _playerData.length; i++) {
updatePlayerQuery.setInt(i + 1, Integer.parseInt(_playerData[i]));
}

updatePlayerQuery.setString(_playerData.length + 1, this.connectedUserID);
updatePlayerQuery.executeUpdate();
} catch (SQLException | NullPointerException ex) {
Expand All @@ -101,12 +109,34 @@ private boolean uploadPlayerInfo(String[] _playerData) {
private boolean uploadInventoryInfo(String[] _inventoryData) {
PreparedStatement updateInventoryQuery = null;
try {
updateInventoryQuery = this.remoteDBConnection.prepareStatement(String.format("UPDATE user_accounts SET Pistol = ?, PistolAmmo = ?, PistolTotalAmmo = ?, Rifle = ?, RifleAmmo = ?, RifleTotalAmmo = ?, FastRifle = ?, FastRifleAmmo = ?, FastRifleTotalAmmo = ?, Shotgun = ?, ShotgunAmmo = ?, ShotgunTotalAmmo = ?, GrenadeLauncher = ?, GrenadeLauncherAmmo = ?, GrenadeLauncherTotalAmmo = ?, Minigun = ?, MinigunAmmo = ?, MinigunTotalAmmo = ?, SuperShotgun = ?, SuperShotgunAmmo = ?, SuperShotgunTotalAmmo = ? WHERE UUID = ?;"));

// Build the string with question marks that set the data when
// uploading it.
StringBuilder inventoryString = new StringBuilder();

// Build the string successively with each property.
for (int i = 0; i < WeaponType.values().length; i++) {
inventoryString.append(WeaponType.values()[i].getType()).append(" = ?, ");
inventoryString.append(WeaponType.values()[i].getType()).append("Ammo = ?, ");

// If we're at the end of the string, we need to not put a comma.
if (i != WeaponType.values().length - 1) {
inventoryString.append(WeaponType.values()[i].getType()).append("TotalAmmo = ?, ");
} else {
inventoryString.append(WeaponType.values()[i].getType()).append("TotalAmmo = ? ");
}
}

updateInventoryQuery = this.remoteDBConnection.prepareStatement(String.format("UPDATE user_accounts SET " + inventoryString + "WHERE UUID = ?;"));

// Iterate through the updateQuery, and place the data in the
// PreparedStatement.
for (int i = 0; i < _inventoryData.length; i++) {
updateInventoryQuery.setInt(i + 1, Integer.parseInt(_inventoryData[i]));
}
updateInventoryQuery.setString(_inventoryData.length + 1, this.connectedUserID);
updateInventoryQuery.executeUpdate();

} catch (SQLException | NullPointerException ex) {
System.out.println(ex);
return false;
Expand Down Expand Up @@ -185,7 +215,8 @@ public boolean load() {
try {
// Perform the query to retrieve the information about the player
// and their inventory from the SQL db.
playerStatsQuery = this.remoteDBConnection.prepareStatement(String.format("SELECT SavedGame, Sex, PlayerX, PlayerY, Money, Health, Pistol, PistolAmmo, PistolTotalAmmo, Rifle, RifleAmmo, RifleTotalAmmo, FastRifle, FastRifleAmmo, FastRifleTotalAmmo, Shotgun, ShotgunAmmo, ShotgunTotalAmmo, GrenadeLauncher, GrenadeLauncherAmmo, GrenadeLauncherTotalAmmo, Minigun, MinigunAmmo, MinigunTotalAmmo,SuperShotgun, SuperShotgunAmmo, SuperShotgunTotalAmmo, LevelID, Wave, LevelTransitionTimer, DifficultyFactor FROM user_accounts WHERE UUID = ?;"));
StringBuilder weaponTypes = WeaponType.buildInventoryString();
playerStatsQuery = this.remoteDBConnection.prepareStatement(String.format("SELECT SavedGame, Sex, PlayerX, PlayerY, Money, Health, " + weaponTypes.toString() + "LevelID, Wave, LevelTransitionTimer, DifficultyFactor FROM user_accounts WHERE UUID = ?;"));
playerStatsQuery.setString(1, this.connectedUserID);
ResultSet playerStatsSet = playerStatsQuery.executeQuery();
ResultSetMetaData playerStatsMetadata = playerStatsSet.getMetaData();
Expand All @@ -203,24 +234,31 @@ public boolean load() {
HashMap<String, Double> playerInfo = new HashMap<>();
ArrayList<Integer> inventoryInfo = new ArrayList<>();
final int PLAYER_AMT = 6;

// Gun status (if they have the gun)
// plus the ammo amt and total ammo for every gun
final int INVENTORY_AMT = WeaponType.values().length * 3;

// Load in the player data from the Result Set
for (int i = 2; i <= PLAYER_AMT; i++) {
playerInfo.put(playerStatsMetadata.getColumnName(i), (double) playerStatsSet.getInt(i));
}

// Load in the inventory data from the Result set
for (int j = PLAYER_AMT + 1; j <= INVENTORY_AMT + PLAYER_AMT; j++) {
inventoryInfo.add(playerStatsSet.getInt(j));
}

// Load in the player statistics.
this.game.getPlayer().readObject(playerInfo);

// Load the weapon statistics.
this.game.getPlayer().getInventory().readObject(inventoryInfo);

// Load in level information from the result set.
this.game.getLevelController().readObject(playerStatsSet.getInt(PLAYER_AMT + INVENTORY_AMT + 1),
playerStatsSet.getInt(PLAYER_AMT + INVENTORY_AMT + 2));

// Load in wave information from the result set.
this.game.getDifficultyController().readObject(playerStatsSet.getInt(PLAYER_AMT + INVENTORY_AMT + 3),
playerStatsSet.getInt(PLAYER_AMT + INVENTORY_AMT + 4));
Expand All @@ -244,6 +282,7 @@ public boolean connect(String _dbName) {
this.generateClassName();
this.loadDatabaseCreds();
String url = String.format("jdbc:mysql://%s:3306/%s", IP_ADDRESS, instanceID);

try {
this.remoteDBConnection = DriverManager.getConnection(url, USERNAME, PASSWORD);
} catch (SQLException ex) {
Expand All @@ -266,7 +305,7 @@ public boolean connect(String _dbName) {
@Override
public AccountStatus userAuthenticated(String _email, String _password) {
try {
// Verify the email
// Verify the email to see if it exists.
if (!this.userEmailInDatabase(_email)) {
return AccountStatus.DOES_NOT_EXIST;
} // Verify the email AND the password.
Expand All @@ -277,6 +316,7 @@ else if (!this.userInDatabase(_email, _password)) {
} catch (SQLException ex) {
Logger.getLogger(PersistentDatabase.class.getName()).log(Level.SEVERE, null, ex);
}

this.connectedUserID = this.getUserID(_email);
return AccountStatus.CORRECT;
}
Expand All @@ -301,6 +341,8 @@ private String getUserID(String _email) {
} catch (SQLException ex) {
Logger.getLogger(PersistentDatabase.class.getName()).log(Level.SEVERE, null, ex);
}

// If we have something in the string, it's, by default, the UUID.
if (idSet.next()) {
return idSet.getString(1);
}
Expand Down Expand Up @@ -329,8 +371,22 @@ public AccountStatus addUser(String _email, String _password) {
return AccountStatus.INVALID_PASS;
} else {
String hashed = BCrypt.hashpw(_password, BCrypt.gensalt(SALT_ITERATIONS));
// Very, VERY long statement to update the user.
insertStatement = this.remoteDBConnection.prepareStatement(String.format("INSERT INTO user_accounts " + "VALUES(UUID(), ?, ?, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT);"));

// First, we want to make sure all weapons are defaulted to having
// no ammo, but are at least exist in the system.
String defaultWeapons = "";

for (int i = 0; i < WeaponType.values().length; i++) {
// Each weapon has three properties, so we initialize these
// to default here (three times successively).
defaultWeapons += "DEFAULT, ";
defaultWeapons += "DEFAULT, ";
defaultWeapons += "DEFAULT, ";
}
insertStatement = this.remoteDBConnection.prepareStatement(String.format("INSERT INTO user_accounts " + "VALUES(UUID(), ?, ?, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, " + defaultWeapons + "DEFAULT, DEFAULT, DEFAULT, DEFAULT);"));

// We set the first and second positions in the prepared statement
// to be the username and password.
insertStatement.setString(1, _email);
insertStatement.setString(2, hashed);
insertStatement.executeUpdate();
Expand Down Expand Up @@ -385,6 +441,7 @@ private boolean isValidPassword(String _email, String _password) throws SQLExcep
PreparedStatement insertStatement = this.remoteDBConnection.prepareStatement(String.format("SELECT Password FROM user_accounts WHERE Email = ?;"));
insertStatement.setString(1, _email);
ResultSet resultQuery = insertStatement.executeQuery();

// If we have a result query value, then we check the password against the hashed pswd.
if (resultQuery.next()) {
return BCrypt.checkpw(_password, resultQuery.getString("Password"));
Expand Down
2 changes: 2 additions & 0 deletions com/dsd/game/database/SerializableType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* [Group Name: Data Structure Deadheads]
*
* @author Joshua, Ronald, Rinty
*
* @updated 12/7/19
*/
public enum SerializableType {
PLAYER, LEVEL, GAME_INFO, WAVE_INFO, INVENTORY;
Expand Down
Loading

0 comments on commit 5ac1339

Please sign in to comment.