-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
484 lines (410 loc) · 13.5 KB
/
app.R
File metadata and controls
484 lines (410 loc) · 13.5 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# app.R - Setback (Pitch) Card Game
# A classic 4-point trick-taking card game with family cookout AI personas
library(shiny)
library(bslib)
library(htmltools)
# Source game modules
source("R/game_engine.R")
source("R/bidding.R")
source("R/scoring.R")
source("R/game_state.R")
source("R/ai_strategy.R")
source("R/ai_personas.R")
source("R/ui_helpers.R")
# All state field names - used to sync state lists back to reactiveValues.
# R removes keys when you set list$field <- NULL, so names(state) misses them.
# This ensures NULL fields are explicitly written to gs (reactiveValues).
STATE_FIELDS <- names(new_game_state())
# ============================================================================
# UI
# ============================================================================
ui <- page_fluid(
theme = bs_theme(
bg = "#0d1117",
fg = "#e6e6e6",
primary = "#238636",
base_font = font_google("Inter"),
heading_font = font_google("Inter")
),
tags$head(
tags$link(rel = "stylesheet", href = "cards.css"),
tags$link(rel = "stylesheet", href = "game.css"),
tags$script(src = "game.js")
),
tags$div(class = "game-container",
# Header / Scoreboard
uiOutput("scoreboard"),
# Main layout: table + sidebar
tags$div(class = "game-layout",
# Left: Game table + actions
tags$div(class = "game-table-wrapper",
tags$div(class = "game-table",
# North (Partner - Auntie Dee)
tags$div(class = "player-area north",
uiOutput("north_label"),
uiOutput("north_hand"),
uiOutput("north_bubble")
),
# West (Opponent - Uncle Earl)
tags$div(class = "player-area west",
uiOutput("west_label"),
uiOutput("west_hand"),
uiOutput("west_bubble")
),
# Center trick area
tags$div(class = "trick-container",
uiOutput("trick_area"),
uiOutput("status_message")
),
# East (Opponent - Cousin Ray)
tags$div(class = "player-area east",
uiOutput("east_label"),
uiOutput("east_hand"),
uiOutput("east_bubble")
),
# South (You)
tags$div(class = "player-area south",
uiOutput("south_label"),
uiOutput("player_hand"),
uiOutput("bid_controls")
)
),
# Game actions (below table)
uiOutput("game_actions")
),
# Right sidebar: scoring breakdown + game log
tags$div(class = "game-sidebar",
uiOutput("scoring_breakdown"),
tags$div(class = "sidebar-label", "Game Log"),
uiOutput("game_log")
)
)
)
)
# ============================================================================
# SERVER
# ============================================================================
server <- function(input, output, session) {
# --- Reactive Game State ---
gs <- reactiveValues()
observe({
state <- new_game_state()
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
}, priority = 100)
# Speech bubbles: list(p2 = text, p3 = text, p4 = text)
bubbles <- reactiveValues(p2 = NULL, p3 = NULL, p4 = NULL)
# --- Helper: trigger AI turn ---
trigger_ai <- function(delay = 1000) {
session$sendCustomMessage('ai_delay', list(delay = delay))
}
# --- Helper: set speech bubble ---
set_bubble <- function(player, text) {
key <- paste0("p", player)
bubbles[[key]] <- text
}
clear_bubbles <- function() {
bubbles$p2 <- NULL
bubbles$p3 <- NULL
bubbles$p4 <- NULL
}
# --- Helper: check if human's turn ---
is_human_turn <- function() {
gs$current_player == 1L && gs$phase %in% c("bidding", "pitching", "playing")
}
# ========================================================================
# GAME FLOW
# ========================================================================
# --- Start / New Game ---
observeEvent(input$start_game, {
state <- new_game_state()
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
clear_bubbles()
# Deal first hand
state <- start_new_hand(isolate(reactiveValuesToList(gs)))
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
# Check if AI bids first
if (gs$current_player != 1L) {
trigger_ai(1500)
}
})
# --- Next Hand ---
observeEvent(input$next_hand, {
clear_bubbles()
state <- start_new_hand(isolate(reactiveValuesToList(gs)))
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
if (gs$current_player != 1L) {
trigger_ai(1500)
}
})
# --- Human Bid ---
observeEvent(input$human_bid, {
req(gs$phase == "bidding", gs$current_player == 1L)
bid_value <- input$human_bid
state <- isolate(reactiveValuesToList(gs))
state <- process_bid(state, player = 1L, bid_value = bid_value)
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
# If bidding continues and next is AI
if (gs$phase == "bidding" && gs$current_player != 1L) {
trigger_ai(1500)
}
# If bidding complete and pitcher is AI, trigger pitch
if (gs$phase == "pitching" && gs$current_player != 1L) {
trigger_ai(1800)
}
})
# --- Human Card Play ---
observeEvent(input$card_played, {
req(gs$phase %in% c("pitching", "playing"), gs$current_player == 1L)
card_id <- input$card_played
state <- isolate(reactiveValuesToList(gs))
state <- process_play(state, player = 1L, card_id = card_id)
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
if (gs$phase == "trick_done") {
session$sendCustomMessage('trick_pause', list(delay = 4000))
} else if (gs$current_player != 1L) {
trigger_ai(1200)
}
})
# --- AI Turn ---
observeEvent(input$ai_turn_ready, {
player <- gs$current_player
req(player != 1L)
state <- isolate(reactiveValuesToList(gs))
if (gs$phase == "bidding") {
# AI bid
bid <- ai_decide_bid(state$hands[[paste0("p", player)]], state$bid_state, player)
dialogue <- get_bid_dialogue(player, bid)
set_bubble(player, dialogue)
state <- process_bid(state, player, bid)
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
# Continue bidding
if (gs$phase == "bidding" && gs$current_player != 1L) {
trigger_ai(1500)
} else if (gs$phase == "pitching" && gs$current_player != 1L) {
trigger_ai(1800)
}
} else if (gs$phase == "pitching") {
# AI pitches
card_id <- ai_choose_pitch(state$hands[[paste0("p", player)]], player)
state <- process_play(state, player, card_id)
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
# After pitch, next player plays (or trick continues)
if (gs$phase == "trick_done") {
session$sendCustomMessage('trick_pause', list(delay = 4000))
} else if (gs$current_player != 1L) {
trigger_ai(1200)
}
} else if (gs$phase == "playing") {
# AI plays a card
hand <- state$hands[[paste0("p", player)]]
card_id <- ai_select_card(hand, state, player)
state <- process_play(state, player, card_id)
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
if (gs$phase == "trick_done") {
session$sendCustomMessage('trick_pause', list(delay = 4000))
} else if (gs$current_player != 1L) {
trigger_ai(1200)
}
}
})
# --- Trick Collected ---
observeEvent(input$trick_collected, {
req(gs$phase == "trick_done")
state <- isolate(reactiveValuesToList(gs))
state <- resolve_completed_trick(state)
# Get trick dialogue from personas
winner <- state$last_trick_winner
if (!is.null(winner)) {
for (p in c(2, 3, 4)) {
dialogue <- get_trick_dialogue(p, winner)
set_bubble(p, dialogue)
}
}
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
if (gs$phase == "scoring") {
# Score the hand
state <- isolate(reactiveValuesToList(gs))
state <- score_hand(state)
# Scoring dialogue
if (!is.null(state$pitcher)) {
pitcher_team <- get_team(state$pitcher)
made_bid <- state$scores[[pitcher_team]] >=
(isolate(gs$scores[[pitcher_team]])) # Compare before/after
for (p in c(2, 3, 4)) {
dialogue <- get_scoring_dialogue(p, state$pitcher,
!any(grepl("SET BACK", tail(state$game_log, 3))))
if (!is.null(dialogue)) set_bubble(p, dialogue)
}
}
for (n in STATE_FIELDS) {
gs[[n]] <- state[[n]]
}
session$sendCustomMessage('scroll_log', list())
} else if (gs$current_player != 1L) {
trigger_ai(1200)
}
})
# ========================================================================
# UI OUTPUTS
# ========================================================================
# --- Scoreboard ---
output$scoreboard <- renderUI({
render_scoreboard(
scores = list(team1 = gs$scores$team1, team2 = gs$scores$team2),
current_bid = gs$current_bid,
pitcher = gs$pitcher,
claude_suit = gs$claude_suit,
hand_number = gs$hand_number
)
})
# --- Player Labels ---
output$north_label <- renderUI({
render_player_label(3, gs$current_player == 3L,
if (!is.null(gs$hands$p3)) nrow(gs$hands$p3) else 0)
})
output$west_label <- renderUI({
render_player_label(2, gs$current_player == 2L,
if (!is.null(gs$hands$p2)) nrow(gs$hands$p2) else 0)
})
output$east_label <- renderUI({
render_player_label(4, gs$current_player == 4L,
if (!is.null(gs$hands$p4)) nrow(gs$hands$p4) else 0)
})
output$south_label <- renderUI({
render_player_label(1, gs$current_player == 1L,
if (!is.null(gs$hands$p1)) nrow(gs$hands$p1) else 0)
})
# --- Opponent Hands (face down) ---
output$north_hand <- renderUI({
n <- if (!is.null(gs$hands$p3)) nrow(gs$hands$p3) else 0
render_opponent_hand(n)
})
output$west_hand <- renderUI({
n <- if (!is.null(gs$hands$p2)) nrow(gs$hands$p2) else 0
render_opponent_hand(n, sideways = TRUE)
})
output$east_hand <- renderUI({
n <- if (!is.null(gs$hands$p4)) nrow(gs$hands$p4) else 0
render_opponent_hand(n, sideways = TRUE)
})
# --- Player Hand (face up, clickable during play) ---
output$player_hand <- renderUI({
hand <- gs$hands$p1
if (is.null(hand) || nrow(hand) == 0) return(NULL)
can_play <- isTRUE(gs$current_player == 1L) && gs$phase %in% c("pitching", "playing")
if (can_play) {
legal <- get_legal_plays(hand, gs$lead_suit, gs$claude_suit)
render_hand(hand, clickable = TRUE, legal_ids = legal$card_id)
} else {
render_hand(hand, clickable = FALSE)
}
})
# --- Trick Area ---
output$trick_area <- renderUI({
render_trick_area(gs$trick_cards)
})
# --- Bid Controls ---
output$bid_controls <- renderUI({
req(gs$phase == "bidding", gs$current_player == 1L)
render_bid_controls(gs$bid_state, player = 1L)
})
# --- Speech Bubbles ---
output$west_bubble <- renderUI({
render_speech_bubble(2, bubbles$p2)
})
output$north_bubble <- renderUI({
render_speech_bubble(3, bubbles$p3)
})
output$east_bubble <- renderUI({
render_speech_bubble(4, bubbles$p4)
})
# --- Status Message ---
output$status_message <- renderUI({
if (gs$phase == "pre_game") {
return(tags$div(class = "status-message", "Press 'Deal' to start!"))
}
if (gs$phase == "trick_done" && !is.null(gs$trick_explanation)) {
return(tags$div(class = "status-message", gs$trick_explanation))
}
if (gs$phase == "bidding" && !isTRUE(gs$current_player == 1L)) {
return(tags$div(class = "status-message",
paste(PLAYER_NAMES[gs$current_player], "is thinking...")))
}
if (gs$phase == "pitching" && isTRUE(gs$current_player == 1L)) {
return(tags$div(class = "status-message",
"You won the bid! Play a card to set the suit."))
}
if (gs$phase == "playing" && isTRUE(gs$current_player == 1L)) {
return(tags$div(class = "status-message", "Your turn - play a card."))
}
if (gs$phase == "game_over") {
winner_name <- if (gs$winner == "team1") "Your Team" else "Opponents"
return(tags$div(class = "status-message",
paste(winner_name, "wins the game!")))
}
NULL
})
# --- Game Actions ---
output$game_actions <- renderUI({
if (gs$phase == "pre_game") {
return(tags$div(class = "game-actions",
tags$button(
class = "action-btn primary",
onclick = "Shiny.setInputValue('start_game', Math.random(), {priority: 'event'})",
"Deal"
)
))
}
if (gs$phase == "hand_complete") {
return(tags$div(class = "game-actions",
tags$button(
class = "action-btn primary",
onclick = "Shiny.setInputValue('next_hand', Math.random(), {priority: 'event'})",
"Next Hand"
)
))
}
if (gs$phase == "game_over") {
return(tags$div(class = "game-actions",
tags$button(
class = "action-btn primary",
onclick = "Shiny.setInputValue('start_game', Math.random(), {priority: 'event'})",
"New Game"
)
))
}
NULL
})
# --- Scoring Breakdown (sidebar) ---
output$scoring_breakdown <- renderUI({
req(gs$phase %in% c("hand_complete", "game_over", "scoring"))
req(!is.null(gs$last_hand_points))
render_scoring_breakdown(gs$last_hand_points, gs$last_scoring_result)
})
# --- Game Log ---
output$game_log <- renderUI({
render_game_log(gs$game_log)
})
}
# Run the app
shinyApp(ui = ui, server = server)