fancy shit yo

This commit is contained in:
ajuvercr 2020-03-28 19:07:55 +01:00
parent 70aabd39e9
commit 498a780854
9 changed files with 133 additions and 132 deletions

3
backend/.gitignore vendored
View file

@ -1,3 +1,6 @@
target/
Cargo.lock
games/
maps/
trace.log
games.ini

View file

@ -1,111 +0,0 @@
path=static/games/
[Alisha Lara.json]
name=Alisha Lara
turns=143
players= "SimpleBot 2 (check if python is correct before executing)" "mybot2"
[Andre Head.json]
name=Andre Head
turns=1229
players= "mybot2" "SimpleBot 2 (check if python is correct before executing)" "mybot"
[Angela Sims.json]
name=Angela Sims
turns=221
players= "SimpleBot 2 (check if python is correct before executing)" "mybot2"
[Buford Humphrey.json]
name=Buford Humphrey
turns=174
players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)"
[Catalina Grant.json]
name=Catalina Grant
turns=202
players= "mybot" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)"
[Cesar Jordan.json]
name=Cesar Jordan
turns=214
players= "Subutai" "SimpleBot 1 (check if python is correct before executing)"
[Chandra Garrett.json]
name=Chandra Garrett
turns=202
players= "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)"
[Cory Mooney.json]
name=Cory Mooney
turns=45
players= "SimpleBot 2 (check if python is correct before executing)" "SimpleBot 1 (check if python is correct before executing)"
[Deana Bishop.json]
name=Deana Bishop
turns=302
players= "mybot" "mybot2"
[game2.json]
name=game2
turns=110
players="bot_auto_player_full_rand" "adversary_auto_player_link_rand"
[Georgina Wiley.json]
name=Georgina Wiley
turns=302
players= "mybot2" "mybot"
[hungergames.json]
name=hungergames
turns=119
players=
[Jeannie Swanson.json]
name=Jeannie Swanson
turns=251
players= "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" "mybot2" "mybot"
[Joanne Blair.json]
name=Joanne Blair
turns=202
players= "mybot2" "mybot" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)"
[large.json]
name=large
turns=1166
players=
[Lester Berg.json]
name=Lester Berg
turns=1132
players= "mybot" "mybot2" "SimpleBot 2 (check if python is correct before executing)"
[Lindsey Farrell.json]
name=Lindsey Farrell
turns=461
players= "jens" "SimpleBot 2 (check if python is correct before executing)" "dinkske" "SimpleBot 1 (check if python is correct before executing)"
[Pansy Le.json]
name=Pansy Le
turns=417
players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" "SammyBot"
[pietmap.json]
name=pietmap
turns=2788
players=
[Randell Wolfe.json]
name=Randell Wolfe
turns=300
players= "mybot" "SimpleBot 2 (check if python is correct before executing)"
[spiral2.json]
name=spiral2
turns=537
players=
[standard_game.json]
name=standard_game
turns=82
players="bot_auto_player_full_rand" "adversary_auto_player_orfirst_rand"

View file

@ -35,6 +35,7 @@ use mozaic::modules::*;
mod planetwars;
mod routes;
mod util;
use util::Games;
use rocket_contrib::templates::{Template, Engines};
use rocket_contrib::templates::tera::{self, Value};
@ -90,6 +91,7 @@ async fn main() {
rocket::ignite()
.manage(gm)
.manage(pool)
.manage(Games::new())
.attach(tera)
.mount("/", routes)
.launch()

View file

@ -52,9 +52,10 @@ async fn map_post(map_req: Json<MapReq>) -> Result<String, String> {
}
#[get("/lobby")]
async fn maps_get() -> Result<Template, String> {
async fn lobby_get(gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result<Template, String> {
let maps = get_maps().await?;
let context = Context::new_with("Lobby", ContextT::Maps(maps));
let games = get_states(&state.get_games(), &gm).await?;
let context = Context::new_with("Lobby", Lobby { games, maps });
Ok(Template::render("lobby", &context))
}
@ -89,14 +90,15 @@ struct GameReq {
}
#[post("/lobby", data="<game_req>")]
async fn game_post(game_req: Json<GameReq>, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>) -> Result<String, String> {
async fn game_post(game_req: Json<GameReq>, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result<String, String> {
let game = build_builder(tp.clone(), game_req.nop, game_req.max_turns, &game_req.map, &game_req.name);
let game_id = gm.start_game(game).await;
Ok(format!("{:?}", gm.get_state(game_id.unwrap()).await))
let game_id = gm.start_game(game).await.unwrap();
state.add_game(game_req.name.clone(), game_id);
Ok(format!("{:?}", gm.get_state(game_id).await))
}
pub fn fuel(routes: &mut Vec<Route>) {
routes.extend(routes![files, index, map_post, map_get, maps_get, builder_get, visualizer_get, game_post]);
routes.extend(routes![files, index, map_post, map_get, lobby_get, builder_get, visualizer_get, game_post]);
}

View file

@ -9,6 +9,15 @@ pub struct Map {
url: String,
}
#[derive(Serialize)]
pub struct GameState {
name: String,
finished: bool,
turns: Option<u64>,
players: Vec<String>,
}
/// Visualiser game option
#[derive(Serialize)]
pub struct GameOption {
name: String,
@ -32,36 +41,43 @@ struct Link {
active: bool,
}
#[derive(Serialize)]
pub struct Lobby {
pub games: Vec<GameState>,
pub maps: Vec<Map>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ContextT {
Maps(Vec<Map>),
Games(Vec<GameOption>),
}
#[derive(Serialize)]
pub struct Context {
pub struct Context<T> {
pub name: String,
nav: Vec<Link>,
#[serde(flatten)]
pub inner: Option<ContextT>,
pub t: Option<T>
}
impl Context {
pub fn new_with(active: &str, inner: ContextT) -> Self {
impl<T> Context<T> {
pub fn new_with(active: &str, t: T) -> Self {
let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect();
Context {
nav, name: String::from(""), inner: Some(inner)
nav, name: String::from(""), t: Some(t)
}
}
}
impl Context<()> {
pub fn new(active: &str) -> Self {
let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect();
Context {
nav, name: String::from(""), inner: None,
nav, name: String::from(""), t: None,
}
}
}
@ -97,3 +113,61 @@ pub async fn get_games() -> Result<Vec<GameOption>, String> {
Ok(games)
}
use mozaic::modules::game;
use mozaic::util::request::Connect;
use futures::future::{join_all, FutureExt};
pub async fn get_states(game_ids: &Vec<(String, u64)>, manager: &game::Manager) -> Result<Vec<GameState>, String> {
let mut states = Vec::new();
let gss = join_all(game_ids.iter().cloned().map(|(name, id)| manager.get_state(id).map(move |f| (f, name)))).await;
for (gs, name) in gss {
if let Some(state) = gs {
states.push(
GameState {
name,
turns: None,
players: state.iter().map(|conn| match conn {
Connect::Waiting(_, key) => format!("Waiting {}", key),
_ => String::from("Some connected player"),
}).collect(),
finished: false,
}
)
} else {
states.push(
GameState {
name,
turns: None,
players: Vec::new(),
finished: true,
}
)
}
}
Ok(states)
}
use std::sync::{Arc, Mutex};
pub struct Games {
inner: Arc<Mutex<Vec<(String, u64)>>>,
}
impl Games {
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(Vec::new()))
}
}
pub fn add_game(&self, name: String, id: u64) {
self.inner.lock().unwrap().push((name, id));
}
pub fn get_games(&self) -> Vec<(String, u64)> {
self.inner.lock().unwrap().clone()
}
}

View file

@ -34,13 +34,17 @@
}
// const real_j = j % 2 == 1 ? j + 0.5 : j;
// const real_j = j % 2 == 1 ? j + 0.5 : j;
planets.push({
const planet = {
"name": parameters[i][j]["name"],
"ship_count": parseInt(parameters[i][j]["shipCount"]),
"owner": parameters[i][j]["colour"],
"x": r_j,
"y": r_i
});
};
if (parameters[i][j]["colour"] !== 0) {
planet["owner"] = parameters[i][j]["colour"];
}
planets.push(planet);
}
}
}
@ -208,4 +212,4 @@
}
init();
}
}

View file

@ -29,7 +29,7 @@ async function start_game() {
xhr.onreadystatechange = async function() {
console.log(this);
console.log(await this.text());
// console.log(await this.text());
// TODO: make response visible
};

View file

@ -17,7 +17,9 @@ body {
.lobby {
flex-grow: 1;
background-color: red;
display: flex;
flex-direction: column;
align-self: flex-start;
}
.input_container {
@ -101,4 +103,14 @@ svg {
top: 0;
width: 100%;
height: 100%;
}
}
.game_state {
background-color: #333;
display: flex;
max-width: 80%;
width: 500px;
margin: 20px auto;
justify-content: space-between;
padding: 20px;
}

View file

@ -2,7 +2,22 @@
{% block content %}
<div class="main">
<div class="lobby"></div>
<div class="lobby">
{% for state in games %}
<div class="game_state">
<div class="info">
<p>{{state.name}}</p>
{% if state.finished %}<p>Finished</p> {% endif %}
{% if state.turns %}<p>Turns: {{ state.turns }} </p> {% endif %}
</div>
<div class="players">
{% for player in state.players %}
<p>{{ player }}</p>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
<div class="creator">
<h1>Start new game</h1>
<div class="input_container">