/*
 * session.h
 * 
 * Copyright (C) 2007  Josef Kufner <jk@myserver.cz>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 */
#ifndef NETBOARD_SESSION_H
#define NETBOARD_SESSION_H

#include "status.h"

enum {
	S_S_NONE,
	S_S_IDLE,		/* nothing */
	S_S_LOADING,		/* game selected, game backend launched,
				   loading player's roles and getting acks
				   from others */
	S_S_LOADED,		/* all game backends are ready */
	S_S_LOAD_ABORT,		/* terminate game before begin */
	S_S_LOAD_ERROR,		/* game cannot be loaded -- wait here */
	S_S_ROLE_SELECT,	/* selecting player's role */
	S_S_READY,		/* waiting to ack game start */
	S_S_START_CHECK,	/* game can be started -- waiting for others */
	S_S_GAME_PLAYING,	/* game in progress */
	S_S_GAME_OVER,		/* game over */
	S_S_GAME_ERROR,		/* game crashed or been killed */
	
	S_S_KILL,		/* killing game backend */
	S_S_SYNCING,		/* no game loaded, recovery after error */
	
	S_S_MAX
};

typedef struct {
	long time;
	int status;
	char *game, *my_role;
	int last_status;
} SESSION_GLOBAL_ST;

extern STATUS *session_status;
extern SESSION_GLOBAL_ST session_global_st;	/* read-only, public only for gui */

void session_init(void);
void session_cleanup(void);

void session_update_info(int time, char cmd, int new_state, const char *new_state_param, const char *new_state_desc,
		const char *new_role, const char *peer_addr, int remote);
void session_message_action_switch_param(int time, char cmd, int new_state, const char *new_state_param, const char *peer_addr, int remote);
void session_message_action_switch(int time, char cmd, int new_state, const char *new_game, const char *new_my_role, const char *peer_addr, int remote);
void session_update_global_state(int time, int state, const char *game, const char *my_role);
void session_calculate_next_state(void);

void session_send_sync_rq(void);
void session_send_state_info(const char *address);

int session_is_synced(void);
void session_record_set_enabled(int enabled);

void session_send_game_check_rq(void);
void session_surrender(void);
	
#endif

