families.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Beginning the organization of families
  3. NOTE: I want SQL saving when data changes since family data won't change frequently...
  4. Likewise with family items: we will load the item that is required when it is required and
  5. update when there is a new item removed or added.
  6. THINGS PENDING (add more here):
  7. 1. Leveling system for families -- how will they gain 'XP' to level?
  8. THINGS IN PROGRESS:
  9. Octet: Inventories
  10. This file should only contain saving, giving of strikes, or anything that may alter or fetchh fiSID, fiName, fiOwner, fiStrikes, fiLevel
  11. or anything else that should belong in FamilyInfo that is considered a CORE component to families.
  12. Example functions may include GiveFamilyStrike(fid), RemoveFamilyStrike(fid), AddFamily(fname, fowner), RemoveFamily(fid)
  13. */
  14. // Definitions
  15. #define MAX_FAMILIES 12
  16. #define MAX_FAMILY_SKINS 6
  17. #define MAX_FAMILY_NAME 30
  18. enum FamiliesInfo {
  19. fiSID,
  20. bool:fiTaken,
  21. fiName[MAX_FAMILY_NAME+1],
  22. fiOwner,
  23. fiMotd,
  24. fiStrikes,
  25. fiLevel,
  26. fiSkins[MAX_FAMILY_SKINS+1]
  27. }
  28. new Families[MAX_FAMILIES][FamiliesInfo];
  29. /*
  30. Function prototypes
  31. */
  32. //public
  33. forward FamilyLog(text[]);
  34. forward CreateFamily(ownerid, name[]);
  35. //protected
  36. forward OnLoadFamily();
  37. //inc/inventories.inc
  38. //forward FetchFamilyInventory(fid);
  39. /*
  40. Includes/Extensions
  41. */
  42. //#include "inc/families/inc/inventories.inc"
  43. /*
  44. Logging
  45. */
  46. static GetTimeString() {
  47. new string[10];
  48. new h, m, s;
  49. gettime(h, m, s);
  50. h = FixHour(h);
  51. format(string, sizeof string, "%d:%d:%d", h, m, s);
  52. return string;
  53. }
  54. public FamilyLog(text[]) {
  55. new File:logfile;
  56. logfile = fopen("../logs/FamilyLog.log", io_append);
  57. new string[144];
  58. format(string, sizeof string, "<FLOG> %s: %s", GetTimeString(), text);
  59. fwrite(logfile, string);
  60. fclose(logfile);
  61. return 1;
  62. }
  63. /*
  64. Loading
  65. */
  66. Hook:Families_OnGameModeInit() {
  67. RequestFamilyInfo();
  68. return 1;
  69. }
  70. //Note, there's no saving function since data is saved when it's modified.
  71. RequestFamilyInfo() {
  72. inline LoadFamilyInfo() {
  73. if(cache_num_rows() < 1) {
  74. FamilyLog("There are no families in the database.");
  75. return;
  76. }
  77. for(new i; i < cache_num_rows(); i++) {
  78. // % Loop invariant: for all rows till i, the data has been imported into an array, Families
  79. if(i == MAX_FAMILIES) {
  80. FamilyLog("max families (12) has been reached.");
  81. break;
  82. }
  83. Families[i][fiSID] = cache_get_field_content_int(i, "ID");
  84. cache_get_field_content(i, "Name", Families[i][fiName]);
  85. Families[i][fiOwner] = cache_get_field_content_int(i, "Owner");
  86. cache_get_field_content(i, "Motd", Families[i][fiMotd]);
  87. Families[i][fiStrikes] = cache_get_field_content_int(i, "Strikes");
  88. Families[i][fiLevel] = cache_get_field_content_int(i, "Level");
  89. Families[i][fiTaken] = true;
  90. }
  91. }
  92. mysql_pquery_inline(sqlGameConnection, "SELECT * FROM Families", using inline LoadFamilyInfo, "");
  93. return;
  94. }
  95. public CreateFamily(ownerid, name[]) {
  96. //get free slot
  97. new slot = MAX_FAMILIES;
  98. for(new i; i < MAX_FAMILIES; i++) {
  99. if(!Families[i][fiTaken]) {
  100. slot = i;
  101. break;
  102. }
  103. }
  104. //max families?
  105. if(slot == MAX_FAMILIES) {
  106. return false;
  107. }
  108. Families[slot][fiOwner] = PlayerInfo[ownerid][pID];
  109. format(Families[slot][fiName], MAX_FAMILY_NAME, "%s", name);
  110. Families[slot][fiTaken] = true;
  111. new query[200];
  112. mysql_format(sqlGameConnection, query, sizeof query, "INSERT INTO Families SET Owner = %d, Name = '%e'", Families[slot][fiOwner], name);
  113. mysql_pquery(sqlGameConnection, query);
  114. return true;
  115. }
  116. /*
  117. CORE COMMANDS: only important commands here
  118. */
  119. CMD:createfamily(playerid, params[]) {
  120. //get params
  121. new name[MAX_FAMILY_NAME+1], owner;
  122. if(sscanf(params, "us[MAX_FAMILY_NAME+1]", owner, name)) {
  123. return SendClientMessage(playerid, 0, "{449b41}Usage:{FFFFFF}/createfamily [partOfName/playerID] [familyName]");
  124. }
  125. if(!IsPlayerConnected(owner)) {
  126. return SendClientMessage(playerid, COLOR_LIGHTRED, "Error: That player is not connected.");
  127. }
  128. if(CreateFamily(owner, name)) {
  129. return SendClientMessage(playerid, COLOR_LIGHTGREEN, "Success: The family has been created.");
  130. }
  131. SendClientMessage(playerid, COLOR_LIGHTRED, "Error: I couldn't create the family. Are there free slots?");
  132. return 1;
  133. }