generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EventBus and Use common event name (#29)
* Implement EventBus and Use common event name * Remove only hint in surrender e2e test * Format code by linter
- Loading branch information
Showing
30 changed files
with
330 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,10 @@ | ||
enum EVENT_NAME { | ||
ARATA = 'ArataEvent', | ||
FURIGOMA = 'FurigomaEvent', | ||
CREATE_GUNGI = 'CreateGungiEvent', | ||
SURRENDER = 'SurrenderEvent', | ||
CONFIGURATION = 'ConfigurationEvent', | ||
GET_GUNGI = 'GetGungiEvent', | ||
} | ||
|
||
export default EVENT_NAME; |
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
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,5 +1,7 @@ | ||
import EVENT_NAME from '../constant/EVENT_NAME'; | ||
|
||
interface Event { | ||
name: string; | ||
name: EVENT_NAME; | ||
data: any; | ||
} | ||
|
||
|
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
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,12 +1,25 @@ | ||
import { Event } from '../../domain/events/Event'; | ||
import EventHandler from './eventHandler/EventHandler'; | ||
import EventBus from '../../usecases/EventBus'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Server } from 'socket.io'; | ||
|
||
@Injectable() | ||
export default class ImplEventBus extends EventBus { | ||
broadcast(events) { | ||
// TODO: should implement | ||
events.forEach((event) => { | ||
console.log('有收到event囉'); | ||
export class ImplEventBus extends EventBus { | ||
private eventHandler: EventHandler; | ||
private server: Server; | ||
|
||
constructor(server: Server, eventHandler: EventHandler) { | ||
super(); | ||
this.eventHandler = eventHandler; | ||
this.server = server; | ||
} | ||
|
||
broadcast(roomId: string, events: Event[]) { | ||
events.forEach((event: Event) => { | ||
const eventName = event.name; | ||
const view = this.eventHandler.handle(event); | ||
if (view) { | ||
this.server.to(roomId).emit(eventName, view); | ||
} | ||
}); | ||
} | ||
} |
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 { Event } from '../../../domain/events/Event'; | ||
|
||
type ReturnEventHandler = { | ||
eventName: string; | ||
view: any; | ||
}; | ||
|
||
export default abstract class EventHandler { | ||
constructor(private next: EventHandler | null) {} | ||
|
||
handle(event: Event) { | ||
if (this.match(event)) { | ||
return this.doHandle(event); | ||
} else if (this.next != null) { | ||
return this.next.handle(event); | ||
} | ||
} | ||
|
||
abstract match(event: Event): boolean; | ||
|
||
abstract doHandle(event: Event): ReturnEventHandler; | ||
} |
26 changes: 26 additions & 0 deletions
26
server/src/gateway/eventBus/eventHandler/class/ArataEventHandler.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,26 @@ | ||
import EventHandler from '../EventHandler'; | ||
import ArataEvent from '../../../../domain/events/ArataEvent'; | ||
import { Event } from '../../../../domain/events/Event'; | ||
import EVENT_NAME from '../../../../domain/constant/EVENT_NAME'; | ||
|
||
export default class ArataEventHandler extends EventHandler { | ||
doHandle(event: ArataEvent): any { | ||
const { goma, to } = event.data; | ||
|
||
return { | ||
goma: { | ||
name: goma.name, | ||
side: goma.side, | ||
}, | ||
to: { | ||
x: to.x, | ||
y: to.y, | ||
z: to.z, | ||
}, | ||
}; | ||
} | ||
|
||
match(event: Event): boolean { | ||
return event.name === EVENT_NAME.ARATA; | ||
} | ||
} |
Oops, something went wrong.