TimStats commited on
Commit
96b5da5
·
verified ·
1 Parent(s): d8e87c6

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +127 -4
app.R CHANGED
@@ -11,9 +11,50 @@ library(gridExtra)
11
  library(gtable)
12
  library(httr)
13
 
14
- # showtext_opts(dpi = 300) # Match plot DPI
15
- # showtext_auto(enable = TRUE)
16
- # font_add_google("Roboto Condensed", "roboto")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  font_add_google("Roboto Condensed")
18
 
19
  is_barrel <- function(df) {
@@ -366,6 +407,86 @@ ui <- fluidPage(
366
 
367
  # Server logic
368
  server <- function(input, output,session) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
370
  observeEvent(c(input$szn,input$level), {
371
  # Filter data based on selected season
@@ -375,6 +496,8 @@ server <- function(input, output,session) {
375
  inputId = "player",
376
  choices = unique(filtered_data$`Batter Name`))
377
  })
 
 
378
  # Create reactive value to store team
379
  team_value <- reactiveVal("MLB")
380
  position_value <- reactiveVal("")
@@ -442,7 +565,7 @@ server <- function(input, output,session) {
442
 
443
  output$statsPlot <- renderPlot({
444
  req(position_value())
445
-
446
  # showtext::showtext_begin()
447
  # on.exit(showtext::showtext_end())
448
 
 
11
  library(gtable)
12
  library(httr)
13
 
14
+ check_patreon_access <- function(email) {
15
+ campaign_id <- Sys.getenv("PATREON_CAMPAIGN_ID")
16
+ access_token <- Sys.getenv("PATREON_ACCESS_TOKEN")
17
+
18
+ base_url <- paste0("https://www.patreon.com/api/oauth2/v2/campaigns/", campaign_id, "/members")
19
+
20
+ params <- list(
21
+ `include` = "currently_entitled_tiers",
22
+ `fields[member]` = "patron_status,email",
23
+ `fields[tier]` = "title"
24
+ )
25
+
26
+ response <- GET(
27
+ base_url,
28
+ query = params,
29
+ add_headers(
30
+ `Authorization` = paste("Bearer", access_token),
31
+ `User-Agent` = "R/httr"
32
+ )
33
+ )
34
+
35
+ content <- fromJSON(rawToChar(response$content))
36
+
37
+ # Check in data$attributes for matching email
38
+ matching_row <- which(content$data$attributes$email == email)
39
+
40
+ if (length(matching_row) > 0) {
41
+ # Get patron status
42
+ patron_status <- content$data$attributes$patron_status[matching_row]
43
+
44
+ if (patron_status == "active_patron") {
45
+ # Get tier info
46
+ tier_data <- content$data$relationships$currently_entitled_tiers$data[[matching_row]]
47
+
48
+ tier_id <- tier_data$id
49
+
50
+ result <- tier_id %in% c("25062087", "25062090")
51
+
52
+ return(result)
53
+ }
54
+ }
55
+
56
+ return(FALSE)
57
+ }
58
  font_add_google("Roboto Condensed")
59
 
60
  is_barrel <- function(df) {
 
407
 
408
  # Server logic
409
  server <- function(input, output,session) {
410
+
411
+ # Reactive value to track authentication status
412
+ credentials <- reactiveValues(logged_in = FALSE)
413
+
414
+ # Render login page or main content based on auth status
415
+ output$page <- renderUI({
416
+ if (!credentials$logged_in) {
417
+ # Login page
418
+ div(class = "login-screen",
419
+ div(class = "login-header",
420
+ h2("Baseball Stats Visualization"),
421
+ p("Please log in with your Patreon email")
422
+ ),
423
+ textInput("email", "Email:"),
424
+ actionButton("login", "Login", class = "btn-primary"),
425
+ p(style = "margin-top: 20px; text-align: center;",
426
+ "Access requires Veteran or Hall of Fame tier on Patreon")
427
+ )
428
+ } else {
429
+ # Main application UI (your existing UI)
430
+ fluidPage(
431
+ titlePanel(NULL, windowTitle = "Baseball Stats Visualization"),
432
+ sidebarLayout(
433
+ sidebarPanel(
434
+ selectInput("szn", "Season:", c(2024, 2023, 2022, 2021, 2021)),
435
+ selectInput("level", "Level:", c("MLB", "AAA", "FSL")),
436
+ selectInput("type", "Player Type:", c("Batter", "Pitcher")),
437
+ selectInput("player", "Player:", choices = unique(temp_players$`Batter Name`)),
438
+ checkboxInput("use_custom_team", "Use Custom Team", FALSE),
439
+ conditionalPanel(
440
+ condition = "input.use_custom_team == true",
441
+ selectInput(
442
+ inputId = "team",
443
+ label = "Select Team",
444
+ choices = c(
445
+ "MLB" = "MLB",
446
+ "Angels" = "LAA",
447
+ # ... rest of your teams ...
448
+ "Yankees" = "NYY"
449
+ ),
450
+ selected = "MLB"
451
+ )
452
+ )
453
+ ),
454
+ mainPanel(
455
+ div(class = "plot-container",
456
+ plotOutput("statsPlot")
457
+ )
458
+ )
459
+ )
460
+ )
461
+ }
462
+ })
463
+
464
+ # Handle login button click
465
+ observeEvent(input$login, {
466
+ # Show loading message
467
+ showModal(modalDialog(
468
+ "Checking credentials...",
469
+ footer = NULL
470
+ ))
471
+
472
+ # Check Patreon access
473
+ has_access <- check_patreon_access(input$email)
474
+
475
+ # Remove loading message
476
+ removeModal()
477
+
478
+ if (has_access) {
479
+ credentials$logged_in <- TRUE
480
+ } else {
481
+ showModal(modalDialog(
482
+ title = "Access Denied",
483
+ "This email does not have access. Please make sure you're using the email associated with your Patreon account and you have an active Veteran or Hall of Fame tier subscription.",
484
+ easyClose = TRUE
485
+ ))
486
+ }
487
+ })
488
+ observe({
489
+ req(credentials$logged_in)
490
 
491
  observeEvent(c(input$szn,input$level), {
492
  # Filter data based on selected season
 
496
  inputId = "player",
497
  choices = unique(filtered_data$`Batter Name`))
498
  })
499
+
500
+ })
501
  # Create reactive value to store team
502
  team_value <- reactiveVal("MLB")
503
  position_value <- reactiveVal("")
 
565
 
566
  output$statsPlot <- renderPlot({
567
  req(position_value())
568
+ req(credentials$logged_in)
569
  # showtext::showtext_begin()
570
  # on.exit(showtext::showtext_end())
571