project_files/frontlib/test.c
changeset 7269 5b0aeef8ba2a
parent 7234 613998625a3c
child 7271 5608ac657362
equal deleted inserted replaced
7267:710f3ced8934 7269:5b0aeef8ba2a
     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 "model/map.h"
     5 #include "model/map.h"
     5 #include "model/weapon.h"
     6 #include "model/weapon.h"
     6 #include "model/schemelist.h"
     7 #include "model/schemelist.h"
     7 #include "ipc/mapconn.h"
     8 #include "ipc/mapconn.h"
     8 #include "ipc/gameconn.h"
     9 #include "ipc/gameconn.h"
     9 #include "net/netbase.h"
    10 #include "net/netconn.h"
    10 
    11 
    11 #include <stdlib.h>
    12 #include <stdlib.h>
    12 #include <stdbool.h>
    13 #include <stdbool.h>
    13 #include <assert.h>
    14 #include <assert.h>
    14 #include <string.h>
    15 #include <string.h>
   196 	while(gameconn) {
   197 	while(gameconn) {
   197 		flib_gameconn_tick(gameconn);
   198 		flib_gameconn_tick(gameconn);
   198 	}
   199 	}
   199 }
   200 }
   200 
   201 
       
   202 void handleNetDisconnect(void *context, int reason, const char *message) {
       
   203 	flib_log_i("Disconnected: %s", message);
       
   204 	flib_netconn_destroy(*(flib_netconn**)context);
       
   205 	*(flib_netconn**)context = NULL;
       
   206 }
       
   207 
       
   208 void handleNetConnected(void *context) {
       
   209 	const flib_roomlist *roomlist = flib_netconn_get_roomlist(*(flib_netconn**)context);
       
   210 	flib_log_i("List of rooms:");
       
   211 	for(int i=0; i<roomlist->roomCount; i++) {
       
   212 		flib_roomlist_room *room = roomlist->rooms[i];
       
   213 		flib_log_i("%1s %20s %20s %2i %2i %20s %20s %20s", room->inProgress ? "X" : " ", room->name, room->owner, room->playerCount, room->teamCount, room->map, room->scheme, room->weapons);
       
   214 	}
       
   215 }
       
   216 
       
   217 void handleLobbyJoin(void *context, const char *nick) {
       
   218 	flib_log_i("%s joined", nick);
       
   219 }
       
   220 
       
   221 void handleChat(void *context, const char *nick, const char *msg) {
       
   222 	flib_log_i("%s: %s", nick, msg);
       
   223 	if(!memcmp("frontbot ", msg, strlen("frontbot "))) {
       
   224 		const char *command = msg+strlen("frontbot ");
       
   225 		if(!memcmp("quit", command, strlen("quit"))) {
       
   226 			flib_netconn_send_quit(*(flib_netconn**)context, "Yeth Mathter");
       
   227 		} else if(!memcmp("describe ", command, strlen("describe "))) {
       
   228 			const char *roomname = command+strlen("describe ");
       
   229 			const flib_roomlist *roomlist = flib_netconn_get_roomlist(*(flib_netconn**)context);
       
   230 			flib_roomlist_room *room = flib_roomlist_find((flib_roomlist*)roomlist, roomname);
       
   231 			if(!room) {
       
   232 				flib_netconn_send_chat(*(flib_netconn**)context, "Unknown room.");
       
   233 			} else {
       
   234 				char *text = flib_asprintf(
       
   235 						"%s is a room created by %s, where %i players (%i teams) are %s on %s%s, using the %s scheme and %s weaponset.",
       
   236 						room->name,
       
   237 						room->owner,
       
   238 						room->playerCount,
       
   239 						room->teamCount,
       
   240 						room->inProgress ? "fighting" : "preparing to fight",
       
   241 						room->map[0]=='+' ? "" : "the map ",
       
   242 						!strcmp("+rnd+", room->map) ? "a random map" :
       
   243 								!strcmp("+maze+", room->map) ? "a random maze" :
       
   244 								!strcmp("+drawn+", room->map) ? "a hand-drawn map" :
       
   245 								room->map,
       
   246 						room->scheme,
       
   247 						room->weapons);
       
   248 				if(text) {
       
   249 					flib_netconn_send_chat(*(flib_netconn**)context, text);
       
   250 				}
       
   251 				free(text);
       
   252 			}
       
   253 		}
       
   254 	}
       
   255 }
       
   256 
       
   257 void handleAskPass(void *context, const char *nick) {
       
   258 	flib_netconn_send_password((flib_netconn*)context, "Lorem");
       
   259 }
       
   260 
   201 int main(int argc, char *argv[]) {
   261 int main(int argc, char *argv[]) {
   202 	flib_init(0);
   262 	flib_init(0);
   203 	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
   263 	flib_log_setLevel(FLIB_LOGLEVEL_ALL);
   204 
   264 
   205 	//testMapPreview();
   265 	//testMapPreview();
   206 	//testDemo();
   266 	//testDemo();
   207 	//testSave();
   267 	//testSave();
   208 	//testGame();
   268 	//testGame();
   209 
   269 
   210 	flib_netbase *conn = flib_netbase_create("140.247.62.101", 46631);
   270 	flib_cfg_meta *meta = flib_cfg_meta_from_ini("metasettings.ini");
   211 
   271 	assert(meta);
   212 	while(flib_netbase_connected(conn)) {
   272 	flib_netconn *conn = flib_netconn_create("Medo42", meta, "140.247.62.101", 46631);
   213 		flib_netmsg *msg = flib_netbase_recv_message(conn);
   273 	assert(conn);
   214 		if(msg && msg->partCount>0) {
   274 	flib_cfg_meta_release(meta);
   215 			flib_log_i("[NET IN] %s", msg->parts[0]);
   275 
   216 			for(int i=1; i<msg->partCount; i++) {
   276 	flib_netconn_onConnected(conn, handleNetConnected, &conn);
   217 				flib_log_i("[NET IN][-] %s", msg->parts[i]);
   277 	flib_netconn_onDisconnected(conn, handleNetDisconnect, &conn);
   218 			}
   278 	flib_netconn_onLobbyJoin(conn, handleLobbyJoin, &conn);
   219 			if(!strcmp(msg->parts[0], "CONNECTED")) {
   279 	flib_netconn_onChat(conn, handleChat, &conn);
   220 				flib_netmsg *nickmsg = flib_netmsg_create();
   280 	flib_netconn_onPasswordRequest(conn, handleAskPass, conn);
   221 				flib_netmsg_append_part(nickmsg, "NICK", 4);
   281 
   222 				flib_netmsg_append_part(nickmsg, "Medo42_frontlib", 15);
   282 	while(conn) {
   223 				flib_netmsg *protomsg = flib_netmsg_create();
   283 		flib_netconn_tick(conn);
   224 				flib_netmsg_append_part(protomsg, "PROTO", 5);
       
   225 				flib_netmsg_append_part(protomsg, "42", 2);
       
   226 				flib_netbase_send_message(conn, nickmsg);
       
   227 				flib_netbase_send_message(conn, protomsg);
       
   228 				flib_netmsg_destroy(nickmsg);
       
   229 				flib_netmsg_destroy(protomsg);
       
   230 			}
       
   231 			if(!strcmp(msg->parts[0], "SERVER_MESSAGE")) {
       
   232 				flib_netmsg *quitmsg = flib_netmsg_create();
       
   233 				flib_netmsg_append_part(quitmsg, "QUIT", 4);
       
   234 				flib_netmsg_append_part(quitmsg, "Just testing", 12);
       
   235 				flib_netbase_send_message(conn, quitmsg);
       
   236 				flib_netmsg_destroy(quitmsg);
       
   237 			}
       
   238 		}
       
   239 		flib_netmsg_destroy(msg);
       
   240 	}
   284 	}
   241 
   285 
   242 	flib_quit();
   286 	flib_quit();
   243 	return 0;
   287 	return 0;
   244 }
   288 }