/// <summary> /// Handles the specified <see cref="Message"/>. /// </summary> /// <param name="Device">The device.</param> /// <param name="Message">The message.</param> /// <param name="Cancellation">The cancellation.</param> public static async Task Handle(Device Device, Message Message, CancellationToken Cancellation) { var LoginMessage = (LoginMessage)Message; if (LoginMessage == null) { throw new LogicException(typeof(LoginHandler), nameof(LoginMessage) + " == null at Handle(Device, Message, CancellationToken)."); } Device.State = State.Login; if (LoginHandler.Trusted(Device, LoginMessage) == false) { return; } Logging.Info(typeof(LoginHandler), "Login(" + LoginMessage.HighId + "-" + LoginMessage.LowId + ")."); if (LoginMessage.HighId == 0 && LoginMessage.LowId == 0 && string.IsNullOrEmpty(LoginMessage.Token)) { Player Player = await Players.Create(); if (Player != null) { await LoginHandler.Login(Device, LoginMessage, Player); } else { Device.NetworkManager.SendMessage(new LoginFailedMessage(Reason.Maintenance)); } } else { Player Player = await Players.Get(LoginMessage.HighId, LoginMessage.LowId); if (Player != null) { if (string.Equals(LoginMessage.Token, Player.Token)) { if (!Player.IsBanned) { await LoginHandler.Login(Device, LoginMessage, Player); } else { Device.NetworkManager.SendMessage(new LoginFailedMessage(Reason.Banned)); } } else { Device.NetworkManager.SendMessage(new LoginFailedMessage(Reason.Reset)); } } else { Device.NetworkManager.SendMessage(new LoginFailedMessage(Reason.Reset)); } } }
public void When_CreatingAnEngine_Then_BoardIsNotNull() { var engine = new Engine( new RuleMapBuilder(), Players.Create(2).ValueOrFailure()); Assert.NotNull(engine.Board); }
/// <summary> /// Handles the creation of the character. /// </summary> /// <param name="player">The player.</param> /// <param name="packet">The packet.</param> private static bool HandleCreation(Models.Entities.Player player, CharacterCreationPacket packet) { if (player.ClientId != packet.ClientId) { Accounts.Ban(player.DbPlayer.Account, Drivers.Messages.INVALID_CLIENT_ID, DbAccount.BanRangeType.Perm); return(false); } if (!packet.Job.ToString().StartsWith("Intern")) { Accounts.Ban(player.DbPlayer.Account, Drivers.Messages.INVALID_CLIENT_ID, DbAccount.BanRangeType.Perm); return(false); } if (packet.Model != 1003 && packet.Model != 1004 && packet.Model != 2001 && packet.Model != 2002) { Accounts.Ban(player.DbPlayer.Account, Drivers.Messages.INVALID_MODEL, DbAccount.BanRangeType.Perm); return(false); } if (!ValidateCharacters(packet.Name)) { player.ClientSocket.Send(MessageController.CreateNewPlayer(Drivers.Messages.Errors.INVALID_CHARS)); return(true); } if (IsBannedName(packet.Name)) { player.ClientSocket.Send(MessageController.CreateNewPlayer(Drivers.Messages.Errors.NAME_BANNED)); return(true); } if (Players.GetPlayerByName(packet.Name, Drivers.Settings.WorldSettings.Server) != null) { player.ClientSocket.Send(MessageController.CreateNewPlayer(Drivers.Messages.Errors.NAME_TAKEN)); return(true); } // stats fallback ... player.DbPlayer.Strength = 0; player.DbPlayer.Agility = 0; player.DbPlayer.Vitality = 0; player.DbPlayer.Spirit = 0; player.DbPlayer.Name = packet.Name; player.Job = packet.Job; player.Level = 1; player.DbPlayer.Model = packet.Model; Players.Create(player.DbPlayer, new ushort[] { player.Strength, player.Vitality, player.Agility, player.Spirit }, player.MaxHP, player.MaxMP); player.ClientSocket.Disconnect(Drivers.Messages.SUCCESS_CREATE); return(true); }
public void When_PlacingFirstTile_Then_ReturnsNewStateWithSinglePlacedTile() { // Arrange var engine = new Engine( new RuleMapBuilder().Set(ImmutableList <IRule <PlaceTileGameAction> > .Empty), Players.Create(2).ValueOrFailure()); // Act var newState = engine.Perform(MakePlaceTile(0, 0)); // Assert Assert.Equal(1, newState.ValueOrFailure().Board.ToImmutableDictionary().Count); }
public void When_PlacingFirstTile_Then_ReturnsANewState() { // Arrange var engine = new Engine( new RuleMapBuilder().Set(ImmutableList <IRule <PlaceTileGameAction> > .Empty), Players.Create(2).ValueOrFailure()); // Act var newState = engine.Perform(MakePlaceTile(0, 0)); // Assert Assert.True(newState.HasValue); }
private async Task StartSimulator() { if (HasStarted) { return; } var fileName = Options.FileName; HasStarted = true; WantToStop = false; var noOfPeriods = Convert.ToInt32((int)Options.NoOfTimes / (int)Options.NoOfTimesPerPeriod); var cardPacks = CardPackFactory.Create(Options.NoOfCardPacks); PlayManager.CardPacks = cardPacks; for (int period = CurrentPeriod; period < noOfPeriods; period++) { for (int i = 0; i < (int)Options.NoOfTimesPerPeriod; i++) { var players = Players.Create(Options.NoOfPlayers); var data = await PlayManager.PlayAsync(players); DataRecorder.Record(new[] { data }); } await DataRecorder.SaveAsync(fileName, period == 0); CurrentPeriod = period; if (OnPeriodChange != null) { OnPeriodChange.Invoke(period, noOfPeriods); } if (WantToStop) { break; } } CurrentPeriod = (WantToStop) ? 0 : CurrentPeriod; if (OnPlayFinished != null && WantToStop == false) { OnPlayFinished.Invoke(Options.NoOfTimes, Options.NoOfTimesPerPeriod); } if (WantToStop && OnSimulatorStopped != null) { OnSimulatorStopped.Invoke(); } HasStarted = false; }
/// <summary> /// The handler for the auth request packet. /// </summary> /// <param name="client">The client.</param> /// <param name="packet">The packet.</param> private static bool Handle(Models.Client.AuthClient client, Models.Packets.AuthRequestPacket packet) { client.Account = packet.Account; client.Password = packet.Password; var server = Repositories.Server.ServerList.GetServerInfo(packet.Server); var canLogin = server != null; var response = new Models.Packets.AuthResponsePacket(); if (canLogin) { try { var dbAccount = Accounts.GetAccountByUserNameAndPassword(client.Account, client.Password); if (dbAccount == null) { response.Status = Enums.AuthenticationStatus.InvalidAccountIDOrPassword; } else { client.Authenticated = true; if (dbAccount.Banned && dbAccount.BanDate.HasValue) { bool shouldUnban = false; switch (dbAccount.BanRange) { case DbAccount.BanRangeType.OneDay: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddDays(1); break; } case DbAccount.BanRangeType.ThreeDays: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddDays(3); break; } case DbAccount.BanRangeType.OneWeek: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddDays(7); break; } case DbAccount.BanRangeType.OneMonth: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddMonths(1); break; } case DbAccount.BanRangeType.ThreeMonths: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddMonths(3); break; } case DbAccount.BanRangeType.SixMonths: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddMonths(6); break; } case DbAccount.BanRangeType.OneYear: { shouldUnban = DateTime.Now >= dbAccount.BanDate.Value.AddYears(1); break; } } canLogin = shouldUnban; if (shouldUnban) { dbAccount.Banned = false; dbAccount.Update(); } else { response.Status = Enums.AuthenticationStatus.AccountBanned; } } if (canLogin) { client.ClientId = Drivers.Repositories.Safe.IdentityGenerator.GetClientId(); response.ClientId = client.ClientId; response.IPAddress = server.IPAddress; response.Port = server.Port; response.Status = Enums.AuthenticationStatus.Ready; if (string.IsNullOrWhiteSpace(dbAccount.FirstLoginIP)) { dbAccount.FirstLoginIP = client.ClientSocket.IPAddress; dbAccount.FirstLoginDate = DateTime.Now; } dbAccount.LastServer = server.Name; dbAccount.LastIP = client.ClientSocket.IPAddress; dbAccount.LastAuthKey = client.ClientId.ToString("X") + ";" + DateTime.Now.ToBinary(); dbAccount.LastLoginDate = DateTime.Now; dbAccount.Update(); var dbPlayer = Players.GetPlayerByAccount(dbAccount); if (dbPlayer == null) { dbPlayer = Players.Create(dbAccount); } dbPlayer.AuthKey = dbAccount.LastAuthKey; dbPlayer.Update(); } } } catch (Exception e) { response.Reset(); response.Status = Enums.AuthenticationStatus.DatebaseError; #if DEBUG #if TRACE ErrorLogger.Log(StackTracing.GetCurrentMethod().Name, e); #else ErrorLogger.Log(Drivers.Messages.Errors.DB_ERROR, e); #endif #else Global.Message(e.ToString()); #endif } } client.ClientSocket.Send(response); return(false); }
public void Given_LastPlayerInBound_When_FindingNextPlayer_Then_Is1() { int count = 4; Assert.Equal(1, Players.Create(count).ValueOrFailure().NextPlayer(count)); }
public void Given_Player1_When_FindingNextPlayer_Then_Is2() { Assert.Equal(2, Players.Create(2).ValueOrFailure().NextPlayer(1)); }
public void Given_NumberOutsideRange_When_FindingNextPlayer_Then_Is1() { Assert.Equal(1, Players.Create(2).ValueOrFailure().NextPlayer(0)); Assert.Equal(1, Players.Create(2).ValueOrFailure().NextPlayer(3)); }
public void Given_6Players_When_CreatingPlayers_Then_NoneIsArgumentOutOfRangeException() { Players.Create(6).MatchNone(exception => Assert.IsType <ArgumentOutOfRangeException>(exception)); }
public void Given_2Players_When_CreatingPlayers_Then_SomeIsReturned() { Assert.NotNull(Players.Create(2).ValueOrFailure()); }