Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#![windows_subsystem = "windows"]
- #[macro_use]
- extern crate serde_derive;
- extern crate serde_json;
- extern crate web_view;
- use web_view::*;
- fn main() {
- let html = format!(r#"
- <!doctype html>
- <html>
- <head>
- {styles}
- </head>
- <body onLoad="load()">
- <!--[if lt IE 9]>
- <div class="ie-upgrade-container">
- <p class="ie-upgrade-message">Please, upgrade Internet Explorer to continue using this software.</p>
- <a class="ie-upgrade-link" target="_blank" href="https://www.microsoft.com/en-us/download/internet-explorer.aspx">Upgrade</a>
- </div>
- <![endif]-->
- <!--[if gte IE 9 | !IE ]> <!-->
- <canvas width="300" height="300" id="c"></canvas>
- {scripts}
- <![endif]-->
- </body>
- </html>
- "#,
- styles = inline_style(include_str!("../website/css/styles.css")),
- scripts = inline_script(include_str!("../website/lib/fabric.min.js")) +
- &inline_script(include_str!("../website/js/main.js"))
- );
- let size = (320, 320);
- let resizable = false;
- let debug = true;
- let init_cb = |_webview| {};
- let frontend_cb = |webview: &mut WebView<Board>, arg: &_, board: &mut Board| {
- use Cmd::*;
- match serde_json::from_str(arg).expect("Failed to extract arguments") {
- Init => board.new_game(),
- Play { cell_x, cell_y } => match board.play(cell_x, cell_y) {
- Some(winner) => {
- render(webview, board);
- update_winner(webview, winner)
- }
- None => report_wrong_move(webview),
- },
- Reset => board.new_game(),
- GetPieceAt { cell_x, cell_y } => consume_piece(webview, &board.piece_at(cell_x, cell_y),
- &cell_x, &cell_y),
- GetTurn => consume_turn(webview, &board.turn()),
- }
- };
- let board = Board::new();
- run("Tic Tac Toe App", Content::Html(html), Some(size), resizable, debug, init_cb, frontend_cb, board);
- }
- #[derive(Debug, Serialize, Deserialize)]
- pub struct Board {
- elements: [Side; 9],
- side_to_play: Side,
- }
- impl Board {
- fn new() -> Board {
- Board { elements: [Side::None; 9], side_to_play: Side::Cross }
- }
- fn piece_at(&self, cell_x: usize, cell_y: usize) -> Side {
- return self.elements[(cell_y * 3) + cell_x]
- }
- fn turn(&self) -> Side {
- self.side_to_play
- }
- fn new_game(&mut self) {
- self.elements = [Side::None; 9];
- self.side_to_play = Side::Cross;
- }
- fn change_player(&mut self) {
- self.side_to_play = match self.side_to_play {
- Side::Cross => Side::Circle,
- Side::Circle => Side::Cross,
- Side::None => Side::None
- }
- }
- pub fn play(&mut self, cell_x: usize, cell_y: usize) -> Option<Side>
- {
- if cell_x > 2 || cell_y > 2 {
- return None;
- }
- let piece_there = self.elements[(cell_y * 3) + cell_x];
- if piece_there != Side::None {
- return None;
- }
- self.elements[(cell_y * 3) + cell_x] = self.side_to_play;
- self.change_player();
- return Some(Side::None);
- }
- }
- #[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq)]
- pub enum Side {
- None,
- Circle,
- Cross,
- }
- #[allow(non_camel_case_types)]
- #[derive(Deserialize)]
- #[serde(tag = "cmd")]
- pub enum Cmd {
- Init,
- Play { cell_x: usize, cell_y: usize },
- Reset,
- GetPieceAt { cell_x: usize, cell_y: usize },
- GetTurn,
- }
- fn render<'a, T>(webview: &mut WebView<'a, T>, board: &Board) {
- let render_task = format!("rpc.render({})",
- serde_json::to_string(board).expect("Failed to send render envent to front-end !"));
- webview.eval(&render_task);
- }
- fn consume_piece<'a, T>(webview: &mut WebView<'a, T>, piece: &Side, cell_x: &usize, cell_y: &usize) {
- let consume_task = format!("rpc.consume_piece({}, {}, {})",
- serde_json::to_string(piece).expect("Failed to send consume piece event to front-end !"),
- serde_json::to_string(cell_x).expect("Failed to send consume piece event to front-end !"),
- serde_json::to_string(cell_y).expect("Failed to send consume piece event to front-end !"),
- );
- webview.eval(&consume_task);
- }
- fn consume_turn<'a, T>(webview: &mut WebView<'a, T>, turn: &Side) {
- let consume_task = format!("rpc.consume_turn({})",
- serde_json::to_string(turn).expect("Failed to send consume turn event to front-end !"));
- webview.eval(&consume_task);
- }
- fn update_winner<'a, T>(_webview: &mut WebView<'a, T>, _winner: Side) {
- // TODO
- }
- fn report_wrong_move<'a, T>(_webview: &mut WebView<'a, T>) {
- // TODO
- }
- fn inline_style(s: &str) -> String {
- format!(r#"<style type="text/css">{}</style>"#, s)
- }
- fn inline_script(s: &str) -> String {
- format!(r#"<script type="text/javascript">{}</script>"#, s)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement