Advertisement
loloof64

server side webview rust

Nov 22nd, 2018
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.51 KB | None | 0 0
  1. //#![windows_subsystem = "windows"]
  2.  
  3. #[macro_use]
  4. extern crate serde_derive;
  5. extern crate serde_json;
  6. extern crate web_view;
  7. use web_view::*;
  8.  
  9. fn main() {
  10.     let html = format!(r#"
  11.         <!doctype html>
  12.         <html>
  13.             <head>
  14.                 {styles}
  15.             </head>
  16.             <body onLoad="load()">
  17.                 <!--[if lt IE 9]>
  18.                 <div class="ie-upgrade-container">
  19.                     <p class="ie-upgrade-message">Please, upgrade Internet Explorer to continue using this software.</p>
  20.                     <a class="ie-upgrade-link" target="_blank" href="https://www.microsoft.com/en-us/download/internet-explorer.aspx">Upgrade</a>
  21.                 </div>
  22.                 <![endif]-->
  23.                 <!--[if gte IE 9 | !IE ]> <!-->
  24.                 <canvas width="300" height="300" id="c"></canvas>
  25.                 {scripts}
  26.                 <![endif]-->
  27.             </body>
  28.         </html>
  29.         "#,
  30.         styles = inline_style(include_str!("../website/css/styles.css")),
  31.         scripts = inline_script(include_str!("../website/lib/fabric.min.js")) +
  32.            &inline_script(include_str!("../website/js/main.js"))
  33.     );
  34.  
  35.    let size = (320, 320);
  36.     let resizable = false;
  37.     let debug = true;
  38.     let init_cb = |_webview| {};
  39.     let frontend_cb = |webview: &mut WebView<Board>, arg: &_, board: &mut Board| {
  40.         use Cmd::*;
  41.  
  42.         match serde_json::from_str(arg).expect("Failed to extract arguments") {
  43.             Init => board.new_game(),
  44.             Play { cell_x, cell_y } => match board.play(cell_x, cell_y) {
  45.                 Some(winner) => {
  46.                     render(webview, board);
  47.                     update_winner(webview, winner)
  48.                 }
  49.                 None => report_wrong_move(webview),
  50.             },
  51.             Reset => board.new_game(),
  52.             GetPieceAt { cell_x, cell_y } => consume_piece(webview, &board.piece_at(cell_x, cell_y),
  53.                                                          &cell_x, &cell_y),
  54.             GetTurn => consume_turn(webview, &board.turn()),
  55.         }
  56.     };
  57.     let board = Board::new();
  58.     run("Tic Tac Toe App", Content::Html(html), Some(size), resizable, debug, init_cb, frontend_cb, board);
  59. }
  60.  
  61. #[derive(Debug, Serialize, Deserialize)]
  62. pub struct Board {
  63.     elements:  [Side; 9],
  64.     side_to_play: Side,
  65. }
  66.  
  67. impl Board {
  68.     fn new() -> Board {
  69.         Board { elements: [Side::None; 9], side_to_play: Side::Cross }
  70.     }
  71.  
  72.     fn piece_at(&self, cell_x: usize, cell_y: usize) -> Side {
  73.         return self.elements[(cell_y * 3) + cell_x]
  74.     }
  75.  
  76.     fn turn(&self) -> Side {
  77.         self.side_to_play
  78.     }
  79.  
  80.     fn new_game(&mut self) {
  81.         self.elements = [Side::None; 9];
  82.         self.side_to_play = Side::Cross;
  83.     }
  84.  
  85.     fn change_player(&mut self) {
  86.         self.side_to_play = match self.side_to_play {
  87.             Side::Cross => Side::Circle,
  88.             Side::Circle => Side::Cross,
  89.             Side::None => Side::None
  90.         }
  91.     }
  92.  
  93.     pub fn play(&mut self, cell_x: usize, cell_y: usize) -> Option<Side>
  94.     {
  95.         if cell_x > 2 || cell_y > 2 {
  96.             return None;
  97.         }
  98.  
  99.         let piece_there = self.elements[(cell_y * 3) + cell_x];
  100.         if piece_there != Side::None {
  101.             return None;
  102.         }
  103.  
  104.         self.elements[(cell_y * 3) + cell_x] = self.side_to_play;
  105.         self.change_player();
  106.  
  107.         return Some(Side::None);
  108.     }
  109. }
  110.  
  111. #[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq)]
  112. pub enum Side {
  113.     None,
  114.     Circle,
  115.     Cross,
  116. }
  117.  
  118. #[allow(non_camel_case_types)]
  119. #[derive(Deserialize)]
  120. #[serde(tag = "cmd")]
  121. pub enum Cmd {
  122.    Init,
  123.    Play { cell_x: usize, cell_y: usize },
  124.     Reset,
  125.     GetPieceAt { cell_x: usize, cell_y: usize },
  126.     GetTurn,
  127. }
  128.  
  129. fn render<'a, T>(webview: &mut WebView<'a, T>, board: &Board) {
  130.     let render_task = format!("rpc.render({})",
  131.         serde_json::to_string(board).expect("Failed to send render envent to front-end !"));
  132.     webview.eval(&render_task);
  133. }
  134.  
  135. fn consume_piece<'a, T>(webview: &mut WebView<'a, T>, piece: &Side, cell_x: &usize, cell_y: &usize) {
  136.     let consume_task = format!("rpc.consume_piece({}, {}, {})",
  137.         serde_json::to_string(piece).expect("Failed to send consume piece event to front-end !"),
  138.         serde_json::to_string(cell_x).expect("Failed to send consume piece event to front-end !"),
  139.         serde_json::to_string(cell_y).expect("Failed to send consume piece event to front-end !"),
  140.     );
  141.     webview.eval(&consume_task);
  142. }
  143.  
  144. fn consume_turn<'a, T>(webview: &mut WebView<'a, T>, turn: &Side) {
  145.     let consume_task = format!("rpc.consume_turn({})",
  146.         serde_json::to_string(turn).expect("Failed to send consume turn event to front-end !"));
  147.     webview.eval(&consume_task);
  148. }
  149.  
  150. fn update_winner<'a, T>(_webview: &mut WebView<'a, T>, _winner: Side) {
  151.     // TODO
  152. }
  153.  
  154. fn report_wrong_move<'a, T>(_webview: &mut WebView<'a, T>) {
  155.     // TODO
  156. }
  157.  
  158. fn inline_style(s: &str) -> String {
  159.    format!(r#"<style type="text/css">{}</style>"#, s)
  160. }
  161.  
  162. fn inline_script(s: &str) -> String {
  163.    format!(r#"<script type="text/javascript">{}</script>"#, s)
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement