Skip to content

Commit

Permalink
Allow specifying lobby defaults; Fixes #242
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Aug 26, 2023
1 parent ce76886 commit 00b9d0d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmd/scribblers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {

// FIXME Global state needs to be deleted.
frontend.SetRootPath(cfg.RootPath)
frontend.SetupRoutes(cfg.RootPath, router)
frontend.SetupRoutes(cfg, router)

state.LaunchCleanupRoutine()

Expand Down
34 changes: 32 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ import (
"github.com/subosito/gotenv"
)

type LobbySettingDefaults struct {
Public string `env:"PUBLIC"`
DrawingTime string `env:"DRAWING_TIME"`
Rounds string `env:"ROUNDS"`
MaxPlayers string `env:"MAX_PLAYERS"`
CustomWords string `env:"CUSTOM_WORDS"`
CustomWordsChance string `env:"CUSTOM_WORDS_CHANCE"`
ClientsPerIPLimit string `env:"CLIENTS_PER_IP_LIMIT"`
EnableVotekick string `env:"ENABLE_VOTEKICK"`
Language string `env:"LANGUAGE"`
}

type Config struct {
Port uint16 `env:"PORT" envDefault:"8080"`
// NetworkAddress is empty by default, since that implies listening on
// all interfaces. For development usecases, on windows for example, this
// is very annoying, as windows will nag you with firewall prompts.
Expand All @@ -23,6 +34,25 @@ type Config struct {
// might have to look like this: painting.com/scribblers/v1
RootPath string `env:"ROOT_PATH"`
CPUProfilePath string `env:"CPU_PROFILE_PATH"`
// LobbySettingDefaults is used for the server side rendering of the lobby
// creation page. It doesn't affect the default values of lobbies created
// via the API.
LobbySettingDefaults LobbySettingDefaults `envPrefix:"LOBBY_SETTING_DEFAULTS_"`
Port uint16 `env:"PORT"`
}

var Default = Config{
Port: 8080,
LobbySettingDefaults: LobbySettingDefaults{
Public: "false",
DrawingTime: "120",
Rounds: "4",
MaxPlayers: "12",
CustomWordsChance: "50",
ClientsPerIPLimit: "1",
EnableVotekick: "true",
Language: "english",
},
}

// Load loads the configuration from the environment. If a .env file is
Expand Down Expand Up @@ -57,7 +87,7 @@ func Load() (*Config, error) {
envVars[pair[0]] = pair[1]
}

var config Config
config := Default
if err := env.ParseWithOptions(&config, env.Options{
Environment: envVars,
OnSet: func(key string, value any, isDefault bool) {
Expand Down
83 changes: 37 additions & 46 deletions internal/frontend/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/scribble-rs/scribble.rs/internal/api"
"github.com/scribble-rs/scribble.rs/internal/config"
"github.com/scribble-rs/scribble.rs/internal/game"
"github.com/scribble-rs/scribble.rs/internal/state"
"github.com/scribble-rs/scribble.rs/internal/translations"
Expand All @@ -14,51 +15,39 @@ import (

// homePage servers the default page for scribble.rs, which is the page to
// create a new lobby.
func homePage(writer http.ResponseWriter, request *http.Request) {
translation, locale := determineTranslation(request)
createPageData := createDefaultLobbyCreatePageData()
createPageData.Translation = translation
createPageData.Locale = locale

err := pageTemplates.ExecuteTemplate(writer, "lobby-create-page", createPageData)
if err != nil {
log.Printf("Error templating home page: %s\n", err)
func newHomePageHandler(lobbySettingDefaults config.LobbySettingDefaults) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
translation, locale := determineTranslation(request)
createPageData := createDefaultLobbyCreatePageData(lobbySettingDefaults)
createPageData.Translation = translation
createPageData.Locale = locale

err := pageTemplates.ExecuteTemplate(writer, "lobby-create-page", createPageData)
if err != nil {
log.Printf("Error templating home page: %s\n", err)
}
}
}

func createDefaultLobbyCreatePageData() *LobbyCreatePageData {
func createDefaultLobbyCreatePageData(defaultLobbySettings config.LobbySettingDefaults) *LobbyCreatePageData {
return &LobbyCreatePageData{
BasePageConfig: currentBasePageConfig,
SettingBounds: game.LobbySettingBounds,
Languages: game.SupportedLanguages,
Public: "false",
DrawingTime: "120",
Rounds: "4",
MaxPlayers: "12",
CustomWordsChance: "50",
ClientsPerIPLimit: "1",
EnableVotekick: "true",
Language: "english",
BasePageConfig: currentBasePageConfig,
SettingBounds: game.LobbySettingBounds,
Languages: game.SupportedLanguages,
LobbySettingDefaults: defaultLobbySettings,
}
}

// LobbyCreatePageData defines all non-static data for the lobby create page.
type LobbyCreatePageData struct {
*BasePageConfig
config.LobbySettingDefaults
game.SettingBounds
Translation translations.Translation
Locale string
Errors []string
Languages map[string]string
Public string
DrawingTime string
Rounds string
MaxPlayers string
CustomWords string
CustomWordsChance string
ClientsPerIPLimit string
EnableVotekick string
Language string

Translation translations.Translation
Locale string
Errors []string
Languages map[string]string
}

// ssrCreateLobby allows creating a lobby, optionally returning errors that
Expand All @@ -81,18 +70,20 @@ func ssrCreateLobby(writer http.ResponseWriter, request *http.Request) {

// Prevent resetting the form, since that would be annoying as hell.
pageData := LobbyCreatePageData{
BasePageConfig: currentBasePageConfig,
SettingBounds: game.LobbySettingBounds,
Languages: game.SupportedLanguages,
Public: request.Form.Get("public"),
DrawingTime: request.Form.Get("drawing_time"),
Rounds: request.Form.Get("rounds"),
MaxPlayers: request.Form.Get("max_players"),
CustomWords: request.Form.Get("custom_words"),
CustomWordsChance: request.Form.Get("custom_words_chance"),
ClientsPerIPLimit: request.Form.Get("clients_per_ip_limit"),
EnableVotekick: request.Form.Get("enable_votekick"),
Language: request.Form.Get("language"),
BasePageConfig: currentBasePageConfig,
SettingBounds: game.LobbySettingBounds,
Languages: game.SupportedLanguages,
LobbySettingDefaults: config.LobbySettingDefaults{
Public: request.Form.Get("public"),
DrawingTime: request.Form.Get("drawing_time"),
Rounds: request.Form.Get("rounds"),
MaxPlayers: request.Form.Get("max_players"),
CustomWords: request.Form.Get("custom_words"),
CustomWordsChance: request.Form.Get("custom_words_chance"),
ClientsPerIPLimit: request.Form.Get("clients_per_ip_limit"),
EnableVotekick: request.Form.Get("enable_votekick"),
Language: request.Form.Get("language"),
},
}

if languageInvalid != nil {
Expand Down
13 changes: 7 additions & 6 deletions internal/frontend/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"

"github.com/go-chi/chi/v5"
"github.com/scribble-rs/scribble.rs/internal/config"
"github.com/scribble-rs/scribble.rs/internal/translations"
)

Expand Down Expand Up @@ -73,14 +74,14 @@ type BasePageConfig struct {
}

// SetupRoutes registers the official webclient endpoints with the http package.
func SetupRoutes(rootPath string, router chi.Router) {
router.Get("/"+rootPath, homePage)
func SetupRoutes(config *config.Config, router chi.Router) {
router.Get("/"+config.RootPath, newHomePageHandler(config.LobbySettingDefaults))

fileServer := http.FileServer(http.FS(frontendResourcesFS))
router.Get(
"/"+path.Join(rootPath, "resources/*"),
"/"+path.Join(config.RootPath, "resources/*"),
http.StripPrefix(
"/"+rootPath,
"/"+config.RootPath,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Duration of 1 year, since we use cachebusting anyway.
w.Header().Set("Cache-Control", "public, max-age=31536000")
Expand All @@ -89,8 +90,8 @@ func SetupRoutes(rootPath string, router chi.Router) {
}),
).ServeHTTP,
)
router.Get("/"+path.Join(rootPath, "ssrEnterLobby/{lobby_id}"), ssrEnterLobby)
router.Post("/"+path.Join(rootPath, "ssrCreateLobby"), ssrCreateLobby)
router.Get("/"+path.Join(config.RootPath, "ssrEnterLobby/{lobby_id}"), ssrEnterLobby)
router.Post("/"+path.Join(config.RootPath, "ssrCreateLobby"), ssrCreateLobby)
}

// errorPageData represents the data that error.html requires to be displayed.
Expand Down
3 changes: 2 additions & 1 deletion internal/frontend/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/scribble-rs/scribble.rs/internal/api"
"github.com/scribble-rs/scribble.rs/internal/config"
"github.com/scribble-rs/scribble.rs/internal/game"
"github.com/scribble-rs/scribble.rs/internal/translations"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func Test_templateRobotPage(t *testing.T) {
}

func Test_templateLobbyCreatePage(t *testing.T) {
createPageData := createDefaultLobbyCreatePageData()
createPageData := createDefaultLobbyCreatePageData(config.Default.LobbySettingDefaults)
createPageData.Translation = translations.DefaultTranslation

var buffer bytes.Buffer
Expand Down

0 comments on commit 00b9d0d

Please sign in to comment.