-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
796ac0c
commit ef31070
Showing
38 changed files
with
899 additions
and
6 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,3 +1,4 @@ | ||
export * from './req' | ||
export * from './types' | ||
export * from './user' | ||
export * from './structure' |
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,15 @@ | ||
import { structuresMap } from 'packages/core/src' | ||
import { req } from './req' | ||
|
||
export class StructureApi { | ||
/** | ||
* 获取我的建组 | ||
*/ | ||
static async getStructures() { | ||
return req<{ | ||
[type in keyof typeof structuresMap]: ConstructorParameters< | ||
(typeof structuresMap)[type] | ||
> | ||
}>('GET', '/structure') | ||
} | ||
} |
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,18 @@ | ||
import { ErrorCode } from '../../error/ErrorCode' | ||
import { GameError } from '../../error/GameError' | ||
import StructureService from '../../service/structure' | ||
import { UserModel } from '@star-angry/db' | ||
import { Context, Next } from 'koa' | ||
|
||
export default class StructureController { | ||
/** | ||
* 获取建筑 | ||
*/ | ||
static async getStructures(ctx: Context) { | ||
const { id } = (ctx.state.user?.data || {}) as UserModel | ||
if (!id) { | ||
throw new GameError(ErrorCode.PARAM_ERROR) | ||
} | ||
ctx.body = await StructureService.getStructures(id) | ||
} | ||
} |
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,8 @@ | ||
import StructureController from '../../controller/structure' | ||
import KoaRouter from 'koa-router' | ||
|
||
const structureRouter = new KoaRouter() | ||
|
||
structureRouter.get('/structure', StructureController.getStructures) | ||
|
||
export default structureRouter |
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,55 @@ | ||
import { EnergyStorage, processor, structuresMap } from '../../../../core/src' | ||
import { ErrorCode } from '../../error/ErrorCode' | ||
import { GameError } from '../../error/GameError' | ||
import { GameDB } from '@star-angry/db' | ||
|
||
export default class StructureService { | ||
/** | ||
* 获取建筑 | ||
*/ | ||
static async getStructures(userId: string) { | ||
const data = await GameDB.getDB().getData() | ||
|
||
const userData = data.userData[userId] | ||
if (!userData) { | ||
return {} | ||
} | ||
Object.values(userData.structure).forEach(async (structure) => { | ||
if ('update' in structure) { | ||
await StructureService.addIntent(userId, structure.id, 'update') | ||
} | ||
}) | ||
|
||
return userData.structure | ||
} | ||
|
||
/** | ||
* 添加意图 | ||
*/ | ||
static async addIntent(userId: string, id: string, type: string) { | ||
const data = await GameDB.getDB().getData() | ||
const userData = data.userData[userId] | ||
if (!userData) { | ||
throw new GameError(ErrorCode.PARAM_ERROR) | ||
} | ||
|
||
const getUserObject = (userId: string, objectId?: string) => { | ||
const userData = data.userData[userId] | ||
if (!userData) { | ||
return [] | ||
} | ||
if (!objectId) { | ||
return Object.values(userData.structure) | ||
} | ||
let object = | ||
userData.structure[objectId as keyof typeof userData.structure] | ||
if (!object) { | ||
object = userData.structure[ | ||
objectId as keyof typeof userData.structure | ||
] = new structuresMap[objectId as keyof typeof structuresMap]() as any | ||
} | ||
return [object!] | ||
} | ||
return processor({ objectId: id, type }, userId, getUserObject) | ||
} | ||
} |
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,28 @@ | ||
import { Server, Socket } from 'socket.io' | ||
import { Result } from '../../../utils/result' | ||
import { ErrorCode } from '../../../error/ErrorCode' | ||
import StructureService from '../../../service/structure' | ||
|
||
export const structureEventHandler = (socket: Socket, io: Server) => { | ||
// 获取自己的所有建筑 | ||
socket.on('getStructures', async (callback) => { | ||
const userId = socket.userId | ||
if (!userId) { | ||
return callback(Result.error(ErrorCode.PARAM_ERROR)) | ||
} | ||
|
||
const data = await StructureService.getStructures(userId) | ||
return callback(Result.success(data)) | ||
}) | ||
|
||
// 添加意图 | ||
socket.on('addIntent', async (id: string, type: string, callback) => { | ||
const userId = socket.userId | ||
if (!userId || !id) { | ||
return callback(Result.error(ErrorCode.PARAM_ERROR)) | ||
} | ||
|
||
const data = await StructureService.addIntent(userId, id, type) | ||
return callback(Result.success(data)) | ||
}) | ||
} |
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 @@ | ||
export * from './res' |
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,4 @@ | ||
export enum ResourceType { | ||
metal = 'metal', | ||
energy = 'energy', | ||
} |
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,4 @@ | ||
export * from './map' | ||
export * from './constant' | ||
export * from './structure' | ||
export * from './processor' |
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 @@ | ||
export * from './processor' |
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,8 @@ | ||
import { mine } from './mine' | ||
import { upgrade } from './upgrade' | ||
|
||
export default { | ||
update: mine, | ||
mine, | ||
upgrade, | ||
} |
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,15 @@ | ||
import { EnergyMine, EnergyStorage, StructureType } from '../../../structure' | ||
|
||
export const mine = (object: EnergyMine, planetObjects: StructureType[]) => { | ||
// 找到仓库 | ||
const storage = planetObjects.find((o) => o instanceof EnergyStorage) | ||
|
||
if (!storage) { | ||
return false | ||
} | ||
|
||
// 更新资源 | ||
const output = object.update() | ||
storage.addStore(output) | ||
return true | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/core/src/processor/intents/energy-mine/upgrade.ts
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,30 @@ | ||
import { | ||
EnergyMine, | ||
EnergyStorage, | ||
MetalStorage, | ||
StructureType, | ||
} from '../../../structure' | ||
|
||
export const upgrade = (object: EnergyMine, planetObjects: StructureType[]) => { | ||
// 找到仓库 | ||
const energyStorage = planetObjects.find((o) => o instanceof EnergyStorage) | ||
const metalStorage = planetObjects.find((o) => o instanceof MetalStorage) | ||
if (!energyStorage || !metalStorage) { | ||
return false | ||
} | ||
|
||
// 获取升级所需资源 | ||
const cost = object.calcUpgradeCost(object.level) | ||
if (energyStorage.store < cost.energy || metalStorage.store < cost.metal) { | ||
return false | ||
} | ||
|
||
// 更新资源 | ||
if (object.upgrade()) { | ||
energyStorage.addStore(-cost.energy) | ||
metalStorage.addStore(-cost.metal) | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,5 @@ | ||
import { upgrade } from './upgrade' | ||
|
||
export default { | ||
upgrade, | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/core/src/processor/intents/energy-storage/upgrade.ts
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,28 @@ | ||
import { EnergyStorage, MetalStorage, StructureType } from '../../../structure' | ||
|
||
export const upgrade = ( | ||
object: EnergyStorage, | ||
planetObjects: StructureType[], | ||
) => { | ||
// 找到仓库 | ||
const energyStorage = planetObjects.find((o) => o instanceof EnergyStorage) | ||
const metalStorage = planetObjects.find((o) => o instanceof MetalStorage) | ||
if (!energyStorage || !metalStorage) { | ||
return false | ||
} | ||
|
||
// 获取升级所需资源 | ||
const cost = object.calcUpgradeCost(object.level) | ||
if (energyStorage.store < cost.energy || metalStorage.store < cost.metal) { | ||
return false | ||
} | ||
|
||
// 更新资源 | ||
if (object.upgrade()) { | ||
energyStorage.addStore(-cost.energy) | ||
metalStorage.addStore(-cost.metal) | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,21 @@ | ||
import { StructureType } from '../../structure' | ||
import energyMineIntents from './energy-mine' | ||
import energyStorageIntents from './energy-storage' | ||
import metalMineIntents from './metal-mine' | ||
import metalStorageIntents from './metal-storage' | ||
|
||
const intentsMap = { | ||
energyMine: energyMineIntents, | ||
energyStorage: energyStorageIntents, | ||
metalMine: metalMineIntents, | ||
metalStorage: metalStorageIntents, | ||
} | ||
|
||
export const getIntentHandler = (object: StructureType, type: string) => { | ||
const intents = intentsMap[object.id as keyof typeof intentsMap] | ||
if (!intents) { | ||
return | ||
} | ||
|
||
return intents[type as keyof typeof intents] | ||
} |
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,8 @@ | ||
import { mine } from './mine' | ||
import { upgrade } from './upgrade' | ||
|
||
export default { | ||
update: mine, | ||
mine, | ||
upgrade, | ||
} |
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,14 @@ | ||
import { MetalMine, MetalStorage, StructureType } from '../../../structure' | ||
|
||
export const mine = (object: MetalMine, planetObjects: StructureType[]) => { | ||
// 找到仓库 | ||
const storage = planetObjects.find((o) => o instanceof MetalStorage) | ||
if (!storage) { | ||
return false | ||
} | ||
|
||
// 更新资源 | ||
const output = object.update() | ||
storage.addStore(output) | ||
return true | ||
} |
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,30 @@ | ||
import { | ||
EnergyStorage, | ||
MetalMine, | ||
MetalStorage, | ||
StructureType, | ||
} from '../../../structure' | ||
|
||
export const upgrade = (object: MetalMine, planetObjects: StructureType[]) => { | ||
// 找到仓库 | ||
const energyStorage = planetObjects.find((o) => o instanceof EnergyStorage) | ||
const metalStorage = planetObjects.find((o) => o instanceof MetalStorage) | ||
if (!energyStorage || !metalStorage) { | ||
return false | ||
} | ||
|
||
// 获取升级所需资源 | ||
const cost = object.calcUpgradeCost(object.level) | ||
if (energyStorage.store < cost.energy || metalStorage.store < cost.metal) { | ||
return false | ||
} | ||
|
||
// 更新资源 | ||
if (object.upgrade()) { | ||
energyStorage.addStore(-cost.energy) | ||
metalStorage.addStore(-cost.metal) | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,5 @@ | ||
import { upgrade } from './upgrade' | ||
|
||
export default { | ||
upgrade, | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/core/src/processor/intents/metal-storage/upgrade.ts
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,28 @@ | ||
import { EnergyStorage, MetalStorage, StructureType } from '../../../structure' | ||
|
||
export const upgrade = ( | ||
object: MetalStorage, | ||
planetObjects: StructureType[], | ||
) => { | ||
// 找到仓库 | ||
const energyStorage = planetObjects.find((o) => o instanceof EnergyStorage) | ||
const metalStorage = planetObjects.find((o) => o instanceof MetalStorage) | ||
if (!energyStorage || !metalStorage) { | ||
return false | ||
} | ||
|
||
// 获取升级所需资源 | ||
const cost = object.calcUpgradeCost(object.level) | ||
if (energyStorage.store < cost.energy || metalStorage.store < cost.metal) { | ||
return false | ||
} | ||
|
||
// 更新资源 | ||
if (object.upgrade()) { | ||
energyStorage.addStore(-cost.energy) | ||
metalStorage.addStore(-cost.metal) | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,22 @@ | ||
import { StructureType } from '../structure' | ||
import { getIntentHandler } from './intents' | ||
|
||
export const processor = ( | ||
intent: any, | ||
userId: string, | ||
getUserObject: (userId: string, objectId?: string) => StructureType[], | ||
) => { | ||
const { objectId, type } = intent | ||
const object = getUserObject(userId, objectId)[0] | ||
if (!object) { | ||
return false | ||
} | ||
|
||
const handler = getIntentHandler(object, type) | ||
if (!handler) { | ||
return false | ||
} | ||
|
||
const planetObjects = getUserObject(userId) | ||
return handler(object as any, planetObjects) | ||
} |
Oops, something went wrong.