Skip to content

Commit

Permalink
restore default emulation settings
Browse files Browse the repository at this point in the history
  • Loading branch information
n-at committed Sep 19, 2023
1 parent 62a4b10 commit a232730
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions templates/game_emulation_settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{% include "includes/form_platform_settings.twig" %}
<hr>
<div class="mb-3 text-end">
<a href="/games/emulation-settings/{{ game.Id }}/restore" class="btn btn-outline-secondary">Restore defaults</a>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Expand Down
24 changes: 24 additions & 0 deletions templates/game_emulation_settings_restore.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "layouts/layout.twig" %}

{% block title %}
Emulation settings restore defaults - playtime
{% endblock %}

{% block content %}

<h1>Emulation settings restore defaults</h1>
<h2>{{ game.Name }}</h2>
<hr>

<form action="/games/emulation-settings/{{ game.Id }}/restore" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="_playtime_csrf" value="{{ _csrf_token }}">
<p>
Restore default emulation settings for the game?
</p>
<hr>
<div class="mb-3 text-end">
<button type="submit" class="btn btn-primary">Restore</button>
</div>
</form>

{% endblock %}
1 change: 1 addition & 0 deletions templates/settings_platform.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{% include "includes/form_platform_settings.twig" %}
<hr>
<div class="mb-3 text-end">
<a href="/settings/{{ platform.Id }}/restore" class="btn btn-outline-secondary">Restore defaults</a>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Expand Down
24 changes: 24 additions & 0 deletions templates/settings_platform_restore.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "layouts/layout.twig" %}

{% block title %}
Restore defaults for {{ platform.Name }} - playtime
{% endblock %}

{% block content %}

<h1>Restore default settings</h1>
<h2>{{ platform.Name }}</h2>
<hr>

<form action="/settings/{{ platform.Id }}/restore" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="_playtime_csrf" value="{{ _csrf_token }}">
<p>
Restore default settings for {{ platform.Name }}?
</p>
<hr>
<div class="mb-3 text-end">
<button type="submit" class="btn btn-primary">Restore</button>
</div>
</form>

{% endblock %}
33 changes: 32 additions & 1 deletion web/handlers_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (s *Server) gameEmulationSettingsForm(c echo.Context) error {

func (s *Server) gameEmulationSettingsSubmit(c echo.Context) error {
context := c.(*PlaytimeContext)

game := context.game

if !game.OverrideEmulatorSettings {
Expand All @@ -192,6 +191,38 @@ func (s *Server) gameEmulationSettingsSubmit(c echo.Context) error {
return c.Redirect(http.StatusFound, "/games")
}

func (s *Server) gameEmulationSettingsRestoreDefaults(c echo.Context) error {
context := c.(*PlaytimeContext)
game := context.game

if !game.OverrideEmulatorSettings {
return errors.New("game does not override emulation settings")
}

return c.Render(http.StatusOK, "game_emulation_settings_restore", pongo2.Context{
"_csrf_token": c.Get("csrf"),
"user": context.user,
"game": context.game,
})
}

func (s *Server) gameEmulationSettingsRestoreDefaultsSave(c echo.Context) error {
context := c.(*PlaytimeContext)
game := context.game

if !game.OverrideEmulatorSettings {
return errors.New("game does not override emulation settings")
}

game.EmulatorSettings = storage.DefaultEmulatorSettings(game.Platform)

if _, err := s.storage.GameSave(*game); err != nil {
return err
}

return c.Redirect(http.StatusFound, "/games/emulation-settings/"+game.Id)
}

func (s *Server) gameDeleteForm(c echo.Context) error {
context := c.(*PlaytimeContext)

Expand Down
48 changes: 47 additions & 1 deletion web/handlers_settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"errors"
"fmt"
"github.com/flosch/pongo2/v6"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -40,8 +41,12 @@ func (s *Server) settingsGeneralSubmit(c echo.Context) error {

func (s *Server) settingsByPlatformForm(c echo.Context) error {
context := c.(*PlaytimeContext)

platform := c.Param("platform")

if _, ok := storage.Platforms[platform]; !ok {
return errors.New("platform not found")
}

settings := context.settings
platformSettings, ok := settings.EmulatorSettings[platform]
if !ok {
Expand All @@ -67,6 +72,10 @@ func (s *Server) settingsByPlatformSubmit(c echo.Context) error {
context := c.(*PlaytimeContext)
platform := c.Param("platform")

if _, ok := storage.Platforms[platform]; !ok {
return errors.New("platform not found")
}

log.Infof("settingsByPlatformSubmit %s for %s", platform, context.user.Login)

settings := context.settings
Expand All @@ -80,6 +89,43 @@ func (s *Server) settingsByPlatformSubmit(c echo.Context) error {
return c.Redirect(http.StatusFound, "/settings?done=1")
}

func (s *Server) settingsByPlatformRestoreDefaults(c echo.Context) error {
context := c.(*PlaytimeContext)
platform := c.Param("platform")

if _, ok := storage.Platforms[platform]; !ok {
return errors.New("platform not found")
}

return c.Render(http.StatusOK, "settings_platform_restore", pongo2.Context{
"_csrf_token": c.Get("csrf"),
"user": context.user,
"platform": storage.Platforms[platform],
})
}

func (s *Server) settingsByPlatformRestoreDefaultsSave(c echo.Context) error {
context := c.(*PlaytimeContext)
platform := c.Param("platform")

if _, ok := storage.Platforms[platform]; !ok {
return errors.New("platform not found")
}

settings, err := s.storage.SettingsGetByUserId(context.user.Id)
if err != nil {
return err
}

settings.EmulatorSettings[platform] = storage.DefaultEmulatorSettings(platform)

if _, err := s.storage.SettingsSave(settings); err != nil {
return err
}

return c.Redirect(http.StatusFound, "/settings/"+platform)
}

///////////////////////////////////////////////////////////////////////////////

func settingsCollectFormData(c echo.Context) storage.EmulatorSettings {
Expand Down
4 changes: 4 additions & 0 deletions web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func New(config *Configuration, storage *storage.Storage) *Server {
settings.POST("", s.settingsGeneralSubmit)
settings.GET("/:platform", s.settingsByPlatformForm)
settings.POST("/:platform", s.settingsByPlatformSubmit)
settings.GET("/:platform/restore", s.settingsByPlatformRestoreDefaults)
settings.POST("/:platform/restore", s.settingsByPlatformRestoreDefaultsSave)

//users
users := e.Group("/users")
Expand Down Expand Up @@ -141,6 +143,8 @@ func New(config *Configuration, storage *storage.Storage) *Server {
gamesEmulationSettings.Use(s.gameRequiredMiddleware)
gamesEmulationSettings.GET("", s.gameEmulationSettingsForm)
gamesEmulationSettings.POST("", s.gameEmulationSettingsSubmit)
gamesEmulationSettings.GET("/restore", s.gameEmulationSettingsRestoreDefaults)
gamesEmulationSettings.POST("/restore", s.gameEmulationSettingsRestoreDefaultsSave)

gamesEdit := games.Group("/edit/:game_id")
gamesEdit.Use(s.gameRequiredMiddleware)
Expand Down

0 comments on commit a232730

Please sign in to comment.