project_files/frontlib/test.c
changeset 7271 5608ac657362
parent 7269 5b0aeef8ba2a
equal deleted inserted replaced
7269:5b0aeef8ba2a 7271:5608ac657362
     1 #include "frontlib.h"
     1 #include "frontlib.h"
     2 #include "util/logging.h"
     2 #include "util/logging.h"
     3 #include "util/buffer.h"
     3 #include "util/buffer.h"
     4 #include "util/util.h"
     4 #include "util/util.h"
       
     5 #include "util/list.h"
     5 #include "model/map.h"
     6 #include "model/map.h"
     6 #include "model/weapon.h"
     7 #include "model/weapon.h"
     7 #include "model/schemelist.h"
     8 #include "model/schemelist.h"
     8 #include "ipc/mapconn.h"
     9 #include "ipc/mapconn.h"
     9 #include "ipc/gameconn.h"
    10 #include "ipc/gameconn.h"
    76 	system(commandbuffer);
    77 	system(commandbuffer);
    77 }
    78 }
    78 
    79 
    79 void testMapPreview() {
    80 void testMapPreview() {
    80 	// Create a map description and check that there was no error
    81 	// Create a map description and check that there was no error
    81 	flib_map *map = flib_map_create_maze("Jungle", MAZE_SIZE_SMALL_TUNNELS);
    82 	flib_map *map = flib_map_create_maze("This is the seed value", "Jungle", MAZE_SIZE_SMALL_TUNNELS);
    82 	assert(map);
    83 	assert(map);
    83 
    84 
    84 	// Create a new connection to the engine and check that there was no error
    85 	// Create a new connection to the engine and check that there was no error
    85 	flib_mapconn *mapConnection = flib_mapconn_create("This is the seed value", map);
    86 	flib_mapconn *mapConnection = flib_mapconn_create(map);
    86 	assert(mapConnection);
    87 	assert(mapConnection);
    87 
    88 
    88 	// We don't need the map description anymore
    89 	// We don't need the map description anymore
    89 	flib_map_release(map);
    90 	flib_map_release(map);
    90 	map = NULL;
    91 	map = NULL;
   110 	flib_weaponset *weapons = flib_weaponset_create("Defaultweaps");
   111 	flib_weaponset *weapons = flib_weaponset_create("Defaultweaps");
   111 	flib_schemelist *schemelist = flib_schemelist_from_ini(metaconf, "schemes.ini");
   112 	flib_schemelist *schemelist = flib_schemelist_from_ini(metaconf, "schemes.ini");
   112 
   113 
   113 	flib_gamesetup setup;
   114 	flib_gamesetup setup;
   114 	setup.gamescheme = flib_schemelist_find(schemelist, "Default");
   115 	setup.gamescheme = flib_schemelist_find(schemelist, "Default");
   115 	setup.map = flib_map_create_maze("Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
   116 	setup.map = flib_map_create_maze("asparagus", "Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
   116 	setup.seed = "asparagus";
       
   117 	setup.script = NULL;
   117 	setup.script = NULL;
   118 	setup.teamcount = 2;
   118 	setup.teamCount = 2;
   119 	setup.teams = calloc(2, sizeof(flib_team*));
   119 	setup.teams = calloc(2, sizeof(flib_team*));
   120 	setup.teams[0] = calloc(1, sizeof(flib_team));
   120 	setup.teams[0] = calloc(1, sizeof(flib_team));
   121 	setup.teams[0]->color = 0xffff0000;
   121 	setup.teams[0]->color = 0xffff0000;
   122 	setup.teams[0]->flag = "australia";
   122 	setup.teams[0]->flag = "australia";
   123 	setup.teams[0]->fort = "Plane";
   123 	setup.teams[0]->fort = "Plane";
   248 				if(text) {
   248 				if(text) {
   249 					flib_netconn_send_chat(*(flib_netconn**)context, text);
   249 					flib_netconn_send_chat(*(flib_netconn**)context, text);
   250 				}
   250 				}
   251 				free(text);
   251 				free(text);
   252 			}
   252 			}
       
   253 		} else if(!memcmp("join ", command, strlen("join "))) {
       
   254 			const char *roomname = command+strlen("join ");
       
   255 			flib_netconn_send_joinRoom(*(flib_netconn**)context, roomname);
       
   256 		} else if(!memcmp("ready", command, strlen("ready"))) {
       
   257 			flib_netconn_send_toggleReady(*(flib_netconn**)context);
   253 		}
   258 		}
   254 	}
   259 	}
   255 }
   260 }
   256 
   261 
   257 void handleAskPass(void *context, const char *nick) {
   262 static flib_gamesetup gGamesetup = {0};
   258 	flib_netconn_send_password((flib_netconn*)context, "Lorem");
   263 static flib_weaponset *gWeaponset = NULL;
       
   264 
       
   265 void handleEnterRoom(void *context, bool isChief) {
       
   266 	flib_netconn_send_toggleReady(*(flib_netconn**)context);
       
   267 }
       
   268 
       
   269 void handleMap(void *context, const flib_map *map, int changeType) {
       
   270 	flib_map_release(gGamesetup.map);
       
   271 	gGamesetup.map = flib_map_copy(map);
       
   272 }
       
   273 
       
   274 void handleCfgScheme(void *context, flib_cfg *cfg) {
       
   275 	flib_cfg_release(gGamesetup.gamescheme);
       
   276 	gGamesetup.gamescheme = flib_cfg_retain(cfg);
       
   277 }
       
   278 
       
   279 void handleWeaponset(void *context, flib_weaponset *weaponset) {
       
   280 	flib_weaponset_release(gWeaponset);
       
   281 	gWeaponset = flib_weaponset_retain(weaponset);
       
   282 }
       
   283 
       
   284 void handleScript(void *context, const char *script) {
       
   285 	free(gGamesetup.script);
       
   286 	gGamesetup.script = flib_strdupnull(script);
       
   287 }
       
   288 
       
   289 void handleTeamAdd(void *context, flib_team *team) {
       
   290 	flib_team *teamptr = flib_team_retain(team);
       
   291 	gGamesetup.teams = flib_list_insert(gGamesetup.teams, &gGamesetup.teamCount, sizeof(*gGamesetup.teams), &teamptr, 0);
       
   292 }
       
   293 
       
   294 void handleTeamRemove(void *context, const char *team) {
       
   295 	for(int i=0; i<gGamesetup.teamCount; i++) {
       
   296 		if(!strcmp(team, gGamesetup.teams[i]->name)) {
       
   297 			flib_team_release(gGamesetup.teams[i]);
       
   298 			gGamesetup.teams = flib_list_delete(gGamesetup.teams, &gGamesetup.teamCount, sizeof(*gGamesetup.teams), i);
       
   299 		}
       
   300 	}
   259 }
   301 }
   260 
   302 
   261 int main(int argc, char *argv[]) {
   303 int main(int argc, char *argv[]) {
   262 	flib_init(0);
   304 	flib_init(0);
   263 	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
   305 	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
   267 	//testSave();
   309 	//testSave();
   268 	//testGame();
   310 	//testGame();
   269 
   311 
   270 	flib_cfg_meta *meta = flib_cfg_meta_from_ini("metasettings.ini");
   312 	flib_cfg_meta *meta = flib_cfg_meta_from_ini("metasettings.ini");
   271 	assert(meta);
   313 	assert(meta);
   272 	flib_netconn *conn = flib_netconn_create("Medo42", meta, "140.247.62.101", 46631);
   314 	flib_netconn *conn = flib_netconn_create("frontbot", meta, "140.247.62.101", 46631);
   273 	assert(conn);
   315 	assert(conn);
   274 	flib_cfg_meta_release(meta);
   316 	flib_cfg_meta_release(meta);
   275 
   317 
   276 	flib_netconn_onConnected(conn, handleNetConnected, &conn);
   318 	flib_netconn_onConnected(conn, handleNetConnected, &conn);
   277 	flib_netconn_onDisconnected(conn, handleNetDisconnect, &conn);
   319 	flib_netconn_onDisconnected(conn, handleNetDisconnect, &conn);
   278 	flib_netconn_onLobbyJoin(conn, handleLobbyJoin, &conn);
   320 	flib_netconn_onLobbyJoin(conn, handleLobbyJoin, &conn);
   279 	flib_netconn_onChat(conn, handleChat, &conn);
   321 	flib_netconn_onChat(conn, handleChat, &conn);
   280 	flib_netconn_onPasswordRequest(conn, handleAskPass, conn);
   322 	flib_netconn_onMapChanged(conn, handleMap, conn);
       
   323 	flib_netconn_onEnterRoom(conn, handleEnterRoom, conn);
       
   324 	flib_netconn_onCfgScheme(conn, handleCfgScheme, conn);
       
   325 	flib_netconn_onWeaponsetChanged(conn, handleWeaponset, conn);
       
   326 	flib_netconn_onScriptChanged(conn, handleScript, conn);
       
   327 	flib_netconn_onTeamAdd(conn, handleTeamAdd, conn);
       
   328 	flib_netconn_onTeamRemove(conn, handleTeamRemove, conn);
       
   329 	flib_netconn_onHogCountChanged(conn, handleHogCountChanged, conn);
   281 
   330 
   282 	while(conn) {
   331 	while(conn) {
   283 		flib_netconn_tick(conn);
   332 		flib_netconn_tick(conn);
   284 	}
   333 	}
   285 
   334