Advertisement
Guest User

ReactJS - zdrojové kódy k ukážkovej aplikácii Pexeso

a guest
Jun 26th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="sk" xml:lang="sk">
  3. <head>
  4.     <meta charset="utf-8" />
  5.     <title>ReactJS - Pexeso</title>
  6.     <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
  7.     <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
  9.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  10.     <script src="https://cdnjs.cloudflare.com/ajax/libs/remarkable/1.6.2/remarkable.min.js"></script>
  11.   </head>
  12.   <body style="margin:0px;"]>
  13.     <div id="app-content"></div>
  14.     <script type="text/babel">
  15.    
  16.       var Game = React.createClass({
  17.      
  18.         getInitialState:function(){
  19.             console.log(new Date());
  20.             var field = this.generateField();
  21.        
  22.             return{running:true, field: field, time:0, points:0, }
  23.         },
  24.        
  25.         setTimer:function(timer){
  26.             this.state.time = timer;
  27.         },
  28.        
  29.         generateField:function(){
  30.             var field = [];
  31.             //var field = new Field();
  32.            
  33.             //var pairs = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8];
  34.             var pairs = [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8];
  35.            
  36.             for (var i=0; i<16; i++){
  37.            
  38.                 var pairID = Math.floor(Math.random()*pairs.length);
  39.                
  40.                 var idValue = pairs[pairID];
  41.                
  42.                 pairs.splice(pairID, 1);
  43.                
  44.                 //console.log(pairID, idValue, pairs);
  45.            
  46.                 field[i] = {
  47.                     id : idValue,
  48.                     opened: false,
  49.                     solved: false,
  50.                 }; 
  51.             }
  52.            
  53.             //console.log(field);
  54.            
  55.             return field;
  56.         },
  57.        
  58.         restartGame:function(e){
  59.             e.preventDefault();
  60.             this.setState(this.getInitialState());
  61.             this.forceUpdate();
  62.         },
  63.        
  64.         changePoints:function(value){
  65.             var newPoints = this.state.points + value;
  66.            
  67.             var toSolve = 0;
  68.             for (var i=0; i<16; i++){
  69.                 if(!this.state.field[i].solved)
  70.                     toSolve++;
  71.             }
  72.            
  73.             if (toSolve == 0)
  74.                 this.state.running = false;
  75.            
  76.             this.setState({points: newPoints});
  77.         },
  78.      
  79.         render:function(){
  80.        
  81.        
  82.             var divStyle = {
  83.                 color: 'white',
  84.                 //backgroundColor: 'black',
  85.                 background: 'linear-gradient(141deg, rgb(142, 7, 7) 0%, rgb(121, 62, 62) 51%, rgb(99, 11, 11) 75%)',
  86.                 textAlign: 'center',
  87.                 width: '100%',
  88.                 height: '20vh',
  89.             };
  90.            
  91.             var fieldStyle = {
  92.                 background: 'linear-gradient(141deg, #0fb8ad 0%, #1fc8db 51%, #2cb5e8 75%)',
  93.                 width: '100%',
  94.                 height: '75vh',
  95.             };
  96.            
  97.             var mainPanelStyle = {
  98.                 //paddingTop: '0.5em',
  99.                 textAlign: 'center',
  100.                 width: '100%',
  101.                 height: '5vh',
  102.                 //background: 'linear-gradient(-141deg, #666666 30%, rgba(117, 14, 14, 0.9) 50%, #666666 70%)',
  103.                 background: '#666666',
  104.                 //background: 'black',
  105.                 //borderTop: '1px solid black',
  106.                 //borderBottom: '1px solid black',
  107.             };
  108.            
  109.             var buttonStyle ={
  110.                 height: '100%',
  111.                
  112.                 margin: '0 1em ',
  113.                 fontWeight: 'bold',
  114.                 color: 'ivory',
  115.                 //background: 'linear-gradient(-141deg, #666666 30%, rgba(117, 14, 14, 0.9) 50%, #666666 70%)',
  116.                 background: '#700000',
  117.                 border: 'none',
  118.                 cursor: 'pointer',
  119.                 float: 'left',
  120.             };
  121.            
  122.             var textStyle ={
  123.                 //height: '100%',
  124.                
  125.                 margin: '0.4em 1em',
  126.                 fontWeight: 'bold',
  127.                 color: 'ivory',
  128.                 //background: 'linear-gradient(-141deg, #666666 30%, rgba(117, 14, 14, 0.9) 50%, #666666 70%)',
  129.                
  130.                 border: 'none',
  131.                
  132.                 float: 'left',
  133.             };
  134.            
  135.             var winnerStyle = {
  136.                 //margin: '1em 0',
  137.                 //padding: '1em 0',
  138.                 //height: '100%',
  139.                 height: '100%',
  140.                 width: '100%',
  141.                 fontWeight: 'bold',
  142.                 display: 'table',
  143.                 textAlign: 'center'
  144.             };
  145.            
  146.             var innerStyle = {
  147.                 display: 'table-cell',
  148.                 verticalAlign: 'middle',
  149.                 margin: '0px',
  150.             };
  151.        
  152.             return (
  153.                 <div>
  154.                     <div style={divStyle}>
  155.                         <br/>
  156.                         <h1 id='title'>.P..E..X..E..S..O.</h1>
  157.                         <h3 id='title-description'>by ReactJS</h3>
  158.                         <br/>
  159.                        
  160.                     </div>
  161.                    
  162.                     <div id='main-panel' style={mainPanelStyle}>
  163.                         <div style={{width: '40%', height: '100%', margin: '0px auto'}}>
  164.                             <button onClick={this.restartGame} style={buttonStyle}>{this.state.running ? "Reštartovať hru" : "Hrať novú hru"}</button>
  165.                            
  166.                             { this.state.running ?
  167.                             <p style={textStyle}>Počet bodov: {this.state.points}</p> : ""}
  168.                             { this.state.running ?
  169.                             <Timer timer={this.setTimer} style={textStyle} running={this.state.running} /> : ""
  170.                             }
  171.                            
  172.                         </div>
  173.                     </div>
  174.                    
  175.                    
  176.                    
  177.                     <div id="field" style={fieldStyle}>
  178.                    
  179.                    
  180.                     {!this.state.running ?
  181.                     <div style={winnerStyle}>
  182.                     <p style={innerStyle}>
  183.                     Gratulujeme, hru ste úspešne vyriešili v celkovom čase  {(this.state.time-(this.state.time%60))/60}  minút a {this.state.time%60} sekúnd...<br/>
  184.                     Dosiahnuté skóre: {this.state.points} bodov
  185.                     </p>
  186.                     </div>
  187.                     : null }
  188.                    
  189.                     {this.state.running ? <Field game={this.state} changePoints={this.changePoints}/> : ""}
  190.                    
  191.                     </div>
  192.                 </div>
  193.             )
  194.         },
  195.       });
  196.      
  197.       var Timer = React.createClass({
  198.      
  199.          _tick: function() {
  200.             if (!this.props.running) {
  201.               clearInterval(this.interval);
  202.             } else {
  203.               this.setState({ timePassed: this.state.timePassed + 1 });
  204.               this.props.timer(this.state.timePassed);
  205.             }
  206.           },
  207.  
  208.           getInitialState: function() {
  209.             return { timePassed: 0 };
  210.           },
  211.           componentWillUnmount: function() {
  212.             clearInterval(this.interval);
  213.           },
  214.           componentDidMount: function() {
  215.             this.interval = setInterval(this._tick, 1000);
  216.           },
  217.           render: function() {
  218.             var sec = this.state.timePassed%60;
  219.             var min = (this.state.timePassed-sec)/60;
  220.             return (
  221.                 <p style={this.props.style}>
  222.                     Čas: {min<10 ? "0" + min : min}:{sec < 10 ? "0" + sec : sec}
  223.                 </p>
  224.             )
  225.           }
  226.      
  227.       });
  228.      
  229.       var Field = React.createClass({
  230.      
  231.         getInitialState:function(){
  232.        
  233.             return{first:null, second: null}
  234.         },
  235.      
  236.         handleClick:function(e){
  237.             e.preventDefault();
  238.            
  239.         },
  240.        
  241.         tileHandleClick:function(tile){
  242.        
  243.             if (this.state.first != tile){
  244.                     tile.opened = !tile.opened;
  245.        
  246.             if (this.state.first != null && this.state.second != null) {
  247.                
  248.                     //najprv zmenime stav
  249.                     this.state.first.opened = !this.state.first.opened;
  250.                     this.state.second.opened = !this.state.second.opened;
  251.                    
  252.                     this.state.second = null;
  253.                     this.state.first = tile;
  254.                 } else {
  255.                     if (this.state.first != null){ //first je obsadena, zaplname second
  256.                         this.state.second = tile;
  257.  
  258.                        
  259.                         if (this.state.first.id == tile.id){
  260.                             this.state.first.solved = true;
  261.                             tile.solved = true;
  262.                             this.props.changePoints(100);
  263.                             this.state.first = null;
  264.                             this.state.second = null;
  265.                         } else if (this.props.game.points >= 10){
  266.                             this.props.changePoints(-10);
  267.                         }
  268.                        
  269.                        
  270.                        
  271.                        
  272.                     } else { //obe su volne, zaplname first
  273.                         this.state.first = tile;
  274.                     }
  275.                 }
  276.                
  277.                
  278.                
  279.                 this.forceUpdate();
  280.             } else {
  281.                 window.alert("dlazdicu nemozno odznacit");
  282.             }
  283.            
  284.         },
  285.          
  286.         render:function(){
  287.            
  288.             var game = this.props.game;
  289.  
  290.             var opened = {
  291.                     first: this.state.first,
  292.                     second: this.state.second,
  293.             };
  294.            
  295.             var tileHandleClick = this.tileHandleClick;
  296.            
  297.             var tiles = game.field.map(function(tile){
  298.                
  299.                 return <Tile tile={tile} opened={opened} clickHandler={tileHandleClick} />
  300.             });
  301.        
  302.             var style = {
  303.                 height: '73vh',
  304.                 width: '73vh',
  305.                 margin: '0 auto',
  306.                 textAlign: 'center',               
  307.             };
  308.        
  309.             return (
  310.                 <div style={style}>
  311.                     {tiles}
  312.                 </div>
  313.             )
  314.         },
  315.      
  316.       });
  317.      
  318.       var Tile = React.createClass({
  319.        
  320.         handleClick:function(e){
  321.             e.preventDefault();
  322.                
  323.             if (this.props.opened.first != null && this.props.opened.second != null) {
  324.            
  325.                 //najprv zmenime stav
  326.  
  327.                 this.props.opened.first.opened = !this.props.opened.first.opened;
  328.                 this.props.opened.second.opened = !this.props.opened.second.opened;
  329.  
  330.                 console.log(this.props.opened.first);
  331.                 console.log(this.props.opened.first.opened);
  332.                
  333.                 this.props.opened.second = null;
  334.                 this.props.opened.first = this.props.tile;
  335.                 console.log("zmena");
  336.             } else {
  337.                 if (this.props.opened.first != null){ //first je obsadena, zaplname second
  338.                     this.props.opened.second = this.props.tile;
  339.                 } else { //obe su volne, zaplname first
  340.                     this.props.opened.first = this.props.tile;
  341.                 }
  342.             }
  343.             this.props.tile.opened = !this.props.tile.opened;  
  344.             this.forceUpdate();
  345.         },
  346.        
  347.         clickHandler:function(){
  348.             this.props.clickHandler(this.props.tile);
  349.         },
  350.        
  351.         render:function(){
  352.            
  353.             var colors = ["#3333cc", "#ff0066", "#008000", "#ff9900", "#100000", "#66004d", "#4d1a00", "#ffff00"];
  354.            
  355.             var openedStyle = {
  356.                 color: 'white',
  357.                 backgroundColor: colors[this.props.tile.id-1],
  358.                 height: '20%',
  359.                 width: '20%',
  360.                 margin: '2.1%',
  361.                 float: 'left',
  362.                 border: '1px solid grey',
  363.                 display: 'table',
  364.                 cursor: 'pointer',
  365.             };
  366.            
  367.             var closedStyle = {
  368.                 color: 'red',
  369.                 background: 'white',
  370.                 height: '20%',
  371.                 width: '20%',
  372.                 margin: '2.1%',
  373.                 float: 'left',
  374.                 border: '1px solid grey',
  375.                 display: 'table',
  376.                 cursor: 'pointer',
  377.             };
  378.        
  379.             var innerStyle = {
  380.                 display: 'table-cell',
  381.                 verticalAlign: 'middle',
  382.                 fontSize: '4em',
  383.             };
  384.                    
  385.             return (
  386.                 <div style={this.props.tile.opened ? openedStyle : closedStyle}
  387.                     onClick={this.props.tile.solved ? null : this.clickHandler}>
  388.                
  389.                     <div style={innerStyle}>
  390.                         {this.props.tile.opened ? this.props.tile.id : "?"}
  391.                     </div>
  392.                 </div>
  393.             )
  394.         },
  395.      
  396.       });
  397.    
  398.     ReactDOM.render(
  399.             <Game />,
  400.         document.getElementById('app-content')
  401.         );
  402.     </script>                          
  403.   </body>
  404.  
  405. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement