Skip to content

Commit

Permalink
Merge pull request #48 from inaka/hernan.fixes
Browse files Browse the repository at this point in the history
Hernan.fixes
  • Loading branch information
Brujo Benavides committed Jun 5, 2015
2 parents 76beaf2 + 4453c6a commit c8dc07b
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 48 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Prefixes all server messages, both server originated and the replies.
Field | Description
----------|-------------
Flags | For now, only used to specify the message type
MessageID | The same message ID the user sent if a reply or an ID
MessageID | The same message ID the user sent if a reply or an unique server generated ID
Time | In the case of a response, the value sent by the client, otherwise, this is the current tick

### Ping Request ###
Expand Down Expand Up @@ -179,14 +179,15 @@ Sent every server tick (50 times per second) with any updates the game had. Ther
NumEvents => uchar
Event => Tick EventType EventData
Tick => ushort
EventType => uchar => LEFT_COMMAND | JOINED_COMMAND | DIRECTION_CHANGED_COMMAND | DIED_COMMAND | START_COMMAND | TURN
EventType => uchar => LEFT_COMMAND | JOINED_COMMAND | DIRECTION_CHANGED_COMMAND | DIED_COMMAND | START_COMMAND | TURN_COMMAND | COUNTDOWN_COMMAND
LEFT_COMMAND => 0
JOINED_COMMAND => 1
DIRECTION_CHANGED_COMMAND => 2
DIED_COMMAND => 3
START_COMMAND => 4
TURN => 5
EventData => Left | Joined | DirectionChanged | Died | GameStart | Turn
TURN_COMMAND => 5
COUNTDOWN_COMMAND => 6
EventData => Left | Joined | DirectionChanged | Died | GameStart | Turn | Countdown
Left => PlayerId
PlayerId => uint
Joined => PlayerId Name
Expand All @@ -195,8 +196,19 @@ Sent every server tick (50 times per second) with any updates the game had. Ther
DirectionChanged => PlayerId Direction
Direction => uchar
Died => PlayerId
GameStart => Ø
Turn => Ø
GameStart => Walls
Walls => Cells
Turn => Fruits Occupied
Fruits => Cells
Occupied => Cells
Cells => NumCells [Cell]
NumCells => ushort
Cell => Status X Y
Status => uchar => ADDED | REMOVED
ADDED => 1
REMOVED => 2
Countdown => TurnsToGo
TurnsToGo => uchar

Field | Description
----------|-------------
Expand All @@ -205,3 +217,5 @@ Time | The tick in which the event happened
EventType | The type of event is indicated by uchar
Direction | The direction, in the same format as in the client update Action
Turn | Notifies the user that the server has executed a turn
Cells | A list of cells expressed as a diff
TurnsToGo | Used on the countdown, used clientside to predict when the game will start
1 change: 1 addition & 0 deletions cclient/build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcc cclient.c -o cclient
101 changes: 101 additions & 0 deletions cclient/cclient.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>

unsigned int getTimestamp();
void sendPing();

int main()
{
printf("Welcome to desktop client v1!\n\n");

char ipString[16];
int port;

bool hasAddress = false;
while(!hasAddress)
{
// Read the IP the user wants to connect to
printf("Game server IP\n");

fgets(ipString, sizeof(ipString), stdin);
fflush(stdin);
strtok(ipString, "\n");

// Read the port
printf("Game server Port\n");
scanf("%d", &port);
getchar();

// Confirm
printf("connect to %s:%d? (y/n)\n", ipString, port);
char confirmation;

confirmation = getchar();
getchar();
while(confirmation != 'y' && confirmation != 'n')
{
printf("eh?");
confirmation = getchar();
getchar();
}

hasAddress = confirmation == 'y';
}

printf("connecting to %s:%d...\n", ipString, port);

// Create a socket
int fd; // The file descriptor
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("unable to create socket\n");
return 0;
}

struct sockaddr_in myaddr;
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(0);

if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0)
{
printf("unable to bind socket\n");
return 0;
}

unsigned int timestamp = getTimestamp();

sendPing(fd, ipString, port);

unsigned int ping = 0;

printf("connected with ping: %d at %u\n", ping, timestamp);

return 0;
}

unsigned int getTimestamp()
{
struct timeval tv;

gettimeofday(&tv, NULL);
return (unsigned int)(tv.tv_sec) * 1000 +
(unsigned int)(tv.tv_usec) / 1000;
}

void sendPing(int socket, const char *ip, int port)
{
struct sockaddr_in servaddr;
memset((char*)&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
memcpy((void *)&servaddr.sin_addr, ip, strlen(ip));

char *pingMessage[7];
sendto(socket, pingMessage, 7, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
}
2 changes: 1 addition & 1 deletion src/models/spts_games.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
, cells => [cell()]
}.
-export_type([
game/0, state/0, id/0, content/0, position/0, direction/0, flag/0]).
game/0, state/0, id/0, content/0, position/0, direction/0, flag/0, cell/0]).

-export(
[ new/10
Expand Down
Loading

0 comments on commit c8dc07b

Please sign in to comment.