#include#include #include #include #include "game.h"#include "engine.h"#include "message.h"#include "member.h"#include #include "timer.h"#include int gqid=0;int current_row=0;int current_col=0;int message_row=2;int message_col=2;WINDOW* message_win =NULL;WINDOW* input_win =NULL;void game_init(){ engine_init(); gqid=message_init(); init_member_list(); signal(SIGINT, signal_handle); signal(EXIT_SIGNAL, signal_handle); game_create_message_win(); game_create_input_win(); create_thread((void*) game_refresh_member); STRUCT_MEMBER member; member.type=0; member.name="sysAdmin"; regist_member(member); move(27, 23); current_row=27; current_col=23;}void game_create_message_win(){ message_win=newwin(20, 70, 2, 2); if(message_win !=NULL) { refresh(); box(message_win, 0, 0); wrefresh(message_win); }}void game_start(){}void game_create_input_win(){ refresh(); input_win=newwin(5, 70, 25, 2); if(message_win !=NULL) { refresh(); box(input_win, 0, 0); wrefresh(input_win); mvwprintw(input_win,2,2,"[membername] say:"); wrefresh(input_win); }}void game_refresh_member(){ int i=0; int y =2; int x=75; int rcv=0; STRUCT_MEMBER_LIST list; list = get_members(); STRUCT_MSG_BUF msg={ 0}; for(;;){ y=2; x=75; mvprintw(y,x,"MEMBER %d", list.number); for(i=0;i 0) { mvwprintw(message_win,message_row,message_col,"[%d,%d,%s]", msg.length,msg.type,msg.data); //scrollok(message_win, TRUE); //scroll(message_win); wrefresh(message_win); refresh(); message_row++; } move(current_row,current_col); interval_time(INTERVAL); }}void game_run(){ char line[70]=""; int ch=0; STRUCT_MSG_BUF msg; for(;;) { //getnstr(line, 70); //move(current_row,current_col); //printw("%s", line); ch=getch(); switch(ch) { case 'q': raise(SIGINT); break; case '\n': mvwprintw(input_win,2,2,"[membername] say:"); mvwprintw(input_win,2,23," "); wrefresh(input_win); move(27,23); game_send_msg(line, ENUM_MSG_NORMAL); memset(line, 0, strlen(line)); //game_receive_msg(&msg); break; default: if(current_col<70) { move(current_row,current_col); printw("%c", ch); refresh(); line[strlen(line)]=ch; } break; } getyx(stdscr, current_row, current_col); }}int game_abort(char* msg){ engine_shut(); //fprintf(stderr, "%s\n", msg); exit(EXIT_FAILURE); }// void game_over(){ engine_shut(); exit(EXIT_SUCCESS);}void game_send_msg(char* pmsg_content, ENUM_MSG_TYPE msgType){ STRUCT_MSG_BUF msg={ 0}; memset(&msg, 0, sizeof(STRUCT_MSG_BUF)); msg.length=strlen(pmsg_content); msg.type=msgType; strncpy(msg.data, pmsg_content, strlen(pmsg_content)); message_send(gqid, &msg, 0);}int game_receive_msg(STRUCT_MSG_BUF* pmsg){ int ret =-1; if(pmsg != NULL) { memset(pmsg, 0, sizeof(STRUCT_MSG_BUF)); ret = message_receive(gqid, pmsg, IPC_NOWAIT); } //printw("receive msg %s", pmsg->data); //refresh(); return ret;}void print_info(){ }void signal_handle(int signal){ switch(signal) { case SIGINT: game_over(); break; case EXIT_SIGNAL: game_over(); break; default: break; }}
#ifndef __GAME_H#define __GAME_H#include "message.h"#define FRAME_ROW 0#define FRAME_COL 0#define MSG_BEGIN_COL 3#define MSG_END_COL #define EXIT_SIGNAL 1818typedef enum tag_mode{ GAME_INIT, GAME_RUN, GAME_ABORT, GAME_OVER}ENUM_GAME_MODE;typedef struct tag_game{ ENUM_GAME_MODE mode; int length;}STRUCT_GAME;void game_init();void game_start();int game_abort(char* msg);void game_over();void game_run();//void game_send_msg();void game_send_msg(char* pmsg_content, ENUM_MSG_TYPE msgType);int game_receive_msg(STRUCT_MSG_BUF* pmsg);void game_show_frame();void signal_handle(int signal);void game_create_input_win();void game_refresh_member();void game_create_message_win();#endif
#ifndef __MESSAGE_H#define __MESSAGE_H#include#include #include #define MSG_PATH "./msg/msg"#define MSG_PJID 1818 #define MAX_MSG_LENGTH 256typedef enum tag_msg_type{ ENUM_MSG_REGIST_MEM = 0, ENUM_MSG_UNRGIST_MEM, ENUM_MSG_NORMAL}ENUM_MSG_TYPE;typedef struct tag_msg{ int length; ENUM_MSG_TYPE type; char data[MAX_MSG_LENGTH];} STRUCT_MSG_BUF;int message_init();int message_receive(int msgid, STRUCT_MSG_BUF* pmsg, int flag);//int message_send(int msgid, const STRUCT_MSG_BUF* pmsg, int flag)int message_send(int msgid, const STRUCT_MSG_BUF* pmsg, int flag);#endif
#include#include #include #include #include #include #include #include "message.h"int qid=0;int message_init(){ key_t key = ftok(MSG_PATH, MSG_PJID); if(key == -1) { perror("ftok failed"); exit(EXIT_FAILURE); } if((qid = msgget(key, IPC_CREAT | 0666)) == -1) { perror("create message queue failed"); exit(EXIT_FAILURE); } return qid; }int message_send(int msgid, const STRUCT_MSG_BUF* pmsg, int flag){ if( pmsg != NULL && pmsg->length >0 ) { if( msgsnd(msgid, pmsg, sizeof(STRUCT_MSG_BUF), 0) == -1) { perror("send msg to message queue failed"); exit(EXIT_FAILURE); } } return 0; } int message_receive(int msgid, STRUCT_MSG_BUF* pmsg, int flag){ if( msgrcv(msgid, pmsg, sizeof(STRUCT_MSG_BUF), 0, flag) == -1 ) { perror("receive msg from message queue failed"); exit(EXIT_FAILURE); } return 0;}
#include#include #include pthread_t create_thread( void* pFunc){ pthread_t tid; if(pthread_create(&tid, NULL, (void*)pFunc, NULL) == 0) { //fprintf(stdout, "create thread success!\n"); }else { //fprintf(stderr, "create thread failed!\n"); exit(EXIT_FAILURE); } return tid;}
#ifndef __THREAD_H#define __THREAD_H#include "pthread.h"pthread_t create_thread(void* pFunc);#endif
/**@file timer.c * * Timer for game */#include#include "timer.h"int interval_time(long useconds){ if(useconds >= MAX_USECONDS) return -1; return usleep((useconds_t)useconds);}
#ifndef __TIMER_H#define __TIMER_H#define MAX_USECONDS 1000000#define INTERVAL 160000int interval_time(long useconds);#endif
#include#include #include #include "game.h"#include "argument.h"#include "engine.h"#include "daemon.h"#include "timer.h"#include "message.h"STRUCT_GAME struct_game;int main(int argc, char* argv[]){ if(argc >1) args_handle(argc, argv); struct_game.mode=GAME_INIT; // init game game_init(); while(1==1) { switch(struct_game.mode) { case GAME_INIT: game_start(); struct_game.mode=GAME_RUN; break; case GAME_RUN: game_run(); struct_game.mode=GAME_OVER; break; case GAME_ABORT: game_abort("GAME is ABORTED\n"); break; case GAME_OVER: game_over(); break; default: fprintf(stdout, "MODE = [%d]\n", struct_game.mode); break; } } //message_init(); return 0;}