play functionality for every versions

This commit is contained in:
2026-01-25 16:18:35 +01:00
parent ceeb8d53fb
commit f79ff46b0c
5 changed files with 132 additions and 11 deletions
+1
View File
@@ -8,4 +8,5 @@ type Release struct {
Version string `gorm:"size:64" json:"version"`
CartridgePath string `gorm:"size:255" json:"-"`
SourcePath string `gorm:"size:255" json:"-"`
WebPlayable bool `gorm:"default:false" json:"web_playable"`
}
+104 -4
View File
@@ -21,7 +21,7 @@ func NewPlayController(softwareService domain.SoftwareServiceInterface) *PlayCon
}
}
func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
func (c *PlayController) PlayV1(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Name not provided", http.StatusBadRequest)
@@ -38,6 +38,58 @@ func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
return
}
var webPlayableRelease *domain.Release
for _, release := range software.Releases {
if release.WebPlayable {
webPlayableRelease = &release
break
}
}
if webPlayableRelease == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
http.Redirect(w, r, fmt.Sprintf("/play/%s/%s", name, webPlayableRelease.Version), http.StatusFound)
}
func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Name not provided", http.StatusBadRequest)
return
}
version := chi.URLParam(r, "version")
if version == "" {
http.Error(w, "Version not provided", http.StatusBadRequest)
return
}
software, err := c.softwareService.GetByNameWithReleases(name)
if err != nil {
if err == domain.ErrSoftwareNotFound {
http.Error(w, "Software not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var webPlayableRelease *domain.Release
for _, release := range software.Releases {
if release.Version == version && release.WebPlayable {
webPlayableRelease = &release
break
}
}
if webPlayableRelease == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
tmpl, err := template_utils.GetTemplate("play_controller_play", "http/views/shared/layout.html", "http/views/play/play.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -45,11 +97,12 @@ func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
}
tmpl.Execute(w, map[string]interface{}{
"Software": software,
"Software": software,
"WebPlayableRelease": webPlayableRelease,
})
}
func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
func (c *PlayController) ServeContentV1(w http.ResponseWriter, r *http.Request) {
contentsPath := os.Getenv("GAMES_DIR")
name := chi.URLParam(r, "name")
if name == "" {
@@ -57,7 +110,26 @@ func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
return
}
htmlBaseDir := filepath.Join(contentsPath, "html", name)
software, err := c.softwareService.GetByNameWithReleases(name)
if err != nil {
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
return
}
var webPlayableRelease *domain.Release
for _, release := range software.Releases {
if release.WebPlayable {
webPlayableRelease = &release
break
}
}
if webPlayableRelease == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
htmlBaseDir := filepath.Join(contentsPath, "html", name, webPlayableRelease.Version)
if _, err := os.Stat(htmlBaseDir); os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
@@ -70,3 +142,31 @@ func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
fs := http.StripPrefix(fmt.Sprintf("/play/%s/content", name), http.FileServer(http.Dir(htmlBaseDir)))
fs.ServeHTTP(w, r)
}
func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
contentsPath := os.Getenv("GAMES_DIR")
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Name not provided", http.StatusBadRequest)
return
}
version := chi.URLParam(r, "version")
if version == "" {
http.Error(w, "Version not provided", http.StatusBadRequest)
return
}
htmlBaseDir := filepath.Join(contentsPath, "html", name, version)
if _, err := os.Stat(htmlBaseDir); os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, fmt.Sprintf("Error accessing content for '%s': %v", name, err), http.StatusInternalServerError)
return
}
fs := http.StripPrefix(fmt.Sprintf("/play/%s/%s/content", name, version), http.FileServer(http.Dir(htmlBaseDir)))
fs.ServeHTTP(w, r)
}
+4 -2
View File
@@ -40,8 +40,10 @@ func (r *Router) Init() *chi.Mux {
router.Get("/download/{name}/cartridge", r.downloadController.GetLatestCartridge)
router.Get("/download/{name}/{version}/source", r.downloadController.GetSource)
router.Get("/download/{name}/{version}/cartridge", r.downloadController.GetCartridge)
router.Get("/play/{name}", r.playController.Play)
router.Get("/play/{name}/content*", r.playController.ServeContent)
router.Get("/play/{name}", r.playController.PlayV1)
router.Get("/play/{name}/content*", r.playController.ServeContentV1)
router.Get("/play/{name}/{version}", r.playController.Play)
router.Get("/play/{name}/{version}/content*", r.playController.ServeContent)
fs := http.FileServer(http.Dir("assets"))
router.Handle("/assets/*", http.StripPrefix("/assets/", fs))
+19 -4
View File
@@ -1,17 +1,32 @@
{{define "content"}}
<h1 class="my-4">Play {{.Software.Title}}</h1>
<div class="ratio ratio-4x3" style="max-width: 640px; margin: 0 auto;">
<iframe id="gameFrame" src="/play/{{.Software.Name}}/content/index.html" frameborder="0" allowfullscreen></iframe>
<div class="ratio ratio-4x3" style="max-width: 960px; margin: 0 auto;">
<iframe id="gameFrame" src="/play/{{.Software.Name}}/{{.WebPlayableRelease.Version}}/content/index.html" frameborder="0" allowfullscreen></iframe>
</div>
<div style="max-width: 640px; margin: 10px auto; text-align: center;">
<div style="max-width: 960px; margin: 10px auto; text-align: center;">
<div class="btn-group" role="group" aria-label="Game controls">
<a href="/" class="btn btn-primary">Back to Softwares</a>
<a href="/releases/{{.Software.Name}}" class="btn btn-primary">Back to Releases</a>
<button id="fullscreenButton" class="btn btn-secondary">Fullscreen</button>
</div>
</div>
<div style="max-width: 960px; margin: 10px auto; text-align: start;">
<h2 class="my-4">Version</h2>
<p>{{.WebPlayableRelease.Version}}</p>
<h2 class="my-4">Other Versions</h2>
<div class="list-group">
{{range .Software.Releases}}
{{if and .WebPlayable (ne .Version $.WebPlayableRelease.Version)}}
<a href="/play/{{$.Software.Name}}/{{.Version}}" class="list-group-item list-group-item-action">
{{.Version}}
</a>
{{end}}
{{end}}
</div>
</div>
<script>
document.getElementById('fullscreenButton').addEventListener('click', function() {
var iframe = document.getElementById('gameFrame');
+4 -1
View File
@@ -10,7 +10,10 @@
<h6 class="card-subtitle mb-2 text-muted">Released At: {{.CreatedAt.Format "2006-01-02 15:04:05"}}</h6>
</div>
<div class="card-footer">
<div class="btn-group d-flex justify-content-end" role="group" aria-label="Operations">
<div class="btn-group d-flex justify-content-center" role="group" aria-label="Operations">
{{if .WebPlayable}}
<a href="/play/{{$.Software.Name}}/{{.Version}}" class="btn btn-success btn-sm">Play</a>
{{end}}
<a href="/download/{{$.Software.Name}}/{{.Version}}/source" class="btn btn-primary btn-sm">Source</a>
<a href="/download/{{$.Software.Name}}/{{.Version}}/cartridge" class="btn btn-primary btn-sm">Cartridge</a>
</div>