Esempio n. 1
0
int c_pvp_room::calc_match_flag(uint8_t *p_flag)
{
    if (ROOM_MODE_PVP_COMPETE == get_mode()) {
        // 竞技模式一定要匹配
        m_match_flag = 1;
        if (p_flag) {
            *p_flag = m_match_flag;
        }
        return 0;
    }

	/* 到此, 一定是自由模式 (ROOM_MODE_PVP_FREE) */
    if (m_player_count * 2 > m_max_seat_count) {
        // 玩家人数过半的不能匹配
        m_match_flag = 0;
        if (p_flag) {
            *p_flag = m_match_flag;
        }
        return 0;
    }

    uint8_t seat = 0;
    if (0 != find_first_player(&seat)) {
        return -1;
    }

    room_seat_t * p_seat = get_seat(seat);
    uint8_t team = p_seat->player->get_team();
    seat++;

    while (seat < m_max_seat_count) {
        p_seat = get_seat(seat);

        if (p_seat->is_taken()) {
            if (team != p_seat->player->get_team()) {
                // 房间中有不同的颜色的不能匹配
                m_match_flag = 0;
                if (p_flag) {
                    *p_flag = m_match_flag;
                }

                return 0;
            }
        }
        seat++;
    }

    // 其他情况均需要匹配
    m_match_flag = 1;
    if (p_flag) {
        *p_flag = m_match_flag;
    }

    return 0;
}
Esempio n. 2
0
static void
handle_surface(struct test_client *client)
{
	uint32_t id;
	struct wl_resource *resource;
	struct weston_surface *surface;
	struct text_test_data *data = client->data;
	struct weston_seat *seat;

	assert(sscanf(client->buf, "surface %u", &id) == 1);
	fprintf(stderr, "got surface id %u\n", id);
	resource = wl_client_get_object(client->client, id);
	assert(resource);
	assert(strcmp(resource->object.interface->name, "wl_surface") == 0);

	surface = (struct weston_surface *) resource;

	weston_surface_configure(surface, 100, 100, 200, 200);
	weston_surface_update_transform(surface);
	weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);

	data->layer = malloc(sizeof *data->layer);
	weston_layer_init(data->layer, &client->compositor->cursor_layer.link);
	wl_list_insert(&data->layer->surface_list, &surface->layer_link);
	weston_surface_damage(surface);

	seat = get_seat(client);
	client->compositor->focus = 1; /* Make it work even if pointer is
					* outside X window. */
	wl_keyboard_set_focus(&seat->keyboard, &surface->surface);

	test_client_send(client, "create-text-model\n");
	client->handle = handle_text_model;
}
Esempio n. 3
0
int c_pvp_room::set_mode(uint8_t mode)
{
    c_room::set_mode(mode);

    if (ROOM_MODE_PVP_FREE == mode) {
        // 将关闭的座位全部打开,已经打开的或者有人的座位不动
        m_max_seat_count = MAX_PVP_ROOM_SEAT_NUM;
        for (uint8_t i = 0; i < MAX_PVP_ROOM_SEAT_NUM; i++) {
            room_seat_t * p_seat = get_seat(i);
            if (p_seat->is_close()) {
                open_seat(i);
            }
        }
    } else if (ROOM_MODE_PVP_COMPETE == mode) {
        // 关闭4个对手位置
        for (uint8_t i = MAX_PVP_ROOM_SEAT_NUM / 2; i < MAX_PVP_ROOM_SEAT_NUM; i++) {
            close_seat(i);
        }

        // 修改max_seat_count一定在close_seat后面
        // 否则get_seat会得到错误的seat
        m_max_seat_count = MAX_PVP_ROOM_SEAT_NUM / 2;
    }

    return 0;
}
Esempio n. 4
0
static gboolean
game_handle_io (GGZMod * mod)
{
  int op = -1;

  fd = ggzmod_get_server_fd (mod);

  // Read the fd
  if (ggz_read_int (fd, &op) < 0) {
    ggz_error_msg ("Couldn't read the game fd");
    return FALSE;
  }

  switch (op) {
  case GN_MSG_SEAT:
    get_seat ();
    break;
  case GN_MSG_PLAYERS:
    get_players ();
    break;

  case GN_MSG_START:
    gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), MAIN_PAGE);
    new_game ();
    break;

  case GN_MSG_SYNC:
    get_sync ();
    break;

  case GN_MSG_MOVE:
    get_move ();
    break;

  case GN_MSG_SETTINGS:
    get_settings ();
    break;

  case GN_MSG_BONI:
    get_boni ();
    break;

  case GN_MSG_NOBONI:
    get_noboni ();
    break;

  default:
    ggz_error_msg ("Incorrect opcode   %d \n", op);
    break;
  }

  return TRUE;
}
Esempio n. 5
0
static void
handle_surface_unfocus(struct test_client *client)
{
	struct weston_seat *seat;

	seat = get_seat(client);

	pre_assert_state(client, 2, 1);

	/* Unfocus the surface */
	wl_keyboard_set_focus(&seat->keyboard, NULL);

	post_assert_state(client, 2, 2, "bye\n", NULL);
}
Esempio n. 6
0
int c_pvp_room::change_team(uint8_t old_seat, uint32_t new_team)
{
    if (ROOM_MODE_PVP_COMPETE == get_mode()) {
        // 竞技pvp不允许调整队伍
        return -1;
    }
    
    room_seat_t * p_old_seat = get_seat(old_seat);
    if (!p_old_seat->is_taken()) {
        return -1;
    }

    c_player * p = p_old_seat->player;
    if (new_team == p->get_team()) {
        return 0;
    }

    uint8_t new_seat = 0;
    if (TEAM_ONE == new_team) {
        // 2换到1
        if (0 != find_first_blank(&new_seat)) {
            // 满了
            return -1;
        }

        if (TEAM_TWO == calc_team(new_seat)) {
            // 队伍1满了
            return -1;
        }

        change_seat(old_seat, new_seat);
        p->set_team(new_team);
    } else {
        // 1换到2
        if (0 != find_first_blank(&new_seat, team_start_seat(TEAM_TWO))) {
            // 满了
            return -1;
        }

        change_seat(old_seat, new_seat);
        p->set_team(new_team);
    }

    return 0;
}
Esempio n. 7
0
gboolean
gvfs_udisks2_utils_is_drive_on_our_seat (UDisksDrive *drive)
{
  gboolean ret = FALSE;
  const gchar *seat;
  const gchar *drive_seat = NULL;

  /* assume our own seat if we don't have seat-support or it doesn't work */
  seat = get_seat ();
  if (seat == NULL)
    {
      ret = TRUE;
      goto out;
    }

  /* If the device is not tagged, assume that udisks does not have
   * working seat-support... so just assume it's available at our
   * seat.
   *
   * Note that seat support was added in udisks 1.95.0 (and so was the
   * UDISKS_CHECK_VERSION macro) - for now, be compatible with older
   * versions instead of bumping requirement in configure.ac
   */
#ifdef UDISKS_CHECK_VERSION
# if UDISKS_CHECK_VERSION(1,95,0)
  drive_seat = udisks_drive_get_seat (drive);
# endif
#endif
  if (drive_seat == NULL || strlen (drive_seat) == 0)
    {
      ret = TRUE;
      goto out;
    }

  /* Otherwise, check if it's on our seat */
  if (g_strcmp0 (seat, drive_seat) == 0)
    ret = TRUE;

 out:
  return ret;
}
Esempio n. 8
0
int c_pvp_room::calc_start_flag(uint8_t * p_flag)
{
    for (uint8_t i = 0; i < m_max_seat_count; i++) {
        room_seat_t * p_seat = get_seat(i);
        if (p_seat->is_taken()) {
            if (PLAYER_STATUS_READY != p_seat->player->get_status()) {
                m_start_flag = 0;
                if (p_flag) {
                    *p_flag = m_start_flag;
                }

                return 0;
            }
        }
    }

    m_start_flag = 1;
    if (p_flag) {
        *p_flag = m_start_flag;
    }

    return 0;
}
Esempio n. 9
0
// executes the actual request based on contents of the task
void load_connection(pool_task_t* task, pool_t* p)
{	
	int seat_id = task->seat_id;
	int connfd = task->connfd;
	int customer_priority = task->priority;
	int user_id = task->user_id;
	int length = task->length;
	char* buf = task->buf;
	char* resource = task->resource;
	int fd;
    char *ok_response = "HTTP/1.0 200 OK\r\n"\
                           "Content-type: text/html\r\n\r\n";

    char *notok_response = "HTTP/1.0 404 FILE NOT FOUND\r\n"\
                            "Content-type: text/html\r\n\r\n"\
                            "<html><body bgColor=white text=black>\n"\
                            "<h2>404 FILE NOT FOUND</h2>\n"\
                            "</body></html>\n";
                              
    if (strncmp(resource, "list_seats", length) == 0)
    {  
        list_seats(buf, BUFSIZE, p);
        // send headers
        writenbytes(connfd, ok_response, strlen(ok_response));
        // send data
        writenbytes(connfd, buf, strlen(buf));
    } 
    else if(strncmp(resource, "view_seat", length) == 0)
    {	
		//if there are seats left
    	if(sem_wait(p->seatsem,&p->seatsem_lock)!=-1){
			//get the lock on the seat
    		if(seat_id<20 && seat_id>-1){  		
    			pthread_mutex_lock(&p->seat_locks[seat_id]);
    		}
			view_seat(buf, BUFSIZE, seat_id, user_id, customer_priority);
			writenbytes(connfd, ok_response, strlen(ok_response));
			writenbytes(connfd, buf, strlen(buf));
			if(seat_id<20 && seat_id>-1){
				pthread_mutex_unlock(&p->seat_locks[seat_id]);
			}
		}
		else{
			//space is available on standbylist
			seat_t* seat = get_seat(seat_id);
			
			//only add to standby list if the seat is pending
			if(sem_wait(p->sbsem,&p->sbsem_lock)!=-1 && seat->state==PENDING){
				pthread_mutex_lock(&p->sblock);
				pool_task_t* prev = NULL;
				pool_task_t* curr = p->standbylist;
				while(curr!=NULL){
					prev = curr;
					curr = curr->next;
				}
				if(prev!=NULL){
					prev->next = task;
				}
				else{
					p->standbylist = task;
				}
				task->next = NULL;
				pthread_mutex_unlock(&p->sblock);
				return;
			}
			//no space available on standbylist
			else{
				snprintf(buf, BUFSIZE, "Seat unavailable\n\n");
				writenbytes(connfd, ok_response, strlen(ok_response));
				writenbytes(connfd, buf, strlen(buf));
			}	
		}
    } 
    else if(strncmp(resource, "confirm", length) == 0)
    {
    	pthread_mutex_lock(&p->seat_locks[seat_id]);
        confirm_seat(buf, BUFSIZE, seat_id, user_id, customer_priority);		
        pthread_mutex_unlock(&p->seat_locks[seat_id]);
		
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
		
		//check the standby list for requests that waited for this seat
        pthread_mutex_lock(&p->sblock);
        pool_task_t* curr = p->standbylist;
        pool_task_t* prev = NULL;
        int spaces = 0;
        while(curr!=NULL){
        	if(curr->seat_id==seat_id){
        		spaces++;
        		if(prev==NULL){
        			p->standbylist = p->standbylist->next;
        		}
        		else{
        			prev->next = curr->next;
        		}
        		pthread_mutex_unlock(&p->sblock);
        		load_connection(curr,p);
        		pthread_mutex_lock(&p->sblock);
        	}
        	else{
        		prev = curr;
        	}
        	curr = curr->next;
        }
        pthread_mutex_unlock(&p->sblock);
        int i;
        for(i=0;i<spaces;i++){
        	sem_post(p->sbsem,&p->sbsem_lock);
        }
    }
    else if(strncmp(resource, "cancel", length) == 0)
    {
    	if(pthread_mutex_lock(&p->seat_locks[seat_id])==EDEADLK){ printf("DEADLOCKED THREAD\n"); fflush(stdout);}
        cancel(buf, BUFSIZE, seat_id, user_id, customer_priority);
        pthread_mutex_unlock(&p->seat_locks[seat_id]);
		
        writenbytes(connfd, ok_response, strlen(ok_response));
        writenbytes(connfd, buf, strlen(buf));
		
        if(pthread_mutex_lock(&p->cancellock)==EDEADLK){ printf("DEADLOCKED THREAD\n"); fflush(stdout);}
		
		//update the cancellation info in pool struct 
        p->last_cancelled = seat_id;
        p->sbtid = pthread_self();
        
        sem_post(p->seatsem,&p->seatsem_lock);
    }
    else
    {
        // try to open the file
        if ((fd = open(resource, O_RDONLY)) == -1)
        {
            writenbytes(connfd, notok_response, strlen(notok_response));
        } 
        else
        {
            writenbytes(connfd, ok_response, strlen(ok_response));
            int ret;
            while ( (ret = read(fd, buf, BUFSIZE)) > 0) {
                writenbytes(connfd, buf, ret);
            }  
            // close file and free space
            close(fd);
        } 
    }
    close(connfd);
}