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