Advertisement
IcaroPeretti

teste

May 1st, 2022
3,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 20.88 KB | None | 0 0
  1. #lang racket
  2.  
  3.  
  4. (define quartasChaveamento null)
  5. (define indexUsados '())
  6. (define jogosQuartasFinal '() )
  7. (define jogosSemiFinal '() )
  8. (define vencedoresQuartas '() )
  9.  
  10. (define vencedoresSemi '() )
  11. (define perdedoresSemi '() )
  12.  
  13. (define jogoFinal '() )
  14. (define jogoTerceiroLugar '() )
  15.  
  16. (define jaGerou #f)
  17.  
  18. (define (randomInt)
  19.   (random 7)
  20.   )
  21.  
  22. (define (retornaIndexSeNaoExisteLista tentativa usados times)
  23.   (cond
  24.     [
  25.      ; Cláusula
  26.      (not(equal? (set-member? (list->set usados) tentativa) #t))
  27.      ; Procedimento
  28.      tentativa
  29.      ]
  30.     [else (
  31.            
  32.            retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) usados times
  33.                                         )
  34.           ]
  35.     )
  36.   )
  37.  
  38. (define (pegarIndexAleatorioLista lista)
  39.   (random (+ (length (rest lista)) 1))
  40.   )
  41.  
  42. ;(retornaIndexSeNExisitr ()
  43.  
  44. (define (quartas times renderHTML fallbackHTML)
  45.   (if (equal? jaGerou #t) (renderHTML) (
  46.                                         (lambda()
  47.                                           (set! jogosQuartasFinal '())
  48.                                           (set! indexUsados '())
  49.  
  50.                                           (for([i '(1 2 3 4)])
  51.                                             ; Cria uma variável local e pega um indice do array de times que não tenha sido colocado no indexUsados
  52.                                             (let
  53.                                                 (
  54.                                                  (indexTime1
  55.                                                   (retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) indexUsados times)
  56.                                                   )
  57.                                                  )
  58.                                               (
  59.                                                ; Execução do escopo do let indexTime1
  60.                                                ; Pega um índice que não tenha sido gerando anteriormente e que não esteja no array de indexUsados
  61.                                                let
  62.                                                   (
  63.                                                    (indexTime2
  64.                                                     (retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) (cons indexTime1 indexUsados) times)
  65.                                                     )
  66.                                                    )
  67.                                                 (
  68.                                                  ; Seta o array de index usados com os valores do time1 e 2 gerados agora, para que na prox vez não venha a ser sorteado
  69.                                                  set! indexUsados (cons indexTime1 (cons indexTime2 indexUsados))
  70.                                                       )
  71.                
  72.                                                 ; Adicionando jogo no array das quartas de final
  73.                                                 (set! jogosQuartasFinal (cons (list (list-ref times indexTime1) (list-ref times indexTime2) 0 0) jogosQuartasFinal))
  74.                                                 ;(fallbackHTML)
  75.                                                 ;(if (equal? i 4)
  76.                                                 ;    renderHTML
  77.                                                 ;    fallbackHTML
  78.                                                 ;    )
  79.                                                 )
  80.                                               )
  81.                                             ;(display (~a (retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) indexUsados)))
  82.                                             ;(display (~a (pegarIndexAleatorioLista times)))
  83.                                             ;(set! indexUsados (cons i indexUsados))
  84.  
  85.                                             )
  86.                                           (set! jaGerou #t)
  87.                                           (renderHTML)
  88.                                           )
  89.                              
  90.                                         ))
  91.   )
  92.  
  93. (define (alterarQuantidadeGolsChaveamentoQuartas jogo time gols)
  94.  
  95.   (
  96.    let
  97.       (
  98.        (jogoLista
  99.         (list-ref jogosQuartasFinal jogo)
  100.         )
  101.        )
  102.     ;(display (~a jogoLista))
  103.     (set! jogoLista (list-set jogoLista (+ time 2) gols))
  104.     ;(display (~a jogoLista))
  105.     ;(define jogo (list-ref jogosQuartasFinal jogo) )
  106.     ;(display (~a (list-ref jogosQuartasFinal jogo)))
  107.     ;set! jogo (jogo (+ time 2) gols)
  108.     (set! jogosQuartasFinal (list-set jogosQuartasFinal jogo jogoLista))
  109.     )
  110.  
  111.   )
  112.  
  113. (define (pegarVencedoresJogosQuartas)
  114.   (for ([ jogo jogosQuartasFinal ])
  115.     (set! vencedoresQuartas (cons
  116.                              (if (> (list-ref jogo 2) (list-ref jogo 3))
  117.                                  (list-ref jogo 0)
  118.                                  (list-ref jogo 1)
  119.                                  )
  120.                              vencedoresQuartas))
  121.     )
  122.   )
  123.  
  124. (define (pegarVencedoresJogosSemi)
  125.   (for ([ jogo jogosSemiFinal ])
  126.     ;(display (~a
  127.     ;          (if (> (list-ref jogo 2) (list-ref jogo 3))
  128.     ;              (list-ref jogo 0)
  129.     ;              (list-ref jogo 1)
  130.     ;              )
  131.     ;          ))
  132.     (set! vencedoresSemi (cons
  133.                           (if (> (list-ref jogo 2) (list-ref jogo 3))
  134.                               (list-ref jogo 0)
  135.                               (list-ref jogo 1)
  136.                               )
  137.                           vencedoresSemi))
  138.     (set! perdedoresSemi (cons
  139.                           (if (> (list-ref jogo 2) (list-ref jogo 3))
  140.                               (list-ref jogo 1)
  141.                               (list-ref jogo 0)
  142.                               )
  143.                           perdedoresSemi))
  144.     )
  145.   )
  146.  
  147. (require web-server/servlet
  148.          web-server/servlet-env)
  149. (require net/uri-codec)
  150.  
  151. ;mysql
  152. ;;; (require db)
  153. ;;; (define pgc
  154. ;;; (mysql-connect #:user "root"  
  155. ;;;            #:password "root"    
  156. ;;;            #:database "worldcup"
  157. ;;;            #:server "localhost"  )
  158. ;;;  )
  159.  
  160. (require db)
  161. (define SQLite
  162.   (sqlite3-connect #:database "db/worldcup.db"
  163.                    #:mode 'create) ;permite escrita/leitura do banco
  164.   )
  165.  
  166. (define teams (query-list SQLite "select name from teams"))
  167.  
  168. ;show teams in html paragraph
  169.  
  170.  
  171.  
  172.  
  173. (define (world-cup req)
  174.  
  175.   (define uri(request-uri req))
  176.  
  177.   (define path (map path/param-path (url-path uri)))
  178.  
  179.   (define page (car path ))
  180.  
  181.   (cond
  182.     [(equal? page "index") ; pagina 1
  183.  
  184.     (define numOfTeams (query-list SQLite "select count(id) from teams"))
  185.  
  186.     (define getNumOfTeams (car numOfTeams))
  187.  
  188.      (response/xexpr
  189.       `(html
  190.         (head (title "Simulação Copa do Mundo - Icaro e Eduardo"))
  191.         (body (center(h1  "Simulação Copa do Mundo")
  192.                      (center(h3 "Programação Lógica e Funcional"))
  193.                      (style ([type "text/css"])
  194.                             "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center}")
  195.  
  196.                      (p "Alunos: Ícaro Peretti e Eduardo Rampon Meireles")
  197.                      (center(h2 "Cadastro de times"))
  198.                       (p "Quantidade de times: ", (number->string getNumOfTeams))
  199.                             (form ([method "POST"] [action "/register-team"])
  200.                                 "Nome do time: " (input ([type "text"] [name "nome"]))
  201.                                   (input ([type "submit"] [value "Cadastrar"]))
  202.                                   )  
  203.                                       (cond (
  204.                                         (equal? (length teams ) 8)
  205.                                           (center(h2 "Times cadastrados"))
  206.                                           (center(p "", (car teams )))
  207.                                           (center(p "", (cadr teams )))
  208.                                           (center(p "", (caddr teams )))
  209.                                           (center(p "", (cadddr teams ))))
  210.                                         (else
  211.                                             (p "Erro")
  212.                                         )
  213.                                       )
  214.                                       ))))]
  215.  
  216.     [(equal? page "register-team")
  217.  
  218.      (define post-data (bytes->string/utf-8 (request-post-data/raw req)))
  219.      
  220.      ; convert to an alist:
  221.      (define form-data (form-urlencoded->alist post-data))
  222.  
  223.      ; pull out the team name:
  224.      (define teamName  (cdr (assq 'nome form-data)))
  225.      
  226.     ;check if team already exists
  227.     ;;; (define teamExists (query-list SQLite "select name from teams where name LIKE $1") teamName)
  228.    
  229.     ;Se a quantidade de times for menor que 8 insere
  230.  
  231.  
  232.      
  233.       (query-exec SQLite
  234.                  "insert into teams (name) values (?)" teamName)
  235.    
  236.  
  237.      (response/xexpr
  238.       `(html
  239.         (body
  240.          (center(p "Time registrado: " ,teamName))
  241.          (style ([type "text/css"])
  242.                 "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
  243.                 "a {color: #008080;}")
  244.          (center(a  ((href "/index")) "Voltar"))
  245.          )))]
  246.  
  247.     [(equal? page "list-teams")
  248.      
  249.      (quartas (take teams 8)
  250.               (lambda()(response/xexpr
  251.                         `(html
  252.                           (body
  253.                            (p "Partida 1: " , (list-ref (list-ref jogosQuartasFinal 0) 0), " X ", (list-ref (list-ref jogosQuartasFinal 0) 1))
  254.                            (style ([type "text/css"])
  255.                                   "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;flex-direction:column;align-items:center}")
  256.                            (img ([src "./img.jpg"]))
  257.                            (form ([method "POST"] [action "/save-goals"])
  258.                                  ;Pegar os times
  259.                                  (input ([type "hidden"] [name "jogo"] [value , "0"]))
  260.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 0) 0)]))
  261.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 0) 1)]))
  262.                            
  263.                                  ;Form para obter os gols
  264.                                  "Gols time 1:"   (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 0) 2))]))
  265.                                  (br)
  266.                                  "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 0) 3))]))
  267.                                  (br)
  268.                                  (input ([type "submit"] [value "Salvar"]))
  269.                                  )
  270.  
  271.                            (form ([method "POST"] [action "/random-goals"])
  272.                            
  273.                                  (input ([type "hidden"] [name "jogo"] [value , "0"]))
  274.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 0) 0)]))
  275.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 0) 1)]))
  276.                                
  277.                          
  278.                                  (input ([type "submit"] [value "Gerar aleatório"]))
  279.                                  )
  280.                            
  281.                            (p "Partida 2: ", (list-ref (list-ref jogosQuartasFinal 1) 0), " X ", (list-ref (list-ref jogosQuartasFinal 1) 1))
  282.                            (form ([method "POST"] [action "/save-goals"])
  283.                                  ;Pegar os times
  284.                                  (input ([type "hidden"] [name "jogo"] [value , "1"]))
  285.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 1) 0)]))
  286.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 1) 1)]))
  287.                            
  288.                                  ;Form para obter os gols
  289.                                  "Gols time 1:"   (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 1) 2))]))
  290.                                  (br)
  291.                                  "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 1) 3))]))
  292.                                  (br)
  293.                                  (input ([type "submit"] [value "Salvar"]))
  294.                                  )
  295.  
  296.                            (form ([method "POST"] [action "/random-goals"])
  297.                            
  298.                                  (input ([type "hidden"] [name "jogo"] [value , "1"]))
  299.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 1) 0)]))
  300.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 1) 1)]))
  301.                                
  302.                          
  303.                                  (input ([type "submit"] [value "Gerar aleatório"]))
  304.                                  )
  305.                            
  306.                            
  307.                            (p "Partida 3: ", (list-ref (list-ref jogosQuartasFinal 2) 0), " X ", (list-ref (list-ref jogosQuartasFinal 2) 1))
  308.                            (form ([method "POST"] [action "/save-goals"])
  309.                                  ;Pegar os times
  310.                                  (input ([type "hidden"] [name "jogo"] [value , "2"]))
  311.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 2) 0)]))
  312.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 2) 1)]))
  313.                            
  314.                                  ;Form para obter os gols
  315.                                  "Gols time 1:"   (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 2) 2))]))
  316.                                  (br)
  317.                                  "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 2) 3))]))
  318.                                  (br)
  319.                                  (input ([type "submit"] [value "Salvar"]))
  320.                                  )
  321.  
  322.                            (form ([method "POST"] [action "/random-goals"])
  323.                            
  324.                                  (input ([type "hidden"] [name "jogo"] [value , "2"]))
  325.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 2) 0)]))
  326.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 2) 1)]))
  327.                                
  328.                          
  329.                                  (input ([type "submit"] [value "Gerar aleatório"]))
  330.                                  )
  331.                            
  332.                            (p "Partida 4: ", (list-ref (list-ref jogosQuartasFinal 3) 0), " X ", (list-ref (list-ref jogosQuartasFinal 3) 1))
  333.                            (form ([method "POST"] [action "/save-goals"])
  334.                                  ;Pegar os times
  335.                                  (input ([type "hidden"] [name "jogo"] [value , "3"]))
  336.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 3) 0)]))
  337.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 3) 1)]))
  338.                            
  339.                                  ;Form para obter os gols
  340.                                  "Gols time 1:"   (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 3) 2))]))
  341.                                  (br)
  342.                                  "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 3) 3))]))
  343.                                  (br)
  344.                                  (input ([type "submit"] [value "Salvar"]))
  345.                                  )
  346.  
  347.                            (form ([method "POST"] [action "/random-goals"])
  348.                            
  349.                                  (input ([type "hidden"] [name "jogo"] [value , "3"]))
  350.                                  (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 3) 0)]))
  351.                                  (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 3) 1)]))
  352.                                
  353.                          
  354.                                  (input ([type "submit"] [value "Gerar aleatório"]))
  355.                                  )    
  356.          
  357.                            )
  358.                           )))
  359.               (lambda()(
  360.                         response/xexpr
  361.                         `(html
  362.                           (body
  363.                            (p "Partida 1: ")
  364.                            )
  365.                           )
  366.                         ))
  367.  
  368.               )    
  369.      
  370.      ]
  371.  
  372.     [(equal? page "save-goals")
  373.  
  374.      (define post-data (bytes->string/utf-8 (request-post-data/raw req)))
  375.      
  376.      ; convert to an alist:
  377.      (define form-data (form-urlencoded->alist post-data))
  378.  
  379.      ; pull out the team name:
  380.      (define jogo  (cdr (assq 'jogo form-data)))
  381.      
  382.      (define nomeTime1  (cdr (assq 'nomeTime1 form-data)))
  383.      (define goal1  (cdr (assq 'goals1 form-data)))
  384.  
  385.      (define nomeTime2  (cdr (assq 'nomeTime2 form-data)))
  386.      (define goal2  (cdr (assq 'goals2 form-data)))
  387.  
  388.      
  389.  
  390.      (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 0 (string->number goal1))
  391.      (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 1 (string->number goal2))
  392.  
  393.      
  394.  
  395.      ; insert to database:
  396.      (query-exec SQLite
  397.                  "insert into matchs (team1,team2,goals_team1,goals_team2,turn)  VALUES (?,?,?,?,?)" nomeTime1 nomeTime2 goal1 goal2 1)
  398.  
  399.  
  400.      (response/xexpr
  401.       `(html
  402.         (body
  403.          (center(p "Gols registrados: ", nomeTime1, " " ,goal1, " x ", nomeTime2, " ", goal2))
  404.          (style ([type "text/css"])
  405.                 "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
  406.                 "a {color: #008080;}")
  407.          (center(a  ((href "/list-teams")) "Voltar"))
  408.          )))]
  409.  
  410.     [(equal? page "random-goals")
  411.  
  412.      (define post-data (bytes->string/utf-8 (request-post-data/raw req)))
  413.      
  414.      ; convert to an alist:
  415.      (define form-data (form-urlencoded->alist post-data))
  416.  
  417.      (define jogo (cdr (assq 'jogo form-data)))
  418.      
  419.      (define nomeTime1  (cdr (assq 'nomeTime1 form-data)))
  420.      (define goal1 (randomInt))
  421.  
  422.      (define nomeTime2  (cdr (assq 'nomeTime2 form-data)))
  423.      (define goal2 (randomInt))
  424.  
  425.      (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 0 goal1)
  426.      (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 1 goal2)
  427.  
  428.      ; insert to database:
  429.      ;(query-exec SQLite
  430.       ;           "insert into matchs (team1,team2,goals_team1,goals_team2,turn)  VALUES (?,?,?,?,?)" idTime1 idTime2 (random-int 1 5) (random-int 1 5) 1)
  431.  
  432.  
  433.      (response/xexpr
  434.       `(html
  435.         (body
  436.          (center(p "Gols registrados: ", nomeTime1, " " ,(number->string goal1), " x ", nomeTime2, " ", (number->string goal2)))
  437.          (style ([type "text/css"])
  438.                 "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
  439.                 "a {color: #008080;}")
  440.          (center(a  ((href "/list-teams")) "Voltar"))
  441.          )))
  442.      ]
  443.  
  444.     [else
  445.      (response/xexpr
  446.       `(html
  447.         (body
  448.          (center(h1 "404"))
  449.          (style ([type "text/css"])
  450.                 "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
  451.                 "a {color: #008080; text-decoration: none;}")
  452.          (center(p "Página não encontrada!"))
  453.          (center(a ((href "/index")) "Voltar"))
  454.          )))]
  455.                        
  456.            
  457.     )
  458.            
  459.   )
  460.  
  461.  
  462.  
  463. (serve/servlet world-cup
  464.                #:servlet-path "/index"
  465.                #:port 8001 ;porta que a pagina ira rodar
  466.                #:servlet-regexp #rx""
  467.                )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement