project_files/frontlib/net/netprotocol.c
author Medo <smaxein@googlemail.com>
Thu, 21 Jun 2012 21:32:12 +0200
changeset 7269 5b0aeef8ba2a
child 7271 5608ac657362
permissions -rw-r--r--
More progress on the netplay part of the frontlib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7269
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "netprotocol.h"
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include "../util/util.h"
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include "../util/logging.h"
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include <string.h>
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <stdio.h>
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <errno.h>
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <stdlib.h>
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
static int fillTeamFromMsg(flib_team *team, char **parts) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	team->name = flib_strdupnull(parts[0]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
	team->grave = flib_strdupnull(parts[2]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
	team->fort = flib_strdupnull(parts[3]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
	team->voicepack = flib_strdupnull(parts[4]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
	team->flag = flib_strdupnull(parts[5]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
	if(!team->name || !team->grave || !team->fort || !team->voicepack || !team->flag) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
		return -1;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
	}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
	long color;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	if(sscanf(parts[1], "#%lx", &color)) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
		team->color = color;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
	} else {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
		return -1;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
	}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	errno = 0;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
	long difficulty = strtol(parts[6], NULL, 10);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	if(errno) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
		return -1;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
		flib_hog *hog = &team->hogs[i];
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
		hog->difficulty = difficulty;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
		hog->name = flib_strdupnull(parts[7+2*i]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
		hog->hat = flib_strdupnull(parts[8+2*i]);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		if(!hog->name || !hog->hat) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
			return -1;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
		}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
	return 0;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
flib_team *flib_team_from_netmsg(char **parts) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	flib_team *result = NULL;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
	flib_team *tmpTeam = flib_calloc(1, sizeof(flib_team));
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	if(tmpTeam) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		if(!fillTeamFromMsg(tmpTeam, parts)) {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
			result = tmpTeam;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
			tmpTeam = NULL;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
		} else {
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
			flib_log_e("Error parsing team from net.");
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
		}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
	}
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
	flib_team_destroy(tmpTeam);
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	return result;
5b0aeef8ba2a More progress on the netplay part of the frontlib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
}