fair/
wasm.rs

1use crate::games::*;
2use crate::ProvablyFairConfig;
3use wasm_bindgen::prelude::*;
4
5use serde::Deserialize;
6
7// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
8// allocator.
9#[cfg(feature = "wee_alloc")]
10#[global_allocator]
11static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
12
13/*
14#[wasm_bindgen]
15extern "C" {
16    fn alert(s: &str);
17}
18
19#[wasm_bindgen]
20pub fn greet() {
21    alert("Hello, hello-wasm!");
22}
23*/
24
25fn default_plinko_risk() -> String {
26    "low".to_string()
27}
28
29fn default_plinko_rows() -> i32 {
30    8
31}
32
33fn default_wheel_segments() -> i32 {
34    10
35}
36
37fn default_wheel_risk() -> String {
38    "low".to_string()
39}
40
41#[derive(Deserialize)]
42struct PlinkoOpts {
43    #[serde(default = "default_plinko_rows")]
44    rows: i32,
45    #[serde(default = "default_plinko_risk")]
46    risk: String,
47}
48
49#[derive(Deserialize)]
50struct WheelOpts {
51    #[serde(default = "default_wheel_segments")]
52    segments: i32,
53    #[serde(default = "default_wheel_risk")]
54    risk: String,
55}
56
57fn default_mines_mines() -> i32 {
58    3
59}
60
61#[derive(Deserialize)]
62struct MinesOpts {
63    #[serde(default = "default_mines_mines")]
64    mines: i32,
65}
66
67fn default_slots_round() -> i32 {
68    0
69}
70
71#[derive(Deserialize)]
72struct SlotsOpts {
73    #[serde(default = "default_slots_round")]
74    round: i32,
75}
76
77#[wasm_bindgen]
78pub fn simulate(
79    game: &str,
80    client_seed: &str,
81    server_seed: &str,
82    nonce: u32,
83    opts: &JsValue,
84) -> String {
85    let config = ProvablyFairConfig::new(client_seed, server_seed, nonce as u64);
86    let result = match game {
87        "baccarat" => baccarat::simulate(config).to_string(),
88        "dice" => dice::simulate(config).to_string(),
89        "limbo" => limbo::simulate(config).to_string(),
90        "hilo" => hilo::simulate(config).to_string(),
91        "blackjack" => blackjack::simulate(config).to_string(),
92        "diamond_poker" => diamond_poker::simulate(config).to_string(),
93        "roulette" => roulette::simulate(config).to_string(),
94        "keno" => keno::simulate(config).to_string(),
95        "plinko" => {
96            let opts: PlinkoOpts = opts.into_serde().unwrap();
97            let rows = opts.rows as u8;
98            let risk = plinko::Risk::from_str(&opts.risk);
99            let res = plinko::simulate(config, Some(plinko::Opts::new(rows, risk))).to_string();
100            // format!("Rows: {} Risk: {:?}\n{}", rows, risk, res)
101            res
102        }
103        "mines" => {
104            let opts: MinesOpts = opts.into_serde().unwrap();
105            let mines = opts.mines as u8;
106            let res = mines::simulate(config, mines).to_string();
107            // format!("Rows: {} Risk: {:?}\n{}", rows, risk, res)
108            res
109        }
110        "video_poker" => video_poker::simulate(config).to_string(),
111        "wheel" => {
112            let opts: WheelOpts = opts.into_serde().unwrap();
113            let segments = opts.segments as u8;
114            let risk = wheel::Risk::from_str(&opts.risk);
115            let res = wheel::simulate(config, Some(wheel::Opts::new(segments, risk))).to_string();
116            res
117        }
118        "slots" => {
119            let opts: SlotsOpts = opts.into_serde().unwrap();
120            let round = opts.round as usize;
121            let res = slots::simulate(config, round).to_string();
122            res
123        }
124        _ => panic!("This branch should never execute. Unimplemented game?"),
125    };
126    result
127}