Ejemplo n.º 1
0
        private async void ListenThread()
        {
            TpNetState lastGameData = new TpNetState();

            DateTime lastUpdate = DateTime.UtcNow;

            while (true)
            {
                StateView.Read <TpNetState>(0, out TpNetState gameData);

                if (refreshList)
                {
                    refreshList = !refreshList;

                    List <LobbyData> lobbies = new List <LobbyData>();

                    try
                    {
                        lobbies = await lobbyClient.GetLobbies(selectedGameId);
                    }
                    catch (System.Net.Http.HttpRequestException ex)
                    {
                        MessageBox.Show("master server offline :(");
                    }

                    Invoke(new Action(() =>
                    {
                        lobbyListView.Items.Clear();
                    }));

                    foreach (LobbyData lobby in lobbies)
                    {
                        if (lobby.GameId == selectedGameId || selectedGameId == GameId.Any)
                        {
                            Invoke(new Action(() =>
                            {
                                ListViewItem item = new ListViewItem(new string[] { lobby.Name, lobby.GameId.ToString(), $"{lobby.MemberCount}/{lobby.MaxMemberCount}" });
                                item.Tag          = lobby;
                                lobbyListView.Items.Add(item);
                            }));
                        }
                    }

                    Invoke(new Action(() =>
                    {
                        refreshLobbiesBtn.Enabled = true;
                    }));
                }

                if (waitingForCreation)
                {
                    if (gameData.lobbyId != 0)
                    {
                        waitingForCreation = !waitingForCreation;

                        LobbyData newLobby = new LobbyData();
                        newLobby.Id     = gameData.lobbyId;
                        newLobby.HostId = gameData.hostId;

                        List <LobbyMember> newMembers = new List <LobbyMember>();

                        for (int i = 0; i < gameData.numMembers; i++)
                        {
                            LobbyMember member = new LobbyMember();
                            unsafe
                            {
                                // TODO: member names.
                                member.Name = gameData.members[i].ToString();
                                member.Id   = gameData.members[i];
                            }
                            newMembers.Add(member);
                        }

                        newLobby.Members        = newMembers;
                        newLobby.Name           = lobbyName;
                        newLobby.GameId         = lobbyGame;
                        newLobby.MaxMemberCount = 2;
                        newLobby.MemberCount    = gameData.numMembers;

                        await lobbyClient.CreateLobby(newLobby);

                        isInLobby      = true;
                        currentLobbyId = gameData.lobbyId;
                        currentLobby   = newLobby;
                    }
                }

                if (createLobby)
                {
                    createLobby = !createLobby;

                    var profileName       = lobbyGame.ToString() + ".xml";
                    ProcessStartInfo info = new ProcessStartInfo("TeknoParrotUi.exe", $"--profile={profileName}");
                    info.UseShellExecute = false;
                    info.Environment.Add("tp_steamnet", "host");

                    launcherProcess = Process.Start(info);

                    waitingForCreation = true;

                    refreshList = true;
                }

                if (waitingForJoin)
                {
                    if (gameData.lobbyId != 0)
                    {
                        waitingForJoin = !waitingForJoin;

                        currentLobbyId = gameData.lobbyId;
                        currentLobby   = await lobbyClient.GetLobby(currentLobbyId);

                        isInLobby = true;
                    }
                }

                if (isInLobby)
                {
                    if (!lastGameData.Equals(gameData) || (DateTime.UtcNow > lastUpdate + TimeSpan.FromSeconds(30)))
                    {
                        lastUpdate = DateTime.UtcNow;

                        currentLobby.HostId = gameData.hostId;

                        List <LobbyMember> newMembers = new List <LobbyMember>();

                        for (int i = 0; i < gameData.numMembers; i++)
                        {
                            LobbyMember member = new LobbyMember();
                            unsafe
                            {
                                member.Name = gameData.members[i].ToString();
                                member.Id   = gameData.members[i];
                            }
                            newMembers.Add(member);
                        }

                        currentLobby.Members     = newMembers;
                        currentLobby.MemberCount = gameData.numMembers;

                        await lobbyClient.UpdateLobby(currentLobbyId, currentLobby);
                    }

                    if (launcherProcess != null && launcherProcess.HasExited)
                    {
                        isInLobby      = false;
                        currentLobby   = null;
                        currentLobbyId = 0;

                        joinLobbyBtn.Enabled = true;

                        if (gameData.hostId == gameData.steamId)
                        {
                            await lobbyClient.DeleteLobby(currentLobbyId);
                        }

                        TpNetState state = new TpNetState();
                        StateView.Write <TpNetState>(0, ref state);
                    }
                }

                if (joinLobby)
                {
                    joinLobby = !joinLobby;

                    var profileName       = lobbyToJoin.GameId.ToString() + ".xml";
                    ProcessStartInfo info = new ProcessStartInfo("TeknoParrotUi.exe", $"--profile={profileName}");
                    info.UseShellExecute = false;
                    info.Environment.Add("tp_steamnet", lobbyToJoin.Id.ToString("x"));

                    launcherProcess = Process.Start(info);

                    waitingForJoin = true;
                }

                lastGameData = gameData;

                Thread.Sleep(100);
            }
        }