/*
 * Copyright 2015 Florian Hassanen
 *
 * This file is part of mmo-server.
 *
 * mmo-server is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * mmo-server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with mmo-server.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

package mmo.server;

import mmo.server.model.Coord;
import mmo.server.model.Player;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class RoomTest {
    private static Player createDummyPlayer() {
        return new Player("dummy");
    }

    @Test
    public void enterEmptyRoom() {
        Room room = new Room();

        Player pl = createDummyPlayer();
        assertThat("empty room can be entered",
                room.enter(new Coord(1, 2), pl),
                allOf(
                        notNullValue(),
                        hasProperty("id", is(0)),
                        hasProperty("coord", is(new Coord(1, 2)))));

        assertThat("player is in room",
                room.contents(), hasItem(pl));
    }

    @Test
    public void enterRoomAndGetDisplaced() {
        Room room = new Room();

        room.enter(new Coord(0, 0), createDummyPlayer());


        assertThat("player is displaced upon entering if tile is occupied",
                room.enter(new Coord(0, 0), createDummyPlayer()),
                allOf(
                        notNullValue(),
                        hasProperty("coord", not(is(new Coord(0, 0))))));

    }

    @Test
    public void useConsecutiveIds() {
        Room room = new Room();

        assertThat("first player gets id 0",
                room.enter(new Coord(0, 0), createDummyPlayer()).getId(),
                is(0));
        Player pl1 = createDummyPlayer();
        assertThat("second player gets id 1",
                room.enter(new Coord(0, 0), pl1).getId(),
                is(1));
        Player pl2 = createDummyPlayer();
        assertThat("third player gets id 2",
                room.enter(new Coord(0, 0), pl2).getId(),
                is(2));
        assertThat("first player gets id 0",
                room.enter(new Coord(0, 0), createDummyPlayer()).getId(),
                is(3));
        assertThat("id 1 leaves room", room.leave(pl1), is(1));
        assertThat("id 2 leaves room", room.leave(pl2), is(2));
        assertThat("next player gets id 1 again",
                room.enter(new Coord(0, 0), createDummyPlayer()).getId(),
                is(1));
    }

    @Test
    public void enterFullRoom() {
        Room room = new Room();

        for (int i = 0; i < Room.SIZE * Room.SIZE; ++i) {
            room.enter(new Coord(0, 0), createDummyPlayer());
        }

        assertThat("room cannot be entered if it is already full",
                room.enter(new Coord(0, 0), createDummyPlayer()),
                nullValue());
    }
}

@@ -863,16 +863,47 @@
 .Test;%0A%0A
+import java.util.Collections;%0A%0A
 import s
@@ -1168,32 +1168,61 @@
 room = new Room(
+Collections.%3CCoord%3EemptySet()
 );%0A%0A        Play
@@ -1700,32 +1700,61 @@
 room = new Room(
+Collections.%3CCoord%3EemptySet()
 );%0A%0A        room
@@ -1800,17 +1800,16 @@
 er());%0A%0A
-%0A
         
@@ -1825,40 +1825,38 @@
 t(%22p
-layer is displaced upon entering
+roper displacment can be found
  if 
@@ -1887,36 +1887,43 @@
            room.
-ente
+findFreeNea
 r(new Coord(0, 0
@@ -1927,170 +1927,206 @@
 , 0)
-, createDummyPlayer()),%0A                allOf(%0A                        notNullValue(),%0A                        hasProperty(%22coord%22, not(is(new Coord(0, 0))))
+),%0A                not(is(new Coord(0, 0))));%0A%0A        assertThat(%22player cannot enter on same spot%22,%0A                room.enter(new Coord(0, 0), createDummyPlayer()),%0A                nullValue(
 ));%0A
-%0A
     
@@ -2197,32 +2197,61 @@
 room = new Room(
+Collections.%3CCoord%3EemptySet()
 );%0A%0A        asse
@@ -2505,25 +2505,25 @@
 ew Coord(0, 
-0
+1
 ), pl1).getI
@@ -2678,17 +2678,17 @@
 oord(0, 
-0
+2
 ), pl2).
@@ -2797,33 +2797,33 @@
 er(new Coord(0, 
-0
+3
 ), createDummyPl
@@ -3073,33 +3073,33 @@
 er(new Coord(0, 
-0
+4
 ), createDummyPl
@@ -3219,16 +3219,45 @@
 ew Room(
+Collections.%3CCoord%3EemptySet()
 );%0A%0A    

fix tests after refactoring