Advertisement
AdamJS

index.php

Oct 15th, 2022 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.19 KB | None | 0 0
  1. <?php
  2.   session_start();
  3.   define('XP__NEEDED','100');
  4.   $rows = '';
  5.    
  6.   //unset($_SESSION['id_user']);
  7.  
  8.   if (isset($_COOKIE["PHPSESSID"]) && isset($_SESSION['id_user'])) {
  9.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  10.       try {
  11.         require_once('db_functions.php');
  12.         $user_info = getUserInfo($_SESSION['id_user']);
  13.         $xp_current = $_POST['xp_current'];
  14.  
  15.         foreach ($user_info as $data) {
  16.           $user_level = $data['level'];
  17.           $xp_all = $data['xp_all'];
  18.           $xp_needed = $data['xp_needed'];
  19.         }
  20.  
  21.         if ($xp_current >= $xp_needed) {
  22.           $user_level += 1;
  23.           setUserInfo('level', $user_level, $_SESSION['id_user']);
  24.  
  25.           $xp_all += $xp_current;
  26.           setUserInfo('xp_all', $xp_all, $_SESSION['id_user']);
  27.  
  28.           $xp_current -= $xp_needed;
  29.           setUserInfo('xp_current', $xp_current, $_SESSION['id_user']);
  30.  
  31.           $xp_needed = (XP__NEEDED * $user_level) + XP__NEEDED;
  32.           setUserInfo('xp_needed', $xp_needed, $_SESSION['id_user']);
  33.         }
  34.  
  35.         $user_info = getUserInfo($_SESSION['id_user']);
  36.         $rows = showUserInfo($user_info);
  37.       } catch (Exception $e) {
  38.         echo $e->getMessage();
  39.       }
  40.     }
  41.   } else {
  42.     // Symulacja zalogowania się użytkownika do Twojego systemu
  43.     // Uzytkownik zalogował się za pomocą formularza - prawidłowo
  44.     $_SESSION['id_user'] = 1;
  45.  
  46.     try {
  47.       require_once('db_functions.php');
  48.       $user_info = getUserInfo($_SESSION['id_user']);
  49.       $rows = showUserInfo($user_info);
  50.     } catch (Exception $e) {
  51.       echo $e->getMessage();
  52.     }
  53.   }
  54. ?>
  55.  
  56. <!-- Demo -->
  57. <!DOCTYPE html>
  58. <html>
  59.   <head lang="pl">
  60.     <meta charset="utf-8">
  61.  
  62.     <style>
  63.       :root {
  64.         --board-width: 20vw;
  65.       }
  66.       * {
  67.         box-sizing: border-box;
  68.       }
  69.       thead tr {
  70.         background-color: gray;
  71.         color: white;
  72.         font: 1em monospace;
  73.       }
  74.       thead th {
  75.         padding: 0.5em;
  76.       }
  77.       tbody td {
  78.         padding: 0.2em;
  79.         text-align: center;
  80.       }
  81.       .game-board {
  82.         width: var(--board-width);
  83.         margin-top: 2em;
  84.         padding: 0.2em;
  85.         font: 1.1em monospace;
  86.       }
  87.       .game-board .playground {
  88.         font: 0.8em bold monospace;
  89.         padding: 0.2em;
  90.         color: green;
  91.         text-align: center;
  92.       }
  93.       .game-board .play-time {
  94.         font-size: 4.1em;
  95.       }
  96.       .game-board button {
  97.         width:  var(--board-width);
  98.         cursor: pointer;
  99.         box-shadow: 2px 2px 1px grey;
  100.         font: 1.2em monospace;
  101.         font-variant: small-caps;
  102.       }
  103.       .game-board button:active {
  104.         transform: translateY(1px);
  105.         box-shadow: none;
  106.       }
  107.     </style>
  108.   </head>
  109.   <body>
  110.     <table>
  111.       <thead>
  112.         <tr><th colspan="5">Dane z bazy danych</th></tr>
  113.         <tr>
  114.             <th>Użytkownik</th>
  115.             <th>Poziom</th>
  116.             <th>Punkty zdobyte</th>
  117.             <th>Punkty obecne</th>
  118.             <th>Punkty następny poziom</th>
  119.         </tr>
  120.       </thead>
  121.       <tbody>
  122.         <?php echo $rows; ?>
  123.       </tbody>
  124.     </table>
  125.     <div class="game-board">
  126.         <div>Punkty: <span id="user_points"></span></div>
  127.         <div id="playground" class="playground play-time">0</div>
  128.         <div><button id="play">Graj</button></div>
  129.     </div>
  130.     <form id="form" action="index.php" method="post">
  131.         <input type="hidden" id="xp_current" name="xp_current" value="">
  132.     </form>
  133.    
  134.     <script>
  135.       const xp_current_db = document.getElementById('xp_current_db'),
  136.             xp_needed = document.getElementById('xp_needed'),
  137.             user_points = document.getElementById('user_points'),
  138.             playground = document.getElementById('playground'),
  139.             button_play = document.getElementById('play');
  140.  
  141.       const XP__NEEDED = xp_needed.textContent * 1;
  142.  
  143.       (setUserPoints = function(points = null) {
  144.         if (points == null) {
  145.           user_points.textContent = xp_current_db.textContent;
  146.         } else {
  147.           let calculate_user_points = user_points.textContent * 1;
  148.           calculate_user_points += points;
  149.           user_points.textContent = calculate_user_points;
  150.  
  151.           if (calculate_user_points >= XP__NEEDED) {
  152.             button_play.disabled = true;
  153.             setTimeout(function() {
  154.               playground.classList.remove('play-time');
  155.               playground.innerHTML = 'Gratulacje !<br>Przechodzisz na następny poziom.';
  156.  
  157.               setTimeout(sendData, 2000, calculate_user_points);
  158.             }, 2000);
  159.           }                
  160.         }
  161.       }).call(this)
  162.  
  163.       button_play.addEventListener('click', () => {
  164.         const min = 1;
  165.         const max = XP__NEEDED;
  166.         const result = Math.floor(Math.random() * (max - min + 1) ) + min;
  167.         playground.textContent = result;
  168.         setUserPoints(result);
  169.       })
  170.  
  171.       function sendData(points) {
  172.         const form = document.getElementById('form');
  173.         const input_xp_current = document.getElementById('xp_current');
  174.         input_xp_current.value = points;
  175.         form.submit();
  176.        }
  177.     </script>
  178.     </body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement