adding multiplayer
This commit is contained in:
Binary file not shown.
@@ -6,7 +6,9 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include "../game_source/entities/Player.h"
|
||||
#include "../game_source/entities/Player_e.h"
|
||||
#include "../game_source/entities/Player_c.h"
|
||||
#include "../game_source/entities/Centery.h"
|
||||
#include <sys/random.h>
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
@@ -78,13 +80,36 @@ int client_init(void* cli) {
|
||||
|
||||
CreateWorld(&c->world, clock());
|
||||
InitWorld(&c->world);
|
||||
entity* cen = CreatePlayer("testing", c, &c->world);
|
||||
cen->position.x = 3;
|
||||
cen->position.y = 3;
|
||||
AddEntity(&c->world, cen);
|
||||
player_data p = {
|
||||
.holding = 0,
|
||||
.class = 0,
|
||||
.health = 20,
|
||||
.ac = 4,
|
||||
.str = 0,
|
||||
.wis = 0,
|
||||
.intel = 0,
|
||||
.dex = 0,
|
||||
.name = "testing",
|
||||
.c = (color){100,100,100},
|
||||
};
|
||||
entity* pc = CreatePlayerCharecter(p,c, &c->world);
|
||||
entity* pe = CreatePlayerEntity(p,0, &c->world);
|
||||
//pc->position.x = 3;
|
||||
//pc->position.y = 3;
|
||||
pe->position.x = 3;
|
||||
pe->position.y = 3;
|
||||
//AddEntity(&c->world, pc);
|
||||
AddEntity(&c->world, pe);
|
||||
GenerateWorld(&c->world);
|
||||
CammeraInit(&c->cam, 150,50,0,0,&c->world, &c->w);
|
||||
clock_gettime(CLOCK_MONOTONIC,&c->last_time);
|
||||
memset(&c->server, 0, sizeof(game_server));
|
||||
if(Connect(&c->server) < 0) {
|
||||
printf("Could not connect to server\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pe->server = c->server.game_info.socket;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -104,6 +129,8 @@ int client_update(ssh_terminal_data* td, void* cli) {
|
||||
|
||||
tart_jump(&w, (tart_vec2){w.windowSize.x, w.windowSize.y-1});
|
||||
tart_insert_cell(&w, 5, 'a');
|
||||
tart_jump(&w, (tart_vec2){0,0});
|
||||
tart_printf(&w, 5, T"Entities %d\n", c->world.ent_list->count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <tart.h>
|
||||
#include "../game_source/cammera.h"
|
||||
#include "../game_source/world.h"
|
||||
#include "../server_source/game_client.h"
|
||||
|
||||
typedef struct {
|
||||
tart_window w;
|
||||
@@ -32,6 +33,8 @@ typedef struct {
|
||||
struct ssh_server_callbacks_struct server_cb;
|
||||
struct ssh_channel_callbacks_struct channel_cb;
|
||||
struct timespec last_time;
|
||||
game_server server;
|
||||
|
||||
|
||||
|
||||
} client;
|
||||
|
||||
@@ -4,7 +4,7 @@ target_sources(${PROJECT_NAME} PRIVATE
|
||||
entity_handler.c
|
||||
cells.c
|
||||
world.c
|
||||
player/player_packet.c
|
||||
player/player_data.c
|
||||
|
||||
)
|
||||
|
||||
|
||||
@@ -37,8 +37,9 @@ int CammeraResize(cammera* c, int sx, int sy) {
|
||||
|
||||
int CammeraRender(cammera*c) {
|
||||
jump(c->tw, (tart_vec2){0,0});
|
||||
int ne = 1;
|
||||
for(int i = 0; i < c->size_y; i++) {
|
||||
for(int j = 0; j < c->size_x - 1; j++) {
|
||||
for(int j = 0; j < c->size_x; j++) {
|
||||
|
||||
if(c->pos_y+i < c->w->size_y-1 && c->pos_y+i >= 0 && c->pos_x+j < c->w->size_x-1 && c->pos_x+j >= 0){
|
||||
c->tw->palette[2].background.r = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.background.r;
|
||||
@@ -49,6 +50,7 @@ int CammeraRender(cammera*c) {
|
||||
c->tw->palette[2].forground.b = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].cell->tile.forground.b;
|
||||
|
||||
tart_jump(c->tw, (tart_vec2){j,i});
|
||||
ne = 1;
|
||||
|
||||
for(int k = 0; k < 1; k++) {
|
||||
if(c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)].entityIds[0] != 0000) {
|
||||
@@ -65,12 +67,14 @@ int CammeraRender(cammera*c) {
|
||||
c->tw->palette[2].forground.b = c->w->wells[at(c->pos_x+j,c->pos_y+i,c->w)].entityIds[k]->tile.forground.b;
|
||||
tart_insert_cell(c->tw, 2, c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)]
|
||||
.entityIds[k]->tile.simble);
|
||||
printf("found entity %c\n", c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)]
|
||||
.entityIds[k]->tile.simble);
|
||||
tart_jump(c->tw,(tart_vec2){j, i});
|
||||
ne = 0;
|
||||
}
|
||||
}
|
||||
if(ne) {
|
||||
tart_insert_cell(c->tw, 2, c->w->wells[at((c->pos_x+j) ,(c->pos_y+i),c->w)]
|
||||
.cell->tile.simble);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
target_sources(${PROJECT_NAME} PRIVATE
|
||||
Player.c
|
||||
Player_c.c
|
||||
Player_e.c
|
||||
Centery.c
|
||||
Gun.c
|
||||
)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "Centery.h"
|
||||
#include "../../server_source/transactions.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int CenteryInit(void* self) {
|
||||
entity* ent = self;
|
||||
@@ -13,8 +15,42 @@ int CenteryUpdate(int arg, void* self) {
|
||||
entity* e = self;
|
||||
world* w = e->world;
|
||||
centery* cen = e->data;
|
||||
vec2 moveTo = {0,0};
|
||||
vec2 moveTo = e->position;
|
||||
int goodMove;
|
||||
user_transaction pos_ut;
|
||||
memset(&pos_ut, 0, sizeof(pos_ut));
|
||||
printf("getting transaction\n");
|
||||
|
||||
//printf("Has Connection\n");
|
||||
//printf("cli sock %d, entity sock ,\n", e->server->socket);
|
||||
if(cen->cli->server.game_info.socket > 0) {
|
||||
|
||||
|
||||
if(RecvUserTransaction(&pos_ut, e->server)) {
|
||||
printf("Centery NG\n");
|
||||
}else {
|
||||
|
||||
memcpy(&moveTo, pos_ut.userArg,sizeof(vec2));
|
||||
printf("size of post_ut %d\n", pos_ut.argsize);
|
||||
printf("move to %d,%d\n", moveTo.x,moveTo.y);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!EntityMove(e, &moveTo)) {
|
||||
|
||||
// st_move = (user_transaction){
|
||||
// .userArg = &mv,
|
||||
// .action = PlayerMove,
|
||||
// .id = SERVER_T_USER_FLAG,
|
||||
// .argsize = sizeof(mv),
|
||||
//};
|
||||
|
||||
|
||||
//w->wells[at(mv.x, mv.y, w)].cell = SetCell(6);
|
||||
}
|
||||
|
||||
//do{
|
||||
// goodMove = EntityMove(e,(vec2){e->position.x+ 0, e-> position.y + 1});
|
||||
// if(goodMove == 1) {
|
||||
@@ -64,7 +100,7 @@ int CenteryUpdate(int arg, void* self) {
|
||||
|
||||
|
||||
|
||||
printf("name of entity %s\n",e->name);
|
||||
//printf("name of entity %s\n",e->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -78,7 +114,7 @@ int CenteryFree(void* self) {
|
||||
|
||||
|
||||
|
||||
entity* CreateCentery(int tear, int classification, world* w) {
|
||||
entity* CreateCentery(int tear, client* cli, world* w) {
|
||||
entity* ent = (entity*)malloc(sizeof(entity));
|
||||
centery* cen = (centery*)malloc(sizeof(centery));
|
||||
|
||||
@@ -94,6 +130,7 @@ entity* CreateCentery(int tear, int classification, world* w) {
|
||||
.str = 3,
|
||||
.wis = 3,
|
||||
.dex = 3,
|
||||
.cli = cli,
|
||||
};
|
||||
|
||||
*ent = (entity){
|
||||
@@ -103,7 +140,7 @@ entity* CreateCentery(int tear, int classification, world* w) {
|
||||
.tile = {
|
||||
.simble = 'C',
|
||||
.background = {0,0,0},
|
||||
.forground = {0x3f,0x5f,0x10},
|
||||
.forground = {0xff,0x10,0x10},
|
||||
},
|
||||
.position = (vec2){5,5},
|
||||
.data = cen,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#define CENTERY_ID 1
|
||||
#include "../entity.h"
|
||||
#include "../world.h"
|
||||
#include "../../client_source/client.h"
|
||||
|
||||
typedef struct {
|
||||
int weapon;
|
||||
@@ -17,9 +18,11 @@ typedef struct {
|
||||
int str;
|
||||
int wis;
|
||||
int dex;
|
||||
client* cli;
|
||||
|
||||
} centery;
|
||||
|
||||
entity* CreateCentery(int tear, int classification,world* w);
|
||||
entity* CreateCentery(int tear, client* cli,world* w);
|
||||
|
||||
int ServerUpdate(void*);
|
||||
int ClientUpdate(int, void*);
|
||||
|
||||
@@ -16,7 +16,7 @@ int GunUpdate(int action, void* data) {
|
||||
case SHOOT_W: bullet_pos.x -= 1; break;
|
||||
}
|
||||
}
|
||||
int entity_colied = EntityMove(e, bullet_pos);
|
||||
int entity_colied = EntityMove(e, &bullet_pos);
|
||||
if((entity_colied) && g->inpact == 0) {
|
||||
if(w->wells[at(bullet_pos.x, bullet_pos.y, w)].cell->id == 1) {
|
||||
w->wells[at(bullet_pos.x, bullet_pos.y, w)].cell = SetCell(2);
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
#include "../entity.h"
|
||||
#include "../../client_source/client.h"
|
||||
#include "../world.h"
|
||||
|
||||
typedef struct {
|
||||
int holding;
|
||||
int class;
|
||||
|
||||
int health;
|
||||
int ac;
|
||||
int str;
|
||||
int wis;
|
||||
int dex;
|
||||
client* cli;
|
||||
|
||||
|
||||
int invintory[30];
|
||||
} player;
|
||||
|
||||
entity* CreatePlayer(const char* username, client* cli ,world* w);
|
||||
|
||||
int PlayerServerUpdate(void*);
|
||||
int PlayerClientUpdate(int, void*);
|
||||
int PlayerInit(void*);
|
||||
int PlayerFree(void*);
|
||||
|
||||
#endif
|
||||
@@ -1,9 +1,17 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "Player.h"
|
||||
#include "Player_c.h"
|
||||
#include "Gun.h"
|
||||
#include "Centery.h"
|
||||
#include "../../client_source/client.h"
|
||||
#include "../../server_source/transactions.h"
|
||||
|
||||
typedef struct {
|
||||
player_data pd;
|
||||
client* cli;
|
||||
}player_c;
|
||||
|
||||
#define PlayerMove 1
|
||||
|
||||
|
||||
void Shoot(entity* e,int direction) {
|
||||
if(direction == SHOOT_N) {
|
||||
@@ -36,9 +44,9 @@ void Shoot(entity* e,int direction) {
|
||||
}
|
||||
}
|
||||
|
||||
int PlayerUpdate(int action, void* data) {
|
||||
int PlayerUpdateC(int action, void* data) {
|
||||
entity* e = (entity*)data;
|
||||
player* p= (player*)e->data;
|
||||
player_c* p= (player_c*)e->data;
|
||||
world* w = e->world;
|
||||
ssh_terminal_data* td = &p->cli->term;
|
||||
vec2 mv = {e->position.x, e->position.y};
|
||||
@@ -48,23 +56,46 @@ int PlayerUpdate(int action, void* data) {
|
||||
};
|
||||
int cammera_fallow_readious = 14;
|
||||
|
||||
printf("input buffer for player %s\n", td->inputBuffer);
|
||||
char* ib = td->inputBuffer;
|
||||
int ibsize = strlen(ib);
|
||||
|
||||
if(!strcmp(td->inputBuffer, "k")) {mv.y -= 1;}
|
||||
if(!strcmp(td->inputBuffer, "j")) {mv.y += 1;}
|
||||
if(!strcmp(td->inputBuffer, "h")) {mv.x -= 1;}
|
||||
if(!strcmp(td->inputBuffer, "l")) {mv.x += 1;}
|
||||
while(ib < ib+ibsize) {
|
||||
|
||||
if(!strcmp(ib, "\e[A")) {mv.y -= 1; ib += 3; continue;}
|
||||
if(!strcmp(ib, "\e[B")) {mv.y += 1; ib += 3; continue;}
|
||||
if(!strcmp(ib, "\e[D")) {mv.x -= 1; ib += 3; continue;}
|
||||
if(!strcmp(ib, "\e[C")) {mv.x += 1; ib += 3; continue;}
|
||||
|
||||
user_transaction st_move;
|
||||
memset(&st_move, 0, sizeof(st_move));
|
||||
|
||||
st_move = (user_transaction){
|
||||
.userArg = &mv,
|
||||
.action = PlayerMove,
|
||||
.id = SERVER_T_USER_FLAG,
|
||||
.argsize = sizeof(mv),
|
||||
};
|
||||
|
||||
|
||||
int entity_can_move = EntityMove(e, mv);
|
||||
if(!strcmp(td->inputBuffer, "w")) {Shoot(e,SHOOT_N);}
|
||||
if(!strcmp(td->inputBuffer, "d")) {Shoot(e,SHOOT_E);}
|
||||
if(!strcmp(td->inputBuffer, "s")) {Shoot(e,SHOOT_S);}
|
||||
if(!strcmp(td->inputBuffer, "a")) {Shoot(e,SHOOT_W);}
|
||||
printf("Cammera Position %d,%d\n", cammera_pos.x, cammera_pos.y);
|
||||
if(p->cli->server.game_info.socket > 0) {
|
||||
printf("Has Connection\n");
|
||||
if(SendUserTransaction(&st_move, p->cli->server.game_info.socket)) {
|
||||
printf("NG\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!strcmp(ib, "w")) {Shoot(e,SHOOT_N); ib += 1; continue;}
|
||||
if(!strcmp(ib, "d")) {Shoot(e,SHOOT_E); ib += 1; continue;}
|
||||
if(!strcmp(ib, "s")) {Shoot(e,SHOOT_S); ib += 1; continue;}
|
||||
if(!strcmp(ib, "a")) {Shoot(e,SHOOT_W); ib += 1; continue;}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int entity_can_move = EntityMove(e, &mv);
|
||||
|
||||
|
||||
if((e->position.x < cammera_pos.x - cammera_fallow_readious )){
|
||||
p->cli->cam.pos_x += - 1;
|
||||
@@ -83,10 +114,9 @@ int PlayerUpdate(int action, void* data) {
|
||||
|
||||
|
||||
|
||||
if(!entity_can_move) {
|
||||
w->wells[at(mv.x, mv.y, w)].cell = SetCell(6);
|
||||
if(entity_can_move) {
|
||||
//w->wells[at(mv.x, mv.y, w)].cell = SetCell(6);
|
||||
}
|
||||
printf("is this a good move? %d\n", EntityMove(e, mv));
|
||||
|
||||
|
||||
|
||||
@@ -94,22 +124,20 @@ int PlayerUpdate(int action, void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
entity* CreatePlayer(const char* username, client* cli,world* w) {
|
||||
int PlayerCFree(void* data) {
|
||||
return 0;
|
||||
}
|
||||
int PlayerCInit(void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
entity* CreatePlayerCharecter(player_data pd, client* cli ,world* w) {
|
||||
entity* ent = malloc(sizeof(entity));
|
||||
player* p= malloc(sizeof(player));
|
||||
|
||||
*p = (player){
|
||||
.holding = 0,
|
||||
.class = 0,
|
||||
|
||||
.health = 0,
|
||||
.ac = 0,
|
||||
.str = 0,
|
||||
.wis = 0,
|
||||
.dex = 0,
|
||||
.cli = cli,
|
||||
|
||||
player_c* p= malloc(sizeof(player_c));
|
||||
|
||||
*p = (player_c){
|
||||
.pd = pd,
|
||||
.cli = cli,
|
||||
};
|
||||
|
||||
|
||||
@@ -126,9 +154,9 @@ entity* CreatePlayer(const char* username, client* cli,world* w) {
|
||||
.data = p,
|
||||
.world = w,
|
||||
.callback = {
|
||||
.init = PlayerInit,
|
||||
.update = PlayerUpdate,
|
||||
.free = PlayerFree,
|
||||
.init = PlayerCInit,
|
||||
.update = PlayerUpdateC,
|
||||
.free = PlayerCFree,
|
||||
},
|
||||
.cleanup = 0,
|
||||
|
||||
@@ -139,8 +167,3 @@ entity* CreatePlayer(const char* username, client* cli,world* w) {
|
||||
return ent;
|
||||
}
|
||||
|
||||
int PlayerServerUpdate(void*);
|
||||
int PlayerClientUpdate(int, void*);
|
||||
int PlayerInit(void* ent) {return 0;}
|
||||
int PlayerFree(void* ent) {return 0;}
|
||||
|
||||
13
source/game_source/entities/Player_c.h
Normal file
13
source/game_source/entities/Player_c.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef PLAYER_C_H
|
||||
#define PLAYER_C_H
|
||||
|
||||
#include "../entity.h"
|
||||
#include "../../client_source/client.h"
|
||||
#include "../world.h"
|
||||
#include "../player/player_data.h"
|
||||
|
||||
|
||||
entity* CreatePlayerCharecter(player_data pd, client* cli ,world* w);
|
||||
|
||||
|
||||
#endif
|
||||
105
source/game_source/entities/Player_e.c
Normal file
105
source/game_source/entities/Player_e.c
Normal file
@@ -0,0 +1,105 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "Player_e.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "../../server_source/transactions.h"
|
||||
|
||||
|
||||
#define PlayerMove 1
|
||||
|
||||
|
||||
|
||||
int PlayerEUpdate(int action, void* data) {
|
||||
entity* e = (entity*)data;
|
||||
player_data* p= (player_data*)e->data;
|
||||
world* w = e->world;
|
||||
vec2 moveTo = e->position;
|
||||
int goodMove;
|
||||
user_transaction pos_ut;
|
||||
memset(&pos_ut, 0, sizeof(pos_ut));
|
||||
printf("getting transaction\n");
|
||||
|
||||
//printf("Has Connection\n");
|
||||
//printf("cli sock %d, entity sock ,\n", e->server->socket);
|
||||
if(e->server > 0) {
|
||||
|
||||
|
||||
if(RecvUserTransaction(&pos_ut, e->server)) {
|
||||
printf("Centery NG\n");
|
||||
}else {
|
||||
|
||||
memcpy(&moveTo, pos_ut.userArg,sizeof(vec2));
|
||||
printf("size of post_ut %d\n", pos_ut.argsize);
|
||||
printf("move to %d,%d\n", moveTo.x,moveTo.y);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!EntityMove(e, &moveTo)) {
|
||||
|
||||
// st_move = (user_transaction){
|
||||
// .userArg = &mv,
|
||||
// .action = PlayerMove,
|
||||
// .id = SERVER_T_USER_FLAG,
|
||||
// .argsize = sizeof(mv),
|
||||
//};
|
||||
|
||||
|
||||
//w->wells[at(mv.x, mv.y, w)].cell = SetCell(6);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PlayerEFree(void* data) {
|
||||
return 0;
|
||||
}
|
||||
int PlayerEInit(void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
entity* CreatePlayerEntity(player_data pd, int serverfd,world* w) {
|
||||
entity* ent = malloc(sizeof(entity));
|
||||
player_data* p= malloc(sizeof(player_data));
|
||||
|
||||
*p = (player_data){
|
||||
.holding = pd.holding,
|
||||
.class = pd.class,
|
||||
|
||||
.health = pd.health,
|
||||
.ac = pd.ac,
|
||||
.str = pd.str,
|
||||
.wis = pd.wis,
|
||||
.dex = pd.dex,
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
*ent = (entity){
|
||||
.id = 30,
|
||||
.name = "Testing",
|
||||
.description = "This is a player",
|
||||
.tile = {
|
||||
.simble = '@',
|
||||
.background = {0,0,0},
|
||||
.forground = {0x5f,0x3f,0x10},
|
||||
},
|
||||
.position = (vec2){5,5},
|
||||
.data = p,
|
||||
.world = w,
|
||||
.callback = {
|
||||
.init = PlayerEInit,
|
||||
.update = PlayerEUpdate,
|
||||
.free = PlayerEFree,
|
||||
},
|
||||
.cleanup = 0,
|
||||
|
||||
};
|
||||
|
||||
return ent;
|
||||
}
|
||||
|
||||
11
source/game_source/entities/Player_e.h
Normal file
11
source/game_source/entities/Player_e.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef PLAYER_E_H
|
||||
#define PLAYER_E_H
|
||||
|
||||
#include "../entity.h"
|
||||
#include "../world.h"
|
||||
#include "../player/player_data.h"
|
||||
|
||||
entity* CreatePlayerEntity(player_data pd, int serverfd, world* w);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -3,19 +3,30 @@
|
||||
#include "vector.h"
|
||||
#include "world.h"
|
||||
|
||||
int EntityMove(entity* e, vec2 pos) {
|
||||
#include "stdio.h"
|
||||
int EntityMove(entity* e, vec2* pos) {
|
||||
world* world = e->world;
|
||||
|
||||
if(world->wells[at(pos.x,pos.y, world)].cell->flags & (CELL_FLAG_BLOCKING)){
|
||||
vec2 p = *pos;
|
||||
if(pos->x < 0) {
|
||||
p.x = (world->size_x + (pos->x- 1)) ;
|
||||
printf("x less then 0 setting p.x to %d\n", p.x);
|
||||
pos->x = p.x;
|
||||
}
|
||||
if(pos->y < 0) {
|
||||
p.y = (world->size_y + (pos->y - 1)) ;
|
||||
printf("y less then 0 setting p.y to %d\n", p.y);
|
||||
pos->y = p.y;
|
||||
}
|
||||
if(world->wells[at(p.x,p.y, world)].cell->flags & (CELL_FLAG_BLOCKING)){
|
||||
return 1;
|
||||
}
|
||||
if(world->wells[at(pos.x,pos.y, world)].entityIds[0] != NULL){
|
||||
if(world->wells[at(p.x,p.y, world)].entityIds[0] != NULL){
|
||||
return 2;
|
||||
}
|
||||
|
||||
world->wells[at(e->position.x,e->position.y, world)].entityIds[0] = 0;
|
||||
world->wells[at(pos.x,pos.y, world)].entityIds[0] = e;
|
||||
e->position = pos;
|
||||
world->wells[at(p.x,p.y, world)].entityIds[0] = e;
|
||||
e->position = p;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -34,12 +34,13 @@ typedef struct {
|
||||
entity_callbacks callback;
|
||||
entity_transaction ta;
|
||||
unsigned char cleanup;
|
||||
int server;
|
||||
} entity;
|
||||
|
||||
entity* CreateEntity(const char* name, const char* description);
|
||||
int EntityAddCallbacks(entity* e, entity_callbacks cb);
|
||||
int EntitySetUserdat(entity* e, void* userdata);
|
||||
int EntityMove(entity* e, vec2 pos);
|
||||
int EntityMove(entity* e, vec2* pos);
|
||||
|
||||
// Gets
|
||||
const char* GetEntityDescription(entity* e);
|
||||
|
||||
@@ -17,10 +17,9 @@ int EntityListUpdate(entity_list* list) {
|
||||
clock_gettime(CLOCK_MONOTONIC, &list->current);
|
||||
double elapsed = (list->current.tv_sec - list->start.tv_sec);
|
||||
elapsed += (list->current.tv_nsec - list->start.tv_nsec) / 1000000000.0;
|
||||
if(elapsed > 0.125/4) {
|
||||
if(elapsed > 0.000125) {
|
||||
for(int i = 0; i < 256; i++) {
|
||||
if(list->entities[i] != NULL) {
|
||||
printf("running index %d\n", i);
|
||||
list->entities[i]->callback.update(0,list->entities[i]);
|
||||
}
|
||||
}
|
||||
|
||||
2
source/game_source/player/player_data.c
Normal file
2
source/game_source/player/player_data.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "player_data.h"
|
||||
|
||||
37
source/game_source/player/player_data.h
Normal file
37
source/game_source/player/player_data.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef PLAYER_PACKET_H
|
||||
#define PLAYER_PACKET_H
|
||||
#include "../color.h"
|
||||
|
||||
#define MOVE_UP 1
|
||||
#define MOVE_DOWN 2
|
||||
#define MOVE_LEFT 3
|
||||
#define MOVE_RIGHT 4
|
||||
#define ATTACK_LEFT 5
|
||||
#define ATTACK_RIGHT 6
|
||||
#define ATTACK_UP 7
|
||||
#define ATTACK_DOWN 8
|
||||
|
||||
#define ATTACK_MELEE 11
|
||||
#define ATTACK_WEAPON_1 12
|
||||
#define ATTACK_WEAPON_2 13
|
||||
#define ATTACK_WEAPON_3 14
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
int holding;
|
||||
int class;
|
||||
int health;
|
||||
int ac;
|
||||
int str;
|
||||
int wis;
|
||||
int intel;
|
||||
int dex;
|
||||
char name[32];
|
||||
color c;
|
||||
}player_data;
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,8 +0,0 @@
|
||||
#include "player_packet.h"
|
||||
|
||||
int SendPlayerPacket(player_packet *pakcet, game_server *config) {
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#ifndef PLAYER_PACKET_H
|
||||
#define PLAYER_PACKET_H
|
||||
#include "../../server_source/game_client.h"
|
||||
|
||||
#define MOVE_UP 1
|
||||
#define MOVE_DOWN 2
|
||||
#define MOVE_LEFT 3
|
||||
#define MOVE_RIGHT 4
|
||||
|
||||
#define ATTACK_MELEE 11
|
||||
#define ATTACK_WEAPON_1 12
|
||||
#define ATTACK_WEAPON_2 13
|
||||
#define ATTACK_WEAPON_3 14
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
|
||||
}player_packet;
|
||||
|
||||
|
||||
|
||||
int SendPlayerPacket(player_packet* pakcet, game_server* config);
|
||||
|
||||
#endif
|
||||
@@ -2,8 +2,6 @@
|
||||
#include "cells.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "./entities/Centery.h"
|
||||
#include "./entities/Player.h"
|
||||
#include "entity_handler.h"
|
||||
#include "vector.h"
|
||||
#include <stdio.h>
|
||||
@@ -96,9 +94,11 @@ int GenerateWorld(world *w) {
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < w->ent_list->count; i++) {
|
||||
for(int i = 0; i < 255; i++) {
|
||||
entity* ent = w->ent_list->entities[i];
|
||||
w->wells[at(ent->position.x + 1, ent->position.y + 1, w)].entityIds[0] = ent;
|
||||
if(ent != NULL) {
|
||||
w->wells[at(ent->position.x , ent->position.y , w)].entityIds[0] = ent;
|
||||
}
|
||||
}
|
||||
|
||||
vec2 building_position = {50,50};
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
target_sources(${PROJECT_NAME} PRIVATE
|
||||
game_client.c
|
||||
server.c
|
||||
transactions.c
|
||||
)
|
||||
|
||||
add_executable(
|
||||
gameServer
|
||||
server.c
|
||||
transactions.c
|
||||
main.c
|
||||
)
|
||||
|
||||
@@ -1,28 +1,115 @@
|
||||
#include "game_client.h"
|
||||
#include "transactions.h"
|
||||
#include "../game_source/color.h"
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
|
||||
int Connect(game_server* gs) {
|
||||
struct sockaddr_in serverAddr;
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_port = htons(gs->info.port);
|
||||
struct sockaddr_in game_serverAddr;
|
||||
struct sockaddr_in server_serverAddr;
|
||||
|
||||
gs->game_info.port = 2223;
|
||||
gs->game_info.ip = "192.168.254.74";
|
||||
|
||||
gs->server_info.port = 2224;
|
||||
gs->server_info.ip = "192.168.254.74";
|
||||
|
||||
game_serverAddr.sin_family = AF_INET;
|
||||
game_serverAddr.sin_port = htons(2223);
|
||||
game_serverAddr.sin_family = AF_INET;
|
||||
|
||||
server_serverAddr.sin_port = htons(2223);
|
||||
server_serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
int blocking_mode = 1;
|
||||
|
||||
|
||||
gs->socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
inet_pton(AF_INET, gs->info.ip, &serverAddr.sin_addr);
|
||||
gs->game_info.socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
gs->server_info.socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
inet_pton(AF_INET, gs->game_info.ip, &game_serverAddr.sin_addr);
|
||||
|
||||
if(connect(gs->server_info.socket, (struct sockaddr*)&game_serverAddr, sizeof(game_serverAddr)) < 0) {
|
||||
//ioctl(gs->server_info.socket, FIONBIO, &blocking_mode); // Shuld be 1
|
||||
|
||||
server_transaction st;
|
||||
server_transaction rst;
|
||||
memset(&st, 0, sizeof(server_transaction));
|
||||
|
||||
st.id = SERVER_T_SERVER_FLAG;
|
||||
st.action = SERVER_T_CONNECT;
|
||||
st.argsize = 0;
|
||||
st.serverArg = 0;
|
||||
|
||||
SendServerTransaction(&st, gs->server_info.socket);
|
||||
if(RecvServerTransaction(&rst, gs->server_info.socket)) {
|
||||
printf("server transaction failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(rst.id == SERVER_T_SERVER_FLAG && rst.action == SERVER_T_CONNECT) {
|
||||
memcpy(&gs->game_info.id, rst.serverArg, sizeof(int));
|
||||
|
||||
char* ip = malloc((rst.argsize - sizeof(int)) + 1);
|
||||
strncpy(ip, rst.serverArg + sizeof(int), (rst.argsize - sizeof(int)) + 1);
|
||||
ip[rst.argsize - sizeof(int)] = '\0';
|
||||
|
||||
printf("Connecting to server %s\n", ip);
|
||||
|
||||
inet_pton(AF_INET, ip, &game_serverAddr.sin_addr);
|
||||
if(connect(gs->game_info.socket, (struct sockaddr*)&game_serverAddr, sizeof(game_serverAddr)) <= 0) {
|
||||
printf("Could not connect to server \"%s\"\n", ip);
|
||||
free(ip);
|
||||
return -1;
|
||||
|
||||
}
|
||||
free(ip);
|
||||
|
||||
color rcolor = {rand()%250, rand()%250, rand()%250};
|
||||
user_transaction ut = {
|
||||
.id = SERVER_T_USER_FLAG,
|
||||
.action = SERVER_T_CONNECT,
|
||||
.argsize = sizeof(color),
|
||||
.userArg = &rcolor,
|
||||
};
|
||||
|
||||
user_transaction rut = {0};
|
||||
|
||||
SendUserTransaction(&ut, gs->game_info.socket);
|
||||
|
||||
if(RecvUserTransaction(&rut, gs->game_info.socket)) {
|
||||
printf("Could not get other users\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(rut.id == SERVER_T_GET_PLAYER_DATA && rut.action == SERVER_T_SET_PLAYER_ID) {
|
||||
//player *players = malloc(rut.argsize/sizeof(player));
|
||||
//memcpy(players, rut.userArg, rut.argsize);
|
||||
FreeUserTransaction(&rut);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
SendServerTransaction(&st, gs->server_info.socket);
|
||||
if(RecvServerTransaction(&rst, gs->server_info.socket)) {
|
||||
printf("server failed to get Player data");
|
||||
}
|
||||
|
||||
if(connect(gs->socket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
|
||||
ioctl(gs->socket, FIONBIO, &blocking_mode); // Shuld be 1
|
||||
|
||||
return 0;
|
||||
}
|
||||
printf("ng\n");
|
||||
return -1;
|
||||
}
|
||||
int Client_Send(game_server* gs,packet* packet) {
|
||||
@@ -32,22 +119,22 @@ int Client_Send(game_server* gs,packet* packet) {
|
||||
memcpy(buffer+sizeof(int), &packet->size, sizeof(int));
|
||||
memcpy(buffer+sizeof(int)+sizeof(int), &packet->packet, packet->size);
|
||||
|
||||
write(gs->socket, buffer, sendPacketSize);
|
||||
write(gs->game_info.socket, buffer, sendPacketSize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Client_Recive(game_server* gs, packet* packet) {
|
||||
|
||||
if(read(gs->socket, &packet->packetTpye, sizeof(int)) <= 0) {
|
||||
if(read(gs->game_info.socket, &packet->packetTpye, sizeof(int)) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if(read(gs->socket, &packet->size, sizeof(int)) <= 0 ) {
|
||||
if(read(gs->game_info.socket, &packet->size, sizeof(int)) <= 0 ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
packet->packet = malloc(packet->size);
|
||||
|
||||
if(read(gs->socket, &packet->packet, packet->size) <= 0) {
|
||||
if(read(gs->game_info.socket, &packet->packet, packet->size) <= 0) {
|
||||
free(packet->packet);
|
||||
return -1;
|
||||
}
|
||||
@@ -55,6 +142,15 @@ int Client_Recive(game_server* gs, packet* packet) {
|
||||
}
|
||||
|
||||
int Disconnect(game_server* gs) {
|
||||
close(gs->socket);
|
||||
server_transaction st = {
|
||||
.id = SERVER_T_SERVER_FLAG,
|
||||
.action = SERVER_T_DISCONNECT,
|
||||
.argsize = 0,
|
||||
.serverArg = 0,
|
||||
|
||||
};
|
||||
SendServerTransaction(&st, gs->server_info.socket);
|
||||
close(gs->game_info.socket);
|
||||
close(gs->server_info.socket);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@ typedef struct {
|
||||
const char* ip;
|
||||
int port;
|
||||
int id;
|
||||
int socket;
|
||||
} server_info;
|
||||
|
||||
typedef struct {
|
||||
int socket;
|
||||
server_info info;
|
||||
server_info game_info;
|
||||
server_info server_info;
|
||||
int userCount;
|
||||
}game_server;
|
||||
|
||||
int Connect(game_server* gs);
|
||||
|
||||
63
source/server_source/main.c
Normal file
63
source/server_source/main.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "server.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "transactions.h"
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "../game_source/vector.h"
|
||||
#include <time.h>
|
||||
|
||||
|
||||
void Vec2Transacgtion(user_transaction* t, vec2* v) {
|
||||
memcpy(v, t->userArg, t->argsize);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
s_game_server s;
|
||||
memset(&s,0,sizeof(s));
|
||||
int serverEnd = 0;
|
||||
|
||||
user_transaction st1;
|
||||
user_transaction st2;
|
||||
user_transaction rst1;
|
||||
user_transaction rst2;
|
||||
memset(&st1,0,sizeof(st1));
|
||||
memset(&st2,0,sizeof(st2));
|
||||
memset(&rst1,0,sizeof(rst1));
|
||||
memset(&rst2,0,sizeof(rst2));
|
||||
vec2 pos = {10,10};
|
||||
struct timespec last_time;
|
||||
clock_gettime(CLOCK_MONOTONIC,&last_time);
|
||||
|
||||
st1 = (user_transaction){
|
||||
.userArg = &pos,
|
||||
.action = 1,
|
||||
.id = SERVER_T_USER_FLAG,
|
||||
.argsize = 8,
|
||||
};
|
||||
st2 = (user_transaction){
|
||||
.userArg = &pos,
|
||||
.action = 1,
|
||||
.id = SERVER_T_USER_FLAG,
|
||||
.argsize = 8,
|
||||
};
|
||||
|
||||
start_server(&s);
|
||||
do {
|
||||
int new_client = 0;
|
||||
if((new_client = accept_client(&s)) > 0) {
|
||||
printf("connection!\n");
|
||||
//if(get_player_data(&s,new_client)) {
|
||||
// close(new_client);
|
||||
//}
|
||||
//
|
||||
}
|
||||
|
||||
}while(!serverEnd);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,29 +1,160 @@
|
||||
#include "server.h"
|
||||
#include "transactions.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int start_server() {
|
||||
|
||||
int start_server(s_game_server* s) {
|
||||
struct sockaddr_in server;
|
||||
int serverSock;
|
||||
|
||||
serverSock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
s->sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(2223);
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if(bind(serverSock, (struct sockaddr*)&serverSock, sizeof(server)) < 0) {
|
||||
if(bind(s->sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
|
||||
printf("could not bind socket\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
listen(serverSock, 4);
|
||||
int blocking_mode = 1;
|
||||
|
||||
ioctl(s->sock, FIONBIO, &blocking_mode); // Shuld be 1
|
||||
|
||||
printf("lissening\n");
|
||||
listen(s->sock, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int accept_client(s_game_server* s) {
|
||||
struct sockaddr_in addr;
|
||||
socklen_t client_addr_size;
|
||||
|
||||
client_addr_size = sizeof(addr);
|
||||
int tmp = accept(s->sock, (struct sockaddr*)&addr, &client_addr_size);
|
||||
if(tmp == -1) {
|
||||
// return -1;
|
||||
} else {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if(s->clients[i].sock == 0) {
|
||||
s->clients[i].sock = tmp;
|
||||
s->clients[i].userId = rand();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//server_transaction st;
|
||||
//memset(&st, 0, sizeof(st));
|
||||
//st.id = SERVER_T_SERVER_FLAG;
|
||||
//st.action = SERVER_T_SET_PLAYER_ID;
|
||||
//st.argsize = sizeof(int);
|
||||
//st.serverArg =
|
||||
|
||||
|
||||
//SendServerTransaction(&st, tmp);
|
||||
|
||||
//user_transaction ut;
|
||||
//memset(&ut, 0, sizeof(user_transaction));
|
||||
|
||||
//for(int i = 0; i < 4; i++) {
|
||||
// if(s->clients[i].sock != 0) {
|
||||
// RecvUserTransaction(&ut, s->sock);
|
||||
|
||||
// }
|
||||
//}
|
||||
// accempt client
|
||||
// send world seed
|
||||
// send player colors
|
||||
// player data
|
||||
// - class
|
||||
// - level
|
||||
// - items
|
||||
// - etc
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int limit_fps(int fps, struct timespec* last_time) {
|
||||
struct timespec current_time;
|
||||
double frame_time = 1.0/fps;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
|
||||
|
||||
double elapsed = (current_time.tv_sec - last_time->tv_sec) +
|
||||
(current_time.tv_nsec - last_time->tv_nsec) / 1e9;
|
||||
|
||||
if (elapsed < frame_time) {
|
||||
return 0;
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, last_time);
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int server_update(s_game_server* gs) {
|
||||
if(limit_fps(60, &gs->current_time)) {
|
||||
for(int i = 0; i < 4; i++) {
|
||||
if(gs->clients[0].sock > 0 && gs->clients[1].sock > 0) {
|
||||
|
||||
if(rst2.userArg != 0) {
|
||||
FreeUserTransaction(&rst2);
|
||||
}
|
||||
if(RecvUserTransaction(&rst2, s.clients[0].sock)){
|
||||
st2 = rst2;
|
||||
}
|
||||
SendUserTransaction(&st2, s.clients[1].sock);
|
||||
SendUserTransaction(&st1, s.clients[0].sock);
|
||||
|
||||
if(rst1.userArg != 0) {
|
||||
FreeUserTransaction(&rst1);
|
||||
}
|
||||
if(RecvUserTransaction(&rst1, s.clients[1].sock)) {
|
||||
st1 = rst1;
|
||||
}
|
||||
SendUserTransaction(&st1, s.clients[0].sock);
|
||||
SendUserTransaction(&st2, s.clients[1].sock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//int get_player_data(s_game_server* s, int client) {
|
||||
//
|
||||
// server_transaction st;
|
||||
// server_transaction rst;
|
||||
// memset(&st,0,sizeof(st));
|
||||
// memset(&rst,0,sizeof(rst));
|
||||
//
|
||||
// st.id = SERVER_T_SERVER_FLAG & SERVER_T_REQUEST;
|
||||
// st.action = SERVER_T_GET_PLAYER_DATA;
|
||||
//
|
||||
// printf("Getting Player Data\n");
|
||||
//
|
||||
//
|
||||
// if(!SendServerTransaction(&st, client)) {
|
||||
// return -1;
|
||||
// }
|
||||
//
|
||||
// if(RecvServerTransaction(&rst, client)) {
|
||||
// return -1;
|
||||
// }
|
||||
// return 0;
|
||||
//}
|
||||
//
|
||||
|
||||
@@ -2,13 +2,23 @@
|
||||
#define SERVER_H
|
||||
|
||||
#include "transactions.h"
|
||||
#include <time.h>
|
||||
|
||||
typedef struct {
|
||||
} client;
|
||||
int sock;
|
||||
int userId;
|
||||
} s_game_client;
|
||||
typedef struct {
|
||||
} client_manager;
|
||||
s_game_client clients[4];
|
||||
int number_of_clients;
|
||||
int sock;
|
||||
struct timespec current_time;
|
||||
|
||||
int start_server();
|
||||
} s_game_server;
|
||||
|
||||
int start_server(s_game_server* s);
|
||||
int accept_client(s_game_server* s);
|
||||
int server_update(s_game_server* gs);
|
||||
int get_player_data(s_game_server* s, int client);
|
||||
|
||||
#endif // !SERVER_H
|
||||
|
||||
@@ -1,32 +1,79 @@
|
||||
#include "transactions.h"
|
||||
#include <asm-generic/errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define PORT 2223
|
||||
#define IP_ADDRESS "127.0.0.1"
|
||||
|
||||
|
||||
int CreateTransaction(action *act) {
|
||||
packet* CreateServerTransaction(server_transaction *act) {
|
||||
|
||||
int bufferSize = sizeof(int)*3 + act->argsize;
|
||||
|
||||
//unsigned char* buffer = malloc(bufferSize);
|
||||
unsigned char* buffer = malloc(bufferSize);
|
||||
if( buffer <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//buffer[0] = act->id;
|
||||
//buffer[sizeof(int)*1] = act->action;
|
||||
//buffer[sizeof(int)*2] = act->argsize;
|
||||
//memcpy(&buffer[sizeof(int)*3], act->userArg, act->argsize);
|
||||
printf("transaction id: %d, action %d, argsize %d\n", act->id, act->action, act->argsize);
|
||||
|
||||
memcpy(buffer, &act->id, sizeof(int));
|
||||
memcpy(&buffer[sizeof(int)*1], &act->action, sizeof(int));
|
||||
memcpy(&buffer[sizeof(int)*2], &act->argsize, sizeof(int));
|
||||
memcpy(&buffer[sizeof(int)*3], act->serverArg, act->argsize);
|
||||
|
||||
packet* p = malloc(sizeof(packet));
|
||||
if(p <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
p->packetTpye = SERVER_PACKET;
|
||||
p->size = bufferSize;
|
||||
p->packet = buffer;
|
||||
|
||||
|
||||
//free(buffer);
|
||||
return p;
|
||||
}
|
||||
packet* CreateUserTransaction(user_transaction *act) {
|
||||
int bufferSize = sizeof(int)*3 + act->argsize;
|
||||
|
||||
return 0;
|
||||
unsigned char* buffer = malloc(bufferSize);
|
||||
if( buffer <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//printf("transaction id: %d, action %d, argsize %d\n", act->id, act->action, act->argsize);
|
||||
|
||||
memcpy(buffer, &act->id, sizeof(int));
|
||||
memcpy(&buffer[sizeof(int)*1], &act->action, sizeof(int));
|
||||
memcpy(&buffer[sizeof(int)*2], &act->argsize, sizeof(int));
|
||||
memcpy(&buffer[sizeof(int)*3], act->userArg, act->argsize);
|
||||
|
||||
packet* p = malloc(sizeof(packet));
|
||||
if(p <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
p->packetTpye = SERVER_PACKET;
|
||||
p->size = bufferSize;
|
||||
p->packet = buffer;
|
||||
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
int GetTransaction(action *act) {
|
||||
void FreePacket(packet* p) {
|
||||
free(p->packet);
|
||||
free(p);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int GetTransaction(user_transaction *act) {
|
||||
|
||||
|
||||
int id;
|
||||
@@ -45,6 +92,163 @@ int GetTransaction(action *act) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ProssesTransaction(action *act) {
|
||||
int ProssesTransaction(user_transaction *act) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define PACKET_SIZE(p) sizeof(int) + sizeof(int) + p->size
|
||||
|
||||
int SendServerTransaction(server_transaction *tract, int fd) {
|
||||
packet* p = CreateServerTransaction(tract);
|
||||
|
||||
int packetSize = PACKET_SIZE(p);
|
||||
unsigned char* buffer = malloc(packetSize);
|
||||
memset(buffer, 0, packetSize);
|
||||
unsigned char* sb = buffer;
|
||||
memcpy(buffer, &p->packetTpye, sizeof(int));
|
||||
memcpy(buffer + sizeof(int), &p->size, sizeof(int));
|
||||
memcpy(buffer + sizeof(int) + sizeof(int), p->packet, p->size);
|
||||
|
||||
printf("buffer :");
|
||||
for(int i = 0; i < PACKET_SIZE(p); i++) {
|
||||
printf("%u", sb[i]);
|
||||
}
|
||||
|
||||
int r = send(fd,sb, PACKET_SIZE(p),0);
|
||||
|
||||
printf("\n");
|
||||
|
||||
free(sb);
|
||||
FreePacket(p);
|
||||
|
||||
if(r == -1) {
|
||||
printf("NG\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int SendUserTransaction(user_transaction* tract, int fd) {
|
||||
packet* p = CreateUserTransaction(tract);
|
||||
|
||||
int packetSize = PACKET_SIZE(p);
|
||||
unsigned char* buffer = malloc(packetSize);
|
||||
memset(buffer, 0, packetSize);
|
||||
unsigned char* sb = buffer;
|
||||
memcpy(buffer, &p->packetTpye, sizeof(int));
|
||||
memcpy(buffer + sizeof(int), &p->size, sizeof(int));
|
||||
memcpy(buffer + sizeof(int) + sizeof(int), p->packet, p->size);
|
||||
|
||||
printf("buffer :");
|
||||
for(int i = 0; i < PACKET_SIZE(p); i++) {
|
||||
printf("%u", sb[i]);
|
||||
}
|
||||
|
||||
int r = send(fd,sb, PACKET_SIZE(p),0);
|
||||
|
||||
printf("\n");
|
||||
|
||||
free(sb);
|
||||
FreePacket(p);
|
||||
|
||||
if(r == -1) {
|
||||
printf("NG\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RecvServerTransaction(server_transaction *tract, int fd) {
|
||||
|
||||
|
||||
packet tp;
|
||||
int sizeOfPacket = 1024;
|
||||
unsigned char* buffer = malloc(1024);
|
||||
int r = 0;
|
||||
|
||||
printf("reading buffer");
|
||||
r = recv(fd, buffer, sizeof(int)*2,0);
|
||||
printf("%d\n",r);
|
||||
|
||||
if(r > 0) {
|
||||
printf("buffer :");
|
||||
for(int i = 0; i < r; i++) {
|
||||
printf("%u", buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
tp.packetTpye = (int)buffer[0];
|
||||
tp.size = (int)buffer[sizeof(int)];
|
||||
r = recv(fd, buffer, tp.size,MSG_DONTWAIT);
|
||||
tp.packet = malloc(tp.size);
|
||||
memcpy(tp.packet, buffer, tp.size);
|
||||
|
||||
memset(tract, 0, sizeof(server_transaction));
|
||||
memcpy(&tract->id, tp.packet, sizeof(int));
|
||||
memcpy(&tract->action, tp.packet + sizeof(int), sizeof(int));
|
||||
memcpy(&tract->argsize, tp.packet + sizeof(int) + sizeof(int), sizeof(int));
|
||||
tract->serverArg = malloc(tract->argsize);
|
||||
|
||||
memcpy(tract->serverArg, &tp.packet[sizeof(int)*3], tract->argsize);
|
||||
if(tract->id != 0) {
|
||||
printf("id: %d, action %d, argsize %d\n", tract->id, tract->action, tract->argsize);
|
||||
printf("Data :%s\n", (char*)tract->serverArg);
|
||||
}
|
||||
FreePacket(&tp);
|
||||
return 0;
|
||||
}
|
||||
free(buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RecvUserTransaction(user_transaction *tract, int fd) {
|
||||
|
||||
packet tp;
|
||||
int sizeOfPacket = 1024;
|
||||
unsigned char* buffer = malloc(1024);
|
||||
int r = 0;
|
||||
|
||||
printf("reading buffer");
|
||||
r = recv(fd, buffer, sizeof(int)*2,MSG_DONTWAIT);
|
||||
printf("%d\n",r);
|
||||
|
||||
if(r > 0) {
|
||||
printf("buffer :");
|
||||
for(int i = 0; i < r; i++) {
|
||||
printf("%u", buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
tp.packetTpye = (int)buffer[0];
|
||||
tp.size = (int)buffer[sizeof(int)];
|
||||
r = recv(fd, buffer, tp.size,MSG_DONTWAIT);
|
||||
tp.packet = malloc(tp.size);
|
||||
memcpy(tp.packet, buffer, tp.size);
|
||||
|
||||
memset(tract, 0, sizeof(server_transaction));
|
||||
memcpy(&tract->id, tp.packet, sizeof(int));
|
||||
memcpy(&tract->action, tp.packet + sizeof(int), sizeof(int));
|
||||
memcpy(&tract->argsize, tp.packet + sizeof(int) + sizeof(int), sizeof(int));
|
||||
tract->userArg = malloc(tract->argsize);
|
||||
|
||||
memcpy(tract->userArg, &tp.packet[sizeof(int)*3], tract->argsize);
|
||||
if(tract->id != 0) {
|
||||
printf("id: %d, action %d, argsize %d\n", tract->id, tract->action, tract->argsize);
|
||||
printf("Data :%s\n", (char*)tract->userArg);
|
||||
}
|
||||
FreePacket(&tp);
|
||||
return 0;
|
||||
}
|
||||
free(buffer);
|
||||
return 1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void FreeUserTransaction(user_transaction* ut) {
|
||||
free(ut->userArg);
|
||||
memset(ut, 0, sizeof(user_transaction));
|
||||
}
|
||||
void FreeServerTransaction(server_transaction* st) {
|
||||
free(st->serverArg);
|
||||
memset(st, 0, sizeof(server_transaction));
|
||||
}
|
||||
|
||||
@@ -9,12 +9,14 @@
|
||||
* =============================================================================
|
||||
* */
|
||||
#ifndef TRANSACTIONS_H
|
||||
#define TRANSACTIONS_H
|
||||
|
||||
#define ENTITY_ACTION_MOVE_UP 1
|
||||
#define ENTITY_ACTION_MOVE_DOWN 2
|
||||
#define ENTITY_ACTION_MOVE_LEFT 3
|
||||
#define ENTITY_ACTION_MOVE_RIGHT 4
|
||||
#define ENTITY_ACTION_MOVE 1
|
||||
|
||||
#define SERVER_PACKET 1
|
||||
#define USER_PACKET 2
|
||||
#define RELAY_PACKET 3
|
||||
#define ADMIN_PACKET 4
|
||||
|
||||
typedef struct {
|
||||
int packetTpye, size;
|
||||
@@ -24,13 +26,49 @@ typedef struct {
|
||||
typedef struct {
|
||||
int id;
|
||||
int action;
|
||||
int userId;
|
||||
int entityId;
|
||||
int argsize;
|
||||
void* userArg;
|
||||
}action;
|
||||
}user_transaction;
|
||||
|
||||
int CreateTransaction(action* act);
|
||||
int GetTransaction(action* act);
|
||||
int ProssesTransaction(action* act);
|
||||
#define SERVER_T_CONNECT 0b0001
|
||||
#define SERVER_T_DISCONNECT 0b0010
|
||||
#define SERVER_T_RECONNECT 0b0011
|
||||
#define SERVER_T_RELAY 0b0100
|
||||
#define SERVER_T_KICK 0b0101
|
||||
#define SERVER_T_REQUEST 0b0110
|
||||
|
||||
#define SERVER_T_USER_FLAG 0b00010000
|
||||
#define SERVER_T_ADMIN_FLAG 0b00100000
|
||||
#define SERVER_T_SERVER_FLAG 0b01000000
|
||||
#define SERVER_T_RELAY_FLAG 0b10000000
|
||||
|
||||
#define SERVER_T_GET_PLAYER_DATA 1
|
||||
#define SERVER_T_SET_PLAYER_ID 2
|
||||
|
||||
|
||||
typedef struct {
|
||||
int id;
|
||||
int action;
|
||||
int userId;
|
||||
unsigned int ipaddress;
|
||||
int argsize;
|
||||
void* serverArg;
|
||||
|
||||
}server_transaction;
|
||||
|
||||
int GetTransaction(user_transaction* tract);
|
||||
int ProssesTransaction(user_transaction* tract);
|
||||
|
||||
int SendServerTransaction(server_transaction* tract, int serverFD);
|
||||
int SendUserTransaction(user_transaction* tract, int serverFD);
|
||||
|
||||
int RecvServerTransaction(server_transaction* tract, int serverFD);
|
||||
int RecvUserTransaction(user_transaction* tract, int serverFD);
|
||||
|
||||
void FreeUserTransaction(user_transaction* ut);
|
||||
void FreeServerTransaction(server_transaction* st);
|
||||
|
||||
#define TRANSACTIONS_H
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user