main.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. Space Construct by Beanzilla
  3. Beanzilla@21:4/110 on fsxNet
  4. */
  5. // Verion 0.1-dev
  6. #define VERSION_MAJOR 0
  7. #define VERSION_MINOR 2
  8. #ifndef VERSION_TYPE
  9. #define VERSION_TYPE "dev"
  10. #endif
  11. #define PATH_MAX 256
  12. #define PATH_SEP "/"
  13. #include <MagiDoor.h>
  14. #include <sqlite3.h>
  15. #if defined(_MSC_VER) || defined(WIN32)
  16. #define snprintf _snprintf
  17. #define strcasecmp _stricmp
  18. #include <winsock2.h>
  19. #ifndef _MSC_VER
  20. #define _MSC_VER 1
  21. #endif
  22. #else
  23. #include <arpa/inet.h>
  24. #endif
  25. // Standard C
  26. #include <time.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <ctype.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <unistd.h> // stat
  35. char * log_path = "logs";
  36. int inuse = 0; // Are any other copies of us running? (We are a single door!)
  37. int debug = 0; // Are we in debug mode?
  38. // User Structure (This includes guns, fuel, armor, shields, HP)
  39. typedef struct user_info {
  40. int uid; // Primary Key
  41. int experience; // Think about this as score
  42. // Was ship structure, now is part of user
  43. int guns; // Placed Guns, More means more damage (Deals from 1-2 damage per attack)
  44. int fuel; // How many "turns" do we have
  45. int metal; // Used to build more guns/armors/shields possibly fuel too (guns take 5, armor takes 3, shields take 7, maybe fuel takes 12)
  46. int armors; // Placed Armors (Each Armor takes 4 points of damage then breaks off)
  47. int shields; // Placed Shields (Each Shields takes 3 points of damage then is offline, regenerates 1 point per attack/turn must reach 50% of max to being protecting again)
  48. int hitpoints; // Total hitpoints till we die (Rules for taking damage, shields first then armor then hitpoints)
  49. int armorpoints; // Points we get from armors (Repairable but costs 1 metal per point, but can break if taking too much damage)
  50. int shieldpoints; // Points we get from shields (Self-Regenerates, and does not get destroyed)
  51. int shieldsup; // 0 means yes, anything else means shields are offline, deduct if not 0 for each attack.
  52. int laston; // 20200630 is 6-30-2020
  53. char nick[256]; // What they go by on score board, this allows duping since we really go by real
  54. char real[256]; // Their real name to match with drop file info, prevents someone from loging in as another
  55. } user_inf;
  56. // DateStamp Structure to provide date difference
  57. typedef struct dateTamp {
  58. int year; // YYYY, or number of years difference
  59. int month; // MM, or number of months difference
  60. int day; // DD, or number of days difference.
  61. int age; // Taking down to aproximate day count.
  62. } dT; // YYYYMMDD it in int form
  63. void dolog(char *fmt, ...) {
  64. // Low end Logging
  65. char buffer[PATH_MAX];
  66. struct tm *time_now;
  67. time_t timen;
  68. FILE *logfptr;
  69. timen = time(NULL);
  70. time_now = localtime(&timen);
  71. if (log_path != NULL) {
  72. snprintf(buffer, PATH_MAX, "%s%s%04d%02d%02d.log", log_path, PATH_SEP,
  73. time_now->tm_year + 1900, time_now->tm_mon + 1, time_now->tm_mday);
  74. } else {
  75. snprintf(buffer, PATH_MAX, "%04d%02d%02d.log", time_now->tm_year + 1900,
  76. time_now->tm_mon + 1, time_now->tm_mday);
  77. }
  78. logfptr = fopen(buffer, "a");
  79. if (!logfptr) {
  80. return;
  81. }
  82. va_list ap;
  83. va_start(ap, fmt);
  84. vsnprintf(buffer, 512, fmt, ap);
  85. va_end(ap);
  86. fprintf(logfptr, "%02d:%02d:%02d %s\n", time_now->tm_hour, time_now->tm_min,
  87. time_now->tm_sec, buffer);
  88. fclose(logfptr);
  89. }
  90. int dateStamp() {
  91. // YYYYMMDD \o/ In a INT so we can store it and compare against it!
  92. struct tm *time_now;
  93. time_t timen;
  94. int result = 0;
  95. timen = time(NULL);
  96. time_now = localtime(&timen);
  97. result += ((time_now->tm_year + 1900) * 10000);
  98. result += (time_now->tm_mon * 100) + 100;
  99. result += time_now->tm_mday;
  100. return result;
  101. }
  102. int compareDate(int dt) {
  103. // Returns integer of difference, from now.
  104. struct tm *time_now;
  105. time_t timen;
  106. int now = 0;
  107. timen = time(NULL);
  108. time_now = localtime(&timen);
  109. now += ((time_now->tm_year + 1900) * 10000);
  110. now += (time_now->tm_mon * 100) + 100;
  111. now += time_now->tm_mday;
  112. return (now - dt); // 10000 = 1 Year, 100 = 1 Month, 1 = 1 Day
  113. }
  114. dT formDate(int diff) {
  115. // Reverse process so we can compare exact days or get the structure back out too.
  116. dT result;
  117. result.year = 0;
  118. result.month = 0;
  119. result.day = 0;
  120. result.age = 0;
  121. int notDone = 1;
  122. while(notDone) {
  123. if(diff >= 10000) {
  124. diff -= 10000;
  125. result.year += 1;
  126. } else if(diff >= 100) {
  127. diff -= 100;
  128. result.month += 1;
  129. } else if(diff >= 1) {
  130. diff -= 1;
  131. result.day += 1;
  132. } else {
  133. notDone = 0;
  134. }
  135. } // Process Aprox day count
  136. if(result.year != 0) {
  137. result.age += (365 * result.year);
  138. } else if(result.month != 0) {
  139. result.age += (30 * result.month);
  140. } else if(result.day != 0) {
  141. result.age += result.day;
  142. }
  143. return result;
  144. }
  145. int db_test() {
  146. sqlite3 *db;
  147. sqlite3_stmt *stmt;
  148. char sqlbuffer[256];
  149. char strbuffer[256];
  150. int sizeofdb = 0;
  151. int rc = sqlite3_open("spaceconstruct.db3", &db);
  152. if(rc) { // Did we do a successful open?
  153. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  154. sqlite3_close(db);
  155. md_exit(1);
  156. }
  157. sqlite3_busy_timeout(db, 5000);
  158. // DB open
  159. strcpy(sqlbuffer, "SELECT * FROM user;");
  160. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer), &stmt, NULL);
  161. while(sqlite3_step(stmt) == SQLITE_ROW) {
  162. md_printf("`white`%s=%d %s=%s %s=%s %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d %s=%d\r\n",
  163. sqlite3_column_name(stmt, 0), // int, uid
  164. sqlite3_column_int(stmt, 0),
  165. sqlite3_column_name(stmt, 1), // text, nick
  166. sqlite3_column_text(stmt, 1),
  167. sqlite3_column_name(stmt, 2), // text, real
  168. sqlite3_column_text(stmt, 2),
  169. sqlite3_column_name(stmt, 3), // int, experiece
  170. sqlite3_column_int(stmt, 3),
  171. sqlite3_column_name(stmt, 4), // int, metal
  172. sqlite3_column_int(stmt, 4),
  173. sqlite3_column_name(stmt, 5), // int, fuel
  174. sqlite3_column_int(stmt, 5),
  175. sqlite3_column_name(stmt, 6), // int, guns
  176. sqlite3_column_int(stmt, 6),
  177. sqlite3_column_name(stmt, 7), // int, armors
  178. sqlite3_column_int(stmt, 7),
  179. sqlite3_column_name(stmt, 8), // int, shields
  180. sqlite3_column_int(stmt, 8),
  181. sqlite3_column_name(stmt, 9), // int, armorpoints
  182. sqlite3_column_int(stmt, 9),
  183. sqlite3_column_name(stmt, 10), // int, shieldpoints
  184. sqlite3_column_int(stmt, 10),
  185. sqlite3_column_name(stmt, 11), // int, hitpoints
  186. sqlite3_column_int(stmt, 11),
  187. sqlite3_column_name(stmt, 12), // int, shieldsup
  188. sqlite3_column_int(stmt, 12),
  189. sqlite3_column_name(stmt, 13), // int, laston
  190. sqlite3_column_int(stmt, 13)
  191. );
  192. sizeofdb += 1;
  193. } // Clean up database
  194. sqlite3_finalize(stmt);
  195. sqlite3_close(db);
  196. return sizeofdb;
  197. }
  198. int check_lock() {
  199. // Checks stats of lock, (1 = Lock is in effect, 0 = No Lock established)
  200. struct stat s;
  201. if (stat("lock.flg", &s) == 0) {
  202. return 1;
  203. } else {
  204. return 0;
  205. }
  206. }
  207. void grab_lock() {
  208. // Attempt to grab lock
  209. int valid = check_lock();
  210. FILE *fhandle;
  211. if (valid == 0) {
  212. fhandle = fopen("lock.flg", "w");
  213. if(!fhandle) {
  214. dolog("E: Unable to make lock.flg!");
  215. fprintf(stderr, "Unable to establish lock!\r\n");
  216. md_exit(-1);
  217. }
  218. fprintf(fhandle, "I am in use already!\n");
  219. fclose(fhandle);
  220. } else {
  221. dolog("W: Lock already established!");
  222. }
  223. }
  224. void rel_lock() {
  225. // Attempt to release lock
  226. int valid = check_lock();
  227. if (valid == 1) {
  228. if (unlink("lock.flg") != 0) {
  229. dolog("E: Unable to release lock.flg!");
  230. fprintf(stderr, "Unable to release lock!\r\n");
  231. md_exit(-1);
  232. }
  233. } else {
  234. dolog("W: Lock already released!");
  235. }
  236. }
  237. void log_drop() {
  238. // Spits out info from Drop File:
  239. md_printf("`white`First=%s Last=%s Alias=%s TimeLeft=%d SecLevel=%d Location=%s Node=%d Socket=%d Sysop=%s\r\n",
  240. mdcontrol.user_firstname,
  241. mdcontrol.user_lastname,
  242. mdcontrol.user_alias,
  243. mdcontrol.user_timeleft,
  244. mdcontrol.user_seclevel,
  245. mdcontrol.user_location,
  246. mdcontrol.node,
  247. mdcontrol.socket,
  248. mdcontrol.sysop_name
  249. );
  250. }
  251. void paws() {
  252. // Aaah, DRY
  253. md_printf("`white`Press any key to continue...");
  254. md_getc();
  255. md_printf("\r\n");
  256. }
  257. int yesNo() {
  258. char ch;
  259. int done = 0;
  260. md_printf("`bright black`(`bright white`Y`bright black`)`white`es `bright black`(`bright red`N`bright black`)`red`o");
  261. ch = md_get_answer("YyNn\r");
  262. md_printf("\r\n");
  263. switch(tolower(ch)) {
  264. case '\r':
  265. case 'y':
  266. done = 1;
  267. break;
  268. case 'n':
  269. break;
  270. }
  271. return done;
  272. }
  273. int locate_player(char first[], char last[]) {
  274. // returns user id for given real name and 0 for no record found
  275. sqlite3 *db;
  276. sqlite3_stmt *stmt;
  277. char sqlbuffer[256];
  278. int result = 0;
  279. char name[256];
  280. int rc = sqlite3_open("spaceconstruct.db3", &db);
  281. if(rc) { // Did we do a successful open?
  282. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  283. sqlite3_close(db);
  284. md_exit(-1);
  285. }
  286. sqlite3_busy_timeout(db, 5000);
  287. // Form Real Name
  288. strcpy(name, "");
  289. strcat(name, first);
  290. strcat(name, " ");
  291. strcat(name, last);
  292. //md_printf("realname = '%s'\r\n", name);
  293. // Locating user with given name
  294. strcpy(sqlbuffer, "SELECT * from user where real=? COLLATE NOCASE;");
  295. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  296. sqlite3_bind_text(stmt, 1, name, strlen(name), SQLITE_STATIC);
  297. rc = sqlite3_step(stmt);
  298. if (rc == SQLITE_ROW) {
  299. //dolog("User=%s uid=%d", name, sqlite3_column_int(stmt, 0));
  300. result = sqlite3_column_int(stmt, 0);
  301. } else {
  302. dolog("W: Unable to locate user=%s", name);
  303. result = 0;
  304. } // Clean Up, return results
  305. sqlite3_finalize(stmt);
  306. sqlite3_close(db);
  307. return result;
  308. }
  309. user_inf load_player(int uuid) {
  310. // Returns a player Structure from database
  311. sqlite3 *db;
  312. sqlite3_stmt *stmt;
  313. char sqlbuffer[256];
  314. user_inf result;
  315. int rc = sqlite3_open("spaceconstruct.db3", &db);
  316. if(rc) { // Did we do a successful open?
  317. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  318. sqlite3_close(db);
  319. md_exit(-1);
  320. }
  321. sqlite3_busy_timeout(db, 5000);
  322. strcpy(sqlbuffer, "SELECT * FROM user WHERE uid=?;");
  323. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  324. sqlite3_bind_int(stmt, 1, uuid);
  325. rc = sqlite3_step(stmt);
  326. if (rc == SQLITE_ROW) {
  327. result.uid = sqlite3_column_int(stmt, 0);
  328. strcpy(result.nick, sqlite3_column_text(stmt, 1));
  329. strcpy(result.real, sqlite3_column_text(stmt, 2));
  330. result.experience = sqlite3_column_int(stmt, 3);
  331. result.metal = sqlite3_column_int(stmt, 4);
  332. result.fuel = sqlite3_column_int(stmt, 5);
  333. result.guns = sqlite3_column_int(stmt, 6);
  334. result.armors = sqlite3_column_int(stmt, 7);
  335. result.shields = sqlite3_column_int(stmt, 8);
  336. result.armorpoints = sqlite3_column_int(stmt, 9);
  337. result.shieldpoints = sqlite3_column_int(stmt, 10);
  338. result.hitpoints = sqlite3_column_int(stmt, 11);
  339. result.shieldsup = sqlite3_column_int(stmt, 12);
  340. result.laston = sqlite3_column_int(stmt, 13);
  341. } else {
  342. dolog("E: Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
  343. sqlite3_finalize(stmt);
  344. sqlite3_close(db);
  345. md_exit(-1);
  346. }
  347. sqlite3_finalize(stmt);
  348. sqlite3_close(db);
  349. return result;
  350. }
  351. void update_player(user_inf data) {
  352. sqlite3 *db;
  353. sqlite3_stmt *stmt;
  354. char sqlbuffer[1024];
  355. int rc = sqlite3_open("spaceconstruct.db3", &db);
  356. if(rc) { // Did we do a successful open?
  357. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  358. sqlite3_close(db);
  359. md_exit(-1);
  360. }
  361. sqlite3_busy_timeout(db, 5000);
  362. // Bad, don't do this... opens to SQL injection!
  363. //snprintf(sqlbuffer, 1024, "UPDATE user SET nick = '%s', experience = %d, metal = %d, fuel = %d, guns = %d, armors = %d, shields = %d, armorpoints = %d, shieldpoints = %d, hitpoints = %d WHERE uid=%d;",
  364. // data.nick, data.experience, data.metal, data.fuel, data.guns, data.armors, data.shields, data.armorpoints, data.shieldpoints, data.hitpoints, data.uid);
  365. strcpy(sqlbuffer, "UPDATE user SET nick=?, experience=?, metal=?, fuel=?, guns=?, armors=?, shields=?, armorpoints=?, shieldpoints=?, hitpoints=?, shieldsup=?, laston=? WHERE uid=?;");
  366. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  367. // Bind All data values
  368. sqlite3_bind_text(stmt, 1, data.nick, strlen(data.nick), SQLITE_STATIC);
  369. sqlite3_bind_int(stmt, 2, data.experience);
  370. sqlite3_bind_int(stmt, 3, data.metal);
  371. sqlite3_bind_int(stmt, 4, data.fuel);
  372. sqlite3_bind_int(stmt, 5, data.guns);
  373. sqlite3_bind_int(stmt, 6, data.armors);
  374. sqlite3_bind_int(stmt, 7, data.shields);
  375. sqlite3_bind_int(stmt, 8, data.armorpoints);
  376. sqlite3_bind_int(stmt, 9, data.shieldpoints);
  377. sqlite3_bind_int(stmt, 10, data.hitpoints);
  378. sqlite3_bind_int(stmt, 11, data.shieldsup);
  379. sqlite3_bind_int(stmt, 12, data.laston);
  380. sqlite3_bind_int(stmt, 13, data.uid);
  381. // Execute
  382. rc = sqlite3_step(stmt);
  383. if(rc != SQLITE_DONE) {
  384. dolog("E: failed updating player=%d got error %s (%d)", data.uid, sqlite3_errmsg(db), rc);
  385. sqlite3_finalize(stmt);
  386. sqlite3_close(db);
  387. md_exit(-1);
  388. }
  389. sqlite3_finalize(stmt);
  390. sqlite3_close(db);
  391. }
  392. int create_player(user_inf data) {
  393. sqlite3 *db;
  394. sqlite3_stmt *stmt;
  395. char sqlbuffer[1024];
  396. int rc = sqlite3_open("spaceconstruct.db3", &db);
  397. if(rc) { // Did we do a successful open?
  398. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  399. sqlite3_close(db);
  400. md_exit(-1);
  401. }
  402. sqlite3_busy_timeout(db, 5000);
  403. strcpy(sqlbuffer, "INSERT INTO user (nick, real, experience, metal, fuel, guns, armors, shields, armorpoints, shieldpoints, hitpoints, shieldsup, laston) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
  404. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  405. sqlite3_bind_text(stmt, 1, data.nick, strlen(data.nick), SQLITE_STATIC);
  406. sqlite3_bind_text(stmt, 2, data.real, strlen(data.real), SQLITE_STATIC);
  407. sqlite3_bind_int(stmt, 3, data.experience);
  408. sqlite3_bind_int(stmt, 4, data.metal);
  409. sqlite3_bind_int(stmt, 5, data.fuel);
  410. sqlite3_bind_int(stmt, 6, data.guns);
  411. sqlite3_bind_int(stmt, 7, data.armors);
  412. sqlite3_bind_int(stmt, 8, data.shields);
  413. sqlite3_bind_int(stmt, 9, data.armorpoints);
  414. sqlite3_bind_int(stmt, 10, data.shieldpoints);
  415. sqlite3_bind_int(stmt, 11, data.hitpoints);
  416. sqlite3_bind_int(stmt, 12, data.shieldsup);
  417. sqlite3_bind_int(stmt, 13, data.laston);
  418. rc = sqlite3_step(stmt);
  419. if(rc != SQLITE_DONE) {
  420. dolog("E: failed inserting player=%d got error %s (%d)", data.nick, sqlite3_errmsg(db), rc);
  421. sqlite3_finalize(stmt);
  422. sqlite3_close(db);
  423. md_exit(-1);
  424. }
  425. sqlite3_finalize(stmt);
  426. sqlite3_close(db);
  427. return 1; // Good
  428. }
  429. void delete_player(int uuid) {
  430. sqlite3 *db;
  431. sqlite3_stmt *stmt;
  432. char sqlbuffer[256];
  433. int rc = sqlite3_open("spaceconstruct.db3", &db);
  434. if(rc) { // Did we do a successful open?
  435. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  436. sqlite3_close(db);
  437. md_exit(-1);
  438. }
  439. sqlite3_busy_timeout(db, 5000);
  440. strcpy(sqlbuffer, "DELETE FROM user WHERE uid=?;");
  441. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  442. sqlite3_bind_int(stmt, 1, uuid);
  443. rc = sqlite3_step(stmt);
  444. if (rc != SQLITE_DONE){
  445. dolog("E: Unable to locate user with id=%d got %s (%d)", uuid, sqlite3_errmsg(db), rc);
  446. sqlite3_finalize(stmt);
  447. sqlite3_close(db);
  448. md_exit(-1);
  449. }
  450. sqlite3_finalize(stmt);
  451. sqlite3_close(db);
  452. }
  453. int display_all_players() {
  454. sqlite3 *db;
  455. sqlite3_stmt *stmt;
  456. char sqlbuffer[1024];
  457. int rc = sqlite3_open("spaceconstruct.db3", &db);
  458. if(rc) { // Did we do a successful open?
  459. dolog("E: failed opening database %s", sqlite3_errmsg(db));
  460. sqlite3_close(db);
  461. md_exit(1);
  462. }
  463. sqlite3_busy_timeout(db, 5000);
  464. strcpy(sqlbuffer, "SELECT * from user ORDER BY experience DESC, nick DESC;");
  465. sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
  466. int total = 0;
  467. md_printf("`bright green`___ The Players ___\r\n");
  468. while(sqlite3_step(stmt) == SQLITE_ROW) {
  469. md_printf("`bright white` %s (%d)\r\n", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 3));
  470. total += 1;
  471. }
  472. md_printf("`green`There are a total of `bright green`%d`green` players\r\n", total);
  473. sqlite3_finalize(stmt);
  474. sqlite3_close(db);
  475. return total;
  476. }
  477. user_inf build_menu(user_inf my) {
  478. int done = 0;
  479. int didUp = 0;
  480. char ch;
  481. if(my.metal != 0) {
  482. while(!done) {
  483. md_clr_scr();
  484. md_sendfile("ansis/sc_build.ans", FALSE);
  485. md_printf("`bright yellow`You have %d metal\r\n", my.metal);
  486. ch = md_get_answer("GgAaSsCcRr\r");
  487. switch(tolower(ch)) {
  488. case 'g':
  489. if(my.metal >= 4) {
  490. my.metal -= 4;
  491. my.guns += 1;
  492. didUp = 1; // We need to save
  493. md_printf("`bright green`Added a new gun!\r\n`white`");
  494. paws();
  495. } else {
  496. md_printf("`bright red`You don't have the 4 metal for this!\r\n`white`");
  497. paws();
  498. }
  499. break;
  500. case 'a':
  501. if(my.metal >= 3) {
  502. my.metal -= 3;
  503. my.armors += 1;
  504. my.armorpoints += 4;
  505. didUp = 1; // We need to save
  506. md_printf("`bright green`Added a new armor!\r\n`white`");
  507. paws();
  508. } else {
  509. md_printf("`bright red`You don't have the 3 metal for this!\r\n`white`");
  510. paws();
  511. }
  512. break;
  513. case 's':
  514. if(my.metal >= 6) {
  515. my.metal -= 6;
  516. my.shields += 1;
  517. if(my.shieldsup == 0) {
  518. my.shieldpoints += 3;
  519. } else {
  520. my.shieldpoints += 1;
  521. }
  522. didUp = 1; // We need to save
  523. md_printf("`bright green`Added a new shield!\r\n`white`");
  524. paws();
  525. } else {
  526. md_printf("`bright red`You don't have the 6 metal for this!\r\n`white`");
  527. paws();
  528. }
  529. break;
  530. case '\r':
  531. case 'c':
  532. done = 1;
  533. break;
  534. case 'r':
  535. if(my.experience > 25) {
  536. if(my.metal > 10) {
  537. my.experience -= 25;
  538. my.metal -= 10;
  539. my.fuel += 5;
  540. didUp = 1;
  541. md_printf("`bright green`Made 5 fuel using 25 experience and 10 metal!\r\n");
  542. paws();
  543. } else {
  544. md_printf("`bright red`You need 25 experience and 10 metal to make fuel!\r\n");
  545. paws();
  546. }
  547. } else {
  548. md_printf("`bright red`You need 25 experience and 10 metal to make fuel!\r\n");
  549. paws();
  550. }
  551. break;
  552. }
  553. }
  554. if(didUp) {
  555. update_player(my);
  556. }
  557. return my;
  558. } else {
  559. return my;
  560. }
  561. }
  562. user_inf combat_menu(user_inf my, int targ_hp, int targ_dmg) {
  563. int done = 0;
  564. int playerWon = 0; // Did the player win? (1 = Yes, 2 = No, 3 = Flee)
  565. char ch;
  566. int armorRep = 0; // Can armor be repaired?
  567. int temp = 0;
  568. int temp1 = 0;
  569. int temp2 = 0;
  570. int flee = 0;
  571. int fire = 0;
  572. int regen = 0;
  573. while(!done) {
  574. md_printf("`bright red`Target ship has %d hitpoints left\r\n", targ_hp);
  575. // Proccess Shields comming back online
  576. if(my.shieldsup != 0) {
  577. my.shieldsup -= 1;
  578. }
  579. // Proccess Crew repairing hull
  580. if(my.hitpoints < (my.guns + my.armors + my.shields + 2)) {
  581. regen = rand() % 100;
  582. if(regen >= 95) {
  583. my.hitpoints += 1;
  584. if(my.hitpoints > (my.guns + my.armors + my.shields + 2)) {
  585. my.hitpoints = (my.guns + my.armors + my.shields + 2);
  586. }
  587. md_printf("`bright green`Your crew repaired your hull for 1 point!\r\n");
  588. }
  589. }
  590. if(my.shieldsup == 0) {
  591. // Proccess Shield Regeneration
  592. if(my.shieldpoints < (my.shields * 3)) {
  593. regen = rand() % 100;
  594. if(regen >= 75) {
  595. my.shieldpoints += (my.shields + 1);
  596. if(my.shieldpoints > (my.shields * 3)) {
  597. my.shieldpoints = (my.shields * 3);
  598. }
  599. md_printf("`bright green`Your shields regenerate %d points!\r\n", my.shields);
  600. }
  601. }
  602. md_printf("`bright yellow`Your ship has %d armor, `bright blue`%d`bright yellow` shields and %d hitpoints left:\r\n", my.armorpoints, my.shieldpoints, my.hitpoints);
  603. } else {
  604. // Shields should still regenerate some
  605. if(my.shieldpoints < (my.shields * 3)) {
  606. regen = rand() % 100;
  607. if(regen >= 75) {
  608. my.shieldpoints += 1;
  609. if(my.shieldpoints > (my.shields * 3)) {
  610. my.shieldpoints = (my.shields * 3);
  611. }
  612. md_printf("`bright green`Your shields regenerate 1 points!\r\n");
  613. }
  614. }
  615. md_printf("`bright yellow`Your ship has %d armor, `blue`%d`bright yellow` shields and %d hitpoints left:\r\n", my.armorpoints, my.shieldpoints, my.hitpoints);
  616. }
  617. // Forming Combat Menu
  618. md_printf("`bright black`(`bright white`A`bright black`)`white`ttack\r\n");
  619. if(my.armorpoints < (my.armors * 4)) {
  620. md_printf("`bright black`(`bright white`R`bright black`)`white`epair Armor\r\n");
  621. armorRep = 1; // Yes we can repair armor
  622. }
  623. md_printf("`bright black`(`bright white`F`bright black`)`white`lee\r\n");
  624. // Proccess User action
  625. ch = md_get_answer("AaRrFf\r");
  626. switch(tolower(ch)) {
  627. case '\r':
  628. case 'a':
  629. fire = rand() % 100;
  630. if(fire >= 50) {
  631. targ_hp -= my.guns;
  632. if(targ_hp <= 0) {
  633. md_printf("`bright red`Target Destroyed!\r\n");
  634. done = 1;
  635. playerWon = 1;
  636. } else {
  637. md_printf("`bright red`Target took %d damage!\r\n", my.guns);
  638. }
  639. } else {
  640. md_printf("`white`Target Missed!\r\n");
  641. }
  642. break;
  643. case 'r':
  644. if(armorRep) {
  645. if(my.metal > 0) {
  646. my.metal -= 1;
  647. my.armorpoints += 2;
  648. if(my.armorpoints < (my.armors * 4)) {
  649. my.armorpoints = (my.armors * 4);
  650. }
  651. md_printf("`bright green`Repaired 2 Armor points!\r\n");
  652. } else {
  653. md_printf("`bright red`You need metal inorder to repair!\r\n");
  654. }
  655. } else {
  656. md_printf("`bright yellow`You don't seem to need repairs!\r\n");
  657. }
  658. break;
  659. case 'f':
  660. flee = rand() % 100;
  661. if(flee >= 82) {
  662. playerWon = 3;
  663. done = 1;
  664. md_printf("`bright green`You ran away!\r\n");
  665. } else {
  666. md_printf("`bright red`Target found us!\r\n");
  667. }
  668. break;
  669. } // If Enemy is alive proccess incomming attack
  670. if(targ_hp > 0) {
  671. temp = targ_dmg;
  672. fire = rand() % 100;
  673. if(fire >= 40) {
  674. // Shields
  675. if(my.shieldsup == 0 && my.shieldpoints != 0) {
  676. if(temp >= my.shieldpoints) {
  677. // Damage will lower shields!
  678. my.shieldsup = (my.shields + 2);
  679. temp -= my.shieldpoints;
  680. my.shieldpoints = 0;
  681. md_printf("`bright blue`Shields absorbed %d damage and went offline!\r\n`white`", targ_dmg);
  682. } else {
  683. // Damage to shields but not lower them
  684. temp1 = temp;
  685. temp -= my.shieldpoints;
  686. my.shieldpoints -= temp1;
  687. temp1 = 0;
  688. md_printf("`bright blue`Shields absorbed %d damage!\r\n`white`", targ_dmg);
  689. }
  690. }
  691. // shields now armor
  692. if(temp > 0) {
  693. if(my.armorpoints != 0) {
  694. // We do have armor
  695. if(temp >= my.armorpoints) {
  696. // Damage will destroy armor!
  697. temp2 = temp;
  698. temp -= my.armorpoints;
  699. my.armorpoints = 0;
  700. md_printf("`yellow`Armor took %d damage and broke!\r\n`white`", temp2);
  701. } else {
  702. // Nope armor is good
  703. temp1 = temp;
  704. temp2 = temp;
  705. temp -= my.armorpoints;
  706. my.armorpoints -= temp1;
  707. temp1 = 0;
  708. md_printf("`bright yellow`Armor took %d damage!\r\n`white`", temp2);
  709. }
  710. }
  711. }
  712. // armor now hull
  713. if(temp > 0) {
  714. if(temp < my.hitpoints) {
  715. temp1 = temp;
  716. temp -= my.hitpoints; // should be 0
  717. my.hitpoints -= temp1;
  718. md_printf("`bright red`Hull took %d damage!\r\n", temp1);
  719. } else {
  720. playerWon = 2; // Nope the target did us in
  721. done = 1;
  722. my.hitpoints = 0;
  723. md_printf("`red`Hull took %d damage and broke!\r\n", temp);
  724. }
  725. }
  726. }
  727. // firing
  728. }
  729. // Winning move?
  730. if(playerWon) {
  731. if(playerWon == 1) {
  732. // Player won
  733. my.experience += (my.guns + my.hitpoints + my.armorpoints + my.shieldpoints);
  734. my.metal += (my.guns + my.hitpoints);
  735. my.fuel -= 2;
  736. if(my.fuel < 0) {
  737. my.fuel = 0;
  738. }
  739. md_printf("`bright green`Congrats on your victory!\r\n`white`");
  740. update_player(my);
  741. return my;
  742. } else if(playerWon == 2) {
  743. // Target won
  744. my.experience -= (targ_hp + my.guns);
  745. my.metal -= (targ_hp + my.guns);
  746. my.fuel -= 3;
  747. if(my.experience < 0) {
  748. my.experience = 0;
  749. }
  750. if(my.metal < 0) {
  751. my.metal = 0;
  752. }
  753. if(my.fuel < 0) {
  754. my.fuel = 0;
  755. }
  756. md_printf("`bright red`Sorry for your loss! (We rebuild your ship)\r\n");
  757. my.hitpoints = 4;
  758. my.armorpoints = 4;
  759. my.shieldpoints = 0;
  760. my.shieldsup = 0;
  761. my.armors = 1;
  762. my.shields = 0;
  763. my.guns = 1;
  764. update_player(my);
  765. return my;
  766. } else if(playerWon == 3) {
  767. // Chicken!
  768. my.fuel -= 1;
  769. update_player(my);
  770. return my;
  771. }
  772. }
  773. }
  774. }
  775. void play_game() {
  776. int done = 0;
  777. char ch;
  778. char ch1[256];
  779. user_inf myself;
  780. int reset = 0;
  781. int doRefuel = 0;
  782. int pirate_encounter = 0;
  783. int pirate_hp = 0; // Reused for asteroid
  784. int pirate_dmg = 0; // Reused for asteroid
  785. int asteroid = 0; // 1 means yes we need to remove damage... completely.
  786. int me = locate_player(mdcontrol.user_firstname, mdcontrol.user_lastname);
  787. if(me != 0) {
  788. myself = load_player(me);
  789. dT age = formDate(compareDate(myself.laston));
  790. // Process player inactivity
  791. if(age.age >= 180) { // 6 Months or 1/2 a Year
  792. me = 0;
  793. reset = 1;
  794. }
  795. // Processing fuel regeneration
  796. if(age.age != 0) {
  797. doRefuel = 1;
  798. }
  799. }
  800. if(me != 0) {
  801. md_printf("`bright white`Welcome back `bright green`%s\r\n", myself.nick);
  802. dT age = formDate(compareDate(myself.laston));
  803. myself.laston = dateStamp();
  804. if(age.day != 0 || age.month != 0 || age.year != 0) {
  805. md_printf("`bright yellow`Haven't seen you for");
  806. }
  807. if(age.day != 0) {
  808. if(age.day > 1) {
  809. md_printf(" %d days", age.day);
  810. } else {
  811. md_printf(" %d day", age.day);
  812. }
  813. }
  814. if(age.month != 0) {
  815. if(age.month > 1) {
  816. md_printf(" %d months", age.month);
  817. } else {
  818. md_printf(" %d month", age.month);
  819. }
  820. }
  821. if(age.year != 0) {
  822. if(age.year > 1) {
  823. md_printf(" %d years", age.year);
  824. } else {
  825. md_printf(" %d year", age.year);
  826. }
  827. }
  828. if(age.day != 0 || age.month != 0 || age.year != 0) {
  829. md_printf("\r\n");
  830. }
  831. if(doRefuel) {
  832. myself.fuel += (age.age * 2);
  833. md_printf("`bright green`You now have %d fuel\r\n", myself.fuel);
  834. }
  835. update_player(myself);
  836. } else {
  837. md_printf("`bright white`You look new here. (Hit Enter to use your alias)\r\n");
  838. while(done == 0) {
  839. md_printf("`bright yellow`What's your name: ");
  840. md_getstring(ch1, 26, 32, 126);
  841. md_printf("\r\n");
  842. if(strlen(ch1) > 0) {
  843. //md_getstring(ch1, 256, 32, 126);
  844. strcpy(myself.nick, ch1);
  845. } else {
  846. strcpy(myself.nick, mdcontrol.user_alias);
  847. }
  848. md_printf("`bright white`Are you sure you want to be called `bright green`%s\r\n");
  849. done = yesNo();
  850. if(done) {
  851. if(reset) { // So we are reseting the user, this wipes all values and uses update instead of create
  852. myself.experience = 0;
  853. myself.metal = 0;
  854. myself.fuel = 10;
  855. myself.guns = 1;
  856. myself.armors = 1;
  857. myself.shields = 0;
  858. myself.armorpoints = 4;
  859. myself.shieldpoints = 0;
  860. myself.hitpoints = 4;
  861. myself.shieldsup = 0;
  862. myself.laston = dateStamp();
  863. update_player(myself);
  864. } else { // Brand new user
  865. char name[256];
  866. strcpy(name, "");
  867. strcat(name, mdcontrol.user_firstname);
  868. strcat(name, " ");
  869. strcat(name, mdcontrol.user_lastname);
  870. strcpy(myself.real, name);
  871. myself.experience = 0;
  872. myself.metal = 0;
  873. myself.fuel = 10;
  874. myself.guns = 1;
  875. myself.armors = 1;
  876. myself.shields = 0;
  877. myself.armorpoints = 4;
  878. myself.shieldpoints = 0;
  879. myself.hitpoints = 4;
  880. myself.shieldsup = 0;
  881. myself.laston = dateStamp();
  882. create_player(myself);
  883. }
  884. }
  885. }
  886. done = 0; // Reset this for our next loop.
  887. }
  888. // Ok user is here now lets ask what they want to do... build/attack
  889. md_clr_scr();
  890. while(!done) {
  891. md_printf("`bright yellow`You have %d fuel left for today,\r\n", myself.fuel);
  892. if(myself.shieldsup == 0) {
  893. md_printf("`bright yellow`Your ship has %d armor, `bright blue`%d`bright yellow` shields and %d hitpoints left:\r\n", myself.armorpoints, myself.shieldpoints, myself.hitpoints);
  894. } else {
  895. md_printf("`bright yellow`Your ship has %d armor, `blue`%d`bright yellow` shields and %d hitpoints left:\r\n", myself.armorpoints, myself.shieldpoints, myself.hitpoints);
  896. }
  897. if(myself.fuel != 0) {
  898. md_sendfile("ansis/sc_gameh.ans", FALSE);
  899. ch = md_get_answer("SsHhBbQqRr\r");
  900. } else {
  901. md_sendfile("ansis/sc_gamel.ans", FALSE);
  902. ch = md_get_answer("BbQqRr\r");
  903. }
  904. md_clr_scr();
  905. switch(tolower(ch)) {
  906. case '\r':
  907. case 's':
  908. if(myself.fuel != 0) {
  909. pirate_encounter = rand() % 100;
  910. if(pirate_encounter >= 75) { // 25% chance to encounter a Pirate
  911. md_printf("`bright yellow`You encounter a `bright red`Pirate\r\n`white`");
  912. asteroid = 0;
  913. } else { // 75% chance to encounter a Asteroid
  914. md_printf("`bright yellow`You encounter a `bright red`Asteroid\r\n`white`");
  915. asteroid = 1;
  916. }
  917. if(asteroid == 0) {
  918. pirate_hp = (myself.hitpoints + 1) + (rand() % 4);
  919. if(myself.armors >= 2 && myself.shields >= 1) {
  920. pirate_dmg = (myself.guns + 1);
  921. } else {
  922. pirate_dmg = myself.guns;
  923. }
  924. // Send off to combat menu
  925. myself = combat_menu(myself, pirate_hp, pirate_dmg);
  926. } else {
  927. pirate_hp = (myself.hitpoints + 1) + (rand() % 10);
  928. // Send off to combat menu
  929. myself = combat_menu(myself, pirate_hp, 0);
  930. }
  931. }
  932. break;
  933. case 'h':
  934. md_printf("Hunt\r\n");
  935. break;
  936. case 'b':
  937. if(myself.metal != 0) {
  938. // Send off to build menu
  939. md_clr_scr();
  940. //md_sendfile("ansis/sc_build.ans", FALSE);
  941. myself = build_menu(myself);
  942. } else {
  943. md_printf("`bright white`I am sorry you have no metal go fight a `bright red`Asteroid`bright white` or `bright red`Pirate`bright white`.`white`\r\n");
  944. }
  945. break;
  946. case 'q':
  947. done = 1;
  948. break;
  949. case 'r':
  950. if(myself.armorpoints < (myself.armors * 4)) {
  951. if(myself.metal > 0) {
  952. myself.metal -= 1;
  953. myself.armorpoints += 3;
  954. if(myself.armorpoints > (myself.armors * 4)) {
  955. myself.armorpoints = (myself.armors * 4);
  956. }
  957. md_printf("`bright green`Repaired 3 Armor Points!\r\n`white`");
  958. } else {
  959. md_printf("`bright red`You need metal inorder to repair!\r\n`white`");
  960. }
  961. } else {
  962. md_printf("`bright yellow`You don't seem to need repairs!\r\n`white`");
  963. }
  964. break;
  965. }
  966. }
  967. }
  968. void main_menu() {
  969. // Main menu for once things are done being initalized
  970. int done = 0;
  971. char ch;
  972. // Check Lock
  973. inuse = check_lock();
  974. // 0 means we got the lock first, 1 means someone else has it already!
  975. // Lock game
  976. if (inuse == 0) {
  977. grab_lock();
  978. }
  979. while(!done) {
  980. md_clr_scr();
  981. md_sendfile("ansis/sc_mainl.ans", TRUE);
  982. ch = md_get_answer("PpLlVvQq\r");
  983. md_clr_scr();
  984. switch(tolower(ch)){
  985. case 'q':
  986. // Quit
  987. md_printf("`bright white`Farewell!\r\n");
  988. // Hmm sleep?
  989. done = 1;
  990. break;
  991. case '\r':
  992. case 'p':
  993. // Play Game (Default if the player just hits enter)
  994. if (inuse == 1) {
  995. md_printf("`bright red`Game in use!\r\n");
  996. paws();
  997. } else {
  998. //md_printf("`bright white`Play\r\n");
  999. play_game();
  1000. }
  1001. break;
  1002. case 'l':
  1003. // List Players in the game
  1004. if(display_all_players() == 0) {
  1005. md_printf("`bright red`There are no players!\r\n");
  1006. }
  1007. paws();
  1008. break;
  1009. case 'v':
  1010. // Version
  1011. md_clr_scr();
  1012. md_printf("`bright yellow`|--------------------------|\r\n");
  1013. md_printf("`bright yellow`| Space Construct v%d.%d-%s |\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_TYPE);
  1014. md_printf("`bright yellow`|--------------------------|\r\n");
  1015. paws();
  1016. break;
  1017. }
  1018. }
  1019. // Unlock game
  1020. if (inuse == 0) {
  1021. rel_lock();
  1022. }
  1023. }
  1024. int main(int argc, char **argv) {
  1025. int socket;
  1026. if (argc < 2) { // Not enough arguments
  1027. fprintf(stderr, "usage:\n%s (DROPFILE [SOCKET])\r\n", argv[0]);
  1028. dolog("E %s (DROPFILE [SOCKET])", argv[0]);
  1029. return 0;
  1030. }
  1031. if (argc > 2) { // If socket is found use socket
  1032. if (argv[2][0] >= '0' && argv[2][0] <= '9') {
  1033. socket = strtol(argv[2], NULL, 10);
  1034. } else {
  1035. socket = -1;
  1036. }
  1037. } else {
  1038. socket = -1;
  1039. }
  1040. // Initiate MagiDoor
  1041. md_init(argv[1], socket);
  1042. md_clr_scr();
  1043. // Debug System
  1044. if (debug) {
  1045. dolog("=== Debug ===");
  1046. md_printf("`white`=== Debug ===\r\n");
  1047. md_printf("DropFile...\r\n");
  1048. log_drop();
  1049. int test = check_lock();
  1050. if (test == 0) {
  1051. md_printf("Lock is: Avalible\r\n");
  1052. md_printf("Database...\r\n");
  1053. db_test();
  1054. } else {
  1055. md_printf("Lock is: Taken\r\n");
  1056. md_printf("CANCELED Database Dump\r\n");
  1057. }
  1058. paws();
  1059. md_clr_scr();
  1060. } else {
  1061. dolog("--- Debug ---");
  1062. }
  1063. // Main Menu
  1064. main_menu();
  1065. // Goodbye
  1066. md_exit(0);
  1067. }