-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
141 lines (118 loc) · 5.92 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
shinyServer(function(input, output, session) {
swarmCount = reactiveVal(0)
swarmOuts = reactiveValues()
previousButtonCounts = reactiveVal(list())
initialText = reactiveVal('Written by Ogan Mancarci\nSource code at github.com/oganm/swarm\nMonster data is released by Wizards of the Coast under open game license\nData is translated into JSON format by Walter Kammerer\n ')
# observe instead of observeEvent because I want this to run
# once when the app initalizes
observe({
input$addSwarm
isolate({
swarmCount(swarmCount()+1)
insertUI(
selector = "#addSwarm",
where = 'beforeBegin',
ui = swarmUI(paste0('swarm',swarmCount()))
)
swarmOuts[[as.character(swarmCount())]] = callModule(swarm,id = paste0('swarm',swarmCount()),swarmLimit = swarmLimit)
})
shinyjs::show('main_content')
})
observeEvent(input$removeSwarm,{
if(swarmCount() > 1){
removeUI(
selector = paste0("#swarm",swarmCount())
)
swarmCount(swarmCount()-1)
}
})
# get all swarm outputs
swarmOut = reactive({
out = seq_len(swarmCount()) %>% lapply(function(i){
swarmOuts[[as.character(i)]]()
})
})
# this is there to prevent returning anything if
# a new swarm is added instead of an attack button press
swarmButtonCount = reactiveVal(0)
# decide which attack button is pressed and send the correct output
# for processing
rolledSwarm = reactive({
swarms = swarmOut()
isolate({
# if this is triggered because add swarm button is pressed, ignore
if(input$addSwarm == swarmButtonCount()){
buttonCounts = swarms %>% purrr::map('buttonCount')
names(buttonCounts) = seq_along(buttonCounts)
previousButtons = previousButtonCounts()
whichButton = seq_along(buttonCounts) %>% sapply(function(i){
i = as.character(i)
(!is.null(buttonCounts[[i]]) && !is.null(previousButtons[[i]]) && buttonCounts[[i]]>previousButtons[[i]]) || (!is.null(buttonCounts[[i]]) && is.null(previousButtons[[i]]) && buttonCounts[[i]]>0)
}) %>% as.logical()
previousButtonCounts(buttonCounts)
# print(which(whichButton))
# print(swarms)
if(sum(whichButton) == 1){
return(swarms[[which(whichButton)]])
} else{
return(NULL)
}
} else{
swarmButtonCount(input$addSwarm)
return(NULL)
}
})
})
# process the output to human readable format
finalOutput = reactive({
# this assignment must be above here. otherwise since initial return
# is the initial text the rest is ignored. It seems like dependency
# tree is created after a run. premature endings prevent some dependencies from
# getting registered
swarmData = rolledSwarm()
isolate({
if(initialText()!=''){
text = initialText()
initialText('')
return(text)
}
})
if(!is.null(swarmData) && names(swarmData)[[1]] == 'hits'){
if(!is.null(swarmData) && length(swarmData$hits) == 1 && swarmData$hits == -1){
text = paste0("Maximum allowed swarm size is ",swarmLimit,"\n ")
} else if(!is.null(swarmData) && length(swarmData$hits)>= 1){
text = glue::glue('{swarmData$swarmName} attacks:\n',
length(swarmData$hits),' members hit for\n',
paste(swarmData$hits,collapse= ', '),' damage\n',
'A total of {sum(swarmData$hits)}\n \n ')
} else if(!is.null(swarmData) && length(swarmData$hits) == 0){
text = glue::glue("{swarmData$swarmName} attacks:\nEveryone missed\n \n ")
} else{
text = ''
}
} else if(!is.null(swarmData) && names(swarmData)[[1]] == 'saves'){
if(swarmData$detailed){
text = glue::glue('{swarmData$swarmName} rolls saves:\n',
'Rolls: {paste(swarmData$saves,collapse = ", ")}\n',
'Str: {length(swarmData$passes$Str)} passes; {paste(swarmData$passes$Str, collapse = ", ")}\n',
'Dex: {length(swarmData$passes$Dex)} passes; {paste(swarmData$passes$Dex, collapse = ", ")}\n',
'Con: {length(swarmData$passes$Con)} passes; {paste(swarmData$passes$Con, collapse = ", ")}\n',
'Int: {length(swarmData$passes$Int)} passes; {paste(swarmData$passes$Int, collapse = ", ")}\n',
'Wis: {length(swarmData$passes$Wis)} passes; {paste(swarmData$passes$Wis, collapse = ", ")}\n',
'Cha: {length(swarmData$passes$Cha)} passes; {paste(swarmData$passes$Cha, collapse = ", ")}\n')
}else{
text = glue::glue('{swarmData$swarmName} rolls saves:\n',
'Str: {length(swarmData$passes$Str)} passes\n',
'Dex: {length(swarmData$passes$Dex)} passes\n',
'Con: {length(swarmData$passes$Con)} passes\n',
'Int: {length(swarmData$passes$Int)} passes\n',
'Wis: {length(swarmData$passes$Wis)} passes\n',
'Cha: {length(swarmData$passes$Cha)} passes\n')
}
}else{
text = ''
}
return(text)
})
callModule(console,'console',consoleLength = 100,finalOutput)
})