From 66ebdd284784f48afbc29ce82a7a2305b75aaf68 Mon Sep 17 00:00:00 2001 From: Jacob Levertov Date: Fri, 16 Nov 2018 20:20:04 +0200 Subject: [PATCH 1/3] Created a classes to save player's data on the server. for #2 --- server/src/Rooms/GameRoom/BodyParts.ts | 13 +++++++++ server/src/Rooms/GameRoom/Essence.ts | 7 +++++ server/src/Rooms/GameRoom/Part.ts | 10 +++++++ server/src/Rooms/GameRoom/Player.ts | 28 ++++++++++++++++-- .../Rooms/GameRoom/PlayerCharactiristics.ts | 10 +++++++ server/src/Rooms/GameRoom/StatsAdditions.ts | 10 +++++++ server/src/Rooms/GameRoom/StatsConverter.ts | 23 +++++++++++++++ .../{BeastsDatabase.json => Characters.json} | 29 +++++++++++++++---- server/src/database/Essences.json | 13 +++++++++ 9 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 server/src/Rooms/GameRoom/BodyParts.ts create mode 100644 server/src/Rooms/GameRoom/Essence.ts create mode 100644 server/src/Rooms/GameRoom/Part.ts create mode 100644 server/src/Rooms/GameRoom/PlayerCharactiristics.ts create mode 100644 server/src/Rooms/GameRoom/StatsAdditions.ts create mode 100644 server/src/Rooms/GameRoom/StatsConverter.ts rename server/src/database/{BeastsDatabase.json => Characters.json} (92%) create mode 100644 server/src/database/Essences.json diff --git a/server/src/Rooms/GameRoom/BodyParts.ts b/server/src/Rooms/GameRoom/BodyParts.ts new file mode 100644 index 0000000..dff7be7 --- /dev/null +++ b/server/src/Rooms/GameRoom/BodyParts.ts @@ -0,0 +1,13 @@ +import { Part } from './Part'; + +export class BodyParts { + public head : Part; + public torso : Part; + public legs : Part; + + constructor(public characterName: string) { + this.head = new Part(characterName, 'Head'); + this.torso = new Part(characterName, 'Torso'); + this.legs = new Part(characterName, 'Legs'); + } +} diff --git a/server/src/Rooms/GameRoom/Essence.ts b/server/src/Rooms/GameRoom/Essence.ts new file mode 100644 index 0000000..2d48bcd --- /dev/null +++ b/server/src/Rooms/GameRoom/Essence.ts @@ -0,0 +1,7 @@ +import * as data from '../../database/Essences.json'; + +export class Essence { + public static getEssence(key : string) : number { + return data.Essence[key]; + } +} diff --git a/server/src/Rooms/GameRoom/Part.ts b/server/src/Rooms/GameRoom/Part.ts new file mode 100644 index 0000000..9aea158 --- /dev/null +++ b/server/src/Rooms/GameRoom/Part.ts @@ -0,0 +1,10 @@ +import { StatsAdditions } from './StatsAdditions'; +import { StatsConverter } from './StatsConverter'; + +export class Part{ + public stats: StatsAdditions; + + constructor(public characterName: string, public part : string) { + this.stats = StatsConverter.getStats(characterName, part); + } +} diff --git a/server/src/Rooms/GameRoom/Player.ts b/server/src/Rooms/GameRoom/Player.ts index 135c9ff..7163d24 100644 --- a/server/src/Rooms/GameRoom/Player.ts +++ b/server/src/Rooms/GameRoom/Player.ts @@ -1,15 +1,37 @@ import { Position } from './Position'; +import { PlayerCharactiristics } from './PlayerCharactiristics'; +import { BodyParts } from './BodyParts'; +import { Essence } from './Essence'; export class Player { private static playerCounter: number; - public name: string; + public score : number; + public currentKarma: number; + public currentHealth: number; + public currentHealthRegeneration: number; + public currentDamage: number; + public currentMovementSpeed: number; + public currentArmor: number; + public essence: number; + public bodyParts: BodyParts; + public charactirisrics: PlayerCharactiristics; constructor( public position: Position = new Position(), public rotation = 0, - ) { - this.name = `player${Player.playerCounter}`; + public name: string = `player${Player.playerCounter}`, + public team: string = 'team ' + `player${Player.playerCounter}`) { Player.playerCounter += 1; + this.score = 0; + this.currentHealth = 100; + this.charactirisrics = new PlayerCharactiristics(); + this.bodyParts = new BodyParts('Human'); + this.currentKarma = this.charactirisrics.basicKarma; + this.currentHealthRegeneration = this.charactirisrics.basicHealthRegeneration; + this.currentDamage = this.charactirisrics.basicDamage; + this.currentMovementSpeed = this.charactirisrics.basicMovementSpeed; + this.currentArmor = this.charactirisrics.basicArmor; + this.essence = Essence.getEssence('NEAUTRAL'); } } diff --git a/server/src/Rooms/GameRoom/PlayerCharactiristics.ts b/server/src/Rooms/GameRoom/PlayerCharactiristics.ts new file mode 100644 index 0000000..711c8d1 --- /dev/null +++ b/server/src/Rooms/GameRoom/PlayerCharactiristics.ts @@ -0,0 +1,10 @@ +export class PlayerCharactiristics{ + constructor(public basicKarma : number = 0, + public basicHealthRegeneration : number = 0.5, + public basicDamage : number = 10, + public basicMovementSpeed : number = 25, + public basicArmor : number = 0, + public maxMovementSpeed: number = 250, + public maxArmor: number = 100, + public maxKarma: number = 150) {} +} diff --git a/server/src/Rooms/GameRoom/StatsAdditions.ts b/server/src/Rooms/GameRoom/StatsAdditions.ts new file mode 100644 index 0000000..d6be247 --- /dev/null +++ b/server/src/Rooms/GameRoom/StatsAdditions.ts @@ -0,0 +1,10 @@ +import { Essence } from './Essence'; + +export class StatsAdditions { + constructor(public health : number = 0, + public healthRegeneration: number = 0, + public armor : number = 0, + public baseDamage: number = 0, + public karma : number = 0, + public movementSpeed : number = 0) {} +} diff --git a/server/src/Rooms/GameRoom/StatsConverter.ts b/server/src/Rooms/GameRoom/StatsConverter.ts new file mode 100644 index 0000000..b65c804 --- /dev/null +++ b/server/src/Rooms/GameRoom/StatsConverter.ts @@ -0,0 +1,23 @@ +import * as data from '../../database/Characters.json'; +import { StatsAdditions } from './StatsAdditions'; + +export class StatsConverter { + public static getStats (name : string = 'Human', part : string) : StatsAdditions { + const databasePart = data[name][part]; + const health = StatsConverter.filterState(databasePart['HP']); + const healthRegeneration = StatsConverter.filterState(databasePart['HRP']); + const armor = StatsConverter.filterState(databasePart['AR']); + const baseDamage = StatsConverter.filterState(databasePart['BD']); + const karma = StatsConverter.filterState(databasePart['CK']); + const movementSpeed = StatsConverter.filterState(databasePart['MS']); + return new StatsAdditions(health, healthRegeneration, armor, + baseDamage, karma, movementSpeed); + } + + private static filterState(state : any) : number { + if (state === 'undefined') { + return 0; + } + return state; + } +} diff --git a/server/src/database/BeastsDatabase.json b/server/src/database/Characters.json similarity index 92% rename from server/src/database/BeastsDatabase.json rename to server/src/database/Characters.json index 0820653..35ffd59 100644 --- a/server/src/database/BeastsDatabase.json +++ b/server/src/database/Characters.json @@ -92,7 +92,7 @@ "Name": "Elwyn's Heart", "Description": "You take the unicorn's heart and become a little bit unicorn yourself.", "Stats": { - "HPR": "2.5", + "HRP": "2.5", "HP": "50" }, "Active": { @@ -122,7 +122,7 @@ "Stats": { "AR": "10", "MS": "15", - "HPR": "2", + "HRP": "2", "BD": "10" } }, @@ -168,7 +168,7 @@ "Evil": { "AR": "20 + 0.15 * CK", "HP": "35 + 0.15 * CK", - "HPR": "1.5" + "HRP": "1.5" } } }, @@ -218,7 +218,7 @@ "Description": "You wear the lions mighty mane as if you were the king of the jungle.", "Stats": { "HP": "80", - "HPR": "2", + "HRP": "2", "AR": "5", "MS": "20", "BD": "10" @@ -251,7 +251,7 @@ "Stats": { "MS": "-30", "HP": "20", - "HPR": "2" + "HRP": "2" }, "Active": { "Effects": [ @@ -286,7 +286,7 @@ "Type": "Change stats", "Stats": { "MS": "25", - "HPR": "5", + "HRP": "5", "AR": "15" }, "Duration": "5" @@ -326,5 +326,22 @@ "Name": "Loxodon", "Title": "The elephant. Biggest of them all.", "Essence": "Good" + }, + "Hunman": { + "Head": { + "Name": "Human Eyes", + "Description": "Basic human eyes, nothing special with those" + }, + "Torso": { + "Name": "Human Torso", + "Description": "Basic human torso, nothing special with those" + }, + "Legs": { + "Name": "Human Legs", + "Description": "Basic human legs, nothing special with those" + }, + "Name": "Human", + "Title": "The basic of them all", + "Essence": "Neutral" } } diff --git a/server/src/database/Essences.json b/server/src/database/Essences.json new file mode 100644 index 0000000..d72292a --- /dev/null +++ b/server/src/database/Essences.json @@ -0,0 +1,13 @@ +{ + "Essence": { + "NEUTRAL": 0, + "GOOD": 1, + "BENEVOLENT": 2, + "RIGHTEOUS": 3, + "DIVINE": 4, + "EVIL": -1, + "MALICIOUS": -2, + "NEFARIOUS": -3, + "SINISTER": -4 + } +} \ No newline at end of file From 607147ef1bb0f1a5f9236aae2a38cf71c3ee4a35 Mon Sep 17 00:00:00 2001 From: jlevertov Date: Sun, 18 Nov 2018 21:35:16 +0200 Subject: [PATCH 2/3] Update StatsConverter.ts --- server/src/Rooms/GameRoom/StatsConverter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/Rooms/GameRoom/StatsConverter.ts b/server/src/Rooms/GameRoom/StatsConverter.ts index b65c804..ff9c8fa 100644 --- a/server/src/Rooms/GameRoom/StatsConverter.ts +++ b/server/src/Rooms/GameRoom/StatsConverter.ts @@ -3,7 +3,7 @@ import { StatsAdditions } from './StatsAdditions'; export class StatsConverter { public static getStats (name : string = 'Human', part : string) : StatsAdditions { - const databasePart = data[name][part]; + const databasePart = data[name][part]['Stats']; const health = StatsConverter.filterState(databasePart['HP']); const healthRegeneration = StatsConverter.filterState(databasePart['HRP']); const armor = StatsConverter.filterState(databasePart['AR']); From 52b2180a9681f877a7f606ec345cb6d8c9ea5fa7 Mon Sep 17 00:00:00 2001 From: Jacob Levertov Date: Fri, 16 Nov 2018 20:20:04 +0200 Subject: [PATCH 3/3] Created a classes to save player's data on the server. for #2 --- server/src/Rooms/GameRoom/BodyParts.ts | 18 ++++++++---- server/src/Rooms/GameRoom/Essence.ts | 6 ++-- server/src/Rooms/GameRoom/Part.ts | 3 +- server/src/Rooms/GameRoom/Player.ts | 20 ++++++------- .../Rooms/GameRoom/PlayerCharactiristics.ts | 10 ------- server/src/Rooms/GameRoom/PlayerStats.ts | 12 ++++++++ server/src/Rooms/GameRoom/StatsAdditions.ts | 17 +++++------ server/src/Rooms/GameRoom/StatsConverter.ts | 28 ++++++++----------- server/src/database/Characters.json | 2 +- server/src/index.ts | 4 +++ 10 files changed, 65 insertions(+), 55 deletions(-) delete mode 100644 server/src/Rooms/GameRoom/PlayerCharactiristics.ts create mode 100644 server/src/Rooms/GameRoom/PlayerStats.ts diff --git a/server/src/Rooms/GameRoom/BodyParts.ts b/server/src/Rooms/GameRoom/BodyParts.ts index dff7be7..a662bdd 100644 --- a/server/src/Rooms/GameRoom/BodyParts.ts +++ b/server/src/Rooms/GameRoom/BodyParts.ts @@ -1,13 +1,19 @@ import { Part } from './Part'; +export enum BodyPart { + Head = 'Head', + Torso = 'Torso', + Legs = 'Legs', +} + export class BodyParts { - public head : Part; - public torso : Part; - public legs : Part; + public head: Part; + public torso: Part; + public legs: Part; constructor(public characterName: string) { - this.head = new Part(characterName, 'Head'); - this.torso = new Part(characterName, 'Torso'); - this.legs = new Part(characterName, 'Legs'); + this.head = new Part(characterName, BodyPart.Head); + this.torso = new Part(characterName, BodyPart.Torso); + this.legs = new Part(characterName, BodyPart.Legs); } } diff --git a/server/src/Rooms/GameRoom/Essence.ts b/server/src/Rooms/GameRoom/Essence.ts index 2d48bcd..534a6d5 100644 --- a/server/src/Rooms/GameRoom/Essence.ts +++ b/server/src/Rooms/GameRoom/Essence.ts @@ -1,7 +1,7 @@ -import * as data from '../../database/Essences.json'; +import * as jsonData from '../../database/Essences.json'; export class Essence { - public static getEssence(key : string) : number { - return data.Essence[key]; + public static getEssence(key: string): number { + return (jsonData).Essence[key]; } } diff --git a/server/src/Rooms/GameRoom/Part.ts b/server/src/Rooms/GameRoom/Part.ts index 9aea158..e74a69e 100644 --- a/server/src/Rooms/GameRoom/Part.ts +++ b/server/src/Rooms/GameRoom/Part.ts @@ -1,10 +1,11 @@ import { StatsAdditions } from './StatsAdditions'; import { StatsConverter } from './StatsConverter'; +import { BodyPart } from './BodyParts'; export class Part{ public stats: StatsAdditions; - constructor(public characterName: string, public part : string) { + constructor(public characterName: string, public part: BodyPart) { this.stats = StatsConverter.getStats(characterName, part); } } diff --git a/server/src/Rooms/GameRoom/Player.ts b/server/src/Rooms/GameRoom/Player.ts index 7163d24..658d470 100644 --- a/server/src/Rooms/GameRoom/Player.ts +++ b/server/src/Rooms/GameRoom/Player.ts @@ -1,5 +1,5 @@ import { Position } from './Position'; -import { PlayerCharactiristics } from './PlayerCharactiristics'; +import { PlayerStats } from './PlayerStats'; import { BodyParts } from './BodyParts'; import { Essence } from './Essence'; @@ -15,7 +15,7 @@ export class Player { public currentArmor: number; public essence: number; public bodyParts: BodyParts; - public charactirisrics: PlayerCharactiristics; + public stats: PlayerStats; constructor( public position: Position = new Position(), @@ -25,13 +25,13 @@ export class Player { Player.playerCounter += 1; this.score = 0; this.currentHealth = 100; - this.charactirisrics = new PlayerCharactiristics(); - this.bodyParts = new BodyParts('Human'); - this.currentKarma = this.charactirisrics.basicKarma; - this.currentHealthRegeneration = this.charactirisrics.basicHealthRegeneration; - this.currentDamage = this.charactirisrics.basicDamage; - this.currentMovementSpeed = this.charactirisrics.basicMovementSpeed; - this.currentArmor = this.charactirisrics.basicArmor; - this.essence = Essence.getEssence('NEAUTRAL'); + this.stats = new PlayerStats(); + this.bodyParts = new BodyParts('Lion'); + this.currentKarma = this.stats.basicKarma; + this.currentHealthRegeneration = this.stats.basicHealthRegeneration; + this.currentDamage = this.stats.basicDamage; + this.currentMovementSpeed = this.stats.basicMovementSpeed; + this.currentArmor = this.stats.basicArmor; + this.essence = Essence.getEssence('NEUTRAL'); } } diff --git a/server/src/Rooms/GameRoom/PlayerCharactiristics.ts b/server/src/Rooms/GameRoom/PlayerCharactiristics.ts deleted file mode 100644 index 711c8d1..0000000 --- a/server/src/Rooms/GameRoom/PlayerCharactiristics.ts +++ /dev/null @@ -1,10 +0,0 @@ -export class PlayerCharactiristics{ - constructor(public basicKarma : number = 0, - public basicHealthRegeneration : number = 0.5, - public basicDamage : number = 10, - public basicMovementSpeed : number = 25, - public basicArmor : number = 0, - public maxMovementSpeed: number = 250, - public maxArmor: number = 100, - public maxKarma: number = 150) {} -} diff --git a/server/src/Rooms/GameRoom/PlayerStats.ts b/server/src/Rooms/GameRoom/PlayerStats.ts new file mode 100644 index 0000000..800d4e5 --- /dev/null +++ b/server/src/Rooms/GameRoom/PlayerStats.ts @@ -0,0 +1,12 @@ +export class PlayerStats{ + constructor( + public basicKarma : number = 0, + public basicHealthRegeneration : number = 0.5, + public basicDamage : number = 10, + public basicMovementSpeed : number = 25, + public basicArmor : number = 0, + public maxMovementSpeed: number = 250, + public maxArmor: number = 100, + public maxKarma: number = 150, + ) {} +} diff --git a/server/src/Rooms/GameRoom/StatsAdditions.ts b/server/src/Rooms/GameRoom/StatsAdditions.ts index d6be247..1d611bf 100644 --- a/server/src/Rooms/GameRoom/StatsAdditions.ts +++ b/server/src/Rooms/GameRoom/StatsAdditions.ts @@ -1,10 +1,11 @@ -import { Essence } from './Essence'; - export class StatsAdditions { - constructor(public health : number = 0, - public healthRegeneration: number = 0, - public armor : number = 0, - public baseDamage: number = 0, - public karma : number = 0, - public movementSpeed : number = 0) {} + constructor( + public health : number = 0, + public healthRegeneration: number = 0, + public armor : number = 0, + public baseDamage: number = 0, + public karma : number = 0, + public movementSpeed : number = 0, + ) { + } } diff --git a/server/src/Rooms/GameRoom/StatsConverter.ts b/server/src/Rooms/GameRoom/StatsConverter.ts index ff9c8fa..d3857c3 100644 --- a/server/src/Rooms/GameRoom/StatsConverter.ts +++ b/server/src/Rooms/GameRoom/StatsConverter.ts @@ -1,23 +1,19 @@ import * as data from '../../database/Characters.json'; import { StatsAdditions } from './StatsAdditions'; +import { BodyPart } from './BodyParts.js'; export class StatsConverter { - public static getStats (name : string = 'Human', part : string) : StatsAdditions { - const databasePart = data[name][part]['Stats']; - const health = StatsConverter.filterState(databasePart['HP']); - const healthRegeneration = StatsConverter.filterState(databasePart['HRP']); - const armor = StatsConverter.filterState(databasePart['AR']); - const baseDamage = StatsConverter.filterState(databasePart['BD']); - const karma = StatsConverter.filterState(databasePart['CK']); - const movementSpeed = StatsConverter.filterState(databasePart['MS']); - return new StatsAdditions(health, healthRegeneration, armor, - baseDamage, karma, movementSpeed); + public static getStats(name: string = 'Human', part: BodyPart) : StatsAdditions { + const databasePart = (data)[name][part].Stats || {}; + const health = StatsConverter.filterState(databasePart.HP); + const healthRegeneration = StatsConverter.filterState(databasePart.HPR); + const armor = StatsConverter.filterState(databasePart.AR); + const baseDamage = StatsConverter.filterState(databasePart.BD); + const karma = StatsConverter.filterState(databasePart.CK); + const movementSpeed = StatsConverter.filterState(databasePart.MS); + return new StatsAdditions( + health, healthRegeneration, armor, baseDamage, karma, movementSpeed); } - private static filterState(state : any) : number { - if (state === 'undefined') { - return 0; - } - return state; - } + private static filterState = (state: any): number => state || 0; } diff --git a/server/src/database/Characters.json b/server/src/database/Characters.json index 35ffd59..685af33 100644 --- a/server/src/database/Characters.json +++ b/server/src/database/Characters.json @@ -327,7 +327,7 @@ "Title": "The elephant. Biggest of them all.", "Essence": "Good" }, - "Hunman": { + "Human": { "Head": { "Name": "Human Eyes", "Description": "Basic human eyes, nothing special with those" diff --git a/server/src/index.ts b/server/src/index.ts index 42ab95a..f8bbdfe 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -7,6 +7,7 @@ import { GameRoom } from './Rooms/GameRoom/GameRoom'; import { FreeForAllLobbyRoom, TeamLobbyRoom } from './Rooms/GameLobbyRoom'; import { ruvenDebug, debugErrors } from './loggers'; import { subscribeToGameStart } from './Rooms/GameLobbyRoom/LobbyRoom'; +import { Player } from './Rooms/GameRoom/Player'; const app: express.Application = express(); const port: number = Number(process.env.PORT) || 3000; @@ -39,6 +40,9 @@ async function main() { gameServer.listen(port, undefined, undefined, () => { ruvenDebug('Server is listening on port %d', port); }); + + const p = new Player(); + debugErrors('%O', p); } main().catch(e => debugErrors('Something went wrong: %O', e));