mod error;
mod key;
mod network;
mod task;
mod utils;
use serde::{Deserialize, Serialize};
pub use serde_json::{json, Value};
pub use error::Error;
pub use ethereum_types::{Address, H160};
pub use key::*;
pub use network::*;
pub use task::*;
pub use tdn_types::primitives::PeerId;
pub use utils::*;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Default)]
pub struct HandleResult<P: Param> {
pub all: Vec<P>,
pub one: Vec<(PeerId, P)>,
pub over: bool,
pub started: bool,
}
impl<P: Param> HandleResult<P> {
pub fn add_all(&mut self, param: P) {
self.all.push(param);
}
pub fn add_one(&mut self, account: PeerId, param: P) {
self.one.push((account, param));
}
pub fn over(&mut self) {
self.over = true;
}
pub fn started(&mut self) {
self.started = true;
}
}
pub trait Param: Sized + Send + Default {
fn to_string(&self) -> String;
fn from_string(s: String) -> Result<Self>;
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(bytes: Vec<u8>) -> Result<Self>;
fn to_value(&self) -> Value;
fn from_value(v: Value) -> Result<Self>;
}
#[async_trait::async_trait]
pub trait Task: Send + Sync {
type H: Handler;
fn timer(&self) -> u64;
async fn run(
&mut self,
state: &mut Self::H,
) -> Result<HandleResult<<Self::H as Handler>::Param>>;
}
pub type Tasks<H> = Vec<Box<dyn Task<H = H>>>;
#[derive(Copy, Clone, Debug)]
pub struct Player {
pub account: Address,
pub peer: PeerId,
pub signer: [u8; 32],
}
pub const PLAYER_BYTES_LEN: usize = 72;
impl Player {
pub fn from_bytes(bytes: &[u8]) -> Result<Player> {
if bytes.len() < PLAYER_BYTES_LEN {
return Err(Error::Serialize);
}
let mut account_bytes = [0u8; 20];
let mut peer_bytes = [0u8; 20];
let mut signer_bytes = [0u8; 32];
account_bytes.copy_from_slice(&bytes[0..20]);
peer_bytes.copy_from_slice(&bytes[20..40]);
signer_bytes.copy_from_slice(&bytes[40..72]);
Ok(Player {
account: H160(account_bytes),
peer: PeerId(peer_bytes),
signer: signer_bytes,
})
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = self.account.0.to_vec();
bytes.extend(self.peer.0.to_vec());
bytes.extend(self.signer.to_vec());
bytes
}
}
#[async_trait::async_trait]
pub trait Handler: Send + Sized + 'static {
type Param: Param;
fn viewable() -> bool {
false
}
async fn chain_accept(_players: &[Player]) -> Vec<u8> {
vec![]
}
async fn chain_create(
_players: &[Player],
_params: Vec<u8>,
_rid: RoomId,
_seed: [u8; 32],
) -> Option<(Self, Tasks<Self>)> {
None
}
async fn pozk_create(
_player: Player,
_params: Vec<u8>,
_rid: RoomId,
) -> Option<(Self, Tasks<Self>)> {
None
}
async fn pozk_join(
&mut self,
_player: Player,
_params: Vec<u8>,
) -> Result<HandleResult<Self::Param>> {
Ok(HandleResult::default())
}
async fn viewer_online(&mut self, _peer: PeerId) -> Result<HandleResult<Self::Param>> {
Ok(HandleResult::default())
}
async fn viewer_offline(&mut self, _peer: PeerId) -> Result<HandleResult<Self::Param>> {
Ok(HandleResult::default())
}
async fn online(&mut self, _peer: PeerId) -> Result<HandleResult<Self::Param>> {
Ok(HandleResult::default())
}
async fn offline(&mut self, _peer: PeerId) -> Result<HandleResult<Self::Param>> {
Ok(HandleResult::default())
}
async fn handle(
&mut self,
_peer: PeerId,
_param: Self::Param,
) -> Result<HandleResult<Self::Param>> {
Ok(HandleResult::default())
}
async fn prove(&mut self) -> Result<(Vec<u8>, Vec<u8>)>;
}
impl Param for Value {
fn to_string(&self) -> String {
serde_json::to_string(&self).unwrap_or("".to_owned())
}
fn from_string(s: String) -> Result<Self> {
Ok(serde_json::from_str(&s)?)
}
fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(&self).unwrap_or(vec![])
}
fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
Ok(serde_json::from_slice(&bytes)?)
}
fn to_value(&self) -> Value {
self.clone()
}
fn from_value(v: Value) -> Result<Self> {
Ok(v)
}
}
impl Param for String {
fn to_string(&self) -> String {
self.clone()
}
fn from_string(s: String) -> Result<Self> {
Ok(s)
}
fn to_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
String::from_utf8(bytes).map_err(|_| Error::Serialize)
}
fn to_value(&self) -> Value {
Value::String(self.clone())
}
fn from_value(v: Value) -> Result<Self> {
v.as_str().map(|v| v.to_owned()).ok_or(Error::Serialize)
}
}
impl Param for Vec<u8> {
fn to_string(&self) -> String {
hex::encode(&self)
}
fn from_string(s: String) -> Result<Self> {
Ok(hex::decode(s)?)
}
fn to_bytes(&self) -> Vec<u8> {
self.clone()
}
fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
Ok(bytes)
}
fn to_value(&self) -> Value {
Value::String(self.to_string())
}
fn from_value(v: Value) -> Result<Self> {
let s = v.as_str().map(|v| v.to_owned()).ok_or(Error::Serialize)?;
Self::from_string(s)
}
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct MethodValues {
pub method: String,
pub params: Vec<Value>,
}
impl MethodValues {
pub fn new(method: &str, params: Vec<Value>) -> Self {
Self {
method: method.to_owned(),
params,
}
}
}
impl Param for MethodValues {
fn to_string(&self) -> String {
serde_json::to_string(&self).unwrap_or("".to_owned())
}
fn from_string(s: String) -> Result<Self> {
Ok(serde_json::from_str(&s)?)
}
fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(&self).unwrap_or(vec![])
}
fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
Ok(serde_json::from_slice(&bytes)?)
}
fn to_value(&self) -> Value {
json!({
"method": self.method,
"params": self.params,
})
}
fn from_value(v: Value) -> Result<Self> {
Ok(serde_json::from_value(v)?)
}
}