Skip to content

Commit

Permalink
Improve file structure, fn structure, rename fns (#27)
Browse files Browse the repository at this point in the history
* Fix ifelse level-up

* Separate update logic, tidy fn definitions

* Add draft tech vignette

* Cry for KEVIN
  • Loading branch information
matt-dray committed Sep 30, 2022
1 parent 060a20e commit 5c7601c
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 242 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ rsconnect/
# macOS
.DS_Store
docs
inst/doc
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
Suggests:
covr
covr,
knitr,
rmarkdown
Depends:
R (>= 4.0)
LazyData: true
VignetteBuilder: knitr
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
export(.check_and_update)
export(clean)
export(feed)
export(get_stats)
export(lay_egg)
export(play)
export(release_pet)
export(release)
export(see_pet)
export(see_stats)
175 changes: 2 additions & 173 deletions R/blueprints.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
.create_blueprint <- function(pet_name) {

if (!is.character(pet_name) | nchar(pet_name) > 8) {
stop("'pet_name' must be a string with 8 characters or fewer.")
stop("Argument 'pet_name' must be a string with 8 characters or fewer.")
}

rolled <- .roll_characteristics()
Expand Down Expand Up @@ -124,7 +124,7 @@
length(blueprint) != 4 |
all(lengths(blueprint) != c(2L, 4L, 2L, 3L))
) {
stop("'blueprint' must be a list of lists")
stop("Argument 'blueprint' must be a list of lists")
}

data_dir <- tools::R_user_dir("tamRgo", which = "data")
Expand Down Expand Up @@ -213,174 +213,3 @@
}

}

#' Update Time-Dependent Blueprint Values
#'
#' @description Update time-dependent blueprint values given how much time has
#' elapsed since the last recorded interaction. Affects statuses ('happy',
#' 'hungry', 'dirty') and experience values ('XP', 'level').
#'
#' @param happy_increment Integer. How many minutes must elapse before the
#' 'happy' status value decreases by 1?
#' @param hungry_increment Integer. How many minutes must elapse before the
#' 'hungry' status value decreases by 1?
#' @param dirty_increment Integer. How many minutes must elapse before the
#' 'dirty' status value decreases by 1?
#' @param xp_increment Integer. How many minutes must elapse before the pet
#' gains 1 XP (experience point)?
#' @param xp_threshold_1 Integer. Minimum experience points (XP) required to
#' reach level 1.
#' @param xp_threshold_2 Integer. Minimum experience points (XP) required to
#' reach level 2.
#' @param xp_threshold_3 Integer. Minimum experience points (XP) required to
#' reach level 3.
#'
#' @return A list.
#'
#' @examples \dontrun{.update_blueprint()}
#'
#' @noRd
.update_blueprint <- function(
happy_increment = 10L,
hungry_increment = 15L,
dirty_increment = 30L,
xp_increment = 5L,
xp_threshold_1 = 100L,
xp_threshold_2 = 250L,
xp_threshold_3 = 500L
) {

data_dir <- tools::R_user_dir("tamRgo", which = "data")
data_file <- file.path(data_dir, "blueprint.rds")
has_data_file <- file.exists(data_file)

if (!has_data_file) {
stop("A pet blueprint hasn't been found.")
}

bp <- .read_blueprint()

current_interaction <- Sys.time()
bp$meta$last_interaction <- current_interaction

time_diff <-
as.numeric(current_interaction - bp$meta$last_interaction, units = "mins")

bp$characteristics$age <-
as.numeric(Sys.Date() - as.Date(bp$characteristics$born), units = "days")

bp <- .update_status(bp, time_diff)
bp <- .update_xp(bp, time_diff)

.write_blueprint(bp, ask = FALSE)

return(bp)

}

#' Update Time-Dependent Status Values
#'
#' @description Update time-dependent blueprint values given how much time has
#' elapsed since the last recorded interaction. Affects statuses ('happy',
#' 'hungry', 'dirty').
#'
#' @param happy_increment Integer. How many minutes must elapse before the
#' 'happy' status value decreases by 1?
#' @param hungry_increment Integer. How many minutes must elapse before the
#' 'hungry' status value decreases by 1?
#' @param dirty_increment Integer. How many minutes must elapse before the
#' 'dirty' status value decreases by 1?
#'
#' @details A sub-function of \code{\link{.update_blueprint}}.
#'
#' @return A list.available
#'
#' @examples \dontrun{.update_status()}
#'
#' @noRd
.update_status <- function(
blueprint,
time_difference,
happy_increment,
hungry_increment,
dirty_increment
) {

if(
!is.integer(happy_increment) |
!is.integer(hungry_increment) |
!is.integer(dirty_increment)
) {
stop("'*_increment' values must integers.")
}

blueprint$status$happy <-
max(blueprint$status$happy - (time_difference %/% happy_increment), 0L)

blueprint$status$hungry <-
min(blueprint$status$hungry + (time_difference %/% hungry_increment), 5L)

blueprint$status$dirty <-
min(blueprint$status$dirty + (time_difference %/% dirty_increment), 5L)

return(blueprint)

}

#' Update Time-Dependent Experience Values
#'
#' @description Update time-dependent blueprint values given how much time has
#' elapsed since the last recorded interaction. Affects experience values
#' ('XP', 'level').
#'
#' @param happy_increment Integer. How many minutes must elapse before the
#' 'happy' status value decreases by 1?
#' @param hungry_increment Integer. How many minutes must elapse before the
#' 'hungry' status value decreases by 1?
#' @param dirty_increment Integer. How many minutes must elapse before the
#' 'dirty' status value decreases by 1?
#'
#' @details A sub-function of \code{\link{.update_blueprint}}.
#'
#' @return A list.available
#'
#' @examples \dontrun{.update_status()}
#'
#' @noRd
.update_xp <- function(
blueprint,
time_difference,
xp_increment,
xp_threshold_1,
xp_threshold_2,
xp_threshold_3
) {

if (!is.list(blueprint) |
length(blueprint) != 4 |
all(lengths(blueprint) != c(2L, 4L, 2L, 3L))
) {
stop("'blueprint' must be a list of lists")
}

if(
!is.integer(xp_threshold_1) |
!is.integer(xp_threshold_2) |
!is.integer(xp_threshold_3)
) {
stop("'xp_threshold_*' values must be integers.")
}

bp$experience$xp <- bp$experience$xp + (time_difference %/% xp_increment)

if (blueprint$experience$xp >= xp_threshold_1) {
blueprint$experience$level <- 1L
} else if (blueprint$experience$xp >= xp_threshold_2) {
blueprint$experience$level <- 2L
} else if (blueprint$experience$xp >= xp_threshold_3) {
blueprint$experience$level <- 3L
}

return(blueprint)

}
4 changes: 2 additions & 2 deletions R/interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ lay_egg <- function(pet_name) {
#' @export
#'
#' @examples \dontrun{see_stats()}
see_stats <- function() {
get_stats <- function() {

bp <- .check_and_update()

Expand Down Expand Up @@ -155,7 +155,7 @@ clean <- function() {
#' @export
#'
#' @examples \dontrun{see_stats()}
release_pet <- function() {
release <- function() {

bp <- .check_and_update()

Expand Down

0 comments on commit 5c7601c

Please sign in to comment.