Guest User

Tribes / Blockland Done 2

a guest
Jul 2nd, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. //PACKAGE START -----------
  2.  
  3. package tribeHook
  4. {
  5.     function gameConnection::autoAdminCheck(%this)
  6.     {
  7.         %p = parent::autoAdminCheck(%this);
  8.        
  9.         if(!tribeContainer.initialized)
  10.         {
  11.             if(discoverFile("config/server/Tribes/TribeContainer/tribeContainer.cs"))
  12.             {
  13.                 //Tribe container file exists, let's load it up.
  14.                 exec("config/server/Tribes/TribeContainer/tribeContainer.cs");
  15.                 echo("Initialized the tribe container.");
  16.             }
  17.             else
  18.             {
  19.                 //First time running this script, we need to create and save the new tribecontainer
  20.                 new scriptGroup(tribeContainer);
  21.                 tribeContainer.save("config/server/Tribes/TribeContainer/tribeContainer.cs");
  22.                 echo("First time initializing the tribe container: Success.");
  23.             }
  24.             tribeContainer.initialized = true;
  25.         }
  26.        
  27.         %path = "config/server/Tribes/Users/" @ %this.bl_id @ ".txt";
  28.         if(discoverFile(%path))
  29.         {
  30.             %f = new fileObject();
  31.             %f.openForRead(%path);
  32.             %tribe = %f.readLine();
  33.             //To deal with user name changes or whatever..
  34.             %userName = %f.readLine();
  35.             %f.close();
  36.             %f.delete();
  37.             %this.tribe = tribeContainer.getTribeByName(%tribe);
  38.             //Did we get an invalid tribe
  39.             if(!%this.tribe)
  40.             {
  41.                 error("Tribe invalid! Deleting user data associated with that Tribe.");
  42.                 %this.tribe = "";
  43.                 fileDelete(%path);
  44.             }
  45.             else
  46.             {
  47.                 %this.tribe.add(%this);
  48.                 echo("Added user " @ %this.name @ " to Tribe " @ %this.getTribeName() @ " successfully.");
  49.             }
  50.         }
  51.         return %p;
  52.     }
  53. }; activatePackage(tribeHook);
  54.  
  55. //PACKAGE END -----------
  56.  
  57.  
  58. //TRIBECONTAINER FUNCTIONS START -----------
  59.  
  60. //Assuming the host will only be able
  61. //to create new tribes
  62. function tribeContainer::newTribe(%this,%name)
  63. {
  64.     %c = %this.getCount();
  65.     for(%i=0;%i<%c;%i++)
  66.     {
  67.         %tribeObj = %this.getObject(%i);
  68.         if(%tribeObj.name $= %name)
  69.         {
  70.             //Already a tribe with that name.. end function
  71.             return false;
  72.         }
  73.     }
  74.    
  75.     %newTribe = new simSet()
  76.     {
  77.         name = %name;
  78.         //owner = ??
  79.         //other data you want to store.
  80.     };
  81.    
  82.     %this.add(%newTribe);
  83.     %this.save("config/server/Tribes/TribeContainer/tribeContainer.cs");
  84.     echo("New tribe " @ %name @ " added to Tribe container.");
  85.     return %newTribe;
  86. }
  87.  
  88. //Using this function will throw errors if users are still
  89. //Associated to this tribe, so make sure the tribe is empty.
  90. function tribeContainer::removeTribe(%this,%name)
  91. {
  92.     %c = %this.getCount();
  93.     for(%i=0;%i<%c;%i++)
  94.     {
  95.         %tribeObj = %this.getObject(%i);
  96.         if(%tribeObj.name $= %name)
  97.         {
  98.             %tribeObj.delete();
  99.             echo("Successfully deleted Tribe " @ %name);
  100.             %this.save("config/server/Tribes/TribeContainer/tribeContainer.cs");
  101.             return true;
  102.         }
  103.     }
  104.     error("Could not find a Tribe object with the name " @ %name);
  105.     return false;
  106. }
  107.  
  108. function tribeContainer::getTribeByName(%this,%tribeName)
  109. {
  110.     %c = %this.getCount();
  111.     for(%i=0;%i<%c;%i++)
  112.     {
  113.         %tribeObj = %this.getObject(%i);
  114.         if(%tribeObj.name $= %tribeName)
  115.         {
  116.             return %tribeObj;
  117.         }
  118.     }
  119.     return false;
  120. }
  121.  
  122. //TRIBECONTAINER FUNCTIONS END -----------
  123.  
  124.  
  125. //GAMECONNECTION FUNCTIONS START -----------
  126.  
  127. function gameConnection::addToTribe(%this,%name)
  128. {
  129.     //Tribe simset..
  130.     %ss = tribeContainer.getTribeByName(%name);
  131.     //No valid simset object supplied
  132.     if(!isObject(%ss))
  133.     {
  134.         error("We need a valid simset object for Tribe named " @ %name @ ".");
  135.         return false;
  136.     }
  137.     //User has a tribe already..
  138.     if(isObject(%this.getTribe()))
  139.     {
  140.         error("This user is already in a tribe.");
  141.         return false;
  142.     }
  143.     %this.tribe = %ss;
  144.     //Save tribe info for a user to load upon joining the server
  145.     %f = new fileObject();
  146.     %f.openForWrite("config/server/Tribes/Users/" @ %this.bl_id @ ".txt");
  147.     %f.writeLine(%ss.name);
  148.     %f.writeLine(%this.name);
  149.     %f.close();
  150.     %f.delete();
  151.     echo(%this.name @ " added to " @ %name @ " (" @ %ss @ ") successfully.");
  152.     %ss.add(%this);
  153.     return true;
  154. }
  155.  
  156. function gameConnection::removeFromTribe(%this)
  157. {
  158.     if(isObject(%this.tribe))
  159.     {
  160.         %tribe = %this.getTribe();
  161.         %tribeName = %this.getTribeName();
  162.         %tribe.remove(%this);
  163.         %this.tribe = "";
  164.         %res = fileDelete("config/server/Tribes/Users/" @ %this.bl_id @ ".txt");
  165.         if(!%res)
  166.         {
  167.             error("No tribe file for " @ %this.name);
  168.             return false;
  169.         }
  170.         else
  171.         {
  172.             echo("Removed " @ %this.name @ " from tribe " @ %tribeName);
  173.             return true;
  174.         }
  175.     }
  176.     else
  177.     {
  178.         error("This user is not in a tribe.");
  179.         return false;
  180.     }
  181. }
  182.  
  183. function gameConnection::getTribe(%this)
  184. {
  185.     //return the tribe however you're storing it
  186.     //I'm just gunna assume it'll be tagged to the client
  187.     return %this.tribe;
  188. }
  189.  
  190. function gameConnection::getTribeName(%this)
  191. {
  192.     return %this.tribe.name;
  193. }
  194.  
  195. //GAMECONNECTION FUNCTIONS END
Advertisement
Add Comment
Please, Sign In to add comment