Skip to content

Commit

Permalink
new feature: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jjppof committed Jun 4, 2023
1 parent f64ee5c commit e8f68ca
Show file tree
Hide file tree
Showing 43 changed files with 225 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ node_modules/
*.old
*.swp
*.tmp
gshtml5.version

## System files
.directory
Expand Down
2 changes: 1 addition & 1 deletion base/Audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Audio {
}
return audio;
} else {
console.warn(`SFX key not registered: ${key}.`);
this.data.logger.log_message(`SFX key not registered: ${key}.`);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion base/ControllableChar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ export abstract class ControllableChar {
}
const animation_obj = this.sprite.animations.getAnimation(animation_key);
if (!animation_obj) {
console.warn("Invalid animation key:", animation_key);
this.data.logger.log_message("Invalid animation key:" + animation_key);
}
if (start) {
this.sprite.animations.play(animation_key, frame_rate, loop);
Expand Down
9 changes: 5 additions & 4 deletions base/Enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as _ from "lodash";
import * as numbers from "./magic_numbers";
import {effect_types} from "./Effect";
import {Classes} from "./Classes";
import {GoldenSun} from "GoldenSun";

export class Enemy extends Player {
public items: {
Expand All @@ -30,8 +31,8 @@ export class Enemy extends Player {
public base_stats: {[main_stat in main_stats]?: number};
public fled: boolean;

constructor(enemy_data, name) {
super(enemy_data.key_name, name ? name : enemy_data.name);
constructor(data: GoldenSun, enemy_data: any, name: string) {
super(data, enemy_data.key_name, name ? name : enemy_data.name);
this.level = enemy_data.level;
this.turns = enemy_data.turns;
this.base_turns = this.turns;
Expand Down Expand Up @@ -162,6 +163,6 @@ export class Enemy extends Player {
}
}

export function get_enemy_instance(enemy_data, suffix: string) {
return new Enemy(enemy_data, enemy_data.name + suffix);
export function get_enemy_instance(data: GoldenSun, enemy_data: any, suffix: string) {
return new Enemy(data, enemy_data, enemy_data.name + suffix);
}
7 changes: 6 additions & 1 deletion base/GoldenSun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {StartMenu} from "./main_menus/StartMenu";
import {initialize_save_menu, SaveMenu} from "./main_menus/SaveMenu";
import {ParticlesWrapper} from "./ParticlesWrapper";
import {initialize_bgm_data} from "./initializers/bgm";
import {Logger} from "./Logger";

/**
* The project has basically two important folders: assets and base. All the source code is located inside base folder.
Expand All @@ -48,7 +49,7 @@ export class GoldenSun {
public ignore_system_scaling: boolean = false;

public electron_app: boolean;
private ipcRenderer: any;
public ipcRenderer: any;

//main game states
public menu_open: boolean = false;
Expand Down Expand Up @@ -114,6 +115,9 @@ export class GoldenSun {
/** A wrapper for Particles Storm. */
public particle_wrapper: ParticlesWrapper = null;

/** Class responsible to log stuff. It logs mainly user bad input. */
public logger: Logger = null;

//managers
public control_manager: ControlManager = null;
public cursor_manager: CursorManager = null;
Expand All @@ -137,6 +141,7 @@ export class GoldenSun {

constructor() {
this.init_electron();
this.logger = new Logger(this);
const config: Phaser.IGameConfig = {
width: numbers.GAME_WIDTH,
height: numbers.GAME_HEIGHT,
Expand Down
47 changes: 47 additions & 0 deletions base/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {GoldenSun} from "GoldenSun";

export enum msg_types {
INFO = "info",
WARNING = "warning",
ERROR = "error",
}

/**
* This class is responsible to log messages mainly in case of
* user bad input. If you're running desktop app, it will create
* a log file inside logs directory.
*/
export class Logger {
private data: GoldenSun;

constructor(data: GoldenSun) {
this.data = data;
this.log_message("Engine started.", msg_types.INFO);
}

log_message(message: string, type: msg_types = msg_types.WARNING) {
if (!message) {
return;
}
const now = new Date().toLocaleString();
switch (type) {
case msg_types.INFO:
message = `GSHTML5 - [${now}] [INFO]: ${message}`;
console.log(message);
break;
case msg_types.WARNING:
message = `GSHTML5 - [${now}] [WARNING]: ${message}`;
console.warn(message);
break;
case msg_types.ERROR:
message = `GSHTML5 - [${now}] [ERROR]: ${message}`;
console.error(message);
break;
default:
return;
}
if (this.data.electron_app) {
this.data.ipcRenderer.send("register-log", message);
}
}
}
5 changes: 3 additions & 2 deletions base/MainChar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class MainChar extends Player {
public in_party: boolean;

constructor(
data,
key_name,
info,
sprite_base,
Expand Down Expand Up @@ -106,7 +107,7 @@ export class MainChar extends Player {
special_class_type,
weapon_sprite_shift
) {
super(key_name, name);
super(data, key_name, name);
this.info = info;
this._sprite_base = sprite_base;
this._weapons_sprite_base = weapons_sprite_base;
Expand Down Expand Up @@ -1069,7 +1070,7 @@ export class MainChar extends Player {
this.current_exp = this.exp_curve[value - 1];
this.update_all();
} else {
console.warn("Target level out of range");
this.data.logger.log_message("Target level out of range");
}
}
}
36 changes: 19 additions & 17 deletions base/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ export class Map {
this.events[this_event_location_key].push(event);
}
} catch {
console.warn(`Tile Event "${property_key}" is not a valid JSON.`);
this.data.logger.log_message(`Tile Event "${property_key}" is not a valid JSON.`);
}
}

Expand Down Expand Up @@ -1006,14 +1006,14 @@ export class Map {
this.npcs.push(npc);
if (npc.label) {
if (npc.label in this._npcs_label_map) {
console.warn(`NPC with '${npc.label}' label is already set in this map.`);
this.data.logger.log_message(`NPC with '${npc.label}' label is already set in this map.`);
} else {
this._npcs_label_map[npc.label] = npc;
}
}
return npc;
} catch {
console.warn(`NPC "${property_key}" is not a valid JSON or does not exist in db file.`);
this.data.logger.log_message(`NPC "${property_key}" is not a valid JSON or does not exist in db file.`);
return null;
}
}
Expand Down Expand Up @@ -1135,15 +1135,17 @@ export class Map {
this.interactable_objects.push(interactable_object);
if (interactable_object.label) {
if (interactable_object.label in this.interactable_objects_label_map) {
console.warn(
this.data.logger.log_message(
`Interactable Object with '${interactable_object.label}' label is already set in this map.`
);
} else {
this.interactable_objects_label_map[interactable_object.label] = interactable_object;
}
}
} catch {
console.warn(`Interactable Object "${property_key}" is not a valid JSON or does not exist in db file.`);
this.data.logger.log_message(
`Interactable Object "${property_key}" is not a valid JSON or does not exist in db file.`
);
}
}

Expand Down Expand Up @@ -1255,7 +1257,7 @@ export class Map {
background_key: this_obj.properties?.background_key ?? null,
});
} catch {
console.warn(`Parties data is not a valid JSON in ${layer_name} layer.`);
this.data.logger.log_message(`Parties data is not a valid JSON in ${layer_name} layer.`);
}
});
return layer_name;
Expand Down Expand Up @@ -1316,7 +1318,7 @@ export class Map {
case "number":
return property_value > this.collision_layer ? property_value : -1;
default:
console.warn(`'${property_type}' is not a valid type for 'over'.`);
this.data.logger.log_message(`'${property_type}' is not a valid type for 'over'.`);
return -1;
}
}
Expand Down Expand Up @@ -1524,7 +1526,7 @@ export class Map {
event.fire(npc);
};
} else {
console.warn("Null NPC gotten...");
this.data.logger.log_message("Null NPC gotten...");
return () => {};
}
}
Expand Down Expand Up @@ -1575,7 +1577,7 @@ export class Map {
}
) {
if (key_name in this.generic_sprites) {
console.warn(`Generic sprite "${key_name}" already exists.`);
this.data.logger.log_message(`Generic sprite "${key_name}" already exists.`);
return null;
}
const sprite_base = this.data.info.misc_sprite_base_list[misc_sprite_key];
Expand Down Expand Up @@ -1626,10 +1628,10 @@ export class Map {
this.game_events.push(event);
});
} else {
console.warn("Map Game Events list is not an Array type.");
this.data.logger.log_message("Map Game Events list is not an Array type.");
}
} catch {
console.warn("Map Game Events list is not a valid JSON.");
this.data.logger.log_message("Map Game Events list is not a valid JSON.");
}
}

Expand Down Expand Up @@ -1719,14 +1721,14 @@ export class Map {
if (typeof this.sprite.properties.real_tile_width === "number") {
this.sprite.properties.real_tile_width = parseInt(this.sprite.properties.real_tile_width);
} else {
console.warn("Map real_tile_width property must be an integer.");
this.data.logger.log_message("Map real_tile_width property must be an integer.");
}
}
if (this.sprite.properties?.real_tile_height) {
if (typeof this.sprite.properties.real_tile_height === "number") {
this.sprite.properties.real_tile_height = parseInt(this.sprite.properties.real_tile_height);
} else {
console.warn("Map real_tile_height property must be an integer.");
this.data.logger.log_message("Map real_tile_height property must be an integer.");
}
}

Expand All @@ -1746,7 +1748,7 @@ export class Map {
},
};
} catch {
console.warn("The sanctum data is not a valid JSON.");
this.data.logger.log_message("The sanctum data is not a valid JSON.");
}
}

Expand All @@ -1771,15 +1773,15 @@ export class Map {
if (typeof this.sprite.properties.background_key === "string") {
this._background_key = this.sprite.properties.background_key;
} else {
console.warn("Map background_key property must be 'string'.");
this.data.logger.log_message("Map background_key property must be 'string'.");
}
}

if (this.sprite.properties?.expected_party_level) {
if (typeof this.sprite.properties.background_key === "number") {
this.expected_party_level = parseInt(this.sprite.properties.expected_party_level);
} else {
console.warn("Map background_key property must be an integer.");
this.data.logger.log_message("Map background_key property must be an integer.");
}
}

Expand All @@ -1798,7 +1800,7 @@ export class Map {
this._retreat_data = closest_retreat_point;
this._retreat_data.direction = directions[closest_retreat_point.direction as string];
} catch {
console.warn("The Retreat data is not a valid JSON.");
this.data.logger.log_message("The Retreat data is not a valid JSON.");
}
}

Expand Down
6 changes: 5 additions & 1 deletion base/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Effect, effect_types} from "./Effect";
import {elements, ordered_elements} from "./utils";
import * as _ from "lodash";
import {Subject} from "rxjs";
import {GoldenSun} from "GoldenSun";

export enum fighter_types {
ALLY = 1,
Expand Down Expand Up @@ -148,6 +149,8 @@ export const debuff_recovery_base_chances = {

/** The base class of MainChar and Enemy. */
export abstract class Player {
/** Main GoldenSun object. */
protected data: GoldenSun;
/** The player key name. */
public key_name: string;
/** The player name. */
Expand Down Expand Up @@ -247,7 +250,8 @@ export abstract class Player {
/** Stats points added/decreased by ability (buffers debuffers) on this player. */
public buff_stats: {[main_stat in main_stats]?: number};

constructor(key_name, name) {
constructor(data: GoldenSun, key_name: string, name: string) {
this.data = data;
this.key_name = key_name;
this.name = name;
this.temporary_status = new Set();
Expand Down
10 changes: 5 additions & 5 deletions base/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Storage {
return storage_types.POSITION;
break;
default:
console.warn(`Invalid data type for "${key}" storage key.`);
this.data.logger.log_message(`Invalid data type for "${key}" storage key.`);
return null;
}
};
Expand Down Expand Up @@ -114,7 +114,7 @@ export class Storage {
) {
const storage = engine_storage ? this.engine_storage : this.internal_storage;
if (key_name in storage) {
console.warn(`${key_name} already defined in game storage.`);
this.data.logger.log_message(`${key_name} already defined in game storage.`);
return;
}
storage[key_name] = {
Expand All @@ -135,7 +135,7 @@ export class Storage {
if (key_name in this.internal_storage) {
return this.internal_storage[key_name];
}
console.warn(`There's no storage value with key '${key_name}'.`);
this.data.logger.log_message(`There's no storage value with key '${key_name}'.`);
return null;
}

Expand All @@ -150,7 +150,7 @@ export class Storage {
} else if (key_name in this.engine_storage) {
return this.engine_storage[key_name].value;
}
console.warn(`There's no storage value with key '${key_name}'.`);
this.data.logger.log_message(`There's no storage value with key '${key_name}'.`);
return null;
}

Expand All @@ -163,7 +163,7 @@ export class Storage {
set(key_name: string, value: RawStorageRecord["value"], engine_storage: boolean = false) {
const storage = engine_storage ? this.engine_storage : this.internal_storage;
if (!(key_name in storage)) {
console.warn(`There's no storage value with key '${key_name}'.`);
this.data.logger.log_message(`There's no storage value with key '${key_name}'.`);
return;
}
storage[key_name].value = value;
Expand Down
2 changes: 1 addition & 1 deletion base/XGamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class Gamepad {
km.button_codes = buttons.map(get_button_code).filter(bc => bc !== undefined);
km.game_buttons = buttons.map(get_game_button).filter(bc => bc !== undefined);
if (km.button_codes.length !== km.game_buttons.length)
console.warn(`${button_code} not well recognized!`);
data.logger.log_message(`${button_code} not well recognized!`);
} else km.button_code = get_button_code(matches[matches.length - 1]);
return km;
}
Expand Down
Loading

0 comments on commit e8f68ca

Please sign in to comment.