TimStats commited on
Commit
f6346c2
·
verified ·
1 Parent(s): b741f6f

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +132 -28
app.R CHANGED
@@ -28,6 +28,50 @@ Sys.setenv(TZ='EST')
28
  model <- xgb.load('TimStuff2.model')
29
 
30
  # Helper functions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  gtable_add_padding <- function(gtable, padding = unit(c(5, 5, 5, 5), "mm")) {
32
  padding <- rep(padding, length.out = 4)
33
 
@@ -513,39 +557,99 @@ fslteamA <- fslid %>%
513
 
514
  # UI Definition
515
  ui <- fluidPage(
516
- theme = bs_theme(version = 5, bootswatch = "flatly"),
517
- titlePanel("202-2025 Daily MLB/AAA/FSL Summary Cards"),
518
- sidebarLayout(
519
- sidebarPanel(
520
- width = 3,
521
- dateInput("date", "Date:"),
522
- selectizeInput("level", "Level:",
523
- c("MLB", "AAA", "FSL"),
524
- options = list(
525
- placeholder = 'Select a level',
526
- onInitialize = I('function() { this.setValue(""); }')
527
- )),
528
- selectizeInput("homeT", "Home Team:", NULL),
529
- selectizeInput("awayT", "Away Team:", NULL),
530
- selectizeInput("gamenum", "Game Number:", c("1", "2")),
531
- actionButton("update", "Find Pitcher", icon("magnifying-glass"),
532
- class = "btn-primary btn-block"),
533
- selectizeInput("pitcher", "Pitcher Name:", NULL),
534
- actionButton("update1", "Make Card", icon("plus"),
535
- class = "btn-success btn-block"),
536
- downloadButton("downloadPlot", "Download Card", class = "btn-info btn-block")
537
  ),
538
- mainPanel(
539
- div(style = "width: 1100px; height: 1100px; overflow: auto;", # Increased from 1000px
540
- plotOutput("combinedPlot", width = "100%", height = "100%")
541
- ),
542
- tableOutput("boxscoreTable")
543
- )
544
  )
545
  )
546
-
547
  # Server Definition
548
  server <- function(input, output, session) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549
  observeEvent(input$level, {
550
  if(input$level == "AAA"){
551
  updateSelectizeInput(session, "homeT", "Home Team:", choices = aaateamH[,1])
 
28
  model <- xgb.load('TimStuff2.model')
29
 
30
  # Helper functions
31
+ check_patreon_access <- function(email) {
32
+ campaign_id <- Sys.getenv("PATREON_CAMPAIGN_ID")
33
+ access_token <- Sys.getenv("PATREON_ACCESS_TOKEN")
34
+
35
+ base_url <- paste0("https://www.patreon.com/api/oauth2/v2/campaigns/", campaign_id, "/members")
36
+
37
+ params <- list(
38
+ `include` = "currently_entitled_tiers",
39
+ `fields[member]` = "patron_status,email",
40
+ `fields[tier]` = "title"
41
+ )
42
+
43
+ response <- GET(
44
+ base_url,
45
+ query = params,
46
+ add_headers(
47
+ `Authorization` = paste("Bearer", access_token),
48
+ `User-Agent` = "R/httr"
49
+ )
50
+ )
51
+
52
+ content <- fromJSON(rawToChar(response$content))
53
+
54
+ # Check in data$attributes for matching email
55
+ matching_row <- which(content$data$attributes$email == email)
56
+
57
+ if (length(matching_row) > 0) {
58
+ # Get patron status
59
+ patron_status <- content$data$attributes$patron_status[matching_row]
60
+
61
+ if (patron_status == "active_patron") {
62
+ # Get tier info
63
+ tier_data <- content$data$relationships$currently_entitled_tiers$data[[matching_row]]
64
+
65
+ tier_id <- tier_data$id
66
+
67
+ result <- tier_id %in% c("25062087", "25062090")
68
+
69
+ return(result)
70
+ }
71
+ }
72
+
73
+ return(FALSE)
74
+ }
75
  gtable_add_padding <- function(gtable, padding = unit(c(5, 5, 5, 5), "mm")) {
76
  padding <- rep(padding, length.out = 4)
77
 
 
557
 
558
  # UI Definition
559
  ui <- fluidPage(
560
+ theme = bs_theme(bg = "#ffffff", fg = "#333333", primary = "#428bca"),
561
+ tagList(
562
+ tags$head(
563
+ tags$link(href = "https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@400;700&display=swap",
564
+ rel = "stylesheet"),
565
+ tags$style(HTML("
566
+ * { font-family: 'Roboto Condensed', sans-serif !important; }
567
+ .login-screen {
568
+ max-width: 400px;
569
+ margin: 100px auto;
570
+ padding: 20px;
571
+ border: 1px solid #ddd;
572
+ border-radius: 5px;
573
+ box-shadow: 0 0 10px rgba(0,0,0,0.1);
574
+ }
575
+ .login-header {
576
+ text-align: center;
577
+ margin-bottom: 20px;
578
+ }
579
+ "))
 
580
  ),
581
+
582
+ # Use uiOutput to conditionally render either login or main page
583
+ uiOutput("page")
 
 
 
584
  )
585
  )
 
586
  # Server Definition
587
  server <- function(input, output, session) {
588
+ credentials <- reactiveValues(logged_in = FALSE)
589
+
590
+ output$page <- renderUI({
591
+ if (!credentials$logged_in) {
592
+ # Login page
593
+ div(class = "login-screen",
594
+ div(class = "login-header",
595
+ h2("Baseball Stats Visualization"),
596
+ p("Please log in with your Patreon email")
597
+ ),
598
+ textInput("email", "Email:"),
599
+ actionButton("login", "Login", class = "btn-primary"),
600
+ p(style = "margin-top: 20px; text-align: center;",
601
+ "Access requires Veteran or Hall of Fame tier on Patreon")
602
+ )
603
+ } else {
604
+ # Main application UI (your existing UI)
605
+ fluidPage(
606
+ theme = bs_theme(version = 5, bootswatch = "flatly"),
607
+ titlePanel("202-2025 Daily MLB/AAA/FSL Summary Cards"),
608
+ sidebarLayout(
609
+ sidebarPanel(
610
+ width = 3,
611
+ dateInput("date", "Date:"),
612
+ selectizeInput("level", "Level:",
613
+ c("MLB", "AAA", "FSL"),
614
+ options = list(
615
+ placeholder = 'Select a level',
616
+ onInitialize = I('function() { this.setValue(""); }')
617
+ )),
618
+ selectizeInput("homeT", "Home Team:", NULL),
619
+ selectizeInput("awayT", "Away Team:", NULL),
620
+ selectizeInput("gamenum", "Game Number:", c("1", "2")),
621
+ actionButton("update", "Find Pitcher", icon("magnifying-glass"),
622
+ class = "btn-primary btn-block"),
623
+ selectizeInput("pitcher", "Pitcher Name:", NULL),
624
+ actionButton("update1", "Make Card", icon("plus"),
625
+ class = "btn-success btn-block"),
626
+ downloadButton("downloadPlot", "Download Card", class = "btn-info btn-block")
627
+ ),
628
+ mainPanel(
629
+ div(style = "width: 1100px; height: 1100px; overflow: auto;", # Increased from 1000px
630
+ plotOutput("combinedPlot", width = "100%", height = "100%")
631
+ ),
632
+ tableOutput("boxscoreTable")
633
+ )
634
+ )
635
+ )
636
+ }
637
+ })
638
+ observeEvent(input$login, {
639
+
640
+ # Check Patreon access
641
+ has_access <- check_patreon_access(input$email)
642
+
643
+ if (has_access) {
644
+ credentials$logged_in <- TRUE
645
+ } else {
646
+ showModal(modalDialog(
647
+ title = "Access Denied",
648
+ "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.",
649
+ easyClose = TRUE
650
+ ))
651
+ }
652
+ })
653
  observeEvent(input$level, {
654
  if(input$level == "AAA"){
655
  updateSelectizeInput(session, "homeT", "Home Team:", choices = aaateamH[,1])