what happened there
This commit is contained in:
parent
0e0cd0fdcd
commit
2996082209
@ -15,7 +15,7 @@ bcrypt = "0.2"
|
||||
|
||||
dotenv = "0.9.0"
|
||||
env_logger = "*"
|
||||
postgres = { version = "0.15", features = ["with-uuid"] }
|
||||
r2d2 = "*"
|
||||
r2d2_postgres = "*"
|
||||
|
||||
r2d2 = "0.8.2"
|
||||
r2d2_sqlite = "0.6"
|
||||
rusqlite = { version = "0.14.0", features = ["bundled"] }
|
||||
|
||||
@ -82,7 +82,7 @@ pub fn test_battle() {
|
||||
|
||||
match outcome.winner() {
|
||||
Some(w) => println!("{:?} is the winner with {:?} hp remaining", w.name, w.hp),
|
||||
// None => println!("{:?} was a draw", outcome),
|
||||
None => println!("{:?} was a draw", outcome),
|
||||
};
|
||||
|
||||
return
|
||||
|
||||
@ -102,7 +102,7 @@ impl Cryp {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn named(self, name: String) -> Cryp {
|
||||
pub fn named(mut self, name: String) -> Cryp {
|
||||
self.name = name.clone();
|
||||
self
|
||||
}
|
||||
|
||||
@ -2,20 +2,19 @@ extern crate rand;
|
||||
extern crate uuid;
|
||||
extern crate ws;
|
||||
extern crate env_logger;
|
||||
extern crate bcrypt;
|
||||
|
||||
// #[macro_use]
|
||||
// extern crate dotenv;
|
||||
#[macro_use]
|
||||
extern crate dotenv;
|
||||
extern crate postgres;
|
||||
extern crate r2d2;
|
||||
extern crate r2d2_sqlite;
|
||||
extern crate rusqlite;
|
||||
extern crate r2d2_postgres;
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_cbor;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
extern crate bcrypt;
|
||||
|
||||
mod cryp;
|
||||
mod battle;
|
||||
mod net;
|
||||
@ -24,8 +23,10 @@ mod skill;
|
||||
mod rpc;
|
||||
mod user;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use net::{start};
|
||||
|
||||
fn main() {
|
||||
dotenv().ok();
|
||||
start()
|
||||
}
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
||||
use serde_cbor::{to_vec};
|
||||
|
||||
use std::env;
|
||||
|
||||
use r2d2::{Pool};
|
||||
use r2d2::{PooledConnection};
|
||||
use r2d2_sqlite::{SqliteConnectionManager};
|
||||
use r2d2_postgres::{TlsMode, PostgresConnectionManager};
|
||||
|
||||
pub type Db = PooledConnection<SqliteConnectionManager>;
|
||||
pub type Db = PooledConnection<PostgresConnectionManager>;
|
||||
|
||||
use cryp::{generate};
|
||||
use rpc::{Rpc,RpcMessage};
|
||||
@ -13,7 +15,7 @@ use rpc::{Rpc,RpcMessage};
|
||||
struct Server {
|
||||
out: Sender,
|
||||
rpc: Rpc,
|
||||
db: Pool<SqliteConnectionManager>,
|
||||
db: Pool<PostgresConnectionManager>,
|
||||
}
|
||||
|
||||
impl Handler for Server {
|
||||
@ -32,7 +34,7 @@ impl Handler for Server {
|
||||
fn on_close(&mut self, code: CloseCode, reason: &str) {
|
||||
match code {
|
||||
CloseCode::Normal => println!("The client is done with the connection."),
|
||||
// CloseCode::Away => println!("The client is leaving the site."),
|
||||
CloseCode::Away => println!("The client is leaving the site."),
|
||||
CloseCode::Abnormal => println!(
|
||||
"Closing handshake failed! Unable to obtain closing status from client."),
|
||||
_ => println!("The client encountered an error: {}", reason),
|
||||
@ -45,7 +47,11 @@ impl Handler for Server {
|
||||
}
|
||||
|
||||
pub fn start() {
|
||||
let manager = SqliteConnectionManager::file("/var/cryps/cryps.db");
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("DATABASE_URL must be set");
|
||||
|
||||
let manager = PostgresConnectionManager::new(database_url, TlsMode::None)
|
||||
.expect("could not instantiate pg manager");
|
||||
|
||||
let pool = Pool::builder()
|
||||
.build(manager)
|
||||
|
||||
@ -3,38 +3,35 @@ use uuid::Uuid;
|
||||
|
||||
use net::Db;
|
||||
use rpc::{AccountCreateParams};
|
||||
use bcrypt::{DEFAULT_COST, hash};
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct User {
|
||||
name: String,
|
||||
password: String,
|
||||
id: Uuid,
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
||||
let pw_hash = hash(¶ms.password, DEFAULT_COST)
|
||||
.expect("unable to hash password");
|
||||
|
||||
let uuid = Uuid::new_v4();
|
||||
|
||||
let id = Uuid::new_v4();
|
||||
let user = User {
|
||||
id: uuid,
|
||||
password: pw_hash,
|
||||
name: params.name,
|
||||
id,
|
||||
name: "heeeya".to_string(),
|
||||
};
|
||||
|
||||
let query = "
|
||||
INSERT INTO users (id, name, password)
|
||||
VALUES ($1, $2, $3)
|
||||
INSERT INTO users (id, name)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *;
|
||||
";
|
||||
|
||||
let entry = db
|
||||
.query(query, &[&user.id.to_string(), &user.name]).unwrap();
|
||||
let result = db.query(query, &[&user.id, &user.name]).expect("user insert failed");
|
||||
let returned = result.iter().next().expect("no row returned");
|
||||
|
||||
let entry = User {
|
||||
id: returned.get(0),
|
||||
name: returned.get(1),
|
||||
};
|
||||
|
||||
println!("{:?}", entry);
|
||||
|
||||
match to_vec(&true) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("couldn't serialize cryp"),
|
||||
}
|
||||
to_vec(&entry).expect("serialising user failed")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user