public bool IsCrewMateWin(AmongUsState state) { if (state.Players.Where(x => x.IsImposter).Count() == 0) { return(true); } return(false); }
public void Update(AmongUsState previous, GameReader reader) { if (previous.State == GameState.Menu && this.State == GameState.Lobby && (previous.allPlayers == reader.AllPlayers || this.Players.Count == 1 || this.Players.Any(x => x.IsLocal))) { this.State = GameState.Menu; } allPlayers = reader.AllPlayers; }
private async Task ReadLoop() { if (!this.IsAttached) { return; } if (!isReading) { isReading = true; } try { PreviousState = new AmongUsState(this); Logger.Write("Starting read..\n" + "Game State:\n" + $" Game state: <Green>{PreviousState.State}</Green>\n" + $" Code: <Green>{PreviousState.GameCode}</Green>\n" + $" Players: <Green>{PreviousState.PlayerCount}</Green>:\n" + $" - " + string.Join("\n - ", PreviousState.Players), Logger.Severity.Game ); while (this.IsAttached) { try { var currentState = new AmongUsState(this); currentState.Update(PreviousState, this); if (currentState.State != PreviousState.State) { Logger.Write($"Gamestate updated from <Gray>{PreviousState.State}</Gray> to <Green>{currentState.State}</Green>", Logger.Severity.Game); } if (currentState.ConnectedToServer != PreviousState.ConnectedToServer) { if (currentState.ConnectedToServer) { Logger.Write($"Connected to the game <Cyan>{currentState.GameCode}</Cyan> with <Cyan>{currentState.PlayerCount}</Cyan> players", Logger.Severity.Game); } else { Logger.Write($"Disconnected from the game with <Cyan>{PreviousState.Players.Count(x => !x.IsDead)}</Cyan> players", Logger.Severity.Game); } } if (currentState.InGame != PreviousState.InGame) { if (currentState.InGame) { GameStarted?.Invoke(null, currentState); Logger.Write($"Game <Cyan>{currentState.GameCode}</Cyan> <Green>Started</Green> with <Green>{currentState.PlayerCount}</Green> players.", Logger.Severity.Game); } else { if (currentState.ConnectedToServer) { GameEnded?.Invoke(null, currentState); var isCrewmateWin = this.IsCrewMateWin(currentState); Logger.Write($"Game <Cyan>{currentState.GameCode}</Cyan> <Red>Ended</Red> with {(isCrewmateWin ? $"<Green>Crewmate Victory</Green>" : "<Red>Imposter Win</Red>")}", Logger.Severity.Game); } } } if (currentState.ConnectedToServer) { var Joined = currentState.Players.Select(x => (x.Name, x.Id)).Except(PreviousState.Players.Select(x => (x.Name, x.Id))).ToList(); if (Joined.Count > 0) { foreach (var item in Joined.Where(x => x.Name != "")) { var newPlayer = currentState.Players.FirstOrDefault(x => x.Id == item.Id); PlayerJoined?.Invoke(null, new PlayerJoinedEventArgs() { GameState = currentState, Player = newPlayer }); Logger.Write($"Player <Gray>{newPlayer.Id}: \'{newPlayer.Name}\'</Gray> joined <Cyan>{currentState.GameCode}</Cyan>", Logger.Severity.Game); } } var LeftIngame = currentState.Players.Where(x => { var plr = PreviousState.Players.FirstOrDefault(y => y.Id == x.Id); if (plr == null) { return(false); } if (x.Disconnected && !plr.Disconnected) { return(true); } return(false); }).Select(x => x.Id); var Left = PreviousState.Players.Select(x => x.Id).Except(currentState.Players.Select(x => x.Id)).Concat(LeftIngame).ToList(); if (Left.Count > 0) { foreach (var item in Left) { var oldPlayer = PreviousState.Players.FirstOrDefault(x => x.Id == item); PlayerLeft?.Invoke(null, new PlayerLeftEventArgs() { GameState = currentState, Player = oldPlayer }); Logger.Write($"Player <Gray>{oldPlayer.Id}: \'{oldPlayer.Name}\'</Gray> Left <Cyan>{currentState.GameCode}</Cyan>", Logger.Severity.Game); } } if (PreviousState.CanTalk != currentState.CanTalk) { VoipSettingUpdate?.Invoke(null, currentState.CanTalk); Logger.Write($"Voice state changed from {(PreviousState.CanTalk ? "<Green>Can talk</Green>" : "<Red>Cannot Talk</Red>")} to {(currentState.CanTalk ? "<Green>Can talk</Green>" : "<Red>Cannot Talk</Red>")}", Logger.Severity.Game); } if (currentState.State == AmongUsReader.GameState.Discussion && currentState.State != PreviousState.State) { MeetingCalled?.Invoke(null, currentState); Logger.Write("Meeting called, Entered discussion", Logger.Severity.Game); } } PreviousState = currentState; } catch (Exception x) { Logger.Write($"Error on read loop: {x}"); } finally { await Task.Delay(50); } } } catch (Exception x) { Logger.Write($"Fatal before read loop: {x}", Logger.Severity.Critical); } finally { isReading = false; } }