Commit 21e11058 authored by Chunchi Che's avatar Chunchi Che

handle damage msg in rust-src

parent 95b51bd0
Pipeline #20648 failed with stages
in 7 minutes and 36 seconds
...@@ -89,6 +89,7 @@ name = "rust-src" ...@@ -89,6 +89,7 @@ name = "rust-src"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"js-sys",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-test", "wasm-bindgen-test",
"wee_alloc", "wee_alloc",
......
...@@ -10,10 +10,11 @@ crate-type = ["cdylib", "rlib"] ...@@ -10,10 +10,11 @@ crate-type = ["cdylib", "rlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features] [features]
default = ["console_error_panic_hook"] default = ["console_error_panic_hook", "wee_alloc"]
[dependencies] [dependencies]
wasm-bindgen = "0.2.63" wasm-bindgen = "0.2.63"
js-sys = "0.3.61"
console_error_panic_hook = { version = "0.1.6", optional = true } console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true } wee_alloc = { version = "0.4.5", optional = true }
......
#![allow(non_snake_case)]
mod buffer;
mod utils; mod utils;
use wasm_bindgen::prelude::*; use std::convert::TryInto;
use wasm_bindgen::prelude::wasm_bindgen;
pub use utils::set_panic_hook; pub use utils::set_panic_hook;
...@@ -11,11 +15,20 @@ pub use utils::set_panic_hook; ...@@ -11,11 +15,20 @@ pub use utils::set_panic_hook;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { pub struct MsgUpdateHp {
fn alert(s: &str); pub player: Option<u8>,
pub value: Option<i32>,
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn greet() { pub fn ocgDamageAdapter(data: js_sys::Uint8Array) -> MsgUpdateHp {
alert("Hello, {{project-name}}!"); let data = data.to_vec();
let player = data[0];
let value = data[1..5].try_into().map(i32::from_le_bytes).ok();
MsgUpdateHp {
player: Some(player),
value,
}
} }
import { ygopro } from "../../../idl/ocgcore"; import { ygopro } from "../../../idl/ocgcore";
import { BufferReader } from "../../bufferIO"; import { ocgDamageAdapter } from "rust-src";
/* /*
* Msg Damage * Msg Damage
...@@ -8,14 +8,7 @@ import { BufferReader } from "../../bufferIO"; ...@@ -8,14 +8,7 @@ import { BufferReader } from "../../bufferIO";
* @param value - 减少的Hp数值 * @param value - 减少的Hp数值
* */ * */
export default (data: Uint8Array) => { export default (data: Uint8Array) => {
const reader = new BufferReader(data, true); const damage = ocgDamageAdapter(data);
const player = reader.readUint8(); return new ygopro.StocGameMessage.MsgUpdateHp(damage);
const value = reader.readInt32();
return new ygopro.StocGameMessage.MsgUpdateHp({
player,
type_: ygopro.StocGameMessage.MsgUpdateHp.ActionType.DAMAGE,
value,
});
}; };
import React, { Suspense } from "react"; import React, { Suspense } from "react";
import { Routes, Route } from "react-router-dom"; import { Routes, Route } from "react-router-dom";
import LazyLoad, { Loading } from "./LazyLoad"; import LazyLoad, { Loading } from "./LazyLoad";
import { greet } from "rust-src";
const Login = React.lazy(() => import("./Login")); const Login = React.lazy(() => import("./Login"));
const WaitRoom = React.lazy(() => import("./WaitRoom")); const WaitRoom = React.lazy(() => import("./WaitRoom"));
...@@ -9,8 +8,6 @@ const Mora = React.lazy(() => import("./Mora")); ...@@ -9,8 +8,6 @@ const Mora = React.lazy(() => import("./Mora"));
const NeosDuel = React.lazy(() => import("./Duel/main")); const NeosDuel = React.lazy(() => import("./Duel/main"));
export default function () { export default function () {
greet();
return ( return (
<Routes> <Routes>
<Route path="/" element={<LazyLoad lazy={<Login />} />} /> <Route path="/" element={<LazyLoad lazy={<Login />} />} />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment