public async Task MakeOrder([FromBody] OrderClass orderClass) { List <Player> players = await _GAMEContext.Player.ToListAsync(); Player player = players.FirstOrDefault(m => m.Id == orderClass.PlayerId); if (player != null) { InventoryInformation inventoryInformation = player.Inventory; } int OrderMadeTo = 0; switch (player.PlayerRoleId) { case (int)Role.Retailer: OrderMadeTo = (int)_GAMEContext.GameTeamPlayerRelationship.Where(m => m.TeamId == player.GameTeamPlayerRelationship.FirstOrDefault().TeamId&& m.Player.PlayerRoleId == (int)Role.Wholesaler).FirstOrDefault().PlayerId; break; case (int)Role.Wholesaler: OrderMadeTo = (int)_GAMEContext.GameTeamPlayerRelationship.Where(m => m.TeamId == player.GameTeamPlayerRelationship.FirstOrDefault().TeamId&& m.Player.PlayerRoleId == (int)Role.Distributor).FirstOrDefault().PlayerId; break; case (int)Role.Distributor: OrderMadeTo = (int)_GAMEContext.GameTeamPlayerRelationship.Where(m => m.TeamId == player.GameTeamPlayerRelationship.FirstOrDefault().TeamId&& m.Player.PlayerRoleId == (int)Role.Factory).FirstOrDefault().PlayerId; break; case (int)Role.Factory: OrderMadeTo = 0; break; default: break; } GameTeamPlayerRelationship gameTeamPlayerRelationship = await _GAMEContext.GameTeamPlayerRelationship.FirstOrDefaultAsync(m => m.PlayerId == player.Id); int currentPeriod = gameTeamPlayerRelationship.Team.CurrentPeriod; PlayerTransactions playerTransactions = await _GAMEContext.PlayerTransactions.FirstOrDefaultAsync(m => m.OrderMadeFrom == player.Id && m.OrderMadePeriod == currentPeriod); if (playerTransactions == null) { playerTransactions = new PlayerTransactions(); _GAMEContext.PlayerTransactions.Add(playerTransactions); } playerTransactions.OrderMadeFrom = player.Id; playerTransactions.OrderMadeTo = OrderMadeTo; playerTransactions.OrderQty = orderClass.OrderQty; playerTransactions.GameId = gameTeamPlayerRelationship.GameId; playerTransactions.TeamId = gameTeamPlayerRelationship.TeamId; playerTransactions.OrderMadePeriod = gameTeamPlayerRelationship.Team.CurrentPeriod; playerTransactions.OrderReceivePeriod = gameTeamPlayerRelationship.Team.CurrentPeriod + 2; player.HasMadeDecision = true; await _GAMEContext.SaveChangesAsync(); if (await dataProvider.ShouldUpdateResults(gameTeamPlayerRelationship.TeamId)) { await dataProvider.UpdateResults(gameTeamPlayerRelationship.TeamId, gameTeamPlayerRelationship.Team.CurrentPeriod); } }
public async Task UpdateResults(int?teamId, int?period) { List <GameTeamPlayerRelationship> gameTeamPlayerRelationships = _GAMEContext.GameTeamPlayerRelationship.Where(m => m.TeamId == teamId).ToList(); List <PlayerTransactions> playerTransactions = _GAMEContext.PlayerTransactions.Where(m => m.TeamId == teamId).ToList(); foreach (GameTeamPlayerRelationship rs in gameTeamPlayerRelationships) { Player player = rs.Player; int newOrder = 0; int sentOrder = 0; PlayerTransactions outgoingPlayerTransaction = null; PlayerTransactions incomingPlayerTransaction = null; switch (player.PlayerRoleId) { case (int)Role.Retailer: newOrder = (int)rs.Team.CurrentOrder; var currentInventory = rs.Player.Inventory.CurrentInventory; incomingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeFrom == player.Id && m.OrderReceivePeriod == period); rs.Player.Inventory.NewOrder = newOrder; if (rs.Player.Inventory.CurrentInventory < 0) { sentOrder = 0; } else { if (rs.Player.Inventory.CurrentInventory > newOrder) { sentOrder = newOrder; } else { sentOrder = rs.Player.Inventory.CurrentInventory; } } break; case (int)Role.Wholesaler: outgoingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeTo == player.Id && m.OrderMadePeriod == period); newOrder = outgoingPlayerTransaction.OrderQty; if (rs.Player.Inventory.CurrentInventory < 0) { sentOrder = 0; } else { if (rs.Player.Inventory.CurrentInventory > newOrder) { sentOrder = newOrder; } else { sentOrder = rs.Player.Inventory.CurrentInventory; } } outgoingPlayerTransaction.SentQty = sentOrder; incomingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeFrom == player.Id && m.OrderReceivePeriod == period); break; case (int)Role.Distributor: outgoingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeTo == player.Id && m.OrderMadePeriod == period); newOrder = outgoingPlayerTransaction.OrderQty; if (rs.Player.Inventory.CurrentInventory < 0) { sentOrder = 0; } else { if (rs.Player.Inventory.CurrentInventory > newOrder) { sentOrder = newOrder; } else { sentOrder = rs.Player.Inventory.CurrentInventory; } } outgoingPlayerTransaction.SentQty = sentOrder; incomingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeFrom == player.Id && m.OrderReceivePeriod == period); break; case (int)Role.Factory: outgoingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeTo == player.Id && m.OrderMadePeriod == period); var transaction = playerTransactions.FirstOrDefault(m => m.OrderMadeFrom == player.Id && m.OrderMadePeriod == period); newOrder = outgoingPlayerTransaction.OrderQty; if (rs.Player.Inventory.CurrentInventory < 0) { sentOrder = 0; } else { if (rs.Player.Inventory.CurrentInventory > newOrder) { sentOrder = newOrder; } else { sentOrder = rs.Player.Inventory.CurrentInventory; } } outgoingPlayerTransaction.SentQty = sentOrder; transaction.SentQty = newOrder; incomingPlayerTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeFrom == player.Id && m.OrderReceivePeriod == period); break; default: break; } rs.Player.Inventory.CurrentInventory -= newOrder; rs.Player.Inventory.IncomingInventory = (period <= 4) ? 4 : incomingPlayerTransaction != null ? incomingPlayerTransaction.OrderQty : 0; rs.Player.Inventory.CurrentInventory += (int)rs.Player.Inventory.IncomingInventory; rs.Player.Inventory.NewOrder = newOrder; double cost = player.Inventory.CurrentInventory > 0 ? player.Inventory.CurrentInventory / 2 : Math.Abs(player.Inventory.CurrentInventory); player.Inventory.TotalCost += cost; PlayerTransactions orderedTransaction = playerTransactions.FirstOrDefault(m => m.OrderMadeFrom == player.Id && m.OrderMadePeriod == period); PlayerTransactions requestedTransactionToPlayer = playerTransactions.FirstOrDefault(m => m.OrderMadeTo == player.Id && m.OrderMadePeriod == period); int totalNeeded = 0; if (rs.Player.Inventory.CurrentInventory < 0) { totalNeeded += Math.Abs(rs.Player.Inventory.CurrentInventory); } if (requestedTransactionToPlayer != null) { totalNeeded += requestedTransactionToPlayer.OrderQty; } Results results = new Results { GameTeamPlayerRelationshipId = rs.Id, Inventory = rs.Player.Inventory.CurrentInventory, IncomingInventory = rs.Player.Inventory.IncomingInventory, TotalCost = player.Inventory.TotalCost, Period = period, PreviousOrder = player.Inventory.NewOrder, OrderQty = orderedTransaction.OrderQty, SentQty = sentOrder, TotalNeeded = totalNeeded }; _GAMEContext.Results.Add(results); await _GAMEContext.SaveChangesAsync(); } Game game = gameTeamPlayerRelationships.FirstOrDefault().Game; Team team = gameTeamPlayerRelationships.FirstOrDefault().Team; List <int> demandData = Newtonsoft.Json.JsonConvert.DeserializeObject <List <int> >(game.DemandInformation); team.CurrentOrder = demandData[team.CurrentPeriod]; team.CurrentPeriod += 1; await _GAMEContext.SaveChangesAsync(); }
public override Task DivideLine(string line) { Console.WriteLine($"{DateTime.Now} Thread {Thread.CurrentThread.ManagedThreadId} started job."); if (line.Length < 5) { Console.WriteLine($"{DateTime.Now} Line too small, skipped."); } else if (line.Contains(" TC: ")) { Team.Add(line); } else if (line.Contains(" MC LS:")) { Main.Add(line); } else if (line.Contains(" MC LV:")) { Main.Add(line); } else if (line.Contains(" MC SF:")) { Main.Add(line); } else if (line.Contains(" (ADVERT) ")) { Advert.Add(line); } else if (line.Contains(" (MYC ")) { Country.Add(line); } else if (line.Contains(" (sup) ")) { Support.Add(line); } else if (line.Contains(" (cad) ")) { Cad.Add(line); } else if (line.Contains(" KILL: ")) { KillDeaths.Add(line); } else if (line.Contains(" DEATH: ")) { KillDeaths.Add(line); } else if (line.Contains(" killed themselves via command")) { KillDeaths.Add(line); } else if (line.Contains(" GrC (")) { Group.Add(line); } else if (line.Contains(" SC (")) { Squad.Add(line); } else if (line.Contains(" UC (")) { Unit.Add(line); } else if (line.Contains(" (alliance) ")) { Group.Add(line); } else if (line.Contains(" SMS from ")) { Sms.Add(line); } else if (line.Contains(" SMS to ")) { Sms.Add(line); } else if (line.Contains(" T$ ")) { TTransactions.Add(line); if (line.Contains("(CITphoneTran")) { PlayerTransactions.Add(line); } else if (line.Contains("CIThit")) { Hit.Add(line); } } else if (line.Contains(" G$ ")) { GTransactions.Add(line); if (line.Contains("(CITphoneTran")) { PlayerTransactions.Add(line); } else if (line.Contains("CIThit")) { Hit.Add(line); } } else if (line.Contains(" BT: ")) { PlayerTransactions.Add(line); } else if (line.Contains(" (FMSG) ")) { Fmsg.Add(line); } else if (line.Contains(" (LOCF)[")) { Fmsg.Add(line); } else if (line.Contains(" (LOC)[")) { Local.Add(line); } else if (line.Contains(" (LOC)[")) { Local.Add(line); } else if (line.Contains(" (LOC)[")) { Local.Add(line); } else if (line.Contains(" LC ")) { Emergency.Add(line); } else if (line.Contains(" GroupPromotion: ")) { Group.Add(line); } else if (line.Contains(" modify ")) { Inventory.Add(line); } else if (line.Contains(" Crafting ")) { Inventory.Add(line); } else if (line.Contains(") sold ")) { Trading.Add(line); } else if (line.Contains(" (Bought ")) { Trading.Add(line); } else if (line.Contains(" NC ")) { JoinQuit.Add(line); } else if (line.Contains(" LOGIN MISC: ")) { JoinQuit.Add(line); } else if (line.Contains(" LOGIN: "******" LOGIN WEPS: ")) { JoinQuit.Add(line); } else if (line.Contains(" QUIT: ")) { JoinQuit.Add(line); } else if (line.Contains(" QUIT MISC: ")) { JoinQuit.Add(line); } else if (line.Contains(" QUIT WEPS: ")) { JoinQuit.Add(line); } else if (line.Contains(" QUIT WEPS: ")) { JoinQuit.Add(line); } else { Other.Add(line); } Console.WriteLine($"{DateTime.Now} Thread {Thread.CurrentThread.ManagedThreadId} ended job."); return(Task.CompletedTask); }