Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (define quartasChaveamento null)
- (define indexUsados '())
- (define jogosQuartasFinal '() )
- (define jogosSemiFinal '() )
- (define vencedoresQuartas '() )
- (define vencedoresSemi '() )
- (define perdedoresSemi '() )
- (define jogoFinal '() )
- (define jogoTerceiroLugar '() )
- (define jaGerou #f)
- (define (randomInt)
- (random 7)
- )
- (define (retornaIndexSeNaoExisteLista tentativa usados times)
- (cond
- [
- ; Cláusula
- (not(equal? (set-member? (list->set usados) tentativa) #t))
- ; Procedimento
- tentativa
- ]
- [else (
- retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) usados times
- )
- ]
- )
- )
- (define (pegarIndexAleatorioLista lista)
- (random (+ (length (rest lista)) 1))
- )
- ;(retornaIndexSeNExisitr ()
- (define (quartas times renderHTML fallbackHTML)
- (if (equal? jaGerou #t) (renderHTML) (
- (lambda()
- (set! jogosQuartasFinal '())
- (set! indexUsados '())
- (for([i '(1 2 3 4)])
- ; Cria uma variável local e pega um indice do array de times que não tenha sido colocado no indexUsados
- (let
- (
- (indexTime1
- (retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) indexUsados times)
- )
- )
- (
- ; Execução do escopo do let indexTime1
- ; Pega um índice que não tenha sido gerando anteriormente e que não esteja no array de indexUsados
- let
- (
- (indexTime2
- (retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) (cons indexTime1 indexUsados) times)
- )
- )
- (
- ; 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
- set! indexUsados (cons indexTime1 (cons indexTime2 indexUsados))
- )
- ; Adicionando jogo no array das quartas de final
- (set! jogosQuartasFinal (cons (list (list-ref times indexTime1) (list-ref times indexTime2) 0 0) jogosQuartasFinal))
- ;(fallbackHTML)
- ;(if (equal? i 4)
- ; renderHTML
- ; fallbackHTML
- ; )
- )
- )
- ;(display (~a (retornaIndexSeNaoExisteLista (pegarIndexAleatorioLista times) indexUsados)))
- ;(display (~a (pegarIndexAleatorioLista times)))
- ;(set! indexUsados (cons i indexUsados))
- )
- (set! jaGerou #t)
- (renderHTML)
- )
- ))
- )
- (define (alterarQuantidadeGolsChaveamentoQuartas jogo time gols)
- (
- let
- (
- (jogoLista
- (list-ref jogosQuartasFinal jogo)
- )
- )
- ;(display (~a jogoLista))
- (set! jogoLista (list-set jogoLista (+ time 2) gols))
- ;(display (~a jogoLista))
- ;(define jogo (list-ref jogosQuartasFinal jogo) )
- ;(display (~a (list-ref jogosQuartasFinal jogo)))
- ;set! jogo (jogo (+ time 2) gols)
- (set! jogosQuartasFinal (list-set jogosQuartasFinal jogo jogoLista))
- )
- )
- (define (pegarVencedoresJogosQuartas)
- (for ([ jogo jogosQuartasFinal ])
- (set! vencedoresQuartas (cons
- (if (> (list-ref jogo 2) (list-ref jogo 3))
- (list-ref jogo 0)
- (list-ref jogo 1)
- )
- vencedoresQuartas))
- )
- )
- (define (pegarVencedoresJogosSemi)
- (for ([ jogo jogosSemiFinal ])
- ;(display (~a
- ; (if (> (list-ref jogo 2) (list-ref jogo 3))
- ; (list-ref jogo 0)
- ; (list-ref jogo 1)
- ; )
- ; ))
- (set! vencedoresSemi (cons
- (if (> (list-ref jogo 2) (list-ref jogo 3))
- (list-ref jogo 0)
- (list-ref jogo 1)
- )
- vencedoresSemi))
- (set! perdedoresSemi (cons
- (if (> (list-ref jogo 2) (list-ref jogo 3))
- (list-ref jogo 1)
- (list-ref jogo 0)
- )
- perdedoresSemi))
- )
- )
- (require web-server/servlet
- web-server/servlet-env)
- (require net/uri-codec)
- ;mysql
- ;;; (require db)
- ;;; (define pgc
- ;;; (mysql-connect #:user "root"
- ;;; #:password "root"
- ;;; #:database "worldcup"
- ;;; #:server "localhost" )
- ;;; )
- (require db)
- (define SQLite
- (sqlite3-connect #:database "db/worldcup.db"
- #:mode 'create) ;permite escrita/leitura do banco
- )
- (define teams (query-list SQLite "select name from teams"))
- ;show teams in html paragraph
- (define (world-cup req)
- (define uri(request-uri req))
- (define path (map path/param-path (url-path uri)))
- (define page (car path ))
- (cond
- [(equal? page "index") ; pagina 1
- (define numOfTeams (query-list SQLite "select count(id) from teams"))
- (define getNumOfTeams (car numOfTeams))
- (response/xexpr
- `(html
- (head (title "Simulação Copa do Mundo - Icaro e Eduardo"))
- (body (center(h1 "Simulação Copa do Mundo")
- (center(h3 "Programação Lógica e Funcional"))
- (style ([type "text/css"])
- "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center}")
- (p "Alunos: Ícaro Peretti e Eduardo Rampon Meireles")
- (center(h2 "Cadastro de times"))
- (p "Quantidade de times: ", (number->string getNumOfTeams))
- (form ([method "POST"] [action "/register-team"])
- "Nome do time: " (input ([type "text"] [name "nome"]))
- (input ([type "submit"] [value "Cadastrar"]))
- )
- (cond (
- (equal? (length teams ) 8)
- (center(h2 "Times cadastrados"))
- (center(p "", (car teams )))
- (center(p "", (cadr teams )))
- (center(p "", (caddr teams )))
- (center(p "", (cadddr teams ))))
- (else
- (p "Erro")
- )
- )
- ))))]
- [(equal? page "register-team")
- (define post-data (bytes->string/utf-8 (request-post-data/raw req)))
- ; convert to an alist:
- (define form-data (form-urlencoded->alist post-data))
- ; pull out the team name:
- (define teamName (cdr (assq 'nome form-data)))
- ;check if team already exists
- ;;; (define teamExists (query-list SQLite "select name from teams where name LIKE $1") teamName)
- ;Se a quantidade de times for menor que 8 insere
- (query-exec SQLite
- "insert into teams (name) values (?)" teamName)
- (response/xexpr
- `(html
- (body
- (center(p "Time registrado: " ,teamName))
- (style ([type "text/css"])
- "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
- "a {color: #008080;}")
- (center(a ((href "/index")) "Voltar"))
- )))]
- [(equal? page "list-teams")
- (quartas (take teams 8)
- (lambda()(response/xexpr
- `(html
- (body
- (p "Partida 1: " , (list-ref (list-ref jogosQuartasFinal 0) 0), " X ", (list-ref (list-ref jogosQuartasFinal 0) 1))
- (style ([type "text/css"])
- "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;flex-direction:column;align-items:center}")
- (img ([src "./img.jpg"]))
- (form ([method "POST"] [action "/save-goals"])
- ;Pegar os times
- (input ([type "hidden"] [name "jogo"] [value , "0"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 0) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 0) 1)]))
- ;Form para obter os gols
- "Gols time 1:" (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 0) 2))]))
- (br)
- "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 0) 3))]))
- (br)
- (input ([type "submit"] [value "Salvar"]))
- )
- (form ([method "POST"] [action "/random-goals"])
- (input ([type "hidden"] [name "jogo"] [value , "0"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 0) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 0) 1)]))
- (input ([type "submit"] [value "Gerar aleatório"]))
- )
- (p "Partida 2: ", (list-ref (list-ref jogosQuartasFinal 1) 0), " X ", (list-ref (list-ref jogosQuartasFinal 1) 1))
- (form ([method "POST"] [action "/save-goals"])
- ;Pegar os times
- (input ([type "hidden"] [name "jogo"] [value , "1"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 1) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 1) 1)]))
- ;Form para obter os gols
- "Gols time 1:" (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 1) 2))]))
- (br)
- "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 1) 3))]))
- (br)
- (input ([type "submit"] [value "Salvar"]))
- )
- (form ([method "POST"] [action "/random-goals"])
- (input ([type "hidden"] [name "jogo"] [value , "1"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 1) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 1) 1)]))
- (input ([type "submit"] [value "Gerar aleatório"]))
- )
- (p "Partida 3: ", (list-ref (list-ref jogosQuartasFinal 2) 0), " X ", (list-ref (list-ref jogosQuartasFinal 2) 1))
- (form ([method "POST"] [action "/save-goals"])
- ;Pegar os times
- (input ([type "hidden"] [name "jogo"] [value , "2"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 2) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 2) 1)]))
- ;Form para obter os gols
- "Gols time 1:" (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 2) 2))]))
- (br)
- "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 2) 3))]))
- (br)
- (input ([type "submit"] [value "Salvar"]))
- )
- (form ([method "POST"] [action "/random-goals"])
- (input ([type "hidden"] [name "jogo"] [value , "2"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 2) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 2) 1)]))
- (input ([type "submit"] [value "Gerar aleatório"]))
- )
- (p "Partida 4: ", (list-ref (list-ref jogosQuartasFinal 3) 0), " X ", (list-ref (list-ref jogosQuartasFinal 3) 1))
- (form ([method "POST"] [action "/save-goals"])
- ;Pegar os times
- (input ([type "hidden"] [name "jogo"] [value , "3"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 3) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 3) 1)]))
- ;Form para obter os gols
- "Gols time 1:" (input ([type "text"] [name "goals1"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 3) 2))]))
- (br)
- "Gols time 2: " (input ([type "text"] [name "goals2"] [value, (number->string (list-ref (list-ref jogosQuartasFinal 3) 3))]))
- (br)
- (input ([type "submit"] [value "Salvar"]))
- )
- (form ([method "POST"] [action "/random-goals"])
- (input ([type "hidden"] [name "jogo"] [value , "3"]))
- (input ([type "hidden"] [name "nomeTime1"] [value , (list-ref (list-ref jogosQuartasFinal 3) 0)]))
- (input ([type "hidden"] [name "nomeTime2"] [value , (list-ref (list-ref jogosQuartasFinal 3) 1)]))
- (input ([type "submit"] [value "Gerar aleatório"]))
- )
- )
- )))
- (lambda()(
- response/xexpr
- `(html
- (body
- (p "Partida 1: ")
- )
- )
- ))
- )
- ]
- [(equal? page "save-goals")
- (define post-data (bytes->string/utf-8 (request-post-data/raw req)))
- ; convert to an alist:
- (define form-data (form-urlencoded->alist post-data))
- ; pull out the team name:
- (define jogo (cdr (assq 'jogo form-data)))
- (define nomeTime1 (cdr (assq 'nomeTime1 form-data)))
- (define goal1 (cdr (assq 'goals1 form-data)))
- (define nomeTime2 (cdr (assq 'nomeTime2 form-data)))
- (define goal2 (cdr (assq 'goals2 form-data)))
- (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 0 (string->number goal1))
- (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 1 (string->number goal2))
- ; insert to database:
- (query-exec SQLite
- "insert into matchs (team1,team2,goals_team1,goals_team2,turn) VALUES (?,?,?,?,?)" nomeTime1 nomeTime2 goal1 goal2 1)
- (response/xexpr
- `(html
- (body
- (center(p "Gols registrados: ", nomeTime1, " " ,goal1, " x ", nomeTime2, " ", goal2))
- (style ([type "text/css"])
- "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
- "a {color: #008080;}")
- (center(a ((href "/list-teams")) "Voltar"))
- )))]
- [(equal? page "random-goals")
- (define post-data (bytes->string/utf-8 (request-post-data/raw req)))
- ; convert to an alist:
- (define form-data (form-urlencoded->alist post-data))
- (define jogo (cdr (assq 'jogo form-data)))
- (define nomeTime1 (cdr (assq 'nomeTime1 form-data)))
- (define goal1 (randomInt))
- (define nomeTime2 (cdr (assq 'nomeTime2 form-data)))
- (define goal2 (randomInt))
- (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 0 goal1)
- (alterarQuantidadeGolsChaveamentoQuartas (string->number jogo) 1 goal2)
- ; insert to database:
- ;(query-exec SQLite
- ; "insert into matchs (team1,team2,goals_team1,goals_team2,turn) VALUES (?,?,?,?,?)" idTime1 idTime2 (random-int 1 5) (random-int 1 5) 1)
- (response/xexpr
- `(html
- (body
- (center(p "Gols registrados: ", nomeTime1, " " ,(number->string goal1), " x ", nomeTime2, " ", (number->string goal2)))
- (style ([type "text/css"])
- "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
- "a {color: #008080;}")
- (center(a ((href "/list-teams")) "Voltar"))
- )))
- ]
- [else
- (response/xexpr
- `(html
- (body
- (center(h1 "404"))
- (style ([type "text/css"])
- "body {margin: 0px; padding: 10px; justify-content:center;background-color: #f2f2f2; display:flex;align-items:center;flex-direction:column}"
- "a {color: #008080; text-decoration: none;}")
- (center(p "Página não encontrada!"))
- (center(a ((href "/index")) "Voltar"))
- )))]
- )
- )
- (serve/servlet world-cup
- #:servlet-path "/index"
- #:port 8001 ;porta que a pagina ira rodar
- #:servlet-regexp #rx""
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement