static void do_set_pos(context_t * ctx,const char * map, int x, int y, bool change_map) { context_set_map(ctx,map); context_set_pos_tx(ctx,x); context_set_pos_ty(ctx,y); entry_write_string(CHARACTER_TABLE,ctx->id,map,CHARACTER_KEY_MAP,NULL); entry_write_int(CHARACTER_TABLE,ctx->id,x,CHARACTER_KEY_POS_X,NULL); entry_write_int(CHARACTER_TABLE,ctx->id,y,CHARACTER_KEY_POS_Y,NULL); context_spread(ctx); if( change_map == true ) { context_request_other_context(ctx); } }
/************************************** Called from client **************************************/ void context_add_or_update_from_network_frame(context_t * context,char * data) { context_t * ctx = NULL; char * user_name = NULL; char * name = NULL; char * map = NULL; int in_game; bool connected; int pos_tx; int pos_ty; char * type = NULL; char * id = NULL; char * selected_character = NULL; char * selected_map = NULL; int selected_map_x = 0; int selected_map_y = 0; char * selected_equipment = NULL; char * selected_item = NULL; /* First decode the data */ user_name = strdup(data); data += (strlen(data)+1); name = strdup(data); data += (strlen(data)+1); map = strdup(data); data += (strlen(data)+1); in_game = atoi(data); data += (strlen(data)+1); connected = atoi(data); data += (strlen(data)+1); pos_tx = atoi(data); data += (strlen(data)+1); pos_ty = atoi(data); data += (strlen(data)+1); type = strdup(data); data += (strlen(data)+1); id = strdup(data); data += (strlen(data)+1); selected_character = strdup(data); data += (strlen(data)+1); selected_map = strdup(data); data += (strlen(data)+1); selected_map_x = atoi(data); data += (strlen(data)+1); selected_map_y = atoi(data); data += (strlen(data)+1); selected_equipment = strdup(data); data += (strlen(data)+1); selected_item = strdup(data); data += (strlen(data)+1); /* search for this context */ context_lock_list(); ctx = context_list_start; while( ctx != NULL ) { if( strcmp( id, ctx->id) == 0 ) { ctx->in_game = in_game; ctx->connected = connected; if( in_game == true ) { wlog(LOGDEBUG,"Updating context %s / %s",user_name,name); /* do not call context_set_* function since we already have the lock */ _context_set_map(ctx,map); _context_set_pos_tx(ctx,pos_tx); _context_set_pos_ty(ctx,pos_ty); free(ctx->type); ctx->type = strdup(type); if( ctx->selection.map ) { free(ctx->selection.map ); } ctx->selection.map = strdup(selected_map); ctx->selection.map_coord[0] = selected_map_x; ctx->selection.map_coord[1] = selected_map_y; if( ctx->selection.id ) { free(ctx->selection.id ); } ctx->selection.id = strdup(selected_character); if( ctx->selection.equipment ) { free(ctx->selection.equipment ); } ctx->selection.equipment = strdup(selected_equipment); if( ctx->selection.inventory ) { free(ctx->selection.inventory ); } ctx->selection.inventory = strdup(selected_item); } if( connected == false ) { wlog(LOGDEBUG,"Deleting context %s / %s",user_name,name); context_free(ctx); } context_unlock_list(); goto context_add_or_update_from_network_frame_free; } ctx = ctx->next; } context_unlock_list(); wlog(LOGDEBUG,"Creating context %s / %s",user_name,name); ctx = context_new(); context_set_username(ctx,user_name); context_set_character_name(ctx,name); context_set_map(ctx,map); context_set_type(ctx,type); context_set_pos_tx(ctx,pos_tx); context_set_pos_ty(ctx,pos_ty); context_set_id(ctx,id); context_set_connected(ctx,connected); context_set_in_game(ctx,in_game); context_set_selected_character(ctx,selected_character); context_set_selected_tile(ctx,selected_map,selected_map_x,selected_map_y); context_set_selected_equipment(ctx,selected_equipment); context_set_selected_item(ctx,selected_item); context_add_or_update_from_network_frame_free: free(user_name); free(name); free(map); free(type); free(id); free(selected_character); free(selected_map); free(selected_equipment); free(selected_item); }