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

      setTimeout done, 58
 
    it "should broadcast that another player joins", (done) ->
      @anotherplayer.on 'join', (msg) =>
      @anyplayer.on 'join', (msg) =>
        expect(msg.player).to.equal "Ananda"
        expect(msg.id).to.equal MatchId
        History += 1
        done()

  
  


  describe "CUSTOM MESSAGING", () ->

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




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

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





  describe "GAME APPublishing", () ->

    xit "should let the server publish game apps", (done) ->
      server.post { game: "new.game", name: "FunnyFoo" }
      @lukas.on 'publish', (game) ->
        expect(game).to.deep.equal {
          game: "new.game",
          name: "FunnyFoo"
        }
        done()





  describe "MATCH MAKING (players <-> games)", () ->

    InBetween = Date.now()

    it "should allow any logged in player to host games", (done) ->
      @anyplayer.emit 'host', { game: "de.gravity", max: 3 }
      @anyplayer.on 'host', (match) -> # host gets it too
        GravityMatchId = match.id # server assigned GUID
        expect(match.game).to.equal "de.gravity"
        expect(match.host).to.equal "Anyname"
        expect(match.max).to.equal 3
        expect(match.id).to.be.ok
        History += 1
      @lukas.on 'host', (match) ->
        expect(match.game).to.equal "de.gravity"
        expect(match.host).to.equal "Anyname"
        expect(match.max).to.equal 3
        expect(match.id).to.be.ok
        done()
      @lukas.emit 'host', { game: "de.mole", max: 3 }
      @anotherplayer.on 'host', (match) ->
        MoleculeMatchId = match.id

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