예제 #1
0
파일: dbtest.go 프로젝트: Cristofori/kmud
func Test_ThreadSafety(t *testing.T) {
	runtime.GOMAXPROCS(2)
	database.Init(&TestSession{}, "unit_dbtest")

	char := database.NewPc("test", testutils.MockId(""), testutils.MockId(""))

	var wg sync.WaitGroup
	wg.Add(2)

	go func() {
		for i := 0; i < 100; i++ {
			char.SetName(strconv.Itoa(i))
		}
		wg.Done()
	}()

	go func() {
		for i := 0; i < 100; i++ {
		}
		wg.Done()
	}()

	wg.Wait()
}
예제 #2
0
파일: model.go 프로젝트: Cristofori/kmud
func CreatePlayerCharacter(name string, userId types.Id, startingRoom types.Room) types.PC {
	pc := db.NewPc(name, userId, startingRoom.GetId())
	events.Broadcast(events.EnterEvent{Character: pc, RoomId: startingRoom.GetId(), Direction: types.DirectionNone})
	return pc
}
예제 #3
0
파일: dbtest.go 프로젝트: Cristofori/kmud
func Test_PlayerCharacter(t *testing.T) {
	fakeId := bson.ObjectId("12345")
	character := database.NewPc("testcharacter", fakeId, fakeId)

	testutils.Assert(character.GetUserId() == fakeId, t, "Call to character.SetUser() failed", fakeId, character.GetUserId())
	testutils.Assert(!character.IsOnline(), t, "Player-Characters should be offline by default")

	character.SetOnline(true)

	testutils.Assert(character.IsOnline(), t, "Call to character.SetOnline(true) failed")

	character.SetRoomId(fakeId)

	testutils.Assert(character.GetRoomId() == fakeId, t, "Call to character.SetRoom() failed", fakeId, character.GetRoomId())

	cashAmount := 1234
	character.SetCash(cashAmount)

	testutils.Assert(character.GetCash() == cashAmount, t, "Call to character.GetCash() failed", cashAmount, character.GetCash())

	character.AddCash(cashAmount)

	testutils.Assert(character.GetCash() == cashAmount*2, t, "Call to character.AddCash() failed", cashAmount*2, character.GetCash())

	// conversation := "this is a fake conversation that is made up for the unit test"

	// character.SetConversation(conversation)

	// testutils.Assert(character.GetConversation() == conversation, t, "Call to character.SetConversation() failed")

	health := 123

	character.SetHealth(health)

	testutils.Assert(character.GetHealth() == health, t, "Call to character.SetHealth() failed")

	hitpoints := health - 10

	character.SetHitPoints(hitpoints)

	testutils.Assert(character.GetHitPoints() == hitpoints, t, "Call to character.SetHitPoints() failed")

	character.SetHitPoints(health + 10)

	testutils.Assert(character.GetHitPoints() == health, t, "Shouldn't be able to set a character's hitpoints to be greater than its maximum health", health, character.GetHitPoints())

	character.SetHealth(character.GetHealth() - 10)

	testutils.Assert(character.GetHitPoints() == character.GetHealth(), t, "Lowering health didn't lower the hitpoint count along with it", character.GetHitPoints(), character.GetHealth())

	character.SetHealth(100)
	character.SetHitPoints(100)

	hitAmount := 51
	character.Hit(hitAmount)

	testutils.Assert(character.GetHitPoints() == character.GetHealth()-hitAmount, t, "Call to character.Hit() failed", hitAmount, character.GetHitPoints())

	character.Heal(hitAmount)

	testutils.Assert(character.GetHitPoints() == character.GetHealth(), t, "Call to character.Heal() failed", hitAmount, character.GetHitPoints())
}