Skip to content

Commit

Permalink
Merge pull request #219 from gavr-games/realtime_ui_disabling_refacto…
Browse files Browse the repository at this point in the history
…ring

Add controllers to game object and other refactoring
  • Loading branch information
andriikovalov authored Jun 14, 2019
2 parents e1edccd + 8a7d5ca commit 749106f
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 37 deletions.
22 changes: 22 additions & 0 deletions web/game/mode/js/cards_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict"

export class CardsController {
constructor() {
this.cards = []
this.cardActionDoneInThisTurn = false
}

setCardActionDoneInThisTurn(cardActionDoneInThisTurn) {
this.cardActionDoneInThisTurn = cardActionDoneInThisTurn
}

canMakeAction() {
let game = window.Game
return (game.TurnController.myTurn && !game.CardsController.cardActionDoneInThisTurn)
}

canBuy() {
return window.Game.PlayersController.getMyPlayer()['gold'].toInt() >= mode_config["card cost"]
&& (realtime_cards || this.canMakeAction());
}
}
18 changes: 18 additions & 0 deletions web/game/mode/js/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict"

export class Game {
constructor(gameData, myPlayerNum) {
this.creation_date = gameData['creation_date']
this.game_id = gameData['game_id']
this.mode_id = gameData['mode_id']
this.owner_id = gameData['owner_id']
this.status_id = gameData['status_id']
this.time_restriction = gameData['time_restriction']
this.title = gameData['title']
this.type_id = gameData['type_id']

this.PlayersController = new window.GameMode.PlayersController(myPlayerNum)
this.CardsController = new window.GameMode.CardsController()
this.TurnController = new window.GameMode.TurnController()
}
}
20 changes: 15 additions & 5 deletions web/game/mode/js/mode.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
"use strict"

import {Unit} from "./unit.js";
import {Building} from "./building.js";
import {BoardObject} from "./board_object.js";
import {Cell} from "./cell.js";
import {Game} from "./game.js"
import {Player} from "./player.js"
import {PlayersController} from "./players_controller.js"
import {CardsController} from "./cards_controller.js"
import {TurnController} from "./turn_controller.js"
import {Unit} from "./unit.js"
import {Building} from "./building.js"
import {BoardObject} from "./board_object.js"
import {Cell} from "./cell.js"

class GameMode {
constructor() {
this.Unit = Unit
this.Building = Building
this.BoardObject = BoardObject
this.Cell = Cell
this.Game = Game
this.Player = Player
this.PlayersController = PlayersController
this.CardsController = CardsController
this.TurnController = TurnController
}
}

window.GameMode = new GameMode();
window.GameMode = new GameMode()
15 changes: 15 additions & 0 deletions web/game/mode/js/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict"

export class Player {
constructor(data) {
this.player_num = data['player_num'].toInt()
this.name = data['name']
this.gold = data['gold'].toInt()
this.owner = data['owner'].toInt()
this.team = data['team'].toInt()
}

setGold(gold) {
this.gold = gold.toInt()
}
}
33 changes: 33 additions & 0 deletions web/game/mode/js/players_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict"

export class PlayersController {
constructor(myPlayerNum) {
this.players = []
this.myPlayerNum = myPlayerNum.toInt()
}

addPlayer(playerData) {
player = new window.GameMode.Player(playerData)
this.players.push(player)
}

removePlayer(playerNum) {
this.players = this.players.filter((player, _index, _arr) => {
return player.num != playerNum
})
}

getMyPlayer() {
return this.getPlayerByNum(this.myPlayerNum)
}

isMyPlayer(playerNum) {
return playerNum.toInt() == this.myPlayerNum
}

getPlayerByNum(playerNum) {
return this.players.find((player) => {
return player.player_num == playerNum
})
}
}
11 changes: 11 additions & 0 deletions web/game/mode/js/turn_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict"

export class TurnController {
constructor() {
this.myTurn = false
}

setMyTurn(myTurn) {
this.myTurn = myTurn
}
}
29 changes: 13 additions & 16 deletions web/game/mode9/js_libs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function publish_api_call() {
function player_set_gold(p_num, amount) {
publish_api_call();
players_by_num[p_num]["gold"] = amount;
window.Game.PlayersController.getPlayerByNum(p_num).setGold(amount)
update_game_info_window();
if (p_num == my_player_num && (turn_state == MY_TURN || realtime_cards)) {
update_controls_activation();
Expand Down Expand Up @@ -74,6 +75,7 @@ function add_player(p_num, name, gold, owner, team) {
players_by_num[p_num]['gold'] = gold;
players_by_num[p_num]['owner'] = owner;
players_by_num[p_num]['team'] = team;
window.Game.PlayersController.addPlayer(players_by_num[p_num]);
if (p_num < 10) { //not neutral
var myP = new Element('p', {
'html': name + ":",
Expand Down Expand Up @@ -109,6 +111,7 @@ function add_player(p_num, name, gold, owner, team) {
function delete_player(p_num) {
publish_api_call();
delete players_by_num[p_num];
window.Game.PlayersController.removePlayer(p_num);
if (p_num < 10) { //not neutral
if ($('player' + p_num)) {
$('player' + p_num).destroy();
Expand Down Expand Up @@ -1463,30 +1466,21 @@ function move_anim(x, y, x2, y2, size) {

function is_active_card(cardDiv) {
var card_id = cardDiv.attributes.card_id.value;
if (players_by_num[my_player_num]["gold"].toInt() < cards[card_id]['cost'].toInt()) {
if (window.Game.PlayersController.getMyPlayer()["gold"].toInt() < cards[card_id]['cost'].toInt()) {
return false;
}
if (realtime_cards && cards[card_id]['type'] == 'm') {
return true;
}
return can_make_card_action();
}

function can_make_card_action() {
return (turn_state == MY_TURN && !card_action_done_in_this_turn);
return window.Game.CardsController.canMakeAction();
}

function is_active_grave(graveDiv) {
return can_make_card_action()
return window.Game.CardsController.canMakeAction()
&& (graveDiv.attributes.turn_when_killed.value != active_players['turn'] || graveDiv.attributes.player_num_when_killed.value != active_players['player_num'])
&& players_by_num[my_player_num]["gold"].toInt() >= cards[graveDiv.attributes.card_id.value]['cost'].toInt() * 2;
}

function is_active_buy_card_button() {
return players_by_num[my_player_num]['gold'].toInt() >= mode_config["card cost"]
&& (realtime_cards || can_make_card_action());
}

function is_active_subsidy_button() {
if (board_buildings[my_castle_id] && board_buildings[my_castle_id]['health'].toInt() < 2) {
return false;
Expand Down Expand Up @@ -2204,6 +2198,7 @@ function end_game() {
clearInterval(shieldInterval);
clearInterval(titleInterval);
turn_state = NOT_MY_TURN;
window.Game.TurnController.setMyTurn(false)
deactivate_controls();
var text = '<a href="#" onclick="get_stats();return false;">' + i18n[USER_LANGUAGE]['game']['show_statistic'] + '</a><br><a href="#" onclick="execute_exit();return false;">' + i18n[USER_LANGUAGE]['game']['exit'] + '</a>';
noCloseWindow = true;
Expand Down Expand Up @@ -2438,13 +2433,14 @@ function set_active_player(player_num, last_end_turn, turn, npc_flag, units_move
subsidy_flag = subsidy_flag || 0;
from_init = from_init || 0;
//clean green cells after my move
if (turn_state == MY_TURN) {
if (window.Game.TurnController.myTurn) {
$$('#board .green').removeClass('green');
$$('#board .activeUnit').removeClass('activeUnit');
}
if (player_num == my_player_num) {
if (window.Game.PlayersController.isMyPlayer(player_num)) {
turn_state = MY_TURN;
card_action_done_in_this_turn = card_played_flag.toInt();
window.Game.TurnController.setMyTurn(true);
window.Game.CardsController.setCardActionDoneInThisTurn(card_played_flag.toInt() == 1);
subsidy_taken_in_this_turn = subsidy_flag.toInt();
movedUnits = units_moves_flag.toInt();
activateMyMove();
Expand All @@ -2454,6 +2450,7 @@ function set_active_player(player_num, last_end_turn, turn, npc_flag, units_move
movedUnits = false;
clearTimeout(remindMoveTimer);
turn_state = NOT_MY_TURN;
window.Game.TurnController.setMyTurn(false)
clean_everything();
stopShield();
if (game_state != 'WAITTING') cancel_execute(); //for interrupting of polza choosing params
Expand Down Expand Up @@ -2532,7 +2529,7 @@ function update_next_turn_timer(left_seconds) {
}

function update_controls_activation() {
button_set_availability($('main_buttons').getChildren('.btn_buycard')[0], is_active_buy_card_button());
button_set_availability($('main_buttons').getChildren('.btn_buycard')[0], window.Game.CardsController.canBuy());
button_set_availability($('main_buttons').getChildren('.btn_subs')[0], is_active_subsidy_button());
button_set_availability($('main_buttons').getChildren('.btn_sendm')[0], is_active_send_button());
button_set_availability($('main_buttons').getChildren('.btn_end_turn')[0], turn_state == MY_TURN);
Expand Down
10 changes: 5 additions & 5 deletions web/game/mode9/js_libs/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ function proc_answer(pr_uid, suc, error_code, error_params, ape_time, php_time)
'php_time':php_time
});
if (playingCard && (!realtime_cards || cards[currently_played_card_id]['type'] != 'm')) {
card_action_done_in_this_turn = 1;
update_controls_activation();
window.Game.CardsController.setCardActionDoneInThisTurn(true);
update_controls_activation();
}
}

Expand Down Expand Up @@ -722,7 +722,7 @@ function post_put_building() {
function post_buy_card() {
if (error_procedure == '') {
if (!realtime_cards) {
card_action_done_in_this_turn = 1;
window.Game.CardsController.setCardActionDoneInThisTurn(true);
}
setTimeout("update_controls_activation();", 1000);
}
Expand All @@ -744,8 +744,8 @@ function post_take_subsidy() {

function post_player_resurrect() {
if (error_procedure == '') {
card_action_done_in_this_turn = 1;
setTimeout("update_controls_activation();", 1000);
window.Game.CardsController.setCardActionDoneInThisTurn(true);
setTimeout("update_controls_activation();", 1000);
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/game/mode9/js_libs/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var was_active = 0; //state of player
var shieldInterval;
var titleInterval;
var realtime_cards;
var card_action_done_in_this_turn = 0;
var subsidy_taken_in_this_turn = 0;

var time_delay_from_server = 0;
Expand Down Expand Up @@ -117,6 +116,7 @@ function initialization() {
try {
parent.WSClient.joinGame(game_info["game_id"]);
//init some variables
window.Game = new window.GameMode.Game(game_info, my_player_num)
time_restriction = game_info["time_restriction"].toInt();
game_status = game_info["status_id"].toInt();
initGameFeatures();
Expand Down
Loading

0 comments on commit 749106f

Please sign in to comment.