-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
168 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * as quest from "application/Quest"; | ||
export * from "./Quest"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Item } from "core"; | ||
|
||
export enum Race { | ||
Egg, | ||
Plant, | ||
} | ||
|
||
export class Avatar { | ||
/** | ||
* Level function is `50x ^ 2`. | ||
*/ | ||
public get level(): number { | ||
return this.totalExp == 0 ? 0 : Math.sqrt(this.totalExp / 50); | ||
} | ||
|
||
constructor(public race: Race, public totalExp: number) {} | ||
|
||
public applyItem(...items: Item[]): void { | ||
items.forEach((item) => { | ||
item.takeEffect(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IAccountRepository } from "infrastructure"; | ||
import { Avatar, Race } from "./Avatar"; | ||
import { Player, User } from "./User"; | ||
|
||
describe("User's functionality", () => { | ||
test("password hashing", () => { | ||
const cases = [ | ||
{ raw: "password", hash: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" }, | ||
{ raw: "cello", hash: "9bbf02efd82322aadc5d06c9bcf35bb4b0e3302ca158dc800407be1a4fea67e2" }, | ||
]; | ||
|
||
cases.forEach((c) => { | ||
expect(User.hashPassword(c.raw)).toBe(c.hash.toLowerCase()); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Player's functionality", () => { | ||
test("player level", () => { | ||
[ | ||
{ exp: 0, lv: 0 }, | ||
{ exp: 1250, lv: 5 }, | ||
{ exp: 5000, lv: 10 }, | ||
].forEach(async (c) => { | ||
const repoMock = jest.fn<Avatar, []>(() => new Avatar(Race.Egg, c.exp)); | ||
const p = await Player.New({ getAvatar: repoMock } as unknown as IAccountRepository, "", ""); | ||
const lv = p.level; | ||
expect(repoMock).toBeCalledTimes(1); | ||
expect(lv).toBe(c.lv); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Avatar } from "core"; | ||
import crypto from "crypto"; | ||
import { IAccountRepository } from "infrastructure"; | ||
|
||
export abstract class User { | ||
public password?: string; | ||
|
||
constructor(public repo: IAccountRepository, public accountId: string, public email: string) {} | ||
|
||
public async login(options: LoginOptions): Promise<boolean> { | ||
if (this.password === undefined) { | ||
this.password = await this.repo.getUserPassword(this.accountId); | ||
} | ||
|
||
let result: boolean = this.password === User.hashPassword(options.password); | ||
let timestamp: number = Date.now(); | ||
|
||
if (result) { | ||
this.repo.setLastLogin(timestamp); | ||
} else { | ||
this.repo.setLastLoginAttempt(timestamp); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static hashPassword(password: string): string { | ||
return crypto.createHash("sha256", {}).update(password).digest("hex"); | ||
} | ||
|
||
public static register(repo: IAccountRepository, email: string, password: string): Promise<void> { | ||
password = this.hashPassword(password); | ||
return Promise.resolve( | ||
repo.registerNewUser({ repo: repo, accountId: "", email: email, password: password } as User) | ||
); | ||
} | ||
|
||
public async updatePassword(password: string): Promise<boolean> { | ||
this.password = this.password ?? (await this.repo.getUserPassword(this.accountId)); | ||
|
||
password = User.hashPassword(password); | ||
|
||
if (password === this.password) { | ||
return false; | ||
} | ||
|
||
this.repo.updateUserPassword(this.accountId, password); | ||
return true; | ||
} | ||
|
||
public upgradeToPlayer(avatar: Avatar): Player { | ||
const p = new Player(this.repo, this.accountId, this.email); | ||
p.avatar = avatar; | ||
return p; | ||
} | ||
} | ||
|
||
export class LoginOptions { | ||
constructor(public email: string, public password: string) {} | ||
} | ||
|
||
export class Player extends User { | ||
public avatar!: Avatar; | ||
|
||
public get level(): number { | ||
return this.avatar.level; | ||
} | ||
|
||
/** | ||
* プレイヤーをインスタンス化する場合は`New`を使ってください。 | ||
*/ | ||
constructor(repo: IAccountRepository, accountId: string, email: string) { | ||
super(repo, accountId, email); | ||
} | ||
|
||
public static async New(repo: IAccountRepository, accountId: string, email: string): Promise<Player> { | ||
const p = new Player(repo, accountId, email); | ||
p.avatar = await p.repo.getAvatar(p.accountId); | ||
return p; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from "./Account/Avatar"; | ||
export * from "./Account/User"; | ||
export * from "./Quest/Answer"; | ||
export * from "./Quest/Effect"; | ||
export * from "./Quest/Item"; | ||
export * from "./Quest/Quest"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * as domain from "./domain"; | ||
export * as domain from "./core"; | ||
export * as infrastructure from "./infrastructure"; | ||
export * as application from "./application"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters