private boolean isValidUserAction(int userAction, int userID, int gameID) { if (!isUserNextPlayer(userID, gameID)) { return false; } Connection con = DatabaseConnector.getConnection(); Statement stmt; ResultSet rs; int nextStationAction = -1; try { stmt = con.createStatement(); // Checken ob Spieler an der Reihe ist rs = stmt.executeQuery( "SELECT next_station_action FROM stations NATURAL JOIN games WHERE game_id=" + gameID + " AND user_id=" + userID); if (rs.next()) { nextStationAction = rs.getInt("next_station_action"); } con.commit(); if (userAction == nextStationAction) return true; else return false; } catch (SQLException e) { e.printStackTrace(); return false; } }
// Sicherheitchecks // Checken ob ein User an einem Spiel teilnimmt. private boolean isUserInGame(int userID, int gameID) { Connection con = DatabaseConnector.getConnection(); Statement stmt; ResultSet rs; try { stmt = con.createStatement(); // Checken ob Spieler am Spiel teilnimmt rs = stmt.executeQuery( "SELECT user_id FROM stations NATURAL JOIN games WHERE user_id=" + userID + " AND game_id=" + gameID); if (rs.next()) { con.commit(); return true; } else { con.commit(); return false; } } catch (SQLException e) { e.printStackTrace(); return false; } }
// Checken ob ein User an der Reihe ist in diesem Spiel. private boolean isUserNextPlayer(int userID, int gameID) { if (!isUserInGame(userID, gameID)) { return false; } Connection con = DatabaseConnector.getConnection(); Statement stmt; ResultSet rs; try { stmt = con.createStatement(); // Checken ob Spieler am Spiel teilnimmt int nextStationType = -1, stationType = -2; rs = stmt.executeQuery("SELECT next_station_type FROM games WHERE game_id=" + gameID); if (rs.next()) { nextStationType = rs.getInt("next_station_type"); con.commit(); } rs = stmt.executeQuery( "SELECT station_type FROM stations NATURAL JOIN users WHERE game_id=" + gameID + " AND user_id=" + userID); if (rs.next()) { stationType = rs.getInt("station_type"); con.commit(); } if (nextStationType == stationType) return true; else return false; } catch (SQLException e) { e.printStackTrace(); return false; } }
private void brewBeer(int userID, int gameID, int amount) { Connection con = DatabaseConnector.getConnection(); Statement stmt; try { int stationID = getStationID(con, userID, gameID); stmt = con.createStatement(); // Bierlieferung eintragen (Produktion) stmt.executeUpdate( "INSERT INTO deliveries(receiver_id,amount,week) VALUES(" + stationID + "," + amount + "," + (getGameWeek(con, gameID) + 2) + ")"); setNextStationAction(con, stationID, ACTION_COLLECT_DELIVERY); endOfTurn(con, gameID, stationID); con.commit(); con.close(); } catch (SQLException e) { try { con.rollback(); con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } }
private void orderBeer(int userID, int gameID, int amount) { Connection con = DatabaseConnector.getConnection(); Statement stmt; try { int stationID = getStationID(con, userID, gameID); stmt = con.createStatement(); // Bierbestellung eintragen stmt.executeUpdate( "INSERT INTO orders(sender_id,amount,week) VALUES(" + getStationID(con, userID, gameID) + "," + amount + "," + getGameWeek(con, gameID) + ")"); int stationType = getStationType(con, stationID); if (stationType == 0) setNextStationAction(con, stationID, ACTION_ORDER_BEER); else setNextStationAction(con, stationID, ACTION_COLLECT_DELIVERY); endOfTurn(con, gameID, stationID); con.commit(); con.close(); } catch (SQLException e) { try { con.rollback(); con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } }
private String acceptNextOrder(int userID, int gameID) { Connection con = DatabaseConnector.getConnection(); Statement stmt; ResultSet rs; try { int stationID = getStationID(con, userID, gameID); stmt = con.createStatement(); rs = stmt.executeQuery( "SELECT amount FROM orders WHERE sender_id=" + getSellerID(con, stationID) + " AND week=" + getGameWeek(con, gameID)); if (rs.next()) { int amount = rs.getInt("amount"); stmt.executeUpdate( "UPDATE stations SET backorder=backorder+" + amount + " WHERE station_id=" + stationID); } setNextStationAction(con, stationID, ACTION_DELIVER_BEER); con.commit(); con.close(); } catch (SQLException e) { try { con.rollback(); con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } Gson gson = new Gson(); return gson.toJson(""); }
// POST-Requests private void collectDelivery(int userID, int gameID) { Connection con = DatabaseConnector.getConnection(); Statement stmt; ResultSet rs; try { int stationID = getStationID(con, userID, gameID); stmt = con.createStatement(); rs = stmt.executeQuery( "SELECT amount FROM deliveries WHERE receiver_id=" + stationID + " AND week=" + getGameWeek(con, gameID)); if (rs.next()) { int amount = rs.getInt("amount"); stmt.executeUpdate( "UPDATE stations SET inventory=inventory+" + amount + " WHERE station_id=" + stationID); } setNextStationAction(con, stationID, ACTION_ACCEPT_NEXT_ORDER); con.commit(); con.close(); } catch (SQLException e) { try { con.rollback(); con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } }
private void deliverBeer(int userID, int gameID) { Connection con = DatabaseConnector.getConnection(); Statement stmt; ResultSet rs; try { stmt = con.createStatement(); int stationID = 0, inventory = 0, backorder = 0; rs = stmt.executeQuery( "SELECT station_id,inventory,backorder FROM stations WHERE game_id=" + gameID + " AND user_id=" + userID); if (rs.next()) { stationID = rs.getInt("station_id"); inventory = rs.getInt("inventory"); backorder = rs.getInt("backorder"); } // Gucken wieviel geliefert werden kann und soviel liefern wie möglich if (inventory >= backorder) { stmt.executeUpdate( "INSERT INTO deliveries(receiver_id,amount,week) VALUES(" + getCustomerID(con, stationID) + "," + backorder + "," + (getGameWeek(con, gameID) + 2) + ")"); stmt.executeUpdate( "UPDATE stations SET inventory=inventory-backorder,backorder=0 WHERE station_id=" + stationID); } else { stmt.executeUpdate( "INSERT INTO deliveries(receiver_id,amount,week) VALUES(" + getCustomerID(con, stationID) + "," + inventory + "," + (getGameWeek(con, gameID) + 2) + ")"); stmt.executeUpdate( "UPDATE stations SET inventory=0,backorder=backorder-inventory WHERE station_id=" + stationID); } if (getStationType(con, stationID) == 4) setNextStationAction(con, stationID, ACTION_BREW_BEER); else setNextStationAction(con, stationID, ACTION_ORDER_BEER); con.commit(); con.close(); } catch (SQLException e) { try { con.rollback(); con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } }