 io = require 'socket.io-client'
 BeginOfTest = 0
 InBetween = 0
-MatchId = ""
+GravityMatchId = ''
+MoleculeMatchId = ''
 History = 0
 
 #   GCP PROTOCOL SPECIFICATION   *StudioLounge Multiplayer Game*

    (@lukas = server.gcp()).on "connect", () -> @emit 'login', "I am Lukas"

    it "should allow players to chat in the public chatroom", (done) ->
      @lukas.send "happy again :-)"
      @anyplayer.on 'message', (msg) ->
        expect(msg).to.equal "Lukas:   happy again :-)"
        History += 1
      @anotherplayer.on 'message', (msg) ->
        expect(msg).to.equal "Lukas:   happy again :-)"
        done()
 
    it "must not let players chat who are not logged in", (done) ->
      server.gcp().on 'connect', () ->
        @send "I did not log in but I chat anayway"
      @lukas.on 'message', (msg) -> expect(true).to.be.not.ok
      setTimeout done, 58





  describe "MATCHING (players with games)", () ->

    InBetween = Date.now()

    it "should allow any logged in player to host games", (done) ->
      @anyplayer.emit 'host', { game: "my.game", min: 2, max: 3}
      @anyplayer.on 'host', (match) -> # host gets it too
        expect(match.host).to.equal "Anyname"
        expect(match.min).to.equal 2
        expect(match.max).to.equal 3
        MatchId = match.id # server assigned GUID
        History += 1
      @lukas.on 'host', (match) ->
        expect(match.host).to.equal "Anyname"
        expect(match.min).to.equal 2
        expect(match.max).to.equal 3
        done()

    it "should deny anyone else to host a new game", (done) ->
      @troll.emit 'host', { game: "a.random.game", min: 43, max: 55 }

      setTimeout done, 58
 
    it "should broadcast that another player joins", (done) ->
      @anotherplayer.emit 'join', { match: GravityMatchId }
      @anyplayer.on 'join', (msg) =>
        expect(msg.player).to.equal "Ananda"
        expect(msg.match).to.equal GravityMatchId
        History += 1
        done()
      @anyplayer.emit 'join', { match: MoleculeMatchId }





  describe "CUSTOM MESSAGING", () ->

    it "should broadcast game messages among players of a match", (done) ->
      @anyplayer.emit 'msg', {
        foo: "my", data: 42,
        match: GravityMatchId
        persist: 1
      }
      @anotherplayer.on 'msg', (msg) =>
        expect(msg).to.deep.equal {
          foo: "my", data: 42,
          match: GravityMatchId,
          sender: "Anyname",
          next: "Ananda",
          persist: 1
        }
        History += 1
        done()





  describe "CHECKING IN AND OUT", () ->

    it "should let player check into the lobby", (done) ->
      @lukas.emit 'checkin', "lobby-xy"
      @anyplayer.on 'checkin', (status) ->
        expect(status.player).to.equal "Lukas"
        expect(status.match).to.equal "lobby-xy"
      # status updates only for matched players
      @anotherplayer.on 'checkin', (status) ->
        expect("this").to.be.not.ok
      setTimeout done, 99 # ms



