Advertisement
Guest User

Code1

a guest
Dec 23rd, 2023
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. using System.Collections;
  2. using IngameDebugConsole;
  3. using System.Collections.Generic;
  4. using Unity.Services.Authentication;
  5. using Unity.Services.Core;
  6. using Unity.Services.Lobbies;
  7. using Unity.Services.Lobbies.Models;
  8. using UnityEngine.UI;
  9. using Unity.Services.Relay;
  10. using Unity.Services.Relay.Models;
  11. using Unity.Netcode;
  12. using Unity.Netcode.Transports.UTP;
  13. using Unity.Networking.Transport.Relay;
  14. using TMPro;
  15. using UnityEngine;
  16.  
  17. public class TestLobby : MonoBehaviour
  18. {
  19. // Start is called before the first frame update
  20.  
  21.  
  22. private Lobby hostLobby;
  23.  
  24. private Lobby joinedLobby;
  25.  
  26. private float timer;
  27.  
  28. private string playerName;
  29.  
  30. private float timer2;
  31.  
  32. public TMP_InputField input;
  33.  
  34. public TMP_InputField nameInput;
  35. public TextMeshProUGUI codeText;
  36.  
  37. public bool createdLobby = false;
  38.  
  39. public string players;
  40.  
  41. public TextMeshProUGUI playersText;
  42.  
  43. public float textTimer = 0.0f;
  44.  
  45. public Transform inputTransform;
  46. public Transform createTransform;
  47. public Transform codeTransform;
  48.  
  49. public TestRelay testRelay;
  50.  
  51. public Transform canvas;
  52.  
  53. private async void Start()
  54. {
  55.  
  56. await UnityServices.InitializeAsync();
  57.  
  58. AuthenticationService.Instance.SignedIn += () =>
  59. {
  60. Debug.Log("Signed in" + AuthenticationService.Instance.PlayerId);
  61. };
  62. //playerName = "User" + UnityEngine.Random.Range(0, 100);
  63. Debug.Log(playerName);
  64. await AuthenticationService.Instance.SignInAnonymouslyAsync();
  65. }
  66.  
  67.  
  68. public void Update()
  69. {
  70. playerName = nameInput.text;
  71. if (Input.GetKeyDown(KeyCode.Y))
  72. {
  73. CreateLobby();
  74.  
  75. }
  76.  
  77. if (Input.GetKeyDown(KeyCode.U))
  78. {
  79. ListLobbies();
  80. }
  81.  
  82. if (Input.GetKeyDown(KeyCode.I))
  83. {
  84. PrintPlayers(joinedLobby);
  85. }
  86.  
  87. codeText.SetText(hostLobby.LobbyCode);
  88. HandleLobbyHeartbeat();
  89. HandleLobbyPollForUpdates();
  90.  
  91. playersText.SetText("Players: " + players);
  92.  
  93. textTimer += 1.0f * Time.deltaTime;
  94. if(textTimer >= 0.5f)
  95. {
  96. PrintPlayers();
  97. UpdatePlayerName(playerName);
  98. textTimer = 0.0f;
  99. }
  100.  
  101. if(joinedLobby == null)
  102. {
  103.  
  104. }
  105.  
  106. Debug.Log(playerName);
  107. }
  108.  
  109. private async void HandleLobbyHeartbeat()
  110. {
  111. if (hostLobby != null)
  112. {
  113. timer -= 1.0f * Time.deltaTime;
  114. if(timer < 0.0f)
  115. {
  116. float timerMax = 15.0f;
  117. timer = timerMax;
  118. await LobbyService.Instance.SendHeartbeatPingAsync(hostLobby.Id);
  119. }
  120. }
  121. }
  122.  
  123. private async void HandleLobbyPollForUpdates()
  124. {
  125. if (joinedLobby != null)
  126. {
  127. timer2 -= 1.0f * Time.deltaTime;
  128. if (timer2 < 0.0f)
  129. {
  130. float timerMax2 = 1.1f;
  131. timer2 = timerMax2;
  132. Lobby lobby = await LobbyService.Instance.GetLobbyAsync(joinedLobby.Id);
  133. joinedLobby = lobby;
  134. }
  135. }
  136.  
  137. if(joinedLobby.Data["StartGame"].Value != "0")
  138. {
  139. if (!IsLobbyHost())
  140. {
  141. testRelay.JoinRelay(joinedLobby.Data["GameStart"].Value);
  142. }
  143. }
  144.  
  145. joinedLobby = null;
  146.  
  147. }
  148.  
  149. private async void CreateLobby()
  150. {
  151. try
  152. {
  153. string lobbyName = "lobby";
  154. int maxPlayers = 4;
  155. CreateLobbyOptions createLobbyOptions = new CreateLobbyOptions
  156. {
  157. IsPrivate = false,
  158. Player = GetPlayer(),
  159. Data = new Dictionary<string, DataObject>
  160. {
  161. {"GameMode", new DataObject(DataObject.VisibilityOptions.Public, "Arena") },
  162. {"StartGame", new DataObject(DataObject.VisibilityOptions.Member, "0") }
  163. }
  164. };
  165.  
  166. Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, createLobbyOptions);
  167.  
  168. hostLobby = lobby;
  169. joinedLobby = hostLobby;
  170.  
  171. Debug.Log("Lobby Created" + lobby.Name + ", " + lobby.MaxPlayers + ", " + lobby.Id + ", " + lobby.LobbyCode);
  172. createTransform.transform.gameObject.SetActive(false);
  173. inputTransform.transform.gameObject.SetActive(false);
  174. codeTransform.transform.gameObject.SetActive(false);
  175. PrintPlayers(hostLobby);
  176. } catch (LobbyServiceException e)
  177. {
  178. Debug.Log(e);
  179. }
  180. }
  181.  
  182. private async void ListLobbies()
  183. {
  184. try
  185. {
  186. QueryLobbiesOptions queryLobbiesOptions = new QueryLobbiesOptions
  187. {
  188. Count = 25,
  189. Filters = new List<QueryFilter> {
  190. new QueryFilter(QueryFilter.FieldOptions.AvailableSlots, "0", QueryFilter.OpOptions.GT)
  191. },
  192. Order = new List<QueryOrder>
  193. {
  194. new QueryOrder(false, QueryOrder.FieldOptions.Created)
  195. }
  196. };
  197.  
  198. QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync(queryLobbiesOptions);
  199.  
  200. Debug.Log("Lobbies Found: " + queryResponse.Results.Count);
  201. foreach(Lobby lobby in queryResponse.Results)
  202. {
  203. Debug.Log(lobby.Name + " " + lobby.MaxPlayers + ", " + lobby.Data["GameMode"].Value);
  204. }
  205. }
  206. catch(LobbyServiceException e)
  207. {
  208. Debug.Log(e);
  209. }
  210. }
  211.  
  212. private async void JoinLobbyByCode(string lobbyCode)
  213. {
  214. try
  215. {
  216. JoinLobbyByCodeOptions joinLobbyByCodeOptions = new JoinLobbyByCodeOptions
  217. {
  218. Player = GetPlayer()
  219. };
  220. Lobby lobby = await Lobbies.Instance.JoinLobbyByCodeAsync(lobbyCode, joinLobbyByCodeOptions);
  221. joinedLobby = lobby;
  222.  
  223. PrintPlayers(joinedLobby);
  224.  
  225. Debug.Log("Joined Lobby: " + lobbyCode);
  226. createTransform.transform.gameObject.SetActive(false);
  227. inputTransform.transform.gameObject.SetActive(false);
  228. codeTransform.transform.gameObject.SetActive(false);
  229. }
  230. catch (LobbyServiceException e)
  231. {
  232. Debug.Log(e);
  233. }
  234. }
  235.  
  236. private async void QuickJoin()
  237. {
  238. try
  239. {
  240. await LobbyService.Instance.QuickJoinLobbyAsync();
  241. }
  242. catch(LobbyServiceException e)
  243. {
  244. Debug.Log(e);
  245. }
  246. }
  247.  
  248. private Player GetPlayer(){
  249. return new Player {
  250. Data = new Dictionary<string, PlayerDataObject> {
  251. { "PlayerName", new PlayerDataObject(PlayerDataObject.VisibilityOptions.Public, playerName) }
  252. }
  253. };
  254. }
  255.  
  256. private void PrintPlayers()
  257. {
  258. PrintPlayers(joinedLobby);
  259. }
  260. private void PrintPlayers(Lobby lobby)
  261. {
  262. //Debug.Log("Players in Lobby " + lobby.Name + ", " + lobby.Data["GameMode"].Value);
  263. players = "";
  264. foreach(Player player in lobby.Players)
  265. {
  266. players += player.Data["PlayerName"].Value + ", ";
  267. }
  268. //Debug.Log(players);
  269. playersText.SetText("Players: " + players);
  270. }
  271.  
  272. private async void UpdateLobbyGamemode(string gamemode)
  273. {
  274. try
  275. {
  276. hostLobby = await Lobbies.Instance.UpdateLobbyAsync(hostLobby.Id, new UpdateLobbyOptions
  277. {
  278. Data = new Dictionary<string, DataObject>{
  279. { "GameMode", new DataObject(DataObject.VisibilityOptions.Public, gamemode ) }
  280. }
  281. });
  282. joinedLobby = hostLobby;
  283.  
  284. PrintPlayers(hostLobby);
  285. }
  286. catch (LobbyServiceException e)
  287. {
  288. Debug.Log(e);
  289. }
  290. }
  291.  
  292. private async void UpdatePlayerName(string newPlayerName)
  293. {
  294. try
  295. {
  296. playerName = newPlayerName;
  297. await LobbyService.Instance.UpdatePlayerAsync(joinedLobby.Id, AuthenticationService.Instance.PlayerId, new UpdatePlayerOptions
  298. {
  299. Data = new Dictionary<string, PlayerDataObject>{
  300. { "PlayerName", new PlayerDataObject(PlayerDataObject.VisibilityOptions.Public, playerName) }
  301. }
  302. });
  303.  
  304. }
  305. catch (LobbyServiceException e)
  306. {
  307. Debug.Log(e);
  308. }
  309. }
  310.  
  311. private async void LeaveLobby()
  312. {
  313. try
  314. {
  315. await LobbyService.Instance.RemovePlayerAsync(joinedLobby.Id, AuthenticationService.Instance.PlayerId);
  316. }
  317. catch (LobbyServiceException e)
  318. {
  319. Debug.Log(e);
  320. }
  321.  
  322. }
  323.  
  324. private async void KickPlayer()
  325. {
  326. try
  327. {
  328. await LobbyService.Instance.RemovePlayerAsync(joinedLobby.Id, joinedLobby.Players[1].Id);
  329. }
  330. catch (LobbyServiceException e)
  331. {
  332. Debug.Log(e);
  333. }
  334. }
  335.  
  336. private async void MigrateLobbyHost()
  337. {
  338. try
  339. {
  340. hostLobby = await Lobbies.Instance.UpdateLobbyAsync(hostLobby.Id, new UpdateLobbyOptions
  341. {
  342. HostId = joinedLobby.Players[1].Id
  343. });
  344. joinedLobby = hostLobby;
  345.  
  346. PrintPlayers(hostLobby);
  347. }
  348. catch (LobbyServiceException e)
  349. {
  350. Debug.Log(e);
  351. }
  352. }
  353.  
  354. private async void DeleteLobby()
  355. {
  356. try
  357. {
  358. await LobbyService.Instance.DeleteLobbyAsync(joinedLobby.Id);
  359. }
  360. catch (LobbyServiceException e)
  361. {
  362. Debug.Log(e);
  363. }
  364. }
  365.  
  366.  
  367. public bool IsLobbyHost()
  368. {
  369. return joinedLobby != null && joinedLobby.HostId == AuthenticationService.Instance.PlayerId;
  370. }
  371.  
  372. public async void StartGame()
  373. {
  374. try
  375. {
  376. canvas.transform.gameObject.SetActive(false);
  377. string relayCode = await testRelay.CreateRelay();
  378.  
  379. Lobby lobby = await Lobbies.Instance.UpdateLobbyAsync(joinedLobby.Id, new UpdateLobbyOptions
  380. {
  381. Data = new Dictionary<string, DataObject>
  382. {
  383. { "StartGame", new DataObject(DataObject.VisibilityOptions.Member, relayCode) }
  384. }
  385. });
  386.  
  387. joinedLobby = lobby;
  388. }
  389. catch (LobbyServiceException e)
  390. {
  391. Debug.Log(e);
  392. }
  393. }
  394.  
  395.  
  396.  
  397. public void ClickSetName()
  398. {
  399. playerName = nameInput.text;
  400. UpdatePlayerName(playerName);
  401. Debug.Log(playerName);
  402. }
  403.  
  404. public void ClickJoin()
  405. {
  406. JoinLobbyByCode(input.text);
  407. }
  408.  
  409. public void ClickCreateLobby()
  410. {
  411. CreateLobby();
  412. }
  413.  
  414. public void ClickStartGame()
  415. {
  416.  
  417. }
  418. }
  419.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement