basic things, get the server to run, start working on rooms

This commit is contained in:
zoe 2022-01-28 21:47:29 +01:00
parent f86e4bb9e1
commit 58303b3769
11 changed files with 4923 additions and 2 deletions

1935
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.5.0-rc.1"
lazy_static = "1.4.0"
clap = { version = "3.0.6", features = ["derive"] }
rocket_dyn_templates = {version="0.1.0-rc.1", features= ["handlebars"] }
rand = "0.8.4"

1347
resources/adjectives.txt Normal file

File diff suppressed because it is too large Load Diff

1524
resources/nouns.txt Normal file

File diff suppressed because it is too large Load Diff

8
src/entity.rs Normal file
View File

@ -0,0 +1,8 @@
pub struct Board {
x : usize,
y : usize,
id : String,
player_one: String,
player_two: String,
full : bool
}

View File

@ -1,3 +1,44 @@
fn main() {
println!("Hello, world!");
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate lazy_static;
use clap::Parser;
use rocket::fs::{relative, FileServer};
use rocket_dyn_templates::Template;
mod names;
mod rooms;
lazy_static! {
static ref ARGS: Args = Args::parse();
}
#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
// Port number for server
#[clap(short, long, default_value_t = 8000)]
port: u16,
}
#[launch]
fn rocket() -> _ {
let mut config = rocket::Config::default();
config.port = ARGS.port;
rocket::custom(config)
.attach(Template::fairing())
.mount("/static", FileServer::from(relative!("static")))
.mount("/", routes![join])
}
/**
* Tries to join a room for the according id, fails if someone with the same id has already joined
**/
#[get("/join/<room_id>/<name>")]
async fn join(room_id: &str, name: &str) -> String {
format!(
"Hey, {}! You're joining '{}', please give us a second!",
name, room_id
)
}

25
src/names.rs Normal file
View File

@ -0,0 +1,25 @@
use std::io::{BufReader, BufRead};
use std::fs::File;
use rand::seq::SliceRandom;
lazy_static!{
static ref NOUNS: Vec<String> = get_word_list("resources/nouns.txt");
static ref ADJECTIVES: Vec<String> = get_word_list("resources/adjectives.txt");
}
pub async fn get_random_name() -> String{
let rng = &mut rand::thread_rng();
let noun: &String = NOUNS.choose(rng).expect("picking name failed");
let adjective: &String = ADJECTIVES.choose(rng).expect("picking name failed");
adjective.to_owned() + noun
}
fn get_word_list(path: &str) -> Vec<String> {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let mut words: Vec<String> = vec![];
for line in reader.lines() {
words.push(line.unwrap());
}
words
}

11
src/rooms.rs Normal file
View File

@ -0,0 +1,11 @@
use std::sync::Mutex;
lazy_static! {
static ref ARRAY: Mutex<Vec<String>> = Mutex::new(vec![]);
}
pub struct Room {
id: String,
players: Vec<String>,
full: bool
}

23
static/index.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link
rel="stylesheet"
type="text/css"
href="static/style.css"
media="all"
/>
<script src="/script.js" charset="utf-8" defer></script>
<title>Reversi Online</title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1"
/>
<link rel="icon" type="image/x-icon" href="./static/think.svg" />
</head>
</html>
<body>
<div id="board"></div>
</body>

1
static/script.js Normal file
View File

@ -0,0 +1 @@

1
static/style.css Normal file
View File

@ -0,0 +1 @@