Quellcode durchsuchen

Add retroarch cmd to non webretro games

Colin Powell vor 3 Jahren
Ursprung
Commit
5da91d64a4
2 geänderte Dateien mit 87 neuen und 6 gelöschten Zeilen
  1. 30 3
      games/models.py
  2. 57 3
      templates/games/game_detail.html

+ 30 - 3
games/models.py

@@ -1,3 +1,6 @@
+import os
+from shlex import quote
+
 from enum import Enum
 
 from django.conf import settings
@@ -205,14 +208,38 @@ class Game(BaseModel):
     def __str__(self):
         return f"{self.name} for {self.game_system}"
 
+    def get_absolute_url(self):
+        return reverse("games:game_detail", args=[self.slug])
+
+    @property
     def rating_by_100(self) -> float:
         if self.rating:
             return int(100 * self.rating)
         return int(0)
 
-    def get_absolute_url(self):
-        return reverse("games:game_detail", args=[self.slug])
+    @property
+    def retroarch_core_path(self):
+        path = ""
+        retroarch_core = self.game_system.defaults.get("retroarch_core", None)
+        if retroarch_core:
+            path = os.path.join(
+                settings.ROMS_DIR,
+                "cores",
+                retroarch_core + "_libretro.so",
+            )
+        return quote(path)
 
-    def get_play_url(self):
+    @property
+    def webretro_url(self):
         if "webretro_core" in self.game_system.defaults.keys():
             return reverse("games:game_play_detail", args=[self.slug])
+
+    def retroarch_cmd(self, platform="linux"):
+        if platform != "linux":
+            return ""
+        if not self.retroarch_core_path:
+            return ""
+        if not self.rom_file:
+            return ""
+        rom_file = quote(self.rom_file.path)
+        return f"retroarch -L {self.retroarch_core_path} {rom_file}"

+ 57 - 3
templates/games/game_detail.html

@@ -1,6 +1,43 @@
 {% extends "base.html" %}
 {% load static %}
 
+{% block head_extra %}
+<script>
+$(document).ready(function() {
+  // Initialize the tooltip.
+  $('#copy-button').tooltip();
+
+  // When the copy button is clicked, select the value of the text box, attempt
+  // to execute the copy command, and trigger event to update tooltip message
+  // to indicate whether the text was successfully copied.
+  $('#copy-button').bind('click', function() {
+    var input = document.querySelector('#copy-input');
+    input.setSelectionRange(0, input.value.length + 1);
+    try {
+      var success = document.execCommand('copy');
+      if (success) {
+        $('#copy-button').trigger('copied', ['Copied!']);
+      } else {
+        $('#copy-button').trigger('copied', ['Copy with Ctrl-c']);
+      }
+    } catch (err) {
+      $('#copy-button').trigger('copied', ['Copy with Ctrl-c']);
+    }
+  });
+
+  // Handler for updating the tooltip message.
+  $('#copy-button').bind('copied', function(event, message) {
+    $(this).attr('title', message)
+        .tooltip('fixTitle')
+        .tooltip('show')
+        .attr('title', "Copy to Clipboard")
+        .tooltip('fixTitle');
+  });
+});
+</script>
+
+
+{% endblock %}
 {% block title %}{{object.name}}{% endblock %}
 
 {% block content %}
@@ -36,8 +73,25 @@
         <dd>{{game.rating_by_100}}</dd>
     </dl>
 
-    <p>{{object.description}}</p>
-    {% if object.get_play_url  %}
-    <p><a href="{{game.get_play_url}}" class="btn btn-primary" targe="_blank">Play</a></p>
+    {% if object.webretro_url  %}
+    <p><a href="{{game.webretro_url}}" class="btn btn-primary" targe="_blank">Play</a></p>
+    {% elif object.retroarch_cmd %}
+    <p>
+        <form>
+            <div class="input-group">
+                <input type="text" class="form-control"
+                    value="{{object.retroarch_cmd}}" placeholder="" id="copy-input">
+                <span class="input-group-btn">
+                <button class="btn btn-default" type="button" id="copy-button"
+                    data-toggle="tooltip" data-placement="button"
+                    title="Copy to Clipboard">
+                    Copy
+                </button>
+                </span>
+            </div>
+        </form>
+    </p>
     {% endif %}
+
+    <p>{{object.description}}</p>
 {% endblock %}