Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Weighted Role Selection System #253

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

AmongUsafk
Copy link

@AmongUsafk AmongUsafk commented May 17, 2024

Related to #252

Implements a weighted role selection system to adjust player roles based on previous game outcomes, enhancing the game experience by reducing role repetition for players across sessions.

  • Adds a new game option to enable or disable the weighted role selection system, allowing hosts to control this feature in game settings.
  • Records each player's role at the end of a game and stores this information in a session-based collection to calculate future role assignments.
  • Updates the role assignment logic to consider the session's role history, aiming to assign roles less repetitively.
  • Ensure the role History List does not exceed 100 entries
  • Implements a placeholder method CalculateWeightedRoleProbabilities in AmongUsExtensions.cs for future calculation of role probabilities based on session history, though the actual implementation is pending.
  • Fix an issue MaxRoleHistorySizeList is not used and throw an exceptions

if there any thing to do modify or something else feel free to ask me

@Enduriel

Related to eDonnes124#252

Implements a weighted role selection system to adjust player roles based on previous game outcomes, enhancing the game experience by reducing role repetition for players across sessions.

- Adds a new game option to enable or disable the weighted role selection system, allowing hosts to control this feature in game settings.
- Records each player's role at the end of a game and stores this information in a session-based collection to calculate future role assignments.
- Updates the role assignment logic to consider the session's role history, aiming to assign roles less repetitively.
- Modifies the game's documentation and options generation to include and describe the new weighted role selection feature.
Enduriel

This comment was marked as duplicate.

Copy link

@Enduriel Enduriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it'd be great if you could make a checklist in the PR with todos as well so we could easily see everything that needs to be done and the current progress more clearly.

Thanks for beginning to set this up, it'd have taken me quite a bit of time to figure this out as while I've used Harmony a decent amount I've never touched the Among Us codebase.

Edit: didn't meant to approve originally, doesn't seem like GitHub has a way of undoing my misclick -_-

Comment on lines 13 to 35
// Implement a method to record the roles assigned to each player at the end of a game.
// Store the role history in a session-based collection.
private static Dictionary<byte, List<RoleEnum>> _sessionRoleHistory = new Dictionary<byte, List<RoleEnum>>();

public static void RecordSessionRoles() {
foreach (var player in PlayerControl.AllPlayerControls) {
var playerId = player.PlayerId;
var roleType = Role.GetRole(player)?.RoleType ?? RoleEnum.None;

if (!_sessionRoleHistory.ContainsKey(playerId)) {
_sessionRoleHistory[playerId] = new List<RoleEnum>();
}

_sessionRoleHistory[playerId].Add(roleType);
}
}

public static void Prefix()
{
if (CustomGameOptions.WeightedRoleSelection)
{
RecordSessionRoles(); // Record roles at the end of each game
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably happen at the start of the game and not the end as the final roles people end up with are not the same as those they initially were assigned and we only care about those original roles.

Additionally, I'm not sure how PlayerIds are handled in Among Us so doing it at game start may handle situations like disconnects better?

Copy link
Author

@AmongUsafk AmongUsafk May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, no this affect end game because you can see there is a harmony patch to patch the end game
[HarmonyPatch(typeof(EndGameManager), nameof(EndGameManager.Start))]

and about player id is a bit complicated

i don't think so that will make disconnects better

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that currently it's targeting the end of the game, I'm suggesting that instead targeting the start of the game would make more sense due to the switching of roles and potentially disconnects (though the latter point you're saying doesn't matter). Conceptually it still makes more sense to me to save this stuff pretty much the moments it is generated

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean by "genereted" ?there

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the original roles are randomly rolled and assigned.

@AlchlcDvl
Copy link

AlchlcDvl commented May 17, 2024

i'm pretty sure when a player leaves the game, the player ids below the leaver's get pushed up, so the method of using a dict employing the use of player ids might not really work/be very ineffective. maybe add a case for disconnections?

also if a session goes on for long enough you might end up with really large lists which might end up affecting performance so you might have to occasionally clear them, maybe clear a list if it exceeds 10 roles?

@Enduriel
Copy link

@AlchlcDvl I like your thoughts on disconnections, agreed that some kind of handling there would be neat, could perform some kind of handshake where the host gives each client a uuid which they can hand back when reconnecting.

As for really large lists, I think this is a bit of an overoptimization, as this is pretty trivial in both computational complexity and memory size, but maybe a limit of 100 in case something goes terribly wrong is reasonable, even then it's in the range of 1000-2000 iterations max which is still unnoticeable on anything that can run among us in the first place.

@AlchlcDvl
Copy link

yeah fair, i was just giving 10 as like a random number, even i felt like it was way too less lmao

@AmongUsafk
Copy link
Author

@Enduriel i have added The Role History List does nit exceed 100 entries and the method CalculateWeightedRoleProbabilities in AmongUsExtensions.cs for future calculation of role probabilities based on session history

for more see the new code

i still dont know if i should call The CalculateWeightedRoleProbabilities

@Enduriel
Copy link

Some issues, you're not using MaxRoleHistoryListSize, and according to spec RemoveRange will throw an exception if a negative number is passed in count.

@AmongUsafk
Copy link
Author

Some issues, you're not using MaxRoleHistoryListSize, and according to spec RemoveRange will throw an exception if a negative number is passed in count.

oh yeah i didn't pay attention to that i will fix it soon i'm busy now

@AmongUsafk AmongUsafk changed the title Implement Weighted Role Selection System (Related to issue #252) Implement Weighted Role Selection System May 18, 2024
@AmongUsafk
Copy link
Author

@Enduriel i have fixed the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants