View::View(QWidget *parent): QGraphicsView(parent) { setGeometry(300,300,GS_WIDTH,GS_HEIGHT); menu_scene = new GraphicMenu(-width()/2,-height()/2,width(),height(),this); board_scene = new GraphicBoard(-width()/2,-height()/2,width(),height(),this); QIcon icon(QPixmap(":/Graphic_source/icon.png")); setWindowIcon(icon); setWindowTitle("Pentago"); setFixedSize(this->size());//TODO:equally resize setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); setCacheMode(QGraphicsView::CacheBackground); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); connect(menu_scene, SIGNAL(New_game_selected(int)), this, SIGNAL(New_game(int))); connect(menu_scene, SIGNAL(Join_game_selected(std::string)), this, SIGNAL(Join_game(std::string))); connect(menu_scene, SIGNAL(Host_game_selected(std::string)), this, SIGNAL(Host_game(std::string))); connect(menu_scene, SIGNAL(Exit_game_pressed()), this, SLOT(close())); connect(menu_scene, SIGNAL(Load_game_selected(std::string)),this,SIGNAL(Load_game(std::string))); connect(board_scene, SIGNAL(quadrant_rotated(IView::quadrant,IView::turn)), this, SIGNAL(Rotate(IView::quadrant,IView::turn))); connect(board_scene, SIGNAL(Stone_clicked(int, int)), this, SIGNAL(Put_stone(int,int))); connect(board_scene, SIGNAL(leave_game()),this,SLOT(on_leave_game())); connect(board_scene, SIGNAL(save_game(QString)),this,SLOT(on_save_game(QString))); connect(board_scene, SIGNAL(message_sended(QString)), this, SLOT(on_msg_sended(QString))); connect(menu_scene, SIGNAL(game_go_to_start()),board_scene, SLOT(clean_board())); connect(menu_scene, SIGNAL(game_go_to_start()),board_scene, SLOT(clean_log())); Set_control_settings(View::MENU);//it must be called in presenter after view constructing }
bool turn(Labyrinth &map) { take_objects_from_cell(map, map.player[map.current_player]); if (DEBUG) print_debug(map); user_message(map.player[map.current_player].name + ", it's your turn"); string message = map.player[map.current_player].name + ", enter what you want (go, bomb, shoot, knife, suicide, stay, leave"; if (SERVER) message += ")"; else message += ", save)"; user_message(message); if (SERVER) server.clear_user_messages(map.current_player); string s = read_user_command(map.current_player); if (s == "leave") return leave_game(map, map.player[map.current_player]); if (s == "suicide") return suicide(map, map.player[map.current_player]); if (s == "knife") return use_knife(map, map.player[map.current_player]); if (s == "bomb") return bomb(map, map.player[map.current_player]); if (s == "go") return go(map, map.player[map.current_player]); if (s == "shoot") return shoot(map, map.player[map.current_player]); if (s == "stay") return stay(map.player[map.current_player]); if (s == "save" && !SERVER) { save_game(map); return false; } if (s == "winthemall") { if (is_developer(map.player[map.current_player])) { user_message("Okay, master"); finish_game(map); return true; } } user_message(map.player[map.current_player].name + ", you entered incorrect command! Try again, if you can!"); return false; }
/** * void process_dgram(char *dgram, struct sockaddr_in *addr) * * Processes accepted datagram. Checks if sequential ID is correct, if it's lower, * we resend the ACK packet and dont bother with that datagram anymore, because * it was already processed before. */ void process_dgram(char *dgram, struct sockaddr_in *addr) { /* Protocol token */ char *token; /* Token length */ int token_len; /* Command */ char *type; /* Generic char buffer */ char *generic_chbuff; /* Sequential ID of received packet */ int packet_seq_id; /* Client which we receive from */ client_t *client; /* Generic unsigned int var */ unsigned int generic_uint; /* String representation of address */ char addr_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &addr->sin_addr, addr_str, INET_ADDRSTRLEN); /* Log */ sprintf(log_buffer, "DATA_IN: %s <--- %s:%d", dgram, addr_str, htons(addr->sin_port) ); log_line(log_buffer, LOG_DEBUG); token = strtok(dgram, ";"); token_len = strlen(token); generic_chbuff = strtok(NULL, ";"); packet_seq_id = (int) strtol(generic_chbuff, NULL, 0); /* Check if datagram belongs to us */ if( (token_len == strlen(STRINGIFY(APP_TOKEN))) && strncmp(token, STRINGIFY(APP_TOKEN), strlen(STRINGIFY(APP_TOKEN))) == 0 && packet_seq_id > 0) { type = strtok(NULL, ";"); /* New client connection */ if(strncmp(type, "CONNECT", 7) == 0) { add_client(addr); client = get_client_by_addr(addr); if(client) { send_ack(client, 1, 0); send_reconnect_code(client); /* Release client */ release_client(client); } } /* Reconnect */ else if(strncmp(type, "RECONNECT", 9) == 0) { client = get_client_by_index(get_client_index_by_rcode(strtok(NULL, ";"))); if(client) { /* Sends ACK aswell after resetting clients SEQ_ID */ reconnect_client(client, addr); /* Release client */ release_client(client); } } /* Client should already exist */ else { client = get_client_by_addr(addr); if(client != NULL) { /* Check if expected seq ID matches */ if(packet_seq_id == client->pkt_recv_seq_id) { /* Get command */ if(strncmp(type, "CREATE_GAME", 11) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); create_game(client); } /* Receive ACK packet */ else if(strncmp(type, "ACK", 3) == 0) { recv_ack(client, (int) strtoul(strtok(NULL, ";"), NULL, 10)); update_client_timestamp(client); } /* Close client connection */ else if(strncmp(type, "CLOSE", 5) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); leave_game(client); remove_client(&client); } /* Keepalive loop */ else if(strncmp(type, "KEEPALIVE", 9) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); } /* Join existing game */ else if(strncmp(type, "JOIN_GAME", 9) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); join_game(client, strtok(NULL, ";")); } /* Leave existing game */ else if(strncmp(type, "LEAVE_GAME", 10) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); leave_game(client); } /* Start game */ else if(strncmp(type, "START_GAME", 10) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); start_game(client); } /* Rolling die */ else if(strncmp(type, "DIE_ROLL", 8) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); roll_die(client); } /* Moving figure */ else if(strncmp(type, "FIGURE_MOVE", 11) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); /* Parse figure id */ generic_chbuff = strtok(NULL, ";"); generic_uint = (unsigned int) strtoul(generic_chbuff, NULL, 10); move_figure(client, generic_uint); } else if(strncmp(type, "MESSAGE", 7) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); broadcast_message(client, strtok(NULL, ";")); } } /* Packet was already processed */ else if(packet_seq_id < client->pkt_recv_seq_id && strncmp(type, "ACK", 3) != 0) { send_ack(client, packet_seq_id, 1); } /* If client didnt close conection */ if(client != NULL) { /* Release client */ release_client(client); } } } } }