Game::Game(shared_ptr<IView> _view) : userInterface(std::move(_view)) { Q_ASSERT(userInterface); players.push_back(std::make_unique<Player>("Player 1")); players.push_back(std::make_unique<Player>("Player 2")); auto v = userInterface.get(); // connect View to Presenter connect(v, SIGNAL(New_game(int)), this, SLOT(new_game(int))); connect(v, SIGNAL(Save_game(std::string)), this, SLOT(save_game(std::string))); connect(v, SIGNAL(Load_game(std::string)), this, SLOT(load_game(std::string))); connect(v, SIGNAL(Host_game(std::string)), this, SLOT(host_game(std::string))); connect(v, SIGNAL(Join_game(std::string)), this, SLOT(join_game(std::string))); connect(v, SIGNAL(Leave()), this, SLOT(leave())); connect(v, SIGNAL(Put_stone(int,int)), this, SLOT(put_stone(int,int))); connect(v, SIGNAL(Rotate(IView::quadrant,IView::turn)), this, SLOT(rotate(IView::quadrant,IView::turn))); // connect Presenter to View connect(this, SIGNAL(draw_stone(int,int,IView::color)),v,SLOT(Draw_stone(int,int,IView::color))); connect(this, SIGNAL(set_control_settings(IView::control_setting)),v,SLOT(Set_control_settings(IView::control_setting))); connect(this, SIGNAL(message(string)), v, SLOT(Show_message(string))); }
void join(int who) { Track *track; track = &tracks[who]; /* if (!(track->t_flags & PU_FILLER)) track->t_flags |= PU_FILLER; */ /*If you have already joined, do nothing*/ if (track->t_flags & (PU_CONTEST|PU_DONE)) { messOne(255,roboname,who,"You have already joined."); return; } if (!inprogress || !toolate2join) { messAll(255,roboname,"Oh, look who's decided to grace us with his presence: %s (%c)", players[who].p_name, shipnos[who]); join_game(track); } else { messOne(255,roboname,who,"It is too late to join this game."); track->t_flags |= PU_SITOUT; } }
void tmp_side_wait::pre_show(CVideo& /*video*/, twindow& window) { player_list_.init(window); waiting_ = find_widget<tlabel>(&window, "waiting", false, true); connect_signal_mouse_left_click( find_widget<tbutton>(&window, "cancel_", false) , boost::bind( &tmp_side_wait::cancel , this , boost::ref(window))); tlistbox& list = find_widget<tlistbox>(&window, "sides", false); sides_table_ = &list; join_game(window, observe_); // Add the map name to the title. tlabel* label = find_widget<tlabel>(&window, "title", false, true); label->set_label(std::string(_("Set Side")) + ": " + level_["name"].t_str()); waiting_->set_label(_("Waiting for game to start...")); // Force first update to be directly. lobby_base::network_handler(); lobby_update_timer_ = add_timer(game_config::lobby_network_timer , boost::bind(&lobby_base::network_handler, this) , true); }
int create_game(int portal, const std::string &scenario, const std::string &name, bool join) { Packet p(MsgType::ORDER_CREATE_GAME); p.write_string(scenario); p.write_string(name); if(join) { const int error_check = standard_order_checking(portal, p); if(error_check != Error::success) return error_check; return join_game(portal, name); } else { return standard_order_checking(portal, p); } }
void tmp_side_wait::pre_show(CVideo& /*video*/, twindow& window) { std::stringstream strstr; runtime_groups::gs.clear(); member_users_.insert(std::make_pair(group.leader().name(), group.to_membership())); player_list_.init(window); waiting_ = find_widget<tlabel>(&window, "waiting", false, true); connect_signal_mouse_left_click( find_widget<tbutton>(&window, "cancel_", false) , boost::bind( &tmp_side_wait::cancel , this , boost::ref(window))); tlistbox& list = find_widget<tlistbox>(&window, "sides", false); sides_table_ = &list; config response; config& join_cfg = response.add_child("join"); join_cfg["id"] = game_id_; join_cfg["observe"] = false; join_cfg["password"] = game_config::checksum; network::send_data(lobby->chat, response); join_game(window, observe_); // Add the map name to the title. strstr.str(""); strstr << tintegrate::generate_img(unit_types.genus(level_["turn_based"].to_bool()? tgenus::TURN_BASED: tgenus::HALF_REALTIME).icon()); strstr << _("Set Side") << ": " << level_["name"].t_str(); tlabel* label = find_widget<tlabel>(&window, "title", false, true); label->set_label(strstr.str()); waiting_->set_label(_("Waiting for game to start...")); join(); }
Game::Game() { players.push_back(unique_ptr<Player>(new Player("Player 1"))); players.push_back(unique_ptr<Player>(new Player("Player 2"))); // init View here userInterface.reset(new View()); shared_ptr<View> v = std::dynamic_pointer_cast<View>(userInterface); // connect View to Presenter connect(v.get(), SIGNAL(New_game(int)), this, SLOT(new_game(int))); connect(v.get(), SIGNAL(Save_game(std::string)), this, SLOT(save_game(std::string))); connect(v.get(), SIGNAL(Load_game(std::string)), this, SLOT(load_game(std::string))); connect(v.get(), SIGNAL(Host_game(std::string)), this, SLOT(host_game(std::string))); connect(v.get(), SIGNAL(Join_game(std::string)), this, SLOT(join_game(std::string))); connect(v.get(), SIGNAL(Leave()), this, SLOT(leave())); connect(v.get(), SIGNAL(Put_stone(int,int)), this, SLOT(put_stone(int,int))); connect(v.get(), SIGNAL(Rotate(IView::quadrant,IView::turn)), this, SLOT(rotate(IView::quadrant,IView::turn))); // connect Presenter to View connect(this, SIGNAL(draw_stone(int,int,IView::color)),v.get(),SLOT(Draw_stone(int,int,IView::color))); connect(this, SIGNAL(set_control_settings(IView::control_setting)),v.get(),SLOT(Set_control_settings(IView::control_setting))); connect(this, SIGNAL(message(string)), v.get(), SLOT(Show_message(string))); }
server::server(int argc, char **argv) { char name[200]; int port=20202,no_net=0; strcpy(name,getlogin() ? getlogin() : "unknown"); in=NULL; // see if this computer is net capable int i; // preprocessing stuff before checking for connect to server for (i=1; i<argc; i++) { if (!strcmp(argv[i],"-port")) { i++; if (sscanf(argv[i],"%d",&port)!=1 || port<1 || port>0xffff) { dprintf("Bad port number, port should be 1..%d\n",0xffff); exit(0); } } else if (!strcmp(argv[i],"-name")) // name player uses when connecting { i++; strcpy(name,argv[i]); } else if (!strcmp(argv[i],"-nonet")) { dprintf("Network bypassed, no player will be able to connect\n"); no_net=1; } else if (!strcmp(argv[i],"-sync")) sync_check=1; } if (no_net) has_net=0; else has_net=net_init(); for (i=1; i<argc; i++) { if (!strcmp(argv[i],"-net")) { if (!has_net) { dprintf("No network detected, load network drivers and try again\n"); exit(1); } else { out_socket *os=NULL; i++; dprintf("Trying to connect to server %s on port %d\n",argv[i],port); if (!os=create_out_socket(argv[i],port)) { dprintf("%s\n",last_sock_err); dprintf("Make sure server is running...\n"); exit(1); } dprintf("Connected!\n"); join_game(os,name,argv[i]); } } } if (!player_list) // if we are not connecting to a server, become one { is_server=1; if (has_net) { in=new in_socket(port); if (current_sock_err) { dprintf("%s\n",last_sock_err); dprintf("Running single player mode\n"); has_net=0; } //else // file_server=new nfs_server(port+1); } set_local_players(1); } }
void MessageWriter::send_join_game(size_t game_id) { char message_type = JoinGameMessage::type_id(); socket_->send_buffer(&message_type, 1); JoinGameMessage join_game(socket_); join_game.send(game_id); }
void d2_client::mcp_thread_function(void * unused) { std::cout << "[MCP] " << "Connecting to realm server " << mcp_ip << ":" << mcp_port << std::endl; if (mcp_socket.connect(mcp_ip, mcp_port)) { if (debugging) std::cout << "[MCP] " << "Successfully connected to realm server" << std::endl; } else std::cout << "[MCP] " << "Failed to connect to the realm server" << std::endl; mcp_socket.send("\x01"); std::string packet = construct_mcp_packet(0x01, mcp_data); mcp_socket.send(packet); std::string mcp_buffer; while (true) { std::string data; if (!get_mcp_packet(mcp_socket, mcp_buffer, data)) break; ulong identifier = read_byte(data, 2); //std::cout << "[MCP] " << "MCP data:" << std::endl; //std::cout << "[MCP] " << data << std::endl; //print_data(data); if (identifier == 0x01) { ulong result = read_dword(data, 3); switch(result) { case 0x00: if (debugging) std::cout << "[MCP] " << "Successfully logged on to realm server" << std::endl; break; case 0x7e: std::cout << "[MCP] " << "Your CD key has been banned from playing on this realm!" << std::endl; break; case 0x7f: std::cout << "[MCP] " << "Your IP has been banned temporarily!" << std::endl; break; default: std::cout << "[MCP] " << "Unknown realm server logon error occured (" << std::hex << result << ")" << std::endl; break; } if (result != 0) return; // D2 only sends this the first time it connects, so... if (!logged_in) { if (debugging) std::cout << "[MCP] " << "Requesting character list" << std::endl; mcp_socket.send(construct_mcp_packet(0x19, dword_to_string(8))); } else mcp_socket.send(construct_mcp_packet(0x07, character + zero)); } else if (identifier == 0x19) { ulong count = read_word(data, 9); if (count == 0) { std::cout << "[MCP] " << "There are no characters on this account." << std::endl; return; } else { bool found_character = false; bool selected_first_character = false; if (debugging) std::cout << "[MCP] " << "List of characters on this account:" << std::endl; std::size_t offset = 11; for (ulong i = 1; i <= count; i++) { offset += 4; std::string character_name = read_string(data, offset); std::string stats = read_string(data, offset); ulong character_class = read_byte(stats, 13); ulong level = read_byte(stats, 25); ulong flags = read_byte(stats, 26); bool hardcore = (flags & 0x04) != 0; bool dead = (flags & 0x08) != 0; bool expansion = (flags & 0x20) != 0; std::string character_class_string = get_character_class_string(character_class); std::string core_string = hardcore ? "Hardcore" : "Softcore"; std::string expansion_string = expansion ? "Expansion" : "Classic"; if (debugging) std::cout << "[MCP] " << i << ". " << character_name << ", Level " << level << " " << character_class_string << " (" << expansion_string << ", " << core_string; if (hardcore && dead) if (debugging) std::cout << ", Dead"; if (debugging) std::cout << ")" << std::endl; //print_data(stats); if (character.empty() && i == 1) { selected_first_character = true; character = character_name; } if (character == character_name) { found_character = true; class_byte = character_class - 1; character_level = level; } } if (selected_first_character) std::cout << "[MCP] " << "No character was specified in the ini file so the first character is being used" << std::endl; if (!found_character) { std::cout << "[MCP] " << "Unable to locate character specified in the ini file"; return; } std::cout << "[MCP] " << "Logging into character " << character << std::endl; mcp_socket.send(construct_mcp_packet(0x07, character + zero)); } } else if(identifier == 0x07) { ulong result = read_dword(data, 3); if (result != 0) { std::cout << "[MCP] " << "Failed to log into character (" << std::hex << result << ")" << std::endl; return; } if (debugging) std::cout << "[MCP] " << "Successfully logged into character" << std::endl; if (debugging) std::cout << "[MCP] " << "Requesting channel list" << std::endl; bncs_socket.send(construct_bncs_packet(0x0b, lod_id)); if (debugging) std::cout << "[MCP] " << "Entering the chat server" << std::endl; bncs_socket.send(construct_bncs_packet(0x0a, character + zero + realm + "," + character + zero)); if (!logged_in) { if (debugging) std::cout << "[MCP] " << "Requesting MOTD" << std::endl; mcp_socket.send(construct_mcp_packet(0x12, "")); logged_in = true; } } else if (identifier == 0x12) { if (debugging) std::cout << "[MCP] " << "Received MOTD" << std::endl; } else if (identifier == 0x03) { //print_data(data); ulong result = read_dword(data, 9); switch (result) { case 0x00: if (debugging) std::cout << "[MCP] " << "Game has been created successfully" << std::endl; break; case 0x1e: std::cout << "[MCP] " << "Invalid game name specified" << std::endl; break; case 0x1f: std::cout << "[MCP] " << "This game already exists" << std::endl; break; case 0x20: std::cout << "[MCP] " << "Game server down (it is probable that you tried to create a nightmare/hell game with a character who doesn't have access to that difficulty yet, or gamename/password were invalid)" << std::endl; break; case 0x6e: std::cout << "[MCP] " << "Your character can't create a game because it's dead!" << std::endl; break; } if (result == 0) { if (debugging) std::cout << "[MCP] " << "Joining the game we just created" << std::endl; join_game(game_name, game_password); } } else if (identifier == 0x04) { //print_data(data); ulong result = read_word(data, 17); switch(result) { case 0x00: if (debugging) std::cout << "[MCP] " << "Successfully joined the game" << std::endl; break; case 0x29: std::cout << "[MCP] " << "Password is incorrect" << std::endl; break; case 0x2A: std::cout << "[MCP] " << "Game does not exist" << std::endl; break; case 0x2B: std::cout << "[MCP] " << "Game is full" << std::endl; break; case 0x2C: std::cout << "[MCP] " << "You do not meet the level requirements for this game" << std::endl; break; case 0x71: std::cout << "[MCP] " << "A non-hardcore character cannot join a game created by a Hardcore character." << std::endl; break; case 0x73: std::cout << "[MCP] " << "Unable to join a Nightmare game." << std::endl; break; case 0x74: std::cout << "[MCP] " << "Unable to join a Hell game." << std::endl; break; case 0x78: std::cout << "[MCP] " << "A non-expansion character cannot join a game created by an Expansion character." << std::endl; break; case 0x79: std::cout << "[MCP] " << "A Expansion character cannot join a game created by a non-expansion character." << std::endl; break; case 0x7D: std::cout << "[MCP] " << "A non-ladder character cannot join a game created by a Ladder character." << std::endl; break; } if (result == 0 || result == 0x2B) { ulong ip = read_dword(data, 9); gs_ip = nil::net::ip_to_string(ip); gs_hash = data.substr(13, 4); gs_token = data.substr(5, 2); bncs_socket.send(construct_bncs_packet(0x22, lod_id + dword_to_string(0xc) + game_name + zero + game_password + zero)); bncs_socket.send(construct_bncs_packet(0x10, "")); gs_thread.start(nil::thread::function_type(*this, &d2_client::gs_thread_function)); mcp_socket.disconnect(); } } } std::cout << "[MCP] " << "Disconnected from MCP server" << std::endl; }
// Show and loop main menu void show_menu_loop() { char *ip = ""; FILE *han; SDL_Event event; int createserver = 0; SDL_Thread *ReadThread; //GConfClient *gcc = NULL; // Init GConf //g_type_init(); //gcc = gconf_client_get_default(); //createserver = gconf_client_get_bool(gcc, BATTLEGWELED_CREATESERVER, NULL); //ip = gconf_client_get_string(gcc, BATTLEGWELED_SERVERIP, NULL); // Single player if (!createserver && !strcmp(ip, "")) { han = fopen("/tmp/.battlegweled-save", "rb"); if (han) { fread(&total_score, sizeof(int), 1, han); fread(&single_timer, sizeof(int), 1, han); fread(&timer_delay, sizeof(int), 1, han); fread(&score, sizeof(int), 1, han); fread(nb_of_tiles, sizeof(nb_of_tiles), 1, han); fread(matrix, BOARD_WIDTH * BOARD_HEIGHT, sizeof(int), han); fclose(han); new_game(true, GM_SINGLE, true); } else new_game(true, GM_SINGLE, false); if (game_loop()) { flush_callback(0); quit_callback(0); } else exit_callback(0); } else { int joined; if (createserver) create_game(); else joined = join_game(ip); while (1) { // If connected then start the game if (ss == SS_CONNECTED) { new_game(true, GM_MULTIPLAYER, false); ReadThread = SDL_CreateThread(multi_player_loop, "ReadThread", NULL); game_loop(); //SDL_KillThread(ReadThread); SDL_WaitThread(ReadThread, NULL); break; } if (ss == SS_ERROR || (SDL_PollEvent(&event) && ((event.key.state == SDL_PRESSED && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_F4 || event.key.keysym.sym == SDLK_F5 || event.key.keysym.sym == SDLK_F6)) || event.type == SDL_QUIT || (event.type == SDL_MOUSEBUTTONDOWN && event.button.x >= BACK_OFFSETX && event.button.y >= BACK_OFFSETY && event.button.x < BACK_OFFSETX2 && event.button.y < BACK_OFFSETY2)))) { SDL_WaitThread(ThreadConnect, NULL); SDL_WaitThread(ThreadAccept, NULL); //if (ThreadConnect) SDL_KillThread(ThreadConnect); //if (ThreadAccept) SDL_KillThread(ThreadAccept); break; } // Update screen draw_waiting_screen(); } quit_callback(0); } }
/** * 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); } } } } }
void game_state(GameState state) { //Game state destructors switch(gamestate) { case GAME_STATE_GAME: //pthread_cancel(game.thread); break; case GAME_STATE_MENU: //ui_event_global_remove(menu_buttons, UI_EVENT_TYPE_BUTTONS); case GAME_STATE_SELECT_NAME: case GAME_STATE_HOST: case GAME_STATE_LOBBY: case GAME_STATE_ENTER_IP: case GAME_STATE_CHARACTERS: break; case GAME_STATE_GAMEROOM: break; case GAME_STATE_GAME_OVER: case GAME_STATE_QUIT: case GAME_STATES: break; } //Game state constructors switch(state) { case GAME_STATE_GAME: ingame_init(); //init game shit //pthread_create(&game.thread, NULL, object_thread, NULL); #ifndef __DEBUG__ //d_input_grab(); #endif break; case GAME_STATE_MENU: //ui_event_global_add(menu_buttons, UI_EVENT_TYPE_BUTTONS); case GAME_STATE_SELECT_NAME: ui_selected_widget = select_name.entry; break; case GAME_STATE_LOBBY: //gameroom.button.start->enabled = false; s->is_host = false; //ui_listbox_clear(lobby.list); break; case GAME_STATE_ENTER_IP: ui_selected_widget = enter_ip.entry; break; case GAME_STATE_HOST: s->is_host = true; server_start(); gameroom.button.start->enabled = true; join_game(network_local_ip()); state = GAME_STATE_GAMEROOM; case GAME_STATE_GAMEROOM: ui_listbox_clear(gameroom.list); case GAME_STATE_CHARACTERS: case GAME_STATE_GAME_OVER: case GAME_STATE_QUIT: d_input_release(); case GAME_STATES: break; } gamestate=state; }