project_files/frontlib/net/netprotocol.c
changeset 7269 5b0aeef8ba2a
child 7271 5608ac657362
equal deleted inserted replaced
7267:710f3ced8934 7269:5b0aeef8ba2a
       
     1 #include "netprotocol.h"
       
     2 
       
     3 #include "../util/util.h"
       
     4 #include "../util/logging.h"
       
     5 
       
     6 #include <string.h>
       
     7 #include <stdio.h>
       
     8 #include <errno.h>
       
     9 #include <stdlib.h>
       
    10 
       
    11 static int fillTeamFromMsg(flib_team *team, char **parts) {
       
    12 	team->name = flib_strdupnull(parts[0]);
       
    13 	team->grave = flib_strdupnull(parts[2]);
       
    14 	team->fort = flib_strdupnull(parts[3]);
       
    15 	team->voicepack = flib_strdupnull(parts[4]);
       
    16 	team->flag = flib_strdupnull(parts[5]);
       
    17 	if(!team->name || !team->grave || !team->fort || !team->voicepack || !team->flag) {
       
    18 		return -1;
       
    19 	}
       
    20 
       
    21 	long color;
       
    22 	if(sscanf(parts[1], "#%lx", &color)) {
       
    23 		team->color = color;
       
    24 	} else {
       
    25 		return -1;
       
    26 	}
       
    27 
       
    28 	errno = 0;
       
    29 	long difficulty = strtol(parts[6], NULL, 10);
       
    30 	if(errno) {
       
    31 		return -1;
       
    32 	}
       
    33 
       
    34 	for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
       
    35 		flib_hog *hog = &team->hogs[i];
       
    36 		hog->difficulty = difficulty;
       
    37 		hog->name = flib_strdupnull(parts[7+2*i]);
       
    38 		hog->hat = flib_strdupnull(parts[8+2*i]);
       
    39 		if(!hog->name || !hog->hat) {
       
    40 			return -1;
       
    41 		}
       
    42 	}
       
    43 	return 0;
       
    44 }
       
    45 
       
    46 flib_team *flib_team_from_netmsg(char **parts) {
       
    47 	flib_team *result = NULL;
       
    48 	flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team));
       
    49 	if(tmpTeam) {
       
    50 		if(!fillTeamFromMsg(tmpTeam, parts)) {
       
    51 			result = tmpTeam;
       
    52 			tmpTeam = NULL;
       
    53 		} else {
       
    54 			flib_log_e("Error parsing team from net.");
       
    55 		}
       
    56 	}
       
    57 	flib_team_destroy(tmpTeam);
       
    58 	return result;
       
    59 }