Merge branch 'sql'
This commit is contained in:
commit
fccfd0e03a
4
client/.gitignore
vendored
Normal file
4
client/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package-lock.json
|
||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
.cache/
|
||||||
5
client/index.html
Executable file
5
client/index.html
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script src="./index.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
19
client/index.js
Executable file
19
client/index.js
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
const cbor = require('borc');
|
||||||
|
const assert = require('assert');
|
||||||
|
// Create WebSocket connection.
|
||||||
|
const ws = new WebSocket('ws://localhost:40000');
|
||||||
|
ws.binaryType = 'arraybuffer';
|
||||||
|
|
||||||
|
// Connection opened
|
||||||
|
ws.addEventListener('open', function (event) {
|
||||||
|
ws.send(cbor.encode({ method: 'cryp_generate', params: { level: 64 }}));
|
||||||
|
ws.send(cbor.encode({ method: 'account_create', params: { name: 'ntr' }}));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for messages
|
||||||
|
ws.addEventListener('message', function (event) {
|
||||||
|
console.log('Message from server ', event.data);
|
||||||
|
const blob = new Uint8Array(event.data);
|
||||||
|
const decoded = cbor.decodeAll(blob);
|
||||||
|
console.log(decoded[0]);
|
||||||
|
});
|
||||||
17
client/package.json
Executable file
17
client/package.json
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "cryps-client",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "parcel index.html",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "UNLICENSED",
|
||||||
|
"dependencies": {
|
||||||
|
"borc": "^2.0.3",
|
||||||
|
"cbor": "^4.1.1",
|
||||||
|
"parcel": "^1.9.7"
|
||||||
|
}
|
||||||
|
}
|
||||||
0
ops/.gitkeep
Normal file
0
ops/.gitkeep
Normal file
6
ops/00000000000000_diesel_initial_setup/down.sql
Normal file
6
ops/00000000000000_diesel_initial_setup/down.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||||
|
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
||||||
36
ops/00000000000000_diesel_initial_setup/up.sql
Normal file
36
ops/00000000000000_diesel_initial_setup/up.sql
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Sets up a trigger for the given table to automatically set a column called
|
||||||
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||||
|
-- in the modified columns)
|
||||||
|
--
|
||||||
|
-- # Example
|
||||||
|
--
|
||||||
|
-- ```sql
|
||||||
|
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
|
||||||
|
--
|
||||||
|
-- SELECT diesel_manage_updated_at('users');
|
||||||
|
-- ```
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||||
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
||||||
|
BEGIN
|
||||||
|
IF (
|
||||||
|
NEW IS DISTINCT FROM OLD AND
|
||||||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||||
|
) THEN
|
||||||
|
NEW.updated_at := current_timestamp;
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
1
ops/2018-09-11-113648_create_lobbies/down.sql
Executable file
1
ops/2018-09-11-113648_create_lobbies/down.sql
Executable file
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE lobbies;
|
||||||
6
ops/2018-09-11-113648_create_lobbies/up.sql
Executable file
6
ops/2018-09-11-113648_create_lobbies/up.sql
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
CREATE TABLE lobbies (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
a uuid NOT NULL,
|
||||||
|
b uuid,
|
||||||
|
data bytea
|
||||||
|
);
|
||||||
3
server/.cargo/config
Executable file
3
server/.cargo/config
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
[target.x86_64-pc-windows-msvc.gnu]
|
||||||
|
rustc-link-search = ["C:\\Program Files\\PostgreSQL\\pg96\\lib"]
|
||||||
|
|
||||||
1
server/.env
Executable file
1
server/.env
Executable file
@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL=postgres://cryps:craftbeer@localhost/cryps
|
||||||
0
.gitignore → server/.gitignore
vendored
0
.gitignore → server/.gitignore
vendored
@ -10,4 +10,10 @@ serde = "1"
|
|||||||
serde_derive = "1"
|
serde_derive = "1"
|
||||||
serde_cbor = "0.9"
|
serde_cbor = "0.9"
|
||||||
ws = "*"
|
ws = "*"
|
||||||
|
|
||||||
|
dotenv = "0.9.0"
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
|
|
||||||
|
r2d2 = "0.8.2"
|
||||||
|
r2d2_sqlite = "0.6"
|
||||||
|
rusqlite = { version = "0.14.0", features = ["bundled"] }
|
||||||
7
WORKLOG.md → server/WORKLOG.md
Normal file → Executable file
7
WORKLOG.md → server/WORKLOG.md
Normal file → Executable file
@ -1,10 +1,13 @@
|
|||||||
* Battling
|
* Battling
|
||||||
* Cryp Serialisation
|
|
||||||
* RPC
|
|
||||||
* Logins
|
* Logins
|
||||||
* Cryp Ownership
|
* Cryp Ownership
|
||||||
* Matchmaking
|
* Matchmaking
|
||||||
|
* Lobbies
|
||||||
|
* Create
|
||||||
|
* Join
|
||||||
|
* Resolve
|
||||||
* Stats
|
* Stats
|
||||||
|
* Missions
|
||||||
|
|
||||||
* Cryp Generation
|
* Cryp Generation
|
||||||
*
|
*
|
||||||
@ -91,16 +91,22 @@ pub fn test_battle() {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use *;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pve_test() {
|
fn pve_test() {
|
||||||
// let player = Cryp::new()
|
let player = Cryp::new()
|
||||||
// .named("ca phe sua da".to_string())
|
.named("ca phe sua da".to_string())
|
||||||
// .level(2)
|
.level(2)
|
||||||
// .create();
|
.create();
|
||||||
|
|
||||||
// levelling(player);
|
levelling(player);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn battle_test() {
|
||||||
|
test_battle();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3,6 +3,12 @@ extern crate uuid;
|
|||||||
extern crate ws;
|
extern crate ws;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
|
// #[macro_use]
|
||||||
|
// extern crate dotenv;
|
||||||
|
extern crate r2d2;
|
||||||
|
extern crate r2d2_sqlite;
|
||||||
|
extern crate rusqlite;
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_cbor;
|
extern crate serde_cbor;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -14,6 +20,7 @@ mod net;
|
|||||||
mod combat;
|
mod combat;
|
||||||
mod skill;
|
mod skill;
|
||||||
mod rpc;
|
mod rpc;
|
||||||
|
mod user;
|
||||||
|
|
||||||
use net::{start};
|
use net::{start};
|
||||||
|
|
||||||
@ -1,11 +1,19 @@
|
|||||||
use cryp::{generate};
|
|
||||||
use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
use ws::{listen, Handler, Sender, Result, Message, Handshake, CloseCode, Error};
|
||||||
use rpc::{Rpc,RpcMessage};
|
|
||||||
use serde_cbor::{to_vec};
|
use serde_cbor::{to_vec};
|
||||||
|
|
||||||
|
use r2d2::{Pool};
|
||||||
|
use r2d2::{PooledConnection};
|
||||||
|
use r2d2_sqlite::{SqliteConnectionManager};
|
||||||
|
|
||||||
|
pub type Db = PooledConnection<SqliteConnectionManager>;
|
||||||
|
|
||||||
|
use cryp::{generate};
|
||||||
|
use rpc::{Rpc,RpcMessage};
|
||||||
|
|
||||||
struct Server {
|
struct Server {
|
||||||
out: Sender,
|
out: Sender,
|
||||||
rpc: Rpc,
|
rpc: Rpc,
|
||||||
|
db: Pool<SqliteConnectionManager>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Handler for Server {
|
impl Handler for Server {
|
||||||
@ -15,7 +23,8 @@ impl Handler for Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn on_message(&mut self, msg: Message) -> Result<()> {
|
fn on_message(&mut self, msg: Message) -> Result<()> {
|
||||||
let reply = self.rpc.receive(msg);
|
let db = self.db.get().expect("unable to get db connection");
|
||||||
|
let reply = self.rpc.receive(msg, db);
|
||||||
println!("{:?}", reply);
|
println!("{:?}", reply);
|
||||||
self.out.send(reply.unwrap())
|
self.out.send(reply.unwrap())
|
||||||
}
|
}
|
||||||
@ -36,5 +45,11 @@ impl Handler for Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {} } }).unwrap();
|
let manager = SqliteConnectionManager::file("/var/cryps/cryps.db");
|
||||||
|
|
||||||
|
let pool = Pool::builder()
|
||||||
|
.build(manager)
|
||||||
|
.expect("Failed to create pool.");
|
||||||
|
|
||||||
|
listen("127.0.0.1:40000", |out| { Server { out, rpc: Rpc {}, db: pool.clone() } }).unwrap();
|
||||||
}
|
}
|
||||||
@ -3,23 +3,14 @@ use ws::{Message};
|
|||||||
use serde_cbor::{from_slice};
|
use serde_cbor::{from_slice};
|
||||||
use serde_cbor::error::Error as CborError;
|
use serde_cbor::error::Error as CborError;
|
||||||
|
|
||||||
|
use net::Db;
|
||||||
use cryp::generate;
|
use cryp::generate;
|
||||||
|
use user::{create};
|
||||||
|
|
||||||
pub struct Rpc;
|
pub struct Rpc;
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
||||||
struct GenerateMsg {
|
|
||||||
method: String,
|
|
||||||
params: GenerateParams,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
||||||
pub struct GenerateParams {
|
|
||||||
pub level: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Rpc {
|
impl Rpc {
|
||||||
pub fn receive(&self, msg: Message) -> StdResult<Vec<u8>, RpcError> {
|
pub fn receive(&self, msg: Message, db: Db) -> StdResult<Vec<u8>, RpcError> {
|
||||||
// consume the ws data into bytes
|
// consume the ws data into bytes
|
||||||
let data = msg.into_data();
|
let data = msg.into_data();
|
||||||
|
|
||||||
@ -36,6 +27,13 @@ impl Rpc {
|
|||||||
Err(_) => Err(RpcError::Parse),
|
Err(_) => Err(RpcError::Parse),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"account_create" => {
|
||||||
|
match from_slice::<AccountCreateMsg>(&data) {
|
||||||
|
Ok(v) => Ok(create(v.params, db)),
|
||||||
|
Err(_) => Err(RpcError::Parse),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
_ => Err(RpcError::UnknownMethod),
|
_ => Err(RpcError::UnknownMethod),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -55,15 +53,38 @@ pub enum RpcError {
|
|||||||
UnknownMethod,
|
UnknownMethod,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
mod tests {
|
struct GenerateMsg {
|
||||||
use super::*;
|
method: String,
|
||||||
use serde_cbor::to_vec;
|
params: GenerateParams,
|
||||||
#[test]
|
|
||||||
fn rpc_parse() {
|
|
||||||
let rpc = Rpc {};
|
|
||||||
let msg = GenerateMsg { method: "cryp_generate".to_string(), params: GenerateParams { level: 64 } };
|
|
||||||
let v = to_vec(&msg).unwrap();
|
|
||||||
let received = rpc.receive(Message::Binary(v));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
pub struct GenerateParams {
|
||||||
|
pub level: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
struct AccountCreateMsg {
|
||||||
|
method: String,
|
||||||
|
params: AccountCreateParams,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
|
pub struct AccountCreateParams {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod tests {
|
||||||
|
// use super::*;
|
||||||
|
// use serde_cbor::to_vec;
|
||||||
|
// #[test]
|
||||||
|
// fn rpc_parse() {
|
||||||
|
// let rpc = Rpc {};
|
||||||
|
// let msg = GenerateMsg { method: "cryp_generate".to_string(), params: GenerateParams { level: 64 } };
|
||||||
|
// let v = to_vec(&msg).unwrap();
|
||||||
|
// let received = rpc.receive(Message::Binary(v));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
29
server/src/user.rs
Normal file
29
server/src/user.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use serde_cbor::to_vec;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use net::Db;
|
||||||
|
use rpc::{AccountCreateParams};
|
||||||
|
|
||||||
|
struct User {
|
||||||
|
name: String,
|
||||||
|
id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(params: AccountCreateParams, db: Db) -> Vec<u8> {
|
||||||
|
let uuid = Uuid::new_v4();
|
||||||
|
let user = User {
|
||||||
|
id: uuid,
|
||||||
|
name: params.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
let entry = db.execute("INSERT INTO users (id, name)
|
||||||
|
VALUES (?1, ?2)",
|
||||||
|
&[&user.id.to_string(), &user.name]).unwrap();
|
||||||
|
|
||||||
|
println!("{:?}", entry);
|
||||||
|
|
||||||
|
match to_vec(&true) {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => panic!("couldn't serialize cryp"),
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user