mirror of
https://github.com/fluencelabs/fluence-shared-canvas
synced 2025-03-14 19:50:49 +00:00
looks like it works
This commit is contained in:
parent
8681d2a8af
commit
81e121c1b9
18
Cargo.toml
18
Cargo.toml
@ -2,6 +2,24 @@
|
||||
name = "hackaton_13-04-19"
|
||||
version = "0.1.0"
|
||||
authors = ["Aleksandrov Vladimir <invis87@gmail.com>"]
|
||||
publish = false
|
||||
description = "Shared canvas on Fluence"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "shared_canvas"
|
||||
path = "src/lib.rs"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[profile.release]
|
||||
debug = false
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
panic = "abort"
|
||||
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.38"
|
||||
linked-hash-map = "0.5.1"
|
||||
fluence = { version = "0.1.4", features = ["wasm_logger"] }
|
||||
|
83
src/canvas_manager.rs
Normal file
83
src/canvas_manager.rs
Normal file
@ -0,0 +1,83 @@
|
||||
use crate::error_type::AppResult;
|
||||
use crate::request_response::Response;
|
||||
|
||||
use std::collections::hash_map::HashMap;
|
||||
use std::iter;
|
||||
use serde_json::Value;
|
||||
use log::info;
|
||||
|
||||
use crate::request_response::CellStateResponse;
|
||||
|
||||
// ================== Colour ==================
|
||||
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
||||
pub struct Colour {
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8
|
||||
}
|
||||
|
||||
impl Colour {
|
||||
pub fn new() -> Self {
|
||||
Colour {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ================== Cell ==================
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
||||
pub struct Cell {
|
||||
x: u32,
|
||||
y: u32
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
pub fn new(x: u32, y: u32) -> Self {
|
||||
Cell{x, y}
|
||||
}
|
||||
}
|
||||
|
||||
// ================== CanvasManager ==================
|
||||
|
||||
pub struct CanvasManager {
|
||||
canvas: HashMap<Cell, String>
|
||||
}
|
||||
|
||||
impl CanvasManager {
|
||||
pub fn new() -> Self {
|
||||
let x_size = 1000;
|
||||
let y_size = 1000;
|
||||
let white_colour = String::from("#ffafff");
|
||||
|
||||
let mut canvas_map: HashMap<Cell, String> = HashMap::new();
|
||||
for x in 0..x_size {
|
||||
for y in 0..y_size {
|
||||
let cell = Cell::new(x, y);
|
||||
canvas_map.insert(cell, white_colour.clone());
|
||||
}
|
||||
}
|
||||
|
||||
CanvasManager {
|
||||
canvas: canvas_map
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_response(&self) -> AppResult<Value> {
|
||||
let result_state: Vec<CellStateResponse> = self.canvas.iter().map(|kv| CellStateResponse::new(kv.0.x, kv.0.y, kv.1.clone())).collect();
|
||||
let result = Response::Matrix {
|
||||
state:result_state
|
||||
};
|
||||
|
||||
let json_response: AppResult<Value> = serde_json::to_value(result).map_err(Into::into);
|
||||
|
||||
match &json_response {
|
||||
Ok(res) => info!("JSON ok : {}", res.to_string()),
|
||||
Err(err) => info!("JSON error: {}", err.to_string())
|
||||
}
|
||||
|
||||
json_response
|
||||
}
|
||||
}
|
3
src/error_type.rs
Normal file
3
src/error_type.rs
Normal file
@ -0,0 +1,3 @@
|
||||
use std::error::Error;
|
||||
|
||||
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;
|
42
src/lib.rs
Normal file
42
src/lib.rs
Normal file
@ -0,0 +1,42 @@
|
||||
mod error_type;
|
||||
mod request_response;
|
||||
mod canvas_manager;
|
||||
|
||||
use crate::error_type::AppResult;
|
||||
use crate::canvas_manager::CanvasManager;
|
||||
use crate::request_response::{Request, Response};
|
||||
|
||||
use fluence::sdk::*;
|
||||
use serde_json::Value;
|
||||
use std::cell::RefCell;
|
||||
use log::info;
|
||||
|
||||
fn init() {
|
||||
logger::WasmLogger::init_with_level(log::Level::Info).unwrap();
|
||||
}
|
||||
|
||||
#[invocation_handler(init_fn = init)]
|
||||
fn main(req: String) -> String {
|
||||
match do_request(req) {
|
||||
Ok(res) => res.to_string(),
|
||||
Err(err) => {
|
||||
let response = Response::Error {
|
||||
message: err.to_string(),
|
||||
};
|
||||
serde_json::to_string(&response).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static CANVAS_MANAGER: RefCell<CanvasManager> = RefCell::new(CanvasManager::new());
|
||||
}
|
||||
|
||||
fn do_request(req: String) -> AppResult<Value> {
|
||||
let request: Request = serde_json::from_str(req.as_str())?;
|
||||
|
||||
match request {
|
||||
Request::Get => CANVAS_MANAGER.with(|cm| cm.borrow_mut().to_response()),
|
||||
Request::Set{x_coord, y_coord, colour} => serde_json::from_str("lala").map_err(Into::into)
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
31
src/request_response.rs
Normal file
31
src/request_response.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(tag = "action")]
|
||||
pub enum Request {
|
||||
Get,
|
||||
Set {
|
||||
x_coord: u32,
|
||||
y_coord: u32,
|
||||
colour: String
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Response {
|
||||
Matrix { state: Vec<CellStateResponse> },
|
||||
PaintResult { error: Option<String> },
|
||||
Error { message: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CellStateResponse {x: u32, y: u32, colour: String}
|
||||
|
||||
impl CellStateResponse {
|
||||
pub fn new(x: u32, y: u32, colour: String) -> Self {
|
||||
CellStateResponse {
|
||||
x, y, colour
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user