Compare commits

..

No commits in common. "9ae95aef1cc3666706d9b1ed4e30d86916b9ede6" and "5fa84e866ff22fb67c1ef4f0767974bfd6f4a497" have entirely different histories.

12 changed files with 77 additions and 62 deletions

View File

@ -11,18 +11,16 @@ include_directories(
add_executable(park
main.cpp
data.cpp
headers/data.h
encrypt.cpp
headers/encrypt.h
Customer.cpp
headers/Customer.h
Park_spot.cpp
headers/Park_spot.h
Park_time.cpp
headers/Park_time.h
encrypt.cpp
headers/encrypt.h
Query.cpp
headers/Query.h
)
@ -36,7 +34,7 @@ if (UNIX)
sqlite3
pthread
dl
sodium
libsodium
)
elseif (MSYS OR MINGW)
target_link_libraries(park

View File

@ -2,7 +2,7 @@
// constructors
Customer::Customer(string name_, string password_, Verhicle_type verhicle_)
: name{name_}, verhicle{verhicle_}, password{hash_password(password_)} {
: name{name_}, verhicle{verhicle_}, password{hash_password(password)} {
id = auto_increment_db() + 1;
save_db();
}

View File

@ -3,12 +3,12 @@
// constructors
Park_spot::Park_spot()
: parked_customer{0}, id{auto_increment_db() + 1}, taken{false} {
: parked{nullptr}, id{auto_increment_db() + 1}, taken{false} {
save_db();
}
Park_spot::Park_spot(int id_, bool taken_, int parked)
: parked_customer{parked},
Park_spot::Park_spot(Customer* parked_, int id_, bool taken_)
: parked{nullptr},
id{id_},
taken{taken_} // TODO: think about how init parked?
{}
@ -16,16 +16,16 @@ Park_spot::Park_spot(int id_, bool taken_, int parked)
// clock in en out, calls de juist(in/out) van de customer aan de hand van
// internal state van taken
void Park_spot::clock(Customer& c_customer) {
void Park_spot::clock(Customer* c_customer) {
if (!taken) {
parked_customer = c_customer.id;
parked = c_customer;
taken = true;
c_customer.clock_in(id);
parked->clock_in(id);
update_db();
} else {
taken = false;
c_customer.clock_out(id);
parked_customer = 0;
parked->clock_out(id);
parked = nullptr;
update_db();
}
}
@ -37,7 +37,7 @@ void Park_spot::update_db() {
"UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
statement.insert(63, to_string(id));
if (taken) {
statement.insert(49, to_string(parked_customer));
statement.insert(49, to_string(parked->id));
statement.insert(30, "1");
} else {
statement.insert(49, "NULL");

View File

@ -25,10 +25,6 @@ vector<Park_time> query_parktimes_for_customer(int cid) {
return park_times;
}
//--------------------------------------------- customers
vector<Customer> query_customer_with_name(string name) {
/*
We use this instead of plain customers because:
@ -70,25 +66,33 @@ Customer query_customer_with_id(int id) {
Customer result{
id, name, password, Verhicle_type(verhicle), park_instances};
// DEBUG
// cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
return result;
}
}
// -------------- paroking spots
vector<Park_spot> query_all_parking_spots() {
vector<Park_spot> spots;
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > 2;");
// query.bind(1, 2);
void query_all_parking_spots() {
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > ?;");
query.bind(1, 0);
while (query.executeStep()) {
int id = query.getColumn(0);
int taken = query.getColumn(1);
int cid = query.getColumn(2);
// park_customers.push_back(query_customer_with_id(cid));
spots.push_back({id, taken, cid});
park_customers.push_back(query_customer_with_id(cid));
parking_spots.push_back(
Park_spot{get_customer_ptr_for_parkspot(cid), id, bool(taken)});
}
return spots;
}
Customer* get_customer_ptr_for_parkspot(int id) {
if (!id) {
return nullptr;
}
for (int i = 0; i < park_customers.size(); i++) {
if (park_customers[i].id == id) {
return &park_customers[i];
}
}
return nullptr;
}

View File

@ -8,7 +8,6 @@ SQLite::Database start_db() {
while (sodium_init() < 0) {
std::cout << "SODIUM NOT WORKING";
}
db.exec(
"create table if not exists Customer (id integer primary key, name "
"text, password text, verhicle int)");

View File

@ -9,8 +9,8 @@ string hash_password(string password) {
*/
const char* password_ = password.c_str();
char hashed_password_[crypto_pwhash_STRBYTES];
int memory_limit = 3.2e+7; // 3.2e7 = 32e6 = 32 mb
int cpu_limit = 1; // this is n_threads
int memory_limit = 1.28e+8; // 1.28 e+8 = 128 e6 = 128 mb
int cpu_limit = 2; // this is n_threads
int result = crypto_pwhash_str(hashed_password_,
password_,

View File

@ -4,7 +4,6 @@
#include "Park_time.h"
#include "data.h"
#include <vector>
using std::vector;
@ -23,6 +22,7 @@ park_time object. Voegt het toe aan een vector.
class Customer {
public:
int id;
string name;
string password;
@ -38,13 +38,17 @@ class Customer {
void delete_db();
void gen_monthly(); // remove, make it a function in data
Verhicle_type verhicle;
private:
Verhicle_type verhicle;
vector<Park_time> park_instances;
void save_db();
int auto_increment_db();
};
static vector<Customer> park_customers; // save the customers that are parked in here
// parking_spot uses pointers, so it's better to save the parked customers here
// where we know they'll be destroyed at the end of this scope, instead of too early
// and end up with dangling pointers
#endif // CUSTOMER_H

View File

@ -1,8 +1,5 @@
#ifndef PARK_SPOT_H
#define PARK_SPOT_H
#pragma once
#include "Customer.h"
/*
db representation:
int id not null
@ -17,10 +14,10 @@ class Park_spot {
public:
int id;
bool taken;
int parked_customer;
Customer* parked;
Park_spot();
Park_spot(int id_, bool taken_, int parked);
void clock(Customer& c_customer);
Park_spot(Customer* parked_, int id_, bool taken_);
void clock(Customer* c_customer);
private:
void save_db();
@ -28,4 +25,5 @@ class Park_spot {
void delete_db();
int auto_increment_db();
};
#endif // CUSTOMER_H
static vector<Park_spot> parking_spots; // to save the parking spots in memory

View File

@ -10,12 +10,9 @@ vector<Park_time> query_parktimes_for_customer(int cid);
vector<Customer> query_customer_with_name(string name);
Customer query_customer_with_id(int id);
Customer* get_customer_ptr_for_parkspot(int id);
vector<Park_spot> query_all_parking_spots(); // used for initializing the parking spots at start of the program
static vector<Park_spot> parking_spots = query_all_parking_spots(); // to save the parking spots in memory
static vector<Customer> park_customers;
// save the customers that are parked in here
void query_all_parking_spots(); // used for initializing the parking spots at start of the program
#endif // CUSTOMER_H

View File

@ -5,8 +5,8 @@
#include "encrypt.h"
namespace data {
SQLite::Database start_db();
SQLite::Database
start_db();
static SQLite::Database db = start_db();
} // namespace data

View File

@ -1,11 +1,8 @@
#include "headers/Query.h"
#include <chrono>
#include <array>
#include <thread>
using namespace std::chrono;
/*
Code strucure like this:
class declarations zijn in /headers/class_naam.h, en definitions van de member
@ -21,6 +18,9 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz.
De client clockt in en uit bij een spot.
*/
void Wait(int sec)
/*
a wait function where 1 sec represents 1 hour irl.
@ -29,14 +29,29 @@ a wait function where 1 sec represents 1 hour irl.
std::this_thread::sleep_for(seconds{sec});
}
int main() {
// Customer sagar = query_customer_with_name("stefan udit")[0];
Customer sagar = query_customer_with_id(2);
cout << sagar.id << "," << sagar.name << "," << sagar.password;
// cout << parking_spots.size();
Customer* get_customer_ptr_for_parkspot(int id);
int main() {
query_all_parking_spots();
Customer p0 = query_customer_with_name("Shaquile")[0];
Customer p1 = query_customer_with_name("Sagar Ramsaransing")[0];
Customer p2 = query_customer_with_name("Joshua karto")[0];
Customer p3 = query_customer_with_name("Stefan udit")[0];
parking_spots[2].clock(&p1);
Wait(2);
parking_spots[2].clock(&p1);
Wait(1);
parking_spots[0].clock(&p2);
Wait(1);
parking_spots[1].clock(&p3);
Wait(1);
parking_spots[0].clock(&p2);
parking_spots[1].clock(&p3);
Wait(1);
parking_spots[1].clock(&p3);
// for (auto i : parking_spots){
// cout << i.id << "," << i.parked_customer;
// }
}

BIN
test.db3

Binary file not shown.