Skip to content

Commit

Permalink
型判定をもう少し厳密にしてみる
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-arataki committed Apr 6, 2019
1 parent b22a1fc commit 9e0d3c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/App/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ export const choiceMagazine = async function() :Promise<m.Magazine> {
return magazine
}

export const choiceBulletGenerator = async function() :Promise<Function> {
export const choiceBulletGenerator = async function() :Promise<() => b.Bullet> {
const response = await prompts({
type: "select",
name: "target",
message: "どの弾丸を試しますか?",
choices: c.BulletFactory.choices()
})

const bulletGenerator :Function = c.BulletFactory[response.target]
const bulletGenerator: () => b.Bullet = c.BulletFactory[response.target]

return bulletGenerator
}

export const operate = async function(gun: g.Gun, magazine: m.Magazine, bulletGenerator: Function) : Promise<boolean> {
export const operate = async function(gun: g.Gun, magazine: m.Magazine, bulletGenerator: () => b.Bullet) : Promise<boolean> {
const response = await prompts({
type: "select",
name: "target",
Expand Down
58 changes: 23 additions & 35 deletions src/App/Factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as b from './Bullet'

interface Factory {
choices(): {title: string, value: string}[]
[key: string]: Function
[key: string]: any
}

export const GunFactory: Factory = {
Expand All @@ -16,18 +16,10 @@ export const GunFactory: Factory = {
{title: ".44オートマグ", value: "automag"}
]
},
glock17() {
return new g.GunGlock17
},
glock17l() {
return new g.GunGlock17L
},
m92f() {
return new g.GunM92F
},
automag() {
return new g.Gun44AutoMag
}
glock17: (): g.Gun => new g.GunGlock17,
glock17l: (): g.Gun => new g.GunGlock17L,
m92f: (): g.Gun => new g.GunM92F,
automag: (): g.Gun => new g.Gun44AutoMag
};

export const MagazineFactory: Factory = {
Expand All @@ -38,15 +30,9 @@ export const MagazineFactory: Factory = {
{title: ".44オートマグ用", value: "automag"}
]
},
glock17() {
return new m.MagazineGlockStandard
},
m92f() {
return new m.MagazineM92
},
automag() {
return new m.Magazine44AutoMag
}
glock17: ():m.Magazine => new m.MagazineGlockStandard,
m92f: (): m.Magazine => new m.MagazineM92,
automag: (): m.Magazine => new m.Magazine44AutoMag
};

export const BulletFactory: Factory = {
Expand All @@ -56,12 +42,8 @@ export const BulletFactory: Factory = {
{title: ".44口径(11.2mm)オートマチック用マグナム弾", value: "a44"}
]
},
p9mm() {
return new b.Bullet9mm
},
a44() {
return new b.Bullet44amp
}
p9mm: (): b.Bullet => new b.Bullet9mm,
a44: (): b.Bullet => new b.Bullet44amp
};

export const ActionFactory: Factory = {
Expand All @@ -76,25 +58,31 @@ export const ActionFactory: Factory = {
{title: "終了", value: "finish"}
]
},
shot(gun: g.Gun, mag: m.Magazine, bulletGenerator: Function) {
shot(gun: g.Gun, mag: m.Magazine, bulletGenerator: () => b.Bullet): boolean {
gun.shot()
return true
},
load(gun: g.Gun, mag: m.Magazine, bulletGenerator: Function) {
load(gun: g.Gun, mag: m.Magazine, bulletGenerator: () => b.Bullet): boolean {
gun.setBullet()
return true
},
loadDirect(gun: g.Gun, mag: m.Magazine, bulletGenerator: Function) {
loadDirect(gun: g.Gun, mag: m.Magazine, bulletGenerator: () => b.Bullet): boolean {
gun.setBullet(bulletGenerator())
return true
},
fullLoad(gun: g.Gun, mag: m.Magazine, bulletGenerator: Function) {
fullLoad(gun: g.Gun, mag: m.Magazine, bulletGenerator: () => b.Bullet): boolean {
mag.fullLoad(bulletGenerator)
return true
},
setMagazine(gun: g.Gun, mag: m.Magazine, bulletGenerator: Function) {
setMagazine(gun: g.Gun, mag: m.Magazine, bulletGenerator: () => b.Bullet): boolean {
gun.setMagazine(mag)
return true
},
unsetMagazine(gun: g.Gun, mag: m.Magazine, bulletGenerator: Function) {
unsetMagazine(gun: g.Gun, mag: m.Magazine, bulletGenerator: () => b.Bullet): boolean {
gun.unsetMagazine()
return true
},
finish() {
finish(): boolean {
return false
}
};
7 changes: 1 addition & 6 deletions src/App/Magazine/Magazine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@ export default abstract class Magazine
return this.stock.shift()
}

fullLoad(bulletGenerator: Function): void {
this.stock = []

let bullet: b.Bullet

fullLoad(bulletGenerator: () => b.Bullet): void {
for (let i = 0; i < this.max; i ++) {
bullet = bulletGenerator()
if (this.pushBullet(bulletGenerator()) === false) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as b from './App/Bullet'

const magazine :m.Magazine = await action.choiceMagazine()

const bulletGenerator :Function = await action.choiceBulletGenerator()
const bulletGenerator :() => b.Bullet = await action.choiceBulletGenerator()

// 操作
while(true) {
Expand Down

0 comments on commit 9e0d3c2

Please sign in to comment.