forked from tpemartin/109-2-app101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.R
158 lines (137 loc) · 4.7 KB
/
backend.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
library(dplyr)
library(ggplot2)
library(lubridate)
#library(plotly)
#library(leaflet)
.root <- rprojroot::is_rstudio_project$make_fix_file()
xfun::download_file("https://www.dropbox.com/s/2rryka3cprtgfok/heritageDataRevised.Rdata?dl=1", mode="wb")
load("heritageDataRevised.Rdata")
# define user, heritage structure
## define complete structure
user <- list()
user <-
list(
sessionData =
list(
county_chosen = character(0),
filtered_data = vector("list",0),
game =
list(
puzzle_guess = vector("list",0)
)
),
checkIns = vector("list",0),
visits = vector("list",0)
)
# heritage structure
heritage <-
list(
data = heritageData,
show_county = function(){},
filter = list(
countyChosen = function(county){}
),
game = list(
puzzle_guess = function(){}
),
validate_checkIn = function(returnedGPS){},
track = function(list_tracks){}
)
# show by county --------------------------------------------
# App function: user wants to zoom into map that shows only the point of interests belong to certain county
# when select choose by county on APP, it calls
heritage$show_county() -> user$sessionData$county_chosen
# which returns a character vector of counties on screen
# 1. prompt names of counties that he can choose on screen
# 2. once chosen, return user's choice and save in the according object element
# Then program continues
heritage$filter$countyChosen(user$sessionData$county_chosen) ->
user$sessionData$filtered_data
# countyChosen returns caseIds that fit the chosen criterion.
# The backend return user$sessionData$filtered_data to the frontend which will redraw the map
# play puzzle -------------------------------------------------------------
# when user selects play puzzle guess, it calls
source("/Users/frankchao/Desktop/109-2-app101/weekly_progress/frank/puzzle.R")
heritage$game$puzzle_guess <- puzzle_guess
heritage$game$puzzle_guess() -> game_result
# the return is a list of elements:
# timestamp=a character of ISO8601 time when the user starts to play
# answer=a caseId of correct answer
# options=a character vector of caseIds of heritage options
# and user_choice=a caseId of user's choice among options
# game_result$timestamp
# game_result$answer
# game_result$options
# game_result$user_choice
# record result
user$sessionData$game$puzzle_guess <-
append(
user$session$game$puzzle_guess,
list(game_result)
)
# check in ----------------------------------------------------------------
# user check in in the webapp UI,
#browseURL("https://tpemartin.github.io/109-2-app101/checkin")
# the frontend returns a numeric vector of GPS
# c(latitude=..., longitude=...)
returnedGPS = c(lon=120.4407, lat=23.47734)
heritage$validate_checkIn <- function(returnedGPS){
time_start <- format_ISO8601(now(),usetz = T)
location <-{
lon <- heritageData$longitude
lat <- heritageData$latitude
lon[is.na(lon)] <- 0
lat[is.na(lat)] <- 0
matrix(lon) -> col1
matrix(lat) -> col2
cbind(col1,col2)
}
if(T %in% (sp::spDistsN1(location,returnedGPS,longlat=T) <= 0.1)){
return(
list(
timestamp = time_start ,
caseId = {
which.min(sp::spDistsN1(location,returnedGPS,longlat=T)) ->
index
heritageData$caseId[index]
}
)
)
}else{
return(NULL)
}
}
heritage$validate_checkIn(returnedGPS) -> checkInResult
# which check if the returnGPS is within 100 meters of any heritage site,
# If not, checkInResult = NULL;
# If yes,
# checkInResult = list(
# timestamp= a string of ISO8601 time when the user checks in,
# caseId= the closest site's caseId
# )
# sp::spDistsN1 can calculate km distance for you
if(!is.null(checkInResult)){
user$checkIns <- append(
user$checkIns, list(checkInResult)
)
}
# track -------------------------------------------------------------------
# when user turn on tracking in the frontend, user's phone will track his GPX
source(
file.path(.root(),"R/field_trips_all.R")
)
# return user's list_tracks
heritage$track(list_tracks) ->
newTraces
# where newTraces is a list of length(list_tracks) for element 1, it transform list_trackes[[1]]
#list_tracks[[1]][c("lon","lat")] -> trace1
#newTraces <- list()
# newTraces[[1]] <-
# list(
# timestamp=date of visit
# trace=trace1,
# caseId=
# )
user$visits <- append(
user$visits, newTraces
)