<commit_before>/*
it should allow cancel vote
it should denied vote against himself
it should allow skip vote
it should denied actions after its duration
it should start another night if vote are unconclusive
*/

var Q = require("Q"),
_ = require("lodash"),
log = require("../../src/misc/log.js")(),
serverHelper = require("../helper/serverHelper.js")(),
jh = require("../helper/jasmineHelper.js")(),
client = require("../helper/client.js"),
lobby = require("../helper/lobby.js"),
game = require("../helper/game.js");

describe("Game's city hall", function() {
  beforeEach(function() {
    _.extend(this, serverHelper.getConfiguredServer({debug: true, verbose: false}));
  });

  afterEach(serverHelper.clearAll);

  jh.it("should launch a city hall with vote enabled if nothing has happened at the last night", function (callback) {
    game.prepareClassicGame({port : this.options.socketport})
    .then(function (data) {
      var lastClient = data.client;
      var Game = require('../../src/game/Game.js');
      Game.delayFactor = 0.1;
      var counter = 0;
      lastClient.on("cityhall_start", function (response) {
        counter++;
        if (counter === 2) {
          // kill all werewolf so the game will finish instantly
          var Werewolf = require('../../src/roles/werewolf/Werewolf.js');
          Werewolf.prototype.isAlive = false;
          expect(response.isVoteDisabled).toBe(false);
          callback();
        }
      });
      return data;
    })
    .then(game.launchClassicGame)
    .done();
  }, this);

  jh.it("should allow user sending and receiving message", function (callback) {
    var cb = function (data) {
      var flag = false;
      data.client.on("msg", function (response) {
        if (response) {
          flag = true;
        }
      });
      data.client.emit("msg", "hello");
      data.client.on("cityhall_stop", function (response) {
        expect(flag).toBe(true);
        callback();
      });
    };
    var data = {
      port : this.options.socketport,
      cityHallCB: cb
    };
    game.prepareClassicGame(data)
    .then(game.goToSecondCityHallAndKillAllWerewolves)
    .then(game.launchClassicGame)
    .done();
  }, this);

  // should refuse bad vote
  // should accept skip vote
  jh.it("should allow user vote", function (callback) {
    var cb = function (data) {
      var flag = false;
      data.client.on("vote_response", function (response) {
        if (response.result) {
          flag = true;
        }
      });
      data.client.emit("vote", {target: 'username0'});
      data.client.on("cityhall_stop", function (response) {
        expect(flag).toBe(true);
        callback();
      });
    };
    var data = {
      port : this.options.socketport,
      cityHallCB: cb
    };
    game.prepareClassicGame(data)
    .then(game.goToSecondCityHallAndKillAllWerewolves)
    .then(game.launchClassicGame)
    .done();
  }, this);

  //
  // jh.it("should denied user vote", function (callback) {
  //   game.prepareClassicGame({port : this.options.socketport})
  //   .then(_.bind(function (data) {
  //     var lastClient = data.client;
  //     var Game = require('../../src/game/Game.js');
  //     Game.delayFactor = 0.1;
  //     var flag = false;
  //     lastClient.on("cityhall_start", function (response) {
  //       expect(response.isVoteEnabled).toBe(false);
  //       lastClient.on("vote_response", function (response) {
  //         if (response) {
  //           flag = true;
  //         }
  //       });
  //       lastClient.emit("vote");
  //       lastClient.on("cityhall_stop", function (response) {
  //         expect(flag).toBe(false);
  //         callback();
  //       });
  //     });
  //     return data;<commit_msg>Game's city hall should allow user vote => Spec<commit_after>/*
it should allow cancel vote
it should denied vote against himself
it should allow skip vote
it should denied actions after its duration
it should start another night if vote are unconclusive
*/

var Q = require("Q"),
_ = require("lodash"),
log = require("../../src/misc/log.js")(),
serverHelper = require("../helper/serverHelper.js")(),
jh = require("../helper/jasmineHelper.js")(),
client = require("../helper/client.js"),
lobby = require("../helper/lobby.js"),
game = require("../helper/game.js");

describe("Game's city hall", function() {
  beforeEach(function() {
    _.extend(this, serverHelper.getConfiguredServer({debug: true, verbose: false}));
  });

  afterEach(serverHelper.clearAll);

  jh.it("should launch a city hall with vote enabled if nothing has happened at the last night", function (callback) {
    game.prepareClassicGame({port : this.options.socketport})
    .then(function (data) {
      var lastClient = data.client;
      var Game = require('../../src/game/Game.js');
      Game.delayFactor = 0.1;
      var counter = 0;
      lastClient.on("cityhall_start", function (response) {
        counter++;
        if (counter === 2) {
          // kill all werewolf so the game will finish instantly
          var Werewolf = require('../../src/roles/werewolf/Werewolf.js');
          Werewolf.prototype.isAlive = false;
          expect(response.isVoteDisabled).toBe(false);
          callback();
        }
      });
      return data;
    })
    .then(game.launchClassicGame)
    .done();
  }, this);

  jh.it("should allow user sending and receiving message", function (callback) {
    var cb = function (data) {
      var flag = false;
      data.client.on("msg", function (response) {
        if (response) {
          flag = true;
        }
      });
      data.client.emit("msg", "hello");
      data.client.on("cityhall_stop", function (response) {
        expect(flag).toBe(true);
        callback();
      });
    };
    var data = {
      port : this.options.socketport,
      cityHallCB: cb
    };
    game.prepareClassicGame(data)
    .then(game.goToSecondCityHallAndKillAllWerewolves)
    .then(game.launchClassicGame)
    .done();
  }, this);

  // should refuse bad vote
  // should accept skip vote
  jh.it("should allow user vote", function (callback) {
    var cb = function (data) {
      var flag = false;
      data.client.on("vote_response", function (response) {
        if (response.result) {
          flag = true;
        }
      });
      data.client.emit("vote", {target: 'username0'});
      data.client.on("cityhall_stop", function (response) {
        expect(flag).toBe(true);
        callback();
      });
    };
    var data = {
      port : this.options.socketport,
      cityHallCB: cb
    };
    game.prepareClassicGame(data)
    .then(game.goToSecondCityHallAndKillAllWerewolves)
    .then(game.launchClassicGame)
    .done();
  }, this);

  jh.it("should refuse bad vote", function (callback) {
    var cb = function (data) {
      var flag = false;
      data.client.on("vote_response", function (response) {
        if (!response.result) {
          flag = true;
        }
      });
      data.client.emit("vote", {target: 'UNKNOWN_username'});
      data.client.on("cityhall_stop", function (response) {
        expect(flag).toBe(true);
        callback();
      });
    };
    var data = {
      port : this.options.socketport,
      cityHallCB: cb
    };
    game.prepareClassicGame(data)
    .then(game.goToSecondCityHallAndKillAllWerewolves)
    .then(game.launchClassicGame)
    .done();
  }, this);

  //
  // jh.it("should denied user vote", function (callback) {
  //   game.prepareClassicGame({port : this.options.socketport})
  //   .then(_.bind(function (data) {
  //     var lastClient = data.client;
  //     var Game = require('../../src/game/Game.js');
  //     Game.delayFactor = 0.1;
  //     var flag = false;
  //     lastClient.on("cityhall_start", function (response) {
  //       expect(response.isVoteEnabled).toBe(false);
  //       lastClient.on("vote_response", function (response) {
  //         if (response) {
  //           flag = true;
  //         }
  //       });
  //       lastClient.emit("vote");
  //       lastClient.on("cityhall_stop", function (response) {
  //         expect(flag).toBe(false);
  //         callback();
  //       });
  //     });
  //     return data;
{'fraction_non_alphanumeric': 0.15609696376101861, 'fraction_numerical': 0.0017140058765915769, 'mean_word_length': 2.3882206553297385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '782404', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2458, 'n_words': 543}