add a message to the unit that gets kicked, with tests

This commit is contained in:
Enno Rehling 2015-01-13 07:43:30 +01:00
parent f52e00d574
commit 4f2d9260d1
5 changed files with 42 additions and 0 deletions

View File

@ -1,5 +1,23 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?>
<messages> <messages>
<message name="force_leave_ship" section="events">
<type>
<arg name="unit" type="unit"/>
<arg name="owner" type="unit"/>
<arg name="ship" type="ship"/>
</type>
<text locale="de">$unit($owner) bittet $unit($unit), $ship($ship) zu verlassen.</text>
<text locale="en">$unit($owner) asks $unit($unit) to leave $ship($ship).</text>
</message>
<message name="force_leave_building" section="events">
<type>
<arg name="unit" type="unit"/>
<arg name="owner" type="unit"/>
<arg name="building" type="building"/>
</type>
<text locale="de">$unit($owner) bittet $unit($unit), $building($building) zu verlassen.</text>
<text locale="en">$unit($owner) asks $unit($unit) to leave $building($building).</text>
</message>
<message name="alp_destroyed" section="events"> <message name="alp_destroyed" section="events">
<type> <type>
<arg name="region" type="region"/> <arg name="region" type="region"/>

View File

@ -4277,14 +4277,20 @@ void force_leave(region *r) {
unit *u; unit *u;
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
unit *uo = NULL; unit *uo = NULL;
message *msg = NULL;
if (u->building) { if (u->building) {
uo = building_owner(u->building); uo = building_owner(u->building);
msg = msg_message("force_leave_building", "unit owner building", u, uo, u->building);
} }
if (u->ship && r->land) { if (u->ship && r->land) {
uo = ship_owner(u->ship); uo = ship_owner(u->ship);
msg = msg_message("force_leave_ship", "unit owner ship", u, uo, u->ship);
} }
if (uo && !help_enter(uo, u)) { if (uo && !help_enter(uo, u)) {
leave(u, false); leave(u, false);
if (msg) {
ADDMSG(&u->faction->msgs, msg);
}
} }
} }
} }

View File

@ -15,6 +15,7 @@
#include <util/base36.h> #include <util/base36.h>
#include <util/language.h> #include <util/language.h>
#include <util/message.h>
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
@ -231,6 +232,7 @@ static void test_force_leave_buildings(CuTest *tc) {
region *r; region *r;
unit *u1, *u2, *u3; unit *u1, *u2, *u3;
building * b; building * b;
message *msg;
test_cleanup(); test_cleanup();
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION)); r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
u1 = test_create_unit(test_create_faction(NULL), r); u1 = test_create_unit(test_create_faction(NULL), r);
@ -245,6 +247,8 @@ static void test_force_leave_buildings(CuTest *tc) {
CuAssertPtrEquals_Msg(tc, "owner should not be forecd to leave", b, u1->building); CuAssertPtrEquals_Msg(tc, "owner should not be forecd to leave", b, u1->building);
CuAssertPtrEquals_Msg(tc, "same faction should not be forced to leave", b, u2->building); CuAssertPtrEquals_Msg(tc, "same faction should not be forced to leave", b, u2->building);
CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u3->building); CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u3->building);
msg = test_get_last_message(u3->faction->msgs);
CuAssertStrEquals(tc, "force_leave_building", test_get_messagetype(msg));
u_set_building(u3, b); u_set_building(u3, b);
al = ally_add(&u1->faction->allies, u3->faction); al = ally_add(&u1->faction->allies, u3->faction);
@ -258,6 +262,7 @@ static void test_force_leave_ships(CuTest *tc) {
region *r; region *r;
unit *u1, *u2; unit *u1, *u2;
ship *sh; ship *sh;
message *msg;
test_cleanup(); test_cleanup();
r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION)); r = test_create_region(0, 0, test_create_terrain("plain", LAND_REGION));
u1 = test_create_unit(test_create_faction(NULL), r); u1 = test_create_unit(test_create_faction(NULL), r);
@ -268,6 +273,8 @@ static void test_force_leave_ships(CuTest *tc) {
ship_set_owner(u1); ship_set_owner(u1);
force_leave(r); force_leave(r);
CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u2->ship); CuAssertPtrEquals_Msg(tc, "non-allies should be forced to leave", NULL, u2->ship);
msg = test_get_last_message(u2->faction->msgs);
CuAssertStrEquals(tc, "force_leave_ship", test_get_messagetype(msg));
test_cleanup(); test_cleanup();
} }

View File

@ -14,6 +14,7 @@
#include <kernel/spell.h> #include <kernel/spell.h>
#include <kernel/spellbook.h> #include <kernel/spellbook.h>
#include <kernel/terrain.h> #include <kernel/terrain.h>
#include <kernel/messages.h>
#include <util/functions.h> #include <util/functions.h>
#include <util/language.h> #include <util/language.h>
#include <util/message.h> #include <util/message.h>
@ -193,6 +194,14 @@ void test_create_world(void)
test_create_shiptype("boat"); test_create_shiptype("boat");
} }
message * test_get_last_message(message_list *msgs) {
struct mlist *iter = msgs->begin;
while (iter->next) {
iter = iter->next;
}
return iter->msg;
}
const char * test_get_messagetype(const message *msg) { const char * test_get_messagetype(const message *msg) {
const char * name = msg->type->name; const char * name = msg->type->name;
if (strcmp(name, "missing_message") == 0) { if (strcmp(name, "missing_message") == 0) {

View File

@ -14,6 +14,7 @@ extern "C" {
struct building; struct building;
struct ship; struct ship;
struct message; struct message;
struct message_list;
struct item_type; struct item_type;
struct building_type; struct building_type;
struct ship_type; struct ship_type;
@ -37,6 +38,7 @@ extern "C" {
int RunAllTests(void); int RunAllTests(void);
void test_translate_param(const struct locale *lang, param_t param, const char *text); void test_translate_param(const struct locale *lang, param_t param, const char *text);
const char * test_get_messagetype(const struct message *msg); const char * test_get_messagetype(const struct message *msg);
struct message * test_get_last_message(struct message_list *mlist);
#ifdef __cplusplus #ifdef __cplusplus
} }