Esempio n. 1
0
// Broadcasts changes in the game to Subscribers
int
doUpdateClientsGame(int updateMapVersion)
{
    Proto_Session *s;
    Proto_Msg_Hdr hdr;

    if (proto_debug())
        fprintf(stderr, "doUpdateClientsGame called\n"); 

    bzero(&hdr, sizeof(hdr));

    s = proto_server_event_session();
    // set sver if nescesary
    hdr.type = PROTO_MT_EVENT_BASE_UPDATE;
    // pthread_spin_lock(&server_data_spinlock);
    hdr.sver.raw = server_gameData.version;
    hdr.gstate.v0.raw = server_gameData.state;
    // pthread_spin_unlock(&server_data_spinlock);
    proto_session_hdr_marshall(s, &hdr);

    // test
    proto_server_test_msg(s);
    proto_server_post_event();
    return 1;
}
Esempio n. 2
0
static int
proto_server_pickup_handler(Proto_Session *s){
  int id,rc,ret;
  Proto_Msg_Hdr sh;
  Proto_Msg_Hdr rh;
  Player* p;
  Proto_Session *us;

  bzero(&sh, sizeof(sh));
  bzero(&rh, sizeof(rh));

  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  proto_session_hdr_unmarshall(s, &rh);
  id = rh.pstate.v0.raw;
  p = gamestate_get_player(Server_Gamestate,id);
  
  ret = player_obj_pickup(p,Server_ObjectMap,Server_Gamestate);
  sh.pstate.v0.raw = id;//same header for both reply and update
  sh.pstate.v1.raw = ret; //sending back the actual thing picked up
  proto_session_hdr_marshall(s, &sh);
  rc = proto_session_send_msg(s,1);
  
  if(ret >= 0){
    us = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_MOVE;
    proto_session_hdr_marshall(us,&sh);
    proto_server_post_event();
  } 
  return rc;
}
int
doUpdateClients(void) {
    Proto_Session *s;
    Proto_Msg_Hdr hdr;

    s = proto_server_event_session();
    hdr.type = PROTO_MT_EVENT_BASE_UPDATE;
    proto_session_hdr_marshall(s, &hdr);
    proto_server_post();
    return 1;
}
Esempio n. 4
0
//send back an ack reply then close session
static int
proto_server_goodbye_handler(Proto_Session *s){
  Proto_Msg_Hdr h;
  Proto_Session *us;
  Player p;
  int rc=1;
  
  proto_session_body_unmarshall_player(s,0,&p);

  bzero(&h, sizeof(h));
  h.type = PROTO_MT_REP_BASE_GOODBYE;
  proto_session_hdr_marshall(s, &h);

  rc = proto_session_send_msg(s,1);

  if(p.team == TEAM1){
    numPlayers1--;
    if(p.state == SAFE)
      numplayershome1--;
  } else{
    numPlayers2--;
    if(p.state == SAFE)
      numplayershome2--;
  }
  gamestate_remove_player(Server_Gamestate,p.id);
  pidx = player_find_next_id(Server_Gamestate->plist);
  if(pidx<0)
    gamefull = 1;
  else
    gamefull = 0;

  us = proto_server_event_session();
  h.type = PROTO_MT_EVENT_BASE_PLAYER_QUIT;
  proto_session_hdr_marshall(us,&h);
  proto_session_body_marshall_player(us, &p);
  proto_server_post_event();

  return rc;
}
Esempio n. 5
0
static int
proto_server_move_handler(Proto_Session *s){
  int i,rx,ry,id,rc, winner;
  dir_t dir;
  Cell *cell = malloc(sizeof(Cell));
  Proto_Msg_Hdr sh;
  Proto_Msg_Hdr rh;
  Player* p;
  Proto_Session *us;
  int valid;
  int flagindex;
  object_t flag = -1;
  Proto_Session *fs;
  bzero(&sh, sizeof(sh));
  bzero(&rh, sizeof(rh));

  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  proto_session_hdr_unmarshall(s, &rh);
  id = rh.pstate.v0.raw;
  dir = rh.pstate.v1.raw;
  p = gamestate_get_player(Server_Gamestate,id);
  valid = 0;
  flagindex = -1;
  valid = player_move(dir,p,Server_ObjectMap, Server_Gamestate);
  //  printf("Valid bit%d\n", valid);
  if (valid>0) {
    sh.pstate.v3.raw = 1;
    printf("Player %d is moving to (%d,%d)\n",id,p->pcell->x,p->pcell->y);
    if(!DEBUG_MAP){
      flagindex = objectmap_flag_visible(p,Server_ObjectMap);
      
      if(flagindex>=0)
	flag = Server_ObjectMap->objects[flagindex]->obj;
      
      if(flag == FLAG1){
	if(!flag1found)
	  flag1found = 1;
	else
	  flagindex = -1;
      }
      if(flag == FLAG2){
	if(!flag2found)
	  flag2found = 1;
	else
	  flagindex = -1;
      }
    }
  } else {
    sh.pstate.v3.raw = 0;    
    printf("Player %d attemped an invalid move\n",id);
  }
  sh.pstate.v0.raw = p->id;
  sh.pstate.v1.raw = p->pcell->x;
  sh.pstate.v2.raw = p->pcell->y;
 
  proto_session_hdr_marshall(s, &sh);
  
  rc = proto_session_send_msg(s,1);
  
  if(valid){
    us = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_MOVE;
    proto_session_hdr_marshall(us,&sh);
    proto_server_post_event();
  }
  
  bzero(&sh, sizeof(sh));
  if(flagindex>=0){
    fs = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_FLAG;
    
    //gstate.v0 holds the flag itself
    //gstate.v1 holds the index in the objectmap where that flag is located
    sh.gstate.v0.raw = Server_ObjectMap->objects[flagindex]->obj;
    sh.gstate.v1.raw = flagindex;
    proto_session_hdr_marshall(fs,&sh);
    proto_server_post_event();
  }
  
  bzero(&sh, sizeof(sh));
  if((winner = gamestate_team_wins()) >= 0){
    sh.type = PROTO_MT_EVENT_BASE_WIN;
    sh.gstate.v0.raw = winner;
    proto_session_hdr_marshall(fs,&sh);
    proto_server_post_event();
  }

  return rc;
}
Esempio n. 6
0
static int
proto_server_hello_handler(Proto_Session *s){
  int rc = 1;
  int x;
  Player* p;
  Proto_Msg_Hdr sh;
  Proto_Session *us;

  p = player_create();

  bzero(&sh, sizeof(sh));
  
  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  x = player_find_empty_home(p,nextTeam, Server_ObjectMap, pidx);
  //  x = !gamefull;
  if(x){
    gamestate_add_player(Server_Gamestate,p);
    printf("New player joining:\n");
    printf("Location: %d,%d\n", p->pcell->x, p->pcell->y);
    player_dump(p);
    sh.pstate.v0.raw = 1;
    sh.pstate.v1.raw = p->id;
    gamestate_dump(Server_Gamestate);
    proto_session_body_marshall_gamestate(s,Server_Gamestate);
    //    proto_session_body_marshall_map(s,Server_Map);
    if(nextTeam == TEAM1){
      nextTeam = TEAM2;
      numPlayers1++;
    } else{
      nextTeam = TEAM1;
      numPlayers2++;
    }
    pidx = player_find_next_id(Server_Gamestate->plist);
    if(pidx<0)
      gamefull = 1;

    s->player = p;
  } else {
    sh.pstate.v0.raw = 0;
  }

  proto_session_hdr_marshall(s, &sh);
  
  //  proto_dump_msghdr(&(s->shdr));
  rc = proto_session_send_msg(s,1);
  
  if(x){ //if new player joining, tell everyone    
    us = proto_server_event_session();
    bzero(&sh, sizeof(sh));
    sh.type = PROTO_MT_EVENT_BASE_PLAYER_JOIN;
    sh.pstate.v0.raw = p->id;
    sh.pstate.v1.raw = p->pcell->x;
    sh.pstate.v2.raw = p->pcell->y;
    proto_session_hdr_marshall(us, &sh);
    proto_session_body_marshall_player(us,p);
    proto_server_post_event();
  }

  return rc;
}