Skip to content

Commit

Permalink
Merge pull request #4 from projeto-de-algoritmos-2024/front/integration
Browse files Browse the repository at this point in the history
Front/integration
  • Loading branch information
iagorrr authored Dec 3, 2024
2 parents 6816bbe + d660e96 commit 9894835
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
target
.idea
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Santa Labyrinth

**Número da Lista**: X<br>
**Conteúdo da Disciplina**: XXXXXXXXXX<br>
**Número da Lista**: 44<br>
**Conteúdo da Disciplina**: Grafos2<br>

## Alunos

Expand All @@ -12,22 +12,27 @@

## Sobre

Descreva os objetivos do seu projeto e como ele funciona.
Este projeto é uma adaptação do jogo [Santa Labyrinth](https://www.digipuzzle.net/christmas/cartoons/puzzles/labyrinth.htm?language=english&linkback=../../../education/christmas/index.htm), que consiste em um labirinto representado por um grid 7x7, onde o objetivo é levar o Papai Noel até a meia.

Nesse projeto, foi utilizado uma variação do algoritmo A* para encontrar um caminho válido para o Papai Noel. A implementação do backend foi feita em Rust, enquanto o frontend foi feito em React.

## Screenshots

Adicione 3 ou mais screenshots do projeto em funcionamento.
![Exemplo](./assets/santa.gif)

## Instalação

**Linguagem**: xxxxxx<br>
**Framework**: (caso exista)<br>
Descreva os pré-requisitos para rodar o seu projeto e os comandos necessários.
Os pré-requisitos para a execução do projeto são:

## Uso
- Docker 27.3.1
- Docker Compose 3.8
- GNU Make 4.3

Explique como usar seu projeto caso haja algum passo a passo após o comando de execução.
## Uso

## Outros
Para executar o projeto basta executar o seguinte comando:
```bash
make start
```

Quaisquer outras informações sobre seu projeto podem ser descritas abaixo.
Após o comando ser executado, você pode acessar o projeto pelo link http://localhost:3000.
Binary file added assets/print1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/print2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/print3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/santa.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ FROM rust:1.82-bookworm

WORKDIR /app

COPY ./api/ .

CMD ["cargo", "run", "--release"]
17 changes: 17 additions & 0 deletions backend/api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
actix-cors = "0.7.0"
actix-web = "4.9.0"
rand = "0.8.5"
serde ={ version = "1.0.215", features = ["derive"] }
Expand Down
12 changes: 8 additions & 4 deletions backend/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use game::GameSchema;
mod solver;
use solver::{is_solved, solve};

use actix_cors::Cors;

use actix_web::{
get,
post,
web::{self},
App, HttpResponse, HttpServer, Responder,
};
Expand All @@ -19,11 +21,11 @@ mod cell_piece;

mod coordinate;

#[get("/solver")]
#[post("/solver")]
async fn solver_endpoint(data: web::Json<GameSchema>) -> impl Responder {
// TODO: MAKE SURE IT'S A RECTANGLE MAP
let result = timeout(
Duration::from_secs(13),
Duration::from_secs(30),
solve(&data.0.source, &data.0.goal, &data.0.grid),
)
.await;
Expand All @@ -36,7 +38,7 @@ async fn solver_endpoint(data: web::Json<GameSchema>) -> impl Responder {
};
}

#[get("/is_solved")]
#[post("/is_solved")]
async fn is_solved_endpoint(game: web::Json<GameSchema>) -> impl Responder {
return HttpResponse::Ok().json(is_solved(&game.0.source, &game.0.goal, &game.0.grid));
}
Expand All @@ -51,7 +53,9 @@ async fn main() -> std::io::Result<()> {
};

HttpServer::new(|| {
let cors = Cors::permissive();
App::new()
.wrap(cors)
.service(solver_endpoint)
.service(is_solved_endpoint)
})
Expand Down
8 changes: 5 additions & 3 deletions backend/api/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ pub async fn solve(
grid: mut u_grid,
}) = heap.pop()
{
print_grid(&u_grid);
// Found solution.
if u_dist == 0 {
print_grid(&u_grid);
return Some(GameSchema {
// TODO: shorthand?
source: source.clone(),
Expand Down Expand Up @@ -165,7 +165,9 @@ pub fn print_grid(grid: &CellGrid) {
println!();
for i in 0..grid.len() as usize {
for j in 0..grid[i].len() as usize {
if grid[i][j].l && grid[i][j].r {
if (i == 0 && j == 0) || (i == grid.len() - 1 && j == grid[0].len() - 1) {
print!("*");
} else if grid[i][j].l && grid[i][j].r {
print!("═");
} else if grid[i][j].u && grid[i][j].d {
print!("║");
Expand All @@ -180,6 +182,6 @@ pub fn print_grid(grid: &CellGrid) {
}
}
println!();
println!();
//println!();
}
}
14 changes: 11 additions & 3 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3"

services:
santa_labyrinth_api:
build:
Expand All @@ -9,5 +7,15 @@ services:
- 8086:8086
env_file:
- .backend-env-file.env

santa_labyrinth_front:
build:
context: ../front
dockerfile: Dockerfile
container_name: frontzin_torado
ports:
- 3000:3000
volumes:
- ./api:/app
- ../front/src:/front/src
- ../front/public:/front/public

13 changes: 13 additions & 0 deletions front/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20-alpine

WORKDIR /front

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]
2 changes: 1 addition & 1 deletion front/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"short_name": "Santa",
"name": "Create React App Sample",
"name": "Santa Labyrinth",
"icons": [
{
"src": "favicon.ico",
Expand Down
31 changes: 28 additions & 3 deletions front/src/components/Grid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,34 @@ function Grid({ map, startGrid }) {

const handleSolve = async (currentState, map) => {
const parsed = parserManager.parseMap(currentState, map);
const { decodedMap, decodedState } = parserManager.resrap(parsed);
console.log(decodedMap);
console.log(decodedState);
const body = JSON.stringify({
"source": {
"x": 0,
"y": 0
},
"goal": {
"x": 6,
"y": 6
},
"grid": parsed
});
const response = await fetch('http://localhost:8086/solver',{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: body
})
if(!response.ok) {
alert('Error');
return;
} else {
const data = await response.json();
const newGrid = data.grid;
const decodedGrid = parserManager.resrap(newGrid);
console.log(decodedGrid);
setCells(decodedGrid);
}
}

return (
Expand Down
Loading

0 comments on commit 9894835

Please sign in to comment.