Advertisement
jeffdeon

ActionEngine

Oct 11th, 2020
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum ActionTypes {
  2.     WantToPlaceRoad,
  3.     PlaceRoad,
  4.     WantToPlaceShip,
  5.     PlaceShip,
  6. }
  7.  
  8. enum NotificationType {
  9.     RoadWasPlaced,
  10.     ShipWasPlaced,
  11. }
  12.  
  13. class Game {
  14.     actionEngine: ActionEngine
  15.     mechanics
  16.     broadcast: Broadcast
  17.     road: Road
  18.     optionalMechanics: Mechanic[]
  19.     longestRoadController: LongestRaodController
  20.  
  21.     constructor() {
  22.         this.broadcast = new Broadcast()
  23.         this.actionEngine = new ActionEngine(this)
  24.         this.road = new Road(this)
  25.         this.longestRoadController = new LongestRaodController(this)
  26.         this.optionalMechanics = []
  27.     }
  28.  
  29.     gotAction(value: string, type: ActionTypes) {
  30.         this.actionEngine.request(value, type)
  31.     }
  32. }
  33.  
  34. class Broadcast {
  35.     broadcastToSocket(data: string) {
  36.         console.log('*Broadcasting: ' + data)
  37.     }
  38. }
  39. // ---
  40.  
  41. class ActionEngine {
  42.     parentGame: Game
  43.     constructor(parentGame: Game) {
  44.         this.parentGame = parentGame
  45.     }
  46.  
  47.     observers: Observer[] = []
  48.     registerObserver(observer: Observer) {
  49.         this.observers.push(observer)
  50.     }
  51.  
  52.     notify(value: string, type: NotificationType) {
  53.         for (const observer of this.observers) {
  54.             observer.onNotify(value, type)
  55.         }
  56.     }
  57.  
  58.     mechanics: Mechanic[] = []
  59.     registerMechanic(mechanic: Mechanic) {
  60.         this.mechanics.push(mechanic)
  61.     }
  62.  
  63.     request(value: string, type: ActionTypes) {
  64.         for(const mechanic of this.mechanics) {
  65.             mechanic.onRequest(value, type)
  66.         }
  67.     }
  68. }
  69.  
  70. abstract class Mechanic {
  71.  
  72.     abstract onRequest(value: string, type: ActionTypes)
  73.  
  74.     parentGame: Game
  75.     constructor(parentGame: Game) {
  76.         this.parentGame = parentGame
  77.         this.parentGame.actionEngine.registerMechanic(this)
  78.     }
  79.  
  80.     protected actionComplete(value: string, type: NotificationType) {
  81.         this.parentGame.actionEngine.notify(value, type)
  82.     }
  83. }
  84.  
  85. abstract class Observer {
  86.  
  87.     abstract onNotify(value: string, type: NotificationType)
  88.  
  89.     parentGame: Game
  90.     constructor(parentGame: Game) {
  91.         this.parentGame = parentGame
  92.         this.parentGame.actionEngine.registerObserver(this)
  93.     }
  94.  
  95. }
  96.  
  97. // ============================
  98. // ============================
  99. // ============================
  100.  
  101. class Road extends Mechanic {
  102.  
  103.     parentGame: Game
  104.  
  105.     constructor(parentGame: Game) {
  106.         super(parentGame)
  107.     }
  108.  
  109.     onRequest(value: string, type: ActionTypes) {
  110.         if(type == ActionTypes.WantToPlaceRoad) {
  111.             console.log('player clicked want to place road button')
  112.             // logic to check if move is ok
  113.             // this.parentGame.changeActionState(ActionState.wantToPlaceRoad)
  114.         }
  115.         if(type == ActionTypes.PlaceRoad) {
  116.             console.log('player click node and is trying to place road at edge: *pass road index data*')
  117.             // logic to check if move is ok
  118.             this.placeRoad()
  119.         }
  120.     }
  121.  
  122.     private placeRoad() {
  123.         this.parentGame.broadcast.broadcastToSocket('update road UI')
  124.         console.log('-Placed road! gonna notify all that are listening')
  125.         this.actionComplete('road placed', NotificationType.RoadWasPlaced)
  126.     }
  127. }
  128. // ============================
  129. // ============================
  130. // ============================
  131.  
  132. class Ship extends Mechanic {
  133.  
  134.     parentGame: Game
  135.  
  136.     constructor(parentGame: Game) {
  137.         super(parentGame)
  138.     }
  139.  
  140.     onRequest(value: string, type: ActionTypes) {
  141.         if(type == ActionTypes.WantToPlaceShip) {
  142.             console.log('player clicked want to place ship button')
  143.             // logic to check if move is ok
  144.             // this.parentGame.changeActionState(ActionState.wantToPlaceRoad)
  145.         }
  146.         if(type == ActionTypes.PlaceShip) {
  147.             console.log('player click node and is trying to place ship at edge: *pass road index data*')
  148.             // logic to check if move is ok
  149.             this.placeShip()
  150.         }
  151.     }
  152.  
  153.     private placeShip() {
  154.         this.parentGame.broadcast.broadcastToSocket('update ship UI')
  155.         console.log('-Placed ship! gonna notify all that are listening')
  156.         this.actionComplete('ship placed', NotificationType.ShipWasPlaced)
  157.     }
  158.  
  159. }
  160. // ============================
  161. // ============================
  162. // ============================
  163.  
  164. class LongestRaodController extends Observer {
  165.     parentGame: Game
  166.  
  167.     constructor(parentGame: Game) {
  168.         super(parentGame)
  169.     }
  170.  
  171.     onNotify(value: string, type: NotificationType) {
  172.         if(type == NotificationType.RoadWasPlaced) {
  173.             this.updateLongestRoad()
  174.         }
  175.         if(type == NotificationType.ShipWasPlaced) {
  176.             this.updateLongestRoad()
  177.         }
  178.     }
  179.  
  180.     updateLongestRoad() {
  181.         console.log('Updating longest road')
  182.     }
  183. }
  184.  
  185. console.log('----')
  186. console.log('normal game with just road -> player tries to place road')
  187. const game = new Game()
  188. game.gotAction('player wants to place road', ActionTypes.WantToPlaceRoad)
  189. game.gotAction('player confirmed where they want to place road', ActionTypes.PlaceRoad)
  190.  
  191. console.log('----')
  192. console.log('normal game -> player tries to place ship')
  193. game.gotAction('player wants to place ship', ActionTypes.WantToPlaceShip)
  194. game.gotAction('player confirmed where they want to place ship', ActionTypes.PlaceShip)
  195. console.log('nothing happens')
  196.  
  197. console.log('----')
  198. console.log('game with ships - player tries to place ship')
  199. game.optionalMechanics.push(new Ship(game))
  200. game.gotAction('player wants to place ship', ActionTypes.WantToPlaceShip)
  201. game.gotAction('player wants to place ship', ActionTypes.PlaceShip)
  202.  
  203.  
  204.  
  205.  
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement