-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(game initialisation): add rng_seed parameter
This is a first step to let user initialise the game with different parameters from the app directly. Here only for one parameter, the `rng_seed`.
- Loading branch information
1 parent
fa75938
commit f24a475
Showing
13 changed files
with
188 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
shinydashboard | ||
shinycssloaders | ||
shinyjs | ||
shinyvalidate | ||
RSQLite | ||
MASS | ||
digest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# functions used to validate game initialisation parameters | ||
# these functions are defined here because they are used at 2 different | ||
# places: | ||
# - The application, trhough the modules of each parameters | ||
# with an `InputValidator` object (to highlight wrong inputs in the UI) | ||
# - The Game Initialisation script (to stop it in case the inputs are wrong) | ||
# | ||
# Technically, the error messages should be slightly different if we raise an | ||
# error from the initialisation script or in the application. But since it | ||
# would complexify the code for few benefit the error messages to show on the | ||
# UI are implemented here (as it is the intended way to setup the game). | ||
# | ||
# These functions can either `stop` or `return` an error message. | ||
# The stop behaviour is intended to be used in the initialisation script | ||
# and the return behaviour is intended to be used with `InputValidator$add_rule` | ||
|
||
|
||
valid_rng_seed <- function(seed, accept_null = TRUE, raise_error = FALSE) { | ||
|
||
error <- return | ||
if (raise_error) { | ||
error <- stop | ||
} | ||
|
||
if (is.null(seed)) { | ||
if (accept_null) { | ||
return(NULL) | ||
} | ||
error("Must not be NULL") | ||
} | ||
|
||
if (is.na(seed)) { | ||
error("Mandatory and should be a positive integer") | ||
} | ||
|
||
if (!is.numeric(seed)) { | ||
error("Should be a positive integer") | ||
} | ||
|
||
if (seed %% 1 != 0 || seed < 0) { | ||
error("Should be a positive integer") | ||
} | ||
|
||
return(NULL) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
# this file list all the game initialisation parameters (implemented in the | ||
# application) | ||
# It takes the forms of "shiny modules" to: | ||
# - include the "input validation" in the module | ||
# - have more complex UI that provides informations to users based on several | ||
# "groups of inputs" (eg. all inputs related to budgets) | ||
# - lighten the code related to "Admin server". | ||
|
||
# in the "server" parts of these modules, the `iv` argument is an | ||
# `InputValidator` object | ||
|
||
library(shinyvalidate) | ||
|
||
|
||
gameInit_seed_ui <- function(id) { | ||
ns <- NS(id) | ||
numericInput(ns("seed"), "RNG seed", value = 1993, step = 1) | ||
} | ||
|
||
gameInit_seed_server <- function(id, iv) { | ||
moduleServer(id, function(input, output, session) { | ||
|
||
iv$add_rule("seed", valid_rng_seed) | ||
return( | ||
list( | ||
value = reactive({ | ||
if (is.null(iv$validate()[[session$ns('seed')]])) { | ||
return(input$seed) | ||
} | ||
return(NA) | ||
}), | ||
iv = iv | ||
) | ||
) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,6 @@ | ||
library(testthat) | ||
|
||
# load functions | ||
invisible( | ||
sapply(FUN = source, | ||
X = list.files("src/fun", pattern = ".R$", full.names = T)) | ||
) | ||
|
||
# run tests | ||
# test_file("tests/testthat/test_0_dependencies.R", | ||
# stop_on_failure = TRUE, | ||
# stop_on_warning = FALSE) | ||
# test_file("tests/testthat/test_game_time.R", | ||
# stop_on_failure = TRUE, | ||
# stop_on_warning = FALSE) | ||
|
||
test_dir("tests/testthat", | ||
stop_on_failure = TRUE, | ||
stop_on_warning = FALSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
library(testthat) | ||
source("../../src/fun/func_gameInit_validation.R", local = TRUE, encoding = "UTF-8") | ||
|
||
|
||
test_that("valid_rng_seed", { | ||
|
||
# OK cases | ||
expect_null(valid_rng_seed(42)) | ||
expect_null(valid_rng_seed(NULL)) | ||
|
||
# invalid cases (no error) | ||
expect_type(valid_rng_seed(NA), "character") | ||
expect_type(valid_rng_seed(24.234), "character") | ||
expect_type(valid_rng_seed(-42), "character") | ||
expect_type(valid_rng_seed("abc"), "character") | ||
expect_type(valid_rng_seed(NULL, FALSE), "character") | ||
|
||
# invalid cases (error) | ||
expect_error(valid_rng_seed(24.234, FALSE, TRUE)) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters