Skip to content

Commit

Permalink
fix: ユーザーとアバターの関連性
Browse files Browse the repository at this point in the history
アバター取得は非同期であるべき、かつ
初期に設定すべき、なので

プレイヤーに静的非同期メソッドで初期化する
外部でプレイヤーをインスタンス化する場合はNew関数を使用すること
  • Loading branch information
CornerSyrup committed Jun 22, 2022
1 parent 0b05fe1 commit 190cff4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
32 changes: 32 additions & 0 deletions core/Account/User.test.ts
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);
});
});
});
19 changes: 10 additions & 9 deletions core/Account/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ export class LoginOptions {
}

export class Player extends User {
private _avatar?: Avatar;

public get avatar(): Avatar {
return (this._avatar = this._avatar ?? this.repo.getAvatar(this.accountId));
}

public set avatar(value: Avatar) {
this._avatar = value;
}
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;
}
}

0 comments on commit 190cff4

Please sign in to comment.