-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomLobbyJoiner.cs
101 lines (85 loc) · 3.36 KB
/
randomLobbyJoiner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Splotch;
using Splotch.Event;
using HarmonyLib;
using Steamworks.Data;
using Steamworks;
using System.Reflection;
using JetBrains.Annotations;
using Steamworks.Ugc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
namespace RandomLobbyJoiner
{
public class RandomLobbyJoiner : SplotchMod
{
public static Random Random = new Random();
public override void OnLoad()
{
Logger.Log("hefhakwdn iehnfisoie");
EventManager.RegisterEventListener(typeof(Events));
Harmony.PatchAll(typeof(HarmonyPatches));
JoinRandomLobby();
}
public static async void JoinRandomLobby()
{
var validLobbies = await GetValidLobbies();
if (validLobbies != null)
{
Lobby selectedLobby = validLobbies[Random.Next(validLobbies.Length)];
JoinLobby(selectedLobby);
} else
{
Lobby? lobby = await SteamMatchmaking.CreateLobbyAsync(4);
if (!lobby.HasValue)
{
Logger.Error("Lobby created but not correctly instantiated");
throw new Exception();
}
SteamManager.instance.currentLobby = lobby.Value;
SteamManager.instance.currentLobby.SetData("isFriendLobby", "FALSE");
SteamManager.instance.currentLobby.SetData("canBeJoinedByMatchMaking", "true");
SteamManager.instance.currentLobby.SetData("ownerNameDataString", SteamClient.Name);
SteamManager.instance.currentLobby.SetPublic();
GameLobby.nrOfAbilities = Settings.Get().NumberOfAbilities;
}
}
public static async Task<Lobby[]> GetValidLobbies()
{
var query = SteamMatchmaking.LobbyList.WithKeyValue("canBeJoinedByMatchMaking", "true");
var matchmakinglobbies = await query.RequestAsync();
if (matchmakinglobbies == null || !matchmakinglobbies.Any()) return null;
var threeplayerlobbies = await query.WithSlotsAvailable(1).RequestAsync();
if (threeplayerlobbies.Any()) return threeplayerlobbies;
var twoplayerlobbies = await query.WithSlotsAvailable(2).RequestAsync();
if (twoplayerlobbies.Any()) return twoplayerlobbies;
var oneplayerlobbies = await query.WithSlotsAvailable(3).RequestAsync();
if (oneplayerlobbies.Any()) return oneplayerlobbies;
return null;
}
public static void JoinLobby(Lobby lobby)
{
MethodInfo joinLobby = AccessTools.Method(typeof(SteamManager), "JoinLobby");
joinLobby.Invoke(SteamManager.instance, new object[] { lobby });
}
}
public static class Events
{
}
public static class HarmonyPatches
{
[HarmonyPatch(typeof(SteamManager), nameof(SteamManager.LeaveLobby))]
[HarmonyPostfix]
public static void OnLobbyLeave()
{
RandomLobbyJoiner.JoinRandomLobby();
}
[HarmonyPatch(typeof(SteamManager), nameof(SteamManager.StartHostedGame))]
[HarmonyPostfix]
public static void StartGame()
{
SteamManager.instance.currentLobby.SetData("canBeJoinedByMatchMaking", "false");
}
}
}