-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.R
124 lines (104 loc) · 3.45 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# ------------------------------------------------------------------------
#
# Title : Memory Hex - Server
# By : dreamRs
# Date : 2019-02-06
#
# ------------------------------------------------------------------------
library("shiny")
function(input, output, session) {
start <- callModule(module = welcome, id = "welcome")
timer <- callModule(module = time, id = "timer", start = start)
hex_png <- sample(list.files(path = "www/hex/", pattern = "png$"), n_hex)
hex_png <- sample(rep(hex_png, 2))
results_mods <- reactiveValues()
results_mods_parse <- reactiveValues(all = NULL, show1 = NULL, show2 = NULL, show3 = NULL)
reset <- reactiveValues(x = NULL)
block <- reactiveValues(x = NULL)
lapply(
X = seq_len(n_hex * 2),
FUN = function(x) {
results_mods[[paste0("module", x)]] <- callModule(
module = hex,
id = paste0("module", x),
hex_logo = hex_png[x],
reset = reset,
block = block
)
}
)
observe({
res_mod <- lapply(
X = reactiveValuesToList(results_mods),
FUN = reactiveValuesToList
)
results_mods_parse$all <- res_mod
results_mods_parse$show1 <- which_show(res_mod, 1)
results_mods_parse$show2 <- which_show(res_mod, 2)
results_mods_parse$show3 <- which_show(res_mod, 3)
})
observeEvent(results_mods_parse$show2, {
hex1 <- which_hex(results_mods_parse$all, results_mods_parse$show1)
hex2 <- which_hex(results_mods_parse$all, results_mods_parse$show2)
if (identical(hex1, hex2)) {
block$x <- hex1
showNotification(
ui = tags$div(
style = "font-size: 160%; font-weight: bold;",
sample(
x = c("Well done!", "Bravo!", "Great!", "Good job!",
"Amazing!", "That's a match!", "Hooray!"),
size = 1
)
), type = "message"
)
}
})
observeEvent(results_mods_parse$show3, {
reset$x <- which_hex(
results_mods_parse$all,
c(results_mods_parse$show1, results_mods_parse$show2)
)
results_mods_parse$show1 <- NULL
results_mods_parse$show2 <- NULL
results_mods_parse$show1 <- results_mods_parse$show3
results_mods_parse$show3 <- NULL
})
observe({
allfound <- all_found(results_mods_parse$all)
if (isTRUE(allfound)) {
showModal(modalDialog(
tags$div(
style = "text-align: center;",
tags$h2(
tags$span(icon("trophy"), style = "color: #F7E32F;"),
"Well done !",
tags$span(icon("trophy"), style = "color: #F7E32F;")
),
tags$h4("You've found all matching hex in"),
tags$h1(isolate(timer()), "seconds!"),
tags$br(), tags$br(),
tags$a(
href = glue(shareurl, time = isolate(timer())),
icon("twitter"), "Tweet your score !",
class = "btn btn-info btn-lg"
),
tags$br(), tags$br(),
tags$p("This app is our submission for the",
tags$a(href = "https://community.rstudio.com/t/shiny-contest-submission-hex-memory-game/25336", "Shiny contest !")),
tags$br(), tags$br(),
actionButton(
inputId = "reload",
label = "Play again !",
style = "width: 100%;"
)
),
footer = NULL,
easyClose = FALSE
))
}
})
observeEvent(input$reload, {
session$reload()
}, ignoreInit = TRUE)
}