DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T s

⟦5664f7157⟧ TextFile

    Length: 7179 (0x1c0b)
    Types: TextFile
    Names: »select.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/General/Hearts/select.c« 

TextFile

#include <curses.h>
#include "misc.h"
#include "defs.h"

extern WINDOW	*card_window[],
		*round_window, *lead_window, *text_window, *play_window;

extern	int	dist_socket;
extern	char	first_game;

typedef struct table *table_ptr;

struct table {
	table_ptr next_table;		/* Points to next table entry */
	int	table_id;		/* Unique ID from distributor */
	char	player_name[4][9];	/* Player names */
	int	hand;			/* Current table hand */
	int	round;			/* Current table round */
	char	closed;			/* If game over */
};

table_ptr	first_table,
		cur_table;

int		table_count,		/* # tables existing */
		cur_screen_table,	/* nth table shown as table 1 */
		screen_table_id[8];	/* table_id of tables on screen */

char		buf[64];


/*
 * Find table given unique table_id in buf[1].
 */
table_ptr
find_table(buf)
char	*buf;
{
	table_ptr	cur_table;
	int		table_num;

	(void) sscanf(buf + 1, "%d", &table_num);
	for (cur_table = first_table;
			cur_table && cur_table->table_id != table_num;
			cur_table = cur_table->next_table)
		;
	return(cur_table);
}

/*
 * Create new table given table_id in buf[1].  Return pointer to new table.
 */
table_ptr
new_table(buf)
char	*buf;
{
	table_ptr	cur_ptr, new_table_ptr;
	int		i, table_num;
	char		*malloc();

	(void) sscanf(buf + 1, "%d", &table_num);
	new_table_ptr = (table_ptr) malloc(sizeof(struct table));
	new_table_ptr->next_table = NULL;
	new_table_ptr->table_id = table_num;
	new_table_ptr->closed = FALSE;
	for (i = 0; i < 4; i++)
		(void) strcpy(new_table_ptr->player_name[i], "<empty>");
	if (first_table) {
		for (cur_ptr = first_table; cur_ptr->next_table;
				cur_ptr = cur_ptr->next_table)
			;
		cur_ptr->next_table = new_table_ptr;
	}
	else
		first_table = new_table_ptr;
	++table_count;
	return(new_table_ptr);
}

/*
 * Update current table entry based on buf.  Return table # modified.
 */
update_table(buf)
char	*buf;
{
	int		i;
	table_ptr	tmp_table;

	switch (buf[0]) {
	case 't' :
		if ((cur_table = find_table(buf)) == NULL)
			cur_table = new_table(buf);
		break;
	case 'h' :
		(void) sscanf(buf + 1, "%d", &cur_table->hand);
		break;
	case 'r' :
		(void) sscanf(buf + 1, "%d", &cur_table->round);
		break;
	case 'p' :
		(void) strcpy(cur_table->player_name[buf[1] - '0'], buf + 2);
		break;
	case 'x' :
		if (tmp_table = find_table(buf)) {
			for (i = 0; i < 8; i++)
				if (screen_table_id[i] = tmp_table->table_id)
					screen_table_id[i] = 0;
			tmp_table->table_id = 0;
			tmp_table->closed = TRUE;
		}
	}
}

/*
 * show_table - display table which is position table_pos on screen.
 */
show_table(cur_ptr, table_pos)
table_ptr	cur_ptr;
int		table_pos;
{
	int	window_num, window_pos;
	int 	i;

	window_num = table_pos % 4 + 1;
	window_pos =  (table_pos + 4) & 8;
	for (i = 0; i < 6; i++) {
		wmove(card_window[window_num], window_pos + i, 0);
		wclrtoeol(card_window[window_num]);
	}
	mvwprintw(card_window[window_num], window_pos++, 0,
		"     Table %d", table_pos + 1);
	if (cur_ptr->closed)
		mvwaddstr(card_window[window_num], window_pos++, 0,
			"    <game over>");
	else {
		if (cur_ptr->hand == 0)
			mvwaddstr(card_window[window_num], window_pos++, 0,
				"     <starting>");
		else
			mvwprintw(card_window[window_num], window_pos++, 0,
					"Hand: %d  Round: %d",
					cur_ptr->hand, cur_ptr->round);
		for (i = 0; i <4; i++)
			mvwaddstr(card_window[window_num], window_pos++, 5,
				cur_ptr->player_name[i]);
	}
	wrefresh(card_window[window_num]);
}

/*
 * show_tables - display up to 8 tables starting with table # start_id.
 */
show_tables(start_id)
int	start_id;
{
	table_ptr	cur_ptr;
	int		cur_id, i;

	for (i = 0; i < 8; i++)
		screen_table_id[i] = 0;
	werase(round_window);
	if (table_count)
		mvwprintw(round_window, 0, 0, "Page %d of %d",
				(start_id + 7) / 8, (table_count + 7) / 8);
	wrefresh(round_window);
	cur_ptr = first_table;
	for (cur_id = 1; cur_id < start_id; cur_id++)
		cur_ptr = cur_ptr->next_table;
	for (cur_id = 0; (cur_id < 8) && cur_ptr;
			cur_ptr = cur_ptr->next_table) {
		screen_table_id[cur_id] = cur_ptr->table_id;
		show_table(cur_ptr, cur_id++);
	}
}

ask_option()
{
	werase(lead_window);
	mvwaddstr(lead_window, 0, 0, "Option: ");
	wrefresh(lead_window);
}

show_options()
{
	werase(play_window);
	mvwaddstr(play_window, 0, 0, "Options are:");
	if (!first_game)
		mvwaddstr(play_window, 1, 0, "<A>nother game");
	mvwaddstr(play_window, 2, 0, "<N>ew game");
	if (table_count)
		mvwaddstr(play_window, 3, 0, "<J>oin game");
	if (table_count > 8)
		mvwaddstr(play_window, 4, 0, "<M>ore games");
	mvwaddstr(play_window, 5, 0, "<Q>uit");
	wrefresh(play_window);
	ask_option();
}

dist_died()
{
	death("Distributor died!!");
}

select_game()
{
	int	dealer_port;
	fd_type	read_fd;
	int	i;
	char	ch;
	char	joined, joining;
	table_ptr	temp_table;

	clear();
	refresh();
	/*
	 * Get current games info
	 */
	do {
		if (read_socket(dist_socket, buf) == 0)
			dist_died();
		if (buf[0] != 'g')
			update_table(buf);
	}
	while (buf[0] != 'g');

	show_tables(cur_screen_table = 1);
	show_options();

	/*
	 * Wait for user input or table update info
	 */
	joined = joining = FALSE;

	do {
		fd_init(0, &read_fd);		/* stdin */
		fd_set(dist_socket, &read_fd);
		if (select(WIDTH, &read_fd, (fd_type *) 0, (fd_type *) 0,
				(struct timeval *) 0) == -1)
			dist_died();
		if (fd_isset(dist_socket, read_fd)) {
			if (read_socket(dist_socket, buf) == 0)
				dist_died();
			i = update_table(buf);
			show_tables(cur_screen_table);
		}
		if (fd_isset(0, read_fd)) {
			ch = get_char();
			werase(text_window);
			wrefresh(text_window);
			switch (ch) {
			case 'Q' :
			case 'q' :
				wimp_out();
			case 'A' :
			case 'a' :
				if (!first_game)
					joined = TRUE;
				break;
			case 'N' :
			case 'n' :
				write_socket(dist_socket, "n");
				joined = TRUE;
				break;
			case 'J' :
			case 'j' :
				mvwaddstr(lead_window, 0, 0, "Join table #:");
				wrefresh(lead_window);
				joining = TRUE;
				break;
			case 'M' :
			case 'm' :
				if (table_count > 8) {
					if ((cur_screen_table += 8) > table_count)
						cur_screen_table = 1;
					for (i = CLUBS; i <= SPADES; i++) {
						werase(card_window[i]);
						wrefresh(card_window[i]);
					}
					show_tables(cur_screen_table);
				}
				break;
			default:
				if (joining && (ch >= '1') && (ch <= '8')) {
					if (i = screen_table_id[ch - '1']) {
						(void) sprintf(buf, "j%d", i);
						write_socket(dist_socket, buf);
						joined = TRUE;
					}
					else {
						mvwaddstr(text_window, 0, 0, "Table not open.");
						wrefresh(text_window);
						ask_option();
					}
				}
				else {
					mvwaddstr(text_window, 0, 0, "Huh?");
					wrefresh(text_window);
					ask_option();
				}
				joining = FALSE;
				break;
			}
		}
	}
	while (!joined);

	/*
	 * Now free up malloced table space
	 */
	while (first_table) {
		temp_table = first_table;
		first_table = first_table->next_table;
		free ((char *) temp_table);
	}
	clear();
	refresh();

	if (!first_game && ((ch == 'a') || (ch == 'A')))
		/*
		 * Play another game with current dealer.
		 */
		dealer_port = 0;
	else {
		if (read_socket(dist_socket, buf) == 0)
			dist_died();
		(void) sscanf(buf + 1, "%d", &dealer_port);
	}
	(void) close(dist_socket);
	return(dealer_port);
}