game: add: game map
This commit is contained in:
52
frontend/static/js/api/game/Game.js
Normal file
52
frontend/static/js/api/game/Game.js
Normal file
@ -0,0 +1,52 @@
|
||||
import { GameConfig } from "./GameConfig.js"
|
||||
|
||||
class Game
|
||||
{
|
||||
/**
|
||||
* @param {Client} client
|
||||
*/
|
||||
constructor(client, id)
|
||||
{
|
||||
/**
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = client;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
async init()
|
||||
{
|
||||
let response = await this.client._get(`/api/games/${this.id}`);
|
||||
|
||||
if (response.status !== 200)
|
||||
return response.status;
|
||||
|
||||
let response_data = await response.json();
|
||||
|
||||
this.players_id = response_data.players_id;
|
||||
this.state = response_data.state;
|
||||
this.started = response_data.started;
|
||||
this.finished = response_data.finished;
|
||||
this.winner_id = this.finished ? response_data.winner_id : undefined;
|
||||
|
||||
//TODO invert line
|
||||
if (false)
|
||||
//if (this.finished === true || this.started === false)
|
||||
return 0;
|
||||
|
||||
this.config = new GameConfig(this.client);
|
||||
|
||||
let ret = await this.config.init();
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
this.players = response_data.players;
|
||||
|
||||
this.ball_pos_x = this.config.ball_spawn_x;
|
||||
this.ball_pos_y = this.config.ball_spawn_y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
export { Game }
|
43
frontend/static/js/api/game/GameConfig.js
Normal file
43
frontend/static/js/api/game/GameConfig.js
Normal file
@ -0,0 +1,43 @@
|
||||
class GameConfig
|
||||
{
|
||||
/**
|
||||
* @param {Client} client
|
||||
*/
|
||||
constructor(client)
|
||||
{
|
||||
/**
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async init()
|
||||
{
|
||||
let response = await this.client._get("/api/games/");
|
||||
|
||||
if (response.status !== 200)
|
||||
return response.status;
|
||||
|
||||
let response_data = await response.json();
|
||||
|
||||
|
||||
this.size_x = response_data.MAP_SIZE_X;
|
||||
this.size_y = response_data.MAP_SIZE_Y;
|
||||
this.center_x = this.size_x / 2;
|
||||
this.center_y = this.size_y / 2;
|
||||
|
||||
this.paddle_ratio = response_data.PADDLE_RATIO;
|
||||
this.wall_ratio = response_data.WALL_RATIO;
|
||||
|
||||
this.ball_speed_inc = response_data.BALL_SPEED_INC;
|
||||
this.ball_speed_start = response_data.BALL_SPEED_START;
|
||||
|
||||
this.ball_size = response_data.BALL_SIZE;
|
||||
this.ball_spawn_x = this.center_x;
|
||||
this.ball_spawn_y = this.center_y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
export { GameConfig }
|
Reference in New Issue
Block a user