5
5
using Archipelago . MultiClient . Net . Models ;
6
6
using ModCore ;
7
7
using System ;
8
+ using System . Collections . Generic ;
9
+ using System . Linq ;
8
10
9
11
namespace ArchipelagoRandomizer
10
12
{
11
13
public class APHandler
12
14
{
13
15
private static APHandler instance ;
14
16
private const int baseId = 238492834 ;
15
- private ArchipelagoSession session ;
16
- private PlayerInfo currentPlayer ;
17
+ private List < ScoutedItemInfo > scoutedItems ;
17
18
18
19
public static APHandler Instance { get { return instance ; } }
19
- public static ArchipelagoSession Session { get { return instance . session ; } }
20
- public PlayerInfo CurrentPlayer { get { return currentPlayer ; } }
20
+ public static ArchipelagoSession Session { get ; private set ; }
21
+ public PlayerInfo CurrentPlayer { get ; private set ; }
21
22
22
23
public APHandler ( )
23
24
{
@@ -26,13 +27,13 @@ public APHandler()
26
27
27
28
public bool TryCreateSession ( string url , string slot , string password , out string message )
28
29
{
29
- if ( session != null )
30
+ if ( Session != null )
30
31
{
31
- session . MessageLog . OnMessageReceived -= OnReceiveMessage ;
32
+ Session . MessageLog . OnMessageReceived -= OnReceiveMessage ;
32
33
}
33
34
try
34
35
{
35
- session = ArchipelagoSessionFactory . CreateSession ( url ) ;
36
+ Session = ArchipelagoSessionFactory . CreateSession ( url ) ;
36
37
}
37
38
catch ( Exception ex )
38
39
{
@@ -44,7 +45,7 @@ public bool TryCreateSession(string url, string slot, string password, out strin
44
45
45
46
try
46
47
{
47
- result = session . TryConnectAndLogin ( "Ittle Dew 2" , slot , ItemsHandlingFlags . AllItems , password : password ) ;
48
+ result = Session . TryConnectAndLogin ( "Ittle Dew 2" , slot , ItemsHandlingFlags . AllItems , password : password ) ;
48
49
}
49
50
catch ( Exception ex )
50
51
{
@@ -68,38 +69,63 @@ public bool TryCreateSession(string url, string slot, string password, out strin
68
69
}
69
70
70
71
var loginSuccess = ( LoginSuccessful ) result ;
71
- currentPlayer = session . Players . GetPlayerInfo ( session . ConnectionInfo . Slot ) ;
72
- session . MessageLog . OnMessageReceived += OnReceiveMessage ;
73
- message = "Successfully connected!\n " +
74
- "Now that you are connected, you can use !help to list commands to run via the server." ;
75
- session . Items . ItemReceived += OnReceivedItem ;
72
+ CurrentPlayer = Session . Players . GetPlayerInfo ( Session . ConnectionInfo . Slot ) ;
73
+ Session . MessageLog . OnMessageReceived += OnReceiveMessage ;
74
+ Session . Locations . CheckedLocationsUpdated += OnLocationChecked ;
75
+ Session . Items . ItemReceived += OnReceivedItem ;
76
+ message = "Successfully connected!\n Now that you are connected, you can use !help to list commands to run via the server." ;
77
+ ScoutLocations ( ) ;
76
78
return true ;
77
79
}
78
80
81
+ public void LocationChecked ( int offset )
82
+ {
83
+ if ( Session == null )
84
+ {
85
+ Plugin . Log . LogError ( "Error in APHandler.LocationChecked(): No session exists yet!" ) ;
86
+ return ;
87
+ }
88
+
89
+ Session . Locations . CompleteLocationChecks ( baseId + offset ) ;
90
+ }
91
+
92
+ private void OnLocationChecked ( System . Collections . ObjectModel . ReadOnlyCollection < long > newCheckedLocations )
93
+ {
94
+ long id = newCheckedLocations [ newCheckedLocations . Count - 1 ] ;
95
+ string locationName = Session . Locations . GetLocationNameFromId ( id ) ;
96
+ ScoutedItemInfo item = scoutedItems . FirstOrDefault ( x => x . LocationId == id ) ;
97
+
98
+ // If sending item
99
+ if ( item != null && item . Player . Slot != CurrentPlayer . Slot )
100
+ ItemRandomizer . Instance . ItemSent ( item . ItemDisplayName , item . Player . Name ) ;
101
+
102
+ Plugin . Log . LogInfo ( $ "Checked location: { locationName } ") ;
103
+ }
104
+
79
105
private void OnReceivedItem ( ReceivedItemsHelper helper )
80
106
{
81
- ItemInfo receivedItem = session . Items . AllItemsReceived [ session . Items . AllItemsReceived . Count - 1 ] ;
107
+ ItemInfo receivedItem = Session . Items . AllItemsReceived [ Session . Items . AllItemsReceived . Count - 1 ] ;
82
108
int itemOffset = ( int ) receivedItem . ItemId - baseId ;
83
- ItemRandomizer . Instance . ItemReceived ( itemOffset , receivedItem . Player . Name ) ;
109
+ ItemRandomizer . Instance . ItemReceived ( itemOffset , receivedItem . ItemDisplayName , receivedItem . Player . Name ) ;
84
110
}
85
111
86
- public void LocationChecked ( int offset )
112
+ private void ScoutLocations ( )
87
113
{
88
- if ( session == null )
114
+ if ( Session == null )
89
115
{
90
- Plugin . Log . LogError ( "Attempted to interact with Archipelago server, but no session has been started yet!") ;
116
+ Plugin . Log . LogError ( $ "Error in APHandler.ScoutLocations(): No session exists yet!") ;
91
117
return ;
92
118
}
93
119
94
- int id = baseId + offset ;
95
- session . Locations . CompleteLocationChecksAsync ( ( completed ) =>
120
+ scoutedItems = new ( ) ;
121
+
122
+ Session . Locations . ScoutLocationsAsync ( ( scoutResult ) =>
96
123
{
97
- if ( completed )
124
+ foreach ( ScoutedItemInfo item in scoutResult . Values )
98
125
{
99
- string locationName = session . Locations . GetLocationNameFromId ( id ) ;
100
- Plugin . Log . LogInfo ( $ "Checked location: { locationName } ({ offset } )") ;
126
+ scoutedItems . Add ( item ) ;
101
127
}
102
- } , id ) ;
128
+ } , Session . Locations . AllLocations . ToArray ( ) ) ;
103
129
}
104
130
105
131
private void OnReceiveMessage ( LogMessage message )
0 commit comments