Skip to content

Commit

Permalink
feat: improve leaderboard (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
EehMauro authored Nov 5, 2024
1 parent b869e1e commit 9c2ec44
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 66 deletions.
78 changes: 78 additions & 0 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ use async_graphql::*;
use async_graphql_rocket::GraphQLRequest as Request;
use async_graphql_rocket::GraphQLResponse as Response;
use rocket::State;
use rand::{distributions::Alphanumeric, Rng};
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::{Header, Method, Status};

pub struct CORS;

#[rocket::async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
kind: Kind::Response,
}
}

async fn on_response<'r>(&self, request: &'r rocket::Request<'_>, response: &mut rocket::Response<'r>) {
if request.method() == Method::Options {
response.set_status(Status::NoContent);
response.set_header(Header::new("Access-Control-Allow-Methods", "POST, PATCH, GET, DELETE"));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
}
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}

struct QueryRoot;

Expand Down Expand Up @@ -220,6 +245,17 @@ pub enum ShipActionType {
GatherFuel,
}

#[derive(SimpleObject, Clone)]
pub struct LeaderboardRecord {
ranking: i32,
address: String,
ship_name: String,
pilot_name: String,
fuel: i32,
movements: i32,
distance: i32,
}

#[derive(Union)]
pub enum MapObject {
Ship(Ship),
Expand Down Expand Up @@ -497,6 +533,47 @@ impl QueryRoot {

actions
}

async fn leaderboard(
&self,
_ctx: &Context<'_>,
) -> Vec<LeaderboardRecord> {
let mut results = Vec::new();

for i in 0..100 {
let address: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(64)
.map(char::from)
.collect();

let ship_name: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(6)
.map(char::from)
.collect();

let pilot_name: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(6)
.map(char::from)
.collect();

results.push(
LeaderboardRecord {
ranking: i + 1,
address: address,
ship_name: ship_name.to_uppercase(),
pilot_name: pilot_name.to_uppercase(),
fuel: rand::thread_rng().gen_range(0..100),
movements: i * 10,
distance: i * 10
}
);
}

results
}
}

type AsteriaSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
Expand Down Expand Up @@ -544,4 +621,5 @@ async fn rocket() -> _ {
rocket::build()
.manage(schema)
.mount("/", routes![index, graphql_request, graphiql])
.attach(CORS)
}
2 changes: 2 additions & 0 deletions docker/Dockerfile.frontend
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ FROM node:20-alpine

WORKDIR /app

ENV API_URL=https://8000-skillful-employee-kb9ou6.us1.demeter.run

COPY --from=build /visualizer/build ./public/visualizer

COPY ./frontend/package*.json ./
Expand Down
3 changes: 3 additions & 0 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const withMDX = require('@next/mdx')()
const nextConfig = {
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
transpilePackages: ['next-mdx-remote'],
env: {
API_URL: process.env.API_URL,
},
}

module.exports = withMDX(nextConfig)
Loading

0 comments on commit 9c2ec44

Please sign in to comment.