Advertisement
plirof2

sgd php v007a2 241120b

Nov 20th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. v007a2 241120b
  4.  
  5.  
  6. */
  7.  
  8.  
  9.  
  10.  
  11. $dat_file='SGD_sample.dat';
  12. $dat_file='ROMSMINE/_SGD304/Arcade4.dat';
  13.  
  14.  
  15.  
  16.  
  17.  
  18. //==========get SGD.ini paths :=======================
  19.  
  20. // Define the path to your .ini file
  21. $iniFilePath = 'ROMSMINE/_SGD304/sgd.ini';
  22.  
  23. // Define the new prefix you want to set
  24. $newPrefix = 'NEW_PREFIX_FOR_PATH'; // Change this to what you need
  25.  
  26. // Initialize an array to hold the GameDir paths
  27. $gameDirs = [];
  28.  
  29. // Open the file for reading
  30. if ($fileHandle = fopen($iniFilePath, 'r')) {
  31.     // Read each line of the file
  32.     while (($line = fgets($fileHandle)) !== false) {
  33.         // Trim whitespace from the line
  34.         $line = trim($line);
  35.        
  36.         // Check if the line starts with 'GameDir'
  37.         if (strpos($line, 'GameDir') === 0) {
  38.             // Extract the path after the '=' sign
  39.             $parts = explode('=', $line);
  40.             if (count($parts) === 2) {
  41.                 $path = trim($parts[1]);
  42.                 // Adjust path with the new prefix
  43.                 ///$newPath = preg_replace('/^E:\\OPT\\SPECTRUM\\ROMSMINE\\/', $newPrefix . '\\', $path);
  44.                 // Store the modified path
  45.                 $gameDirs[]=$path;
  46.                 //$gameDirs[] = $newPath;
  47.             }
  48.         }
  49.     }
  50.     // Close the file handle
  51.     fclose($fileHandle);
  52. } else {
  53.     echo "Unable to open the file.";
  54. }
  55.  
  56.  
  57. // Output the modified GameDir paths
  58. foreach ($gameDirs as $gameDir) {
  59.     //echo $gameDir . PHP_EOL."<BR>";
  60. }
  61.  
  62. //==END of========get SGD.ini paths :=======================
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. // Database field definitions
  75. $fieldDefinitions = [
  76.     'Name' => ['start' => 1, 'length' => 36],
  77.     'Year' => ['start' => 38, 'length' => 4],
  78.     'Publisher' => ['start' => 43, 'length' => 36],
  79.     'Memory' => ['start' => 80, 'length' => 3],
  80.     'PC-Name' => ['start' => 94, 'length' => 12],
  81.     'Type' => ['start' => 107, 'length' => 7],
  82.     'PathIndex' => ['start' => 115, 'length' => 3],
  83.     'FileSize' => ['start' => 119, 'length' => 7],
  84.     'Orig_screen' => ['start' => 127, 'length' => 1],
  85.     'Origin' => ['start' => 129, 'length' => 1],
  86.     'FloppyId' => ['start' => 131, 'length' => 4],
  87.     'Emul_override' => ['start' => 136, 'length' => 2],
  88.     'AYSound' => ['start' => 139, 'length' => 1],
  89.     'MultiLoad' => ['start' => 141, 'length' => 1],
  90.     'Language' => ['start' => 143, 'length' => 3],
  91.     'Score' => ['start' => 147, 'length' => 3],
  92.     'Author' => ['start' => 151, 'length' => 100],
  93.     'Joysticks' => ['start' => 88, 'length' => 5],
  94.     '#OfPlayers' => ['start' => 84, 'length' => 1],
  95.     'Together' => ['start' => 86, 'length' => 1],
  96. ];
  97.  
  98. // Helper function to extract a field from a line
  99. function getField($line, $field) {
  100.     global $fieldDefinitions;
  101.     $start = $fieldDefinitions[$field]['start'];
  102.     $length = $fieldDefinitions[$field]['length'];
  103.     return trim(substr($line, $start - 1, $length));
  104. }
  105.  
  106. // Read the file and build the games list
  107. $file = fopen($dat_file, 'r');
  108. $games = [];
  109. while (($line = fgets($file)) !== false) {
  110.     $game = [];
  111.     foreach ($fieldDefinitions as $field => $definition) {
  112.         $game[$field] = getField($line, $field);
  113.     }
  114.     $games[] = $game;
  115. }
  116. fclose($file);
  117.  
  118. // Handle game update via POST
  119. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  120.     $gameData = $_POST;
  121.     $lineIndex = $gameData['lineIndex']; // Index of the game to update
  122.     unset($gameData['lineIndex']); // Remove the lineIndex from POST data
  123.  
  124.     // Update the game data logic here
  125.     $fileLines = file($dat_file);
  126.     $lineToUpdate = $fileLines[$lineIndex];
  127.     foreach ($fieldDefinitions as $field => $definition) {
  128.         $start = $definition['start'] - 1;
  129.         $length = $definition['length'];
  130.         $lineToUpdate = substr_replace($lineToUpdate, str_pad($gameData[$field], $length), $start, $length);
  131.     }
  132.     $fileLines[$lineIndex] = $lineToUpdate;
  133.     file_put_contents($dat_file, implode('', $fileLines));
  134.  
  135.     // Return the updated game as well
  136.     $updatedGame = [];
  137.     foreach ($fieldDefinitions as $field => $definition) {
  138.         $updatedGame[$field] = $gameData[$field];
  139.     }
  140.    
  141.     echo json_encode(['status' => 'success', 'updatedGame' => $updatedGame, 'lineIndex' => $lineIndex]);
  142.     exit;
  143. }
  144. ?>
  145. <!DOCTYPE html>
  146. <html lang="en">
  147. <head>
  148.     <meta charset="UTF-8">
  149.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  150.     <title>Game Database</title>
  151.     <style>
  152.         body {
  153.             display: flex;
  154.             margin: 0;
  155.             #font-family: Arial, sans-serif;
  156.             background-color: lightgrey;
  157.             font-family: "Courier New", Courier, monospace;
  158.         }
  159.         #left-panel, #right-panel {
  160.             padding: 20px;
  161.             overflow-y: auto;
  162.         }
  163.         #left-panel {
  164.             width: 60%;
  165.             border-right: 1px solid #ccc;
  166.             background-color: lightgrey;
  167.         }
  168.         #right-panel {
  169.             #flex: 1;
  170.             position: fixed;
  171.             top: 10px; /* Distance from the top */
  172.             right: 10px; /* Distance from the right */
  173.             background-color: gainsboro; /* Semi-transparent background */
  174.             border: 1px solid #ccc;
  175.             padding: 10px;
  176.             box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  177.             z-index: 1000; /* Make sure it appears above other content */
  178.             max-height: calc(100vh - 20px); /* max height minus top and bottom offsets */
  179.             overflow-y: auto; /* Enable vertical scrolling when needed */
  180.         }
  181.         .game-item {
  182.             padding: 10px;
  183.             border-bottom: 1px solid #ddd;
  184.             cursor: pointer;
  185.         }
  186.         .game-item:hover {
  187.             background-color: #eee;
  188.         }
  189.         .game-item.active {
  190.             background-color: #dcdcdc;
  191.         }
  192.         .form-field {
  193.             margin-bottom: 10px;
  194.         }
  195.         .form-field label {
  196.             font-weight: bold;
  197.         }
  198.         .form-field input, .form-field textarea {
  199.             padding: 8px;
  200.             border: 1px solid #ddd;
  201.             border-radius: 4px;
  202.             background-color: Silver;
  203.         }
  204.     </style>
  205.     <script>
  206.         document.addEventListener('DOMContentLoaded', () => {
  207.             const gameItems = document.querySelectorAll('.game-item');
  208.             const form = document.getElementById('game-form');
  209.             const fields = <?php echo json_encode(array_keys($fieldDefinitions)); ?>;
  210.  
  211.             // Attach click handlers to game items
  212.             gameItems.forEach(item => {
  213.                 item.addEventListener('click', () => {
  214.                     // Remove active class from all items
  215.                     document.querySelectorAll('.game-item').forEach(i => i.classList.remove('active'));
  216.                     item.classList.add('active');
  217.  
  218.                     // Load game data into form
  219.                     const gameData = JSON.parse(item.dataset.game);
  220.                     fields.forEach(field => {
  221.                         const input = form.querySelector(`[name="${field}"]`);
  222.                         if (input) input.value = gameData[field];
  223.                     });
  224.                     form.querySelector('[name="lineIndex"]').value = item.dataset.index; // Store the index for the form submission
  225.                 });
  226.             });
  227.  
  228.             // Handle form submission
  229.             form.addEventListener('submit', (e) => {
  230.                 e.preventDefault();
  231.                 const formData = new FormData(form);
  232.  
  233.                 fetch('', {
  234.                     method: 'POST',
  235.                     body: formData
  236.                 })
  237.                 .then(response => response.json())
  238.                 .then(data => {
  239.                     if (data.status === 'success') {
  240.                         // Update the left panel with the updated game
  241.                         const updatedGame = data.updatedGame;
  242.                         const lineIndex = data.lineIndex;
  243.                         const updatedGameItem = document.querySelector(`.game-item[data-index="${lineIndex}"]`);
  244.                         if (updatedGameItem) {
  245.                             updatedGameItem.dataset.game = JSON.stringify(updatedGame);
  246.                             updatedGameItem.querySelector('strong').textContent = updatedGame.Name;
  247.                             //updatedGameItem.querySelector('small').textContent = updatedGame['PC-Name'];
  248.                         }
  249.  
  250.                         alert('Game updated successfully!');
  251.                     }
  252.                 });
  253.             });
  254.         });
  255.     </script>
  256. </head>
  257. <body>
  258.     <div id="left-panel">
  259.         <h3>Game List</h3>
  260.         <?php foreach ($games as $index => $game): ?>
  261.             <div class="game-item" data-game='<?php echo json_encode($game); ?>' data-index="<?php echo $index; ?>">
  262.                 <strong><?php echo htmlspecialchars($game['Name']); ?></strong><br>
  263.                 <small><?php //echo htmlspecialchars($game['PC-Name']); ?></small>
  264.             </div>
  265.         <?php endforeach; ?>
  266.     </div>
  267.  
  268.     <div id="right-panel">
  269.         <h3>Game Details</h3>
  270.         <form id="game-form">
  271.             <input type="hidden" name="lineIndex" value="">
  272.  
  273.             <button type="submit">Save</button>
  274.  
  275.             <div class="form-field">
  276.                 <label for="Name">Name:</label>
  277.                 <input type="text" id="Name" name="Name" value="" maxlength="36" size="36" />
  278.             </div>
  279.  
  280.             <div class="form-field">
  281.                 <label for="Publisher">Publisher:</label>
  282.                 <input type="text" id="Publisher" name="Publisher" value="" maxlength="36" size="36" />
  283.             </div>
  284.  
  285.             <div class="form-field">
  286.                 <label for="Author">Author:</label>
  287.                 <input type="text" id="Author" name="Author" value="" maxlength="50" size="50" />
  288.             </div>
  289.  
  290.             <div class="form-field"></div>
  291.  
  292.             <div class="form-field">
  293.                 <label for="Year">Year:</label>
  294.                 <input type="text" id="Year" name="Year" value="" maxlength="4" size="4" />
  295.                 <label for="Memory">Memory:</label>
  296.                 <input type="text" id="Memory" name="Memory" value="" maxlength="3" size="3" />
  297.             </div>
  298.  
  299.             <div class="form-field">
  300.                 <label for="#OfPlayers">#OfPlayers:</label>
  301.                 <input type="text" id="#OfPlayers" name="#OfPlayers" value="" maxlength="1" size="1" />
  302.                 <label for="Together">Together:</label>
  303.                 <input type="text" id="Together" name="Together" value="" maxlength="1" size="1" />
  304.                 <label for="Score">Score:</label>
  305.                 <input type="text" id="Score" name="Score" value="" maxlength="3" size="3" />
  306.             </div>
  307.  
  308.             <div class="form-field">
  309.                 <label for="PC-Name">PC-Name:</label>
  310.                 <input type="text" id="PC-Name" name="PC-Name" value="" maxlength="12" size="12" />
  311.                 <label for="Type">Type:</label>
  312.                 <input type="text" id="Type" name="Type" value="" maxlength="7" size="7" />
  313.             </div>
  314.  
  315.             <div class="form-field">
  316.                 <label for="Joysticks">Joysticks:</label>
  317.                 <input type="text" id="Joysticks" name="Joysticks" value="" maxlength="5" size="5" />
  318.             </div>
  319.  
  320.             <div class="form-field">
  321.                 <label for="Orig_screen">Orig_screen:</label>
  322.                 <input type="text" id="Orig_screen" name="Orig_screen" value="" maxlength="1" size="1" />
  323.                 <label for="Origin">Origin:</label>
  324.                 <input type="text" id="Origin" name="Origin" value="" maxlength="1" size="1" />
  325.             </div>
  326.  
  327.             <div class="form-field">
  328.                 <label for="AYSound">AYSound:</label>
  329.                 <input type="text" id="AYSound" name="AYSound" value="" maxlength="1" size="1" />
  330.                 <label for="MultiLoad">MultiLoad:</label>
  331.                 <input type="text" id="MultiLoad" name="MultiLoad" value="" maxlength="1" size="1" />
  332.                 <label for="FloppyId">FloppyId:</label>
  333.                 <input type="text" id="FloppyId" name="FloppyId" value="" maxlength="4" size="4" />
  334.             </div>
  335.  
  336.             <div class="form-field">
  337.                 <label for="Language">Language:</label>
  338.                 <input type="text" id="Language" name="Language" value="" maxlength="3" size="3" />
  339.                 <label for="Emul_override">Emul_override:</label>
  340.                 <input type="text" id="Emul_override" name="Emul_override" value="" maxlength="2" size="2" />
  341.             </div>
  342.  
  343.             <div class="form-field">
  344.                 <label for="PathIndex">PathIndex:</label>
  345.                 <input type="text" id="PathIndex" name="PathIndex" value="" maxlength="3" size="3" />
  346.                 <label for="FileSize">FileSize:</label>
  347.                 <input type="text" id="FileSize" name="FileSize" value="" maxlength="7" size="7" />
  348.             </div>
  349.  
  350.             <div class="form-field"></div>
  351.             <button type="submit">Save</button>
  352.         </form>
  353.     </div>
  354. </body>
  355. </html>
  356.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement