This commit is contained in:
ntr 2018-09-13 18:52:27 +10:00
parent 407c62fc8a
commit 0e0cd0fdcd
8 changed files with 31 additions and 13 deletions

View File

@ -1,7 +1,12 @@
exports.up = async knex => { exports.up = async knex => {
return knex.schema.createTable('users', table => { return knex.schema.createTable('users', table => {
table.uuid('id').primary(); table.uuid('id').primary();
table.text('name'); table.string('name', 42).notNullable().unique();
table.string('password').notNullable();
table.string('token', 64).notNullable();
table.index('name');
table.index('id');
}); });
}; };

View File

@ -9,7 +9,9 @@ uuid = { version = "0.5", features = ["serde", "v4"] }
serde = "1" serde = "1"
serde_derive = "1" serde_derive = "1"
serde_cbor = "0.9" serde_cbor = "0.9"
ws = "*" ws = "*"
bcrypt = "0.2"
dotenv = "0.9.0" dotenv = "0.9.0"
env_logger = "*" env_logger = "*"

View File

@ -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

View File

@ -1,8 +1,6 @@
use uuid::Uuid; use uuid::Uuid;
use rand::prelude::*; use rand::prelude::*;
use serde_cbor::*; use serde_cbor::*;
use std::fs::File;
use std::io::prelude::*;
use rpc::{GenerateParams}; use rpc::{GenerateParams};
use skill::{Skill}; use skill::{Skill};
@ -104,7 +102,7 @@ impl Cryp {
}; };
} }
pub fn named(mut self, name: String) -> Cryp { pub fn named(self, name: String) -> Cryp {
self.name = name.clone(); self.name = name.clone();
self self
} }

View File

@ -14,6 +14,8 @@ 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;

View File

@ -32,7 +32,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),

View File

@ -1,7 +1,6 @@
use std::result::Result as StdResult; use std::result::Result as StdResult;
use ws::{Message}; use ws::{Message};
use serde_cbor::{from_slice}; use serde_cbor::{from_slice};
use serde_cbor::error::Error as CborError;
use net::Db; use net::Db;
use cryp::generate; use cryp::generate;
@ -73,6 +72,7 @@ struct AccountCreateMsg {
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct AccountCreateParams { pub struct AccountCreateParams {
pub name: String, pub name: String,
pub password: String,
} }

View File

@ -3,22 +3,33 @@ use uuid::Uuid;
use net::Db; use net::Db;
use rpc::{AccountCreateParams}; use rpc::{AccountCreateParams};
use bcrypt::{DEFAULT_COST, hash};
struct User { struct User {
name: String, name: String,
password: String,
id: Uuid, id: Uuid,
} }
pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> { pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
let pw_hash = hash(&params.password, DEFAULT_COST)
.expect("unable to hash password");
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let user = User { let user = User {
id: uuid, id: uuid,
password: pw_hash,
name: params.name, name: params.name,
}; };
let entry = db.execute("INSERT INTO users (id, name) let query = "
VALUES (?1, ?2)", INSERT INTO users (id, name, password)
&[&user.id.to_string(), &user.name]).unwrap(); VALUES ($1, $2, $3)
";
let entry = db
.query(query, &[&user.id.to_string(), &user.name]).unwrap();
println!("{:?}", entry); println!("{:?}", entry);