server/src/triggers/timeout.c

110 lines
2.8 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
2015-01-30 22:10:29 +01:00
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
2010-08-08 10:06:34 +02:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include "timeout.h"
/* util includes */
#include <util/attrib.h>
#include <util/event.h>
#include <util/gamedata.h>
2010-08-08 10:06:34 +02:00
#include <util/log.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
#include <stdio.h>
#include <stdlib.h>
/***
** timeout
**/
typedef struct timeout_data {
trigger *triggers;
int timer;
variant trigger_data;
2010-08-08 10:06:34 +02:00
} timeout_data;
2011-03-07 08:02:35 +01:00
static void timeout_init(trigger * t)
2010-08-08 10:06:34 +02:00
{
t->data.v = calloc(sizeof(timeout_data), 1);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void timeout_free(trigger * t)
2010-08-08 10:06:34 +02:00
{
timeout_data *td = (timeout_data *)t->data.v;
free_triggers(td->triggers);
free(t->data.v);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static int timeout_handle(trigger * t, void *data)
2010-08-08 10:06:34 +02:00
{
/* call an event handler on timeout.
* data.v -> ( variant event, int timer )
*/
timeout_data *td = (timeout_data *)t->data.v;
if (--td->timer == 0) {
handle_triggers(&td->triggers, NULL);
return -1;
}
unused_arg(data);
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void timeout_write(const trigger * t, struct storage *store)
2010-08-08 10:06:34 +02:00
{
timeout_data *td = (timeout_data *)t->data.v;
WRITE_INT(store, td->timer);
write_triggers(store, td->triggers);
2010-08-08 10:06:34 +02:00
}
static int timeout_read(trigger * t, gamedata *data)
2010-08-08 10:06:34 +02:00
{
timeout_data *td = (timeout_data *)t->data.v;
READ_INT(data->store, &td->timer);
read_triggers(data, &td->triggers);
if (td->timer > 20) {
trigger *tr = td->triggers;
log_warning("there is a timeout lasting for another %d turns\n", td->timer);
while (tr) {
log_warning(" timeout triggers: %s\n", tr->type->name);
tr = tr->next;
}
2010-08-08 10:06:34 +02:00
}
if (td->triggers != NULL && td->timer > 0)
return AT_READ_OK;
return AT_READ_FAIL;
2010-08-08 10:06:34 +02:00
}
trigger_type tt_timeout = {
"timeout",
timeout_init,
timeout_free,
timeout_handle,
timeout_write,
timeout_read
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
trigger *trigger_timeout(int time, trigger * callbacks)
2010-08-08 10:06:34 +02:00
{
trigger *t = t_new(&tt_timeout);
timeout_data *td = (timeout_data *)t->data.v;
td->triggers = callbacks;
td->timer = time;
return t;
2010-08-08 10:06:34 +02:00
}