GitHub PullRequestReviewCommentEvent at 2014-03-28 14:36:35
Repository: risk
Actor: a14n
Payload: {"comment":{"url":"https://api.github.com/repos/ggirou/risk/pulls/comments/11068293","id":11068293,"diff_hunk":"@@ -120,8 +106,120 @@ class _RiskGameEngine implements RiskGameEngine {\n       if (countryState.playerId == playerId) {\n         playerState.reinforcement--;\n         countryState.armies++;\n-        return event;\n       }\n     }\n+    if (game.activePlayerId == null && game.players.values.every((ps) =>\n+        ps.reinforcement == 0)) {\n+      sendNextPlayer(game.playersOrder.first);\n+    }\n+  }\n+\n+  void onNextPlayer(NextPlayer event) {\n+    game.activePlayerId = event.playerId;\n+    game.players[event.playerId].reinforcement = event.reinforcement;\n+  }\n+\n+  void onAttack(Attack event) {\n+    if (event.playerId != game.activePlayerId) return;\n+\n+    game.lastAttack = event;\n+  }\n+\n+  void onDefend(Defend event) {\n+    if (event.playerId != game.countries[game.lastAttack.to].playerId) return;\n+\n+    int computeLostByAttacker(attacks, defends) {\n+      int result = 0;\n+      for (int i = 0; i < defends.length; i++) {\n+        if (attacks[i] <= defends[i]) result++;\n+      }\n+      return result;\n+    }\n+    rollDices(n) => (new List<int>.generate(n, (_) => random.nextInt(6) + 1\n+        )..sort()).reversed.toList();\n+    final attacks = rollDices(game.lastAttack.armies);\n+    final defends = rollDices(event.armies);\n+    final lostByAttacker = computeLostByAttacker(attacks, defends);\n+    final lostByDefender = defends.length - lostByAttacker;\n+    _broadcast(new BattleEnded()\n+        ..attackDices = attacks\n+        ..defendDices = defends\n+        ..lostByAttacker = lostByAttacker\n+        ..lostByDefender = lostByDefender);\n+  }\n+\n+  void onBattleEnded(BattleEnded event) {\n+    final from = game.countries[game.lastAttack.from];\n+    final to = game.countries[game.lastAttack.to];\n+    from.armies -= event.lostByAttacker;\n+    to.armies -= event.lostByDefender;\n+    if (to.armies == 0) {\n+      to.playerId = game.lastAttack.playerId;\n+      if (from.armies == 2) {\n+        _broadcast(new Move()\n+            ..playerId = game.lastAttack.playerId\n+            ..from = from\n+            ..to = to\n+            ..armies = 1);\n+      }\n+    }\n+    game.lastAttack = null;\n+  }\n+\n+  void onMove(Move event) {\n+    if (event.playerId != game.activePlayerId) return;\n+\n+    game.countries[event.from].armies -= event.armies;\n+    game.countries[event.to].armies += event.armies;\n+\n+    if (game.step == 'fortification') {\n+      game.step = null;\n+      _broadcast(new EndTurn()..playerId = event.playerId);\n+    }\n+  }\n+\n+  void onEndAttack(EndAttack event) {\n+    if (event.playerId != game.activePlayerId) return;\n+\n+    game.step = 'fortification';\n+  }\n+\n+  void onEndTurn(EndTurn event) {\n+    if (event.playerId != game.activePlayerId) return;\n+\n+    final l = game.playersOrder;\n+    final index = l.indexOf(game.activePlayerId);\n+    final nextIndex = (index + 1) % l.length;\n+    final nextActivePlayer = l[nextIndex];\n+    sendNextPlayer(nextActivePlayer);\n+  }\n+\n+  void onLeaveGame(LeaveGame event) {\n+  }\n+\n+  @override\n+  void addError(errorEvent, [StackTrace stackTrace]) {}\n+\n+  @override\n+  void close() {}\n+\n+  void sendGameStarted() {\n+    _broadcast(new GameStarted()\n+        ..armies = START_ARMIES[game.players.length]\n+        ..playersOrder = (game.players.keys.toList()..shuffle(random)));\n+  }\n+\n+  void sendNextPlayer(nextPlayerId) {\n+    //TODO compute reinforcement\n+    int reinforcement = 3;\n+    _broadcast(new NextPlayer()\n+        ..playerId = nextPlayerId\n+        ..reinforcement = reinforcement);\n+  }\n+\n+  _broadcast(event) {\n+    if (outSink == null) return;\n+    outSink.add(event);\n+    add(event);\n   }","path":"lib/game.dart","position":299,"original_position":299,"commit_id":"793865d3f4704acf1e5565585e937017cd172502","original_commit_id":"793865d3f4704acf1e5565585e937017cd172502","user":{"login":"a14n","id":1206632,"avatar_url":"https://avatars.githubusercontent.com/u/1206632?","gravatar_id":"198449a7e196c4b0f2ed25f65279bf84","url":"https://api.github.com/users/a14n","html_url":"https://github.com/a14n","followers_url":"https://api.github.com/users/a14n/followers","following_url":"https://api.github.com/users/a14n/following{/other_user}","gists_url":"https://api.github.com/users/a14n/gists{/gist_id}","starred_url":"https://api.github.com/users/a14n/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/a14n/subscriptions","organizations_url":"https://api.github.com/users/a14n/orgs","repos_url":"https://api.github.com/users/a14n/repos","events_url":"https://api.github.com/users/a14n/events{/privacy}","received_events_url":"https://api.github.com/users/a14n/received_events","type":"User","site_admin":false},"body":"I don't think so. Don't forget this class should live also on client side. On this side the engine must not update its state by itself.\r\nMoreover, this is really simpler to understand (and write).","created_at":"2014-03-28T14:36:35Z","updated_at":"2014-03-28T14:36:35Z","html_url":"https://github.com/ggirou/risk/pull/29#discussion_r11068293","pull_request_url":"https://api.github.com/repos/ggirou/risk/pulls/29","_links":{"self":{"href":"https://api.github.com/repos/ggirou/risk/pulls/comments/11068293"},"html":{"href":"https://github.com/ggirou/risk/pull/29#discussion_r11068293"},"pull_request":{"href":"https://api.github.com/repos/ggirou/risk/pulls/29"}}}}