Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. using GTANetworkAPI;
  2. using UW.Data.Models.Players;
  3. using UW.Data.Repositories;
  4. using Newtonsoft.Json;
  5. using System.Collections.Generic;
  6. using UW.Data.Models.Factions;
  7. using System.Linq;
  8. using UW.Global;
  9. using UW.Misc;
  10. using UW.Player;
  11. using TestData = UW.Global.TestDataFunctions;
  12. using Microsoft.EntityFrameworkCore.Storage;
  13. using Microsoft.EntityFrameworkCore.Infrastructure;
  14. using Microsoft.EntityFrameworkCore;
  15. using Newtonsoft.Json.Linq;
  16. using Microsoft.CSharp.RuntimeBinder;
  17. using UW.Business;
  18. using System.Threading.Tasks;
  19. using UW.Warehouse;
  20. using System.Text;
  21.  
  22. namespace GTAUnderworld
  23. {
  24. public class Main : Script
  25. {
  26. [Command("setadminlevel")]
  27. public void SetAdminLevel(Client sender, int level)
  28. {
  29. if(level > -1 && level < 8)
  30. {
  31. var senderPlayer = GlobalFunctions.GetPlayerModelFromClient(sender);
  32. if (senderPlayer == null) return;
  33.  
  34. senderPlayer.AdminLevel = level;
  35. var repo = new PlayerRepository();
  36. repo.Update(senderPlayer);
  37.  
  38. ClientFunctions.SetClientAdminLevel(sender, level);
  39. ClientFunctions.SetClientAdminRank(sender, GlobalFunctions.GetAdminNameFromLevel(level));
  40. GlobalFunctions.AdminMessage(sender, "You have been promoted to "+ GlobalFunctions.GetAdminNameFromLevel(level) + ".");
  41. }
  42. else {
  43. GlobalFunctions.ErrorMessage(sender, "Available levels: 0-7");
  44. }
  45. }
  46.  
  47. [ServerEvent(Event.ResourceStart)]
  48. public void OnResourceStart()
  49. {
  50. NAPI.Server.SetGlobalServerChat(false);
  51.  
  52. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  53.  
  54. string host = NAPI.Resource.GetSetting<string>(this, "host");
  55. string user = NAPI.Resource.GetSetting<string>(this, "username");
  56. string pass = NAPI.Resource.GetSetting<string>(this, "password");
  57. string db = NAPI.Resource.GetSetting<string>(this, "database");
  58.  
  59. UW.Data.ContextFactory.SetConnectionString(host,db,user,pass);
  60.  
  61.  
  62. // using (var context = new UW.Data.UnderworldContext())
  63. // {
  64. // Creates the database if not exists
  65. bool dbExists = (UW.Data.ContextFactory.Instance.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists();
  66.  
  67. if (!dbExists)
  68. {
  69. NAPI.Util.ConsoleOutput("Database didn't exist before, it does now.");
  70. }
  71. else
  72. {
  73. NAPI.Util.ConsoleOutput("Database exists.");
  74.  
  75. }
  76.  
  77. UW.Data.ContextFactory.Instance.Database.Migrate();
  78. // context.Database.Migrate();
  79.  
  80. // context.Database.EnsureCreated();
  81. LoadDataFunctions.InsertInteriorsInDbIfTableEmpty();
  82. TestData.LoadTestData();
  83. // }
  84.  
  85. LoadDataFunctions.LoadInteriorIPLs();
  86. LoadDataFunctions.LoadAllHouses();
  87. LoadDataFunctions.LoadAllATMs();
  88. LoadDataFunctions.LoadTurfs();
  89. LoadDataFunctions.LoadAllLabels();
  90. LoadDataFunctions.LoadAllTeleports();
  91. LoadDataFunctions.SpawnAllVehicles();
  92. BusinessFunctions.LoadAllBusinesses();
  93. WarehouseFunctions.LoadAllWarehouses();
  94.  
  95. }
  96.  
  97.  
  98. [ServerEvent(Event.PlayerConnected)]
  99. public void OnPlayerConnected(Client sender)
  100. {
  101. if (!GlobalData.HasAnyoneLoggedIn)
  102. {
  103. GlobalData.HasAnyoneLoggedIn = true;
  104. foreach (var atm in Constants.ATM_LIST)
  105. {
  106. //set streetname of ATM todo.
  107. }
  108. }
  109.  
  110. int count = 0;
  111.  
  112. while (GlobalData.ServerIDs.ContainsKey(count))
  113. {
  114. count++;
  115. }
  116.  
  117. ClientFunctions.SetClientPlayerServerId(sender, count);
  118.  
  119. GlobalData.ServerIDs.Add(count, sender);
  120.  
  121. CharacterFunctions.InitializePlayerData(sender);
  122. sender.SendChatMessage("You are now connected. Log into your account. (Logged in with ID " + count + "). Use /testlogin <accountName> to automatically log into your character.");
  123.  
  124. //To be improved:
  125. var repo = new TurfRepository();
  126. var turfs = repo.GetAll();
  127.  
  128. Task.Run(() =>
  129. {
  130. sender.TriggerEvent("turf:playerConnected", NAPI.Util.ToJson(turfs));
  131. });
  132. }
  133.  
  134. [ServerEvent(Event.PlayerSpawn)]
  135. public void OnPlayerSpawn(Client sender)
  136. {
  137.  
  138. if (ClientFunctions.GetClientPlayerId(sender) > 0)
  139. {
  140. Player player = GlobalFunctions.GetPlayerModelFromClient(sender, sender);
  141. if (player != null)
  142. {
  143. sender.Position = new Vector3(player.PositionX, player.PositionY, player.PositionZ);
  144. sender.SendChatMessage("[DEBUG] You have spawned.");
  145.  
  146. }
  147. }
  148. //set dimension for correct spawn..
  149. }
  150.  
  151. [ServerEvent(Event.PlayerDisconnected)]
  152. public void OnPlayerDisconnected(Client player, DisconnectionType type, string reason)
  153. {
  154. switch (type)
  155. {
  156. case DisconnectionType.Left:
  157. NAPI.Chat.SendChatMessageToAll("~b~" + player.Name + "~w~ has quit the server.");
  158. break;
  159.  
  160. case DisconnectionType.Timeout:
  161. NAPI.Chat.SendChatMessageToAll("~b~" + player.Name + "~w~ has timed out.");
  162. break;
  163.  
  164. case DisconnectionType.Kicked:
  165. NAPI.Chat.SendChatMessageToAll("~b~" + player.Name + "~w~ has been kicked for " + reason);
  166. break;
  167. }
  168.  
  169. if (GlobalData.ServerIDs.ContainsValue(player))
  170. {
  171. GlobalData.ServerIDs.Remove(ClientFunctions.GetClientPlayerServerId(player));
  172. }
  173.  
  174. if (GlobalData.rePM.ContainsKey(player))
  175. {
  176. GlobalData.rePM.Remove(player);
  177. }
  178. }
  179.  
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement