fix gameserver

This commit is contained in:
ajuvercr 2020-03-30 11:33:20 +02:00
parent 9460323b48
commit 0dc4d6f662
6 changed files with 35 additions and 13 deletions

1
backend/.gitignore vendored
View file

@ -4,3 +4,4 @@ games/
maps/
trace.log
games.ini
static/bot

View file

@ -5,7 +5,7 @@ use serde_json;
use std::collections::HashMap;
use std::convert::TryInto;
use std::fs::{File, create_dir};
use std::fs::{create_dir, File};
use std::io::Write;
mod pw_config;
@ -60,8 +60,6 @@ impl PlanetWarsGame {
)
.unwrap();
// println!("{}", serde_json::to_string(&state).unwrap());
for player in self
.state
.players
@ -83,7 +81,6 @@ impl PlanetWarsGame {
));
if !player.alive || self.state.is_finished() {
println!("Kicking player {}", player.id);
updates.push(HostMsg::Kick(player.id as u64));
}
}
@ -172,8 +169,8 @@ impl PlanetWarsGame {
}
}
use std::fs::OpenOptions;
use ini::Ini;
use std::fs::OpenOptions;
impl game::Controller for PlanetWarsGame {
fn start(&mut self) -> Vec<HostMsg> {
let mut updates = Vec::new();
@ -200,14 +197,19 @@ impl game::Controller for PlanetWarsGame {
fn is_done(&mut self) -> bool {
if self.state.is_finished() {
if !self.logged {
let mut f = match OpenOptions::new().create(true).append(true).open("games.ini") { Err(_) => return true, Ok(f) => f };
let mut f = match OpenOptions::new()
.create(true)
.append(true)
.open("games.ini")
{
Err(_) => return true,
Ok(f) => f,
};
let mut conf = Ini::new();
conf.with_section(Some(self.log_file_loc.clone()))
.set("name", &self.name)
.set("turns", format!("{}", self.turns));
.set("name", &self.name)
.set("turns", format!("{}", self.turns));
conf.write_to(&mut f).unwrap();

View file

@ -1,4 +1,5 @@
/// The planet wars game rules.
#[derive(Debug)]
pub struct PlanetWars {
pub players: Vec<Player>,
pub planets: Vec<Planet>,

View file

@ -47,7 +47,7 @@ impl<'a> Serializer<'a> {
/// rotated based on the number offset for this serializer.
fn player_num(&self, player_id: usize) -> usize {
let num_players = self.state.players.len();
let rotated_id = (player_id + self.player_num_offset) % num_players;
let rotated_id = (player_id + num_players - self.player_num_offset) % num_players;
// protocol player ids start at 1
return rotated_id + 1;
}

View file

@ -104,12 +104,28 @@ struct GameReq {
name: String,
}
#[derive(Serialize)]
struct GameRes {
players: Vec<u64>,
}
use mozaic::util::request::Connect;
#[post("/lobby", data="<game_req>")]
async fn game_post(game_req: Json<GameReq>, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result<String, String> {
async fn game_post(game_req: Json<GameReq>, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result<Json<GameRes>, 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.unwrap();
state.add_game(game_req.name.clone(), game_id);
Ok(format!("{:?}", gm.get_state(game_id).await))
if let Some(conns) = gm.get_state(game_id).await {
let players: Vec<u64> = conns.iter().map(|conn| match conn {
Connect::Waiting(_, key) => *key,
_ => 0,
}).collect();
Ok(Json(GameRes { players }))
} else {
Err(String::from("Fuck the world"))
}
}
pub fn fuel(routes: &mut Vec<Route>) {

View file

@ -19,6 +19,8 @@ body {
display: flex;
flex-direction: column;
align-self: flex-start;
overflow-y: scroll;
height: 100%;;
}
.input_container {