public static async void HandleIncomingConnections() { bool runServer = true; // While a user hasn't visited the `shutdown` url, keep on handling requests while (runServer) { // Will wait here until we hear from a connection HttpListenerContext ctx = await Listener.GetContextAsync(); // Peel out the requests and response objects HttpListenerRequest req = ctx.Request; HttpListenerResponse resp = ctx.Response; // Print out some info about the request Console.WriteLine(req.Url.ToString()); World world = Game.OrderManager.World; if (world == null || world.LocalPlayer == null) { continue; } PlayerResources playerResources = world.LocalPlayer.PlayerActor.Trait <PlayerResources>(); if (req.Url.AbsolutePath == "/review") { int n = 0; HashSet <string> refineries = new HashSet <string> { "proc", "refinery" }; foreach (Actor a in world.Actors) { if (a.Owner == world.LocalPlayer && refineries.Contains(a.Info.Name.ToLower())) { n++; } } int cash = (int)(Math.Sqrt(n) * double.Parse(req.Headers.Get("multiplier"))); Console.WriteLine("Giving {0}", cash); playerResources.GiveCash(cash); } // If `shutdown` url requested w/ POST, then shutdown the server after serving the page if ((req.HttpMethod == "POST") && (req.Url.AbsolutePath == "/shutdown")) { Console.WriteLine("Shutdown requested"); runServer = false; } // Make sure we don't increment the page views counter if `favicon.ico` is requested if (req.Url.AbsolutePath != "/favicon.ico") { PageViews += 1; } // Write the response info string disableSubmit = !runServer ? "disabled" : ""; byte[] data = Encoding.UTF8.GetBytes(string.Format(playerResources.Cash.ToString())); resp.ContentType = "text/html"; resp.ContentEncoding = Encoding.UTF8; resp.ContentLength64 = data.LongLength; // Write out to the response stream (asynchronously), then close it await resp.OutputStream.WriteAsync(data, 0, data.Length); resp.Close(); } }
public Player(World world, Session.Client client, PlayerReference pr, MersenneTwister playerRandom) { World = world; InternalName = pr.Name; PlayerReference = pr; inMissionMap = world.Map.Visibility.HasFlag(MapVisibility.MissionSelector); // Real player or host-created bot if (client != null) { ClientIndex = client.Index; Color = client.Color; PlayerName = ResolvePlayerName(client, world.LobbyInfo.Clients, world.Map.Rules.Actors["player"].TraitInfos <IBotInfo>()); BotType = client.Bot; Faction = ResolveFaction(world, client.Faction, playerRandom, !pr.LockFaction); DisplayFaction = ResolveDisplayFaction(world, client.Faction); var assignSpawnPoints = world.WorldActor.TraitOrDefault <IAssignSpawnPoints>(); HomeLocation = assignSpawnPoints?.AssignHomeLocation(world, client, playerRandom) ?? pr.HomeLocation; SpawnPoint = assignSpawnPoints?.SpawnPointForPlayer(this) ?? client.SpawnPoint; DisplaySpawnPoint = client.SpawnPoint; Handicap = client.Handicap; } else { // Map player ClientIndex = 0; // Owned by the host (TODO: fix this) Color = pr.Color; PlayerName = pr.Name; NonCombatant = pr.NonCombatant; Playable = pr.Playable; spectating = pr.Spectating; BotType = pr.Bot; Faction = ResolveFaction(world, pr.Faction, playerRandom, false); DisplayFaction = ResolveDisplayFaction(world, pr.Faction); HomeLocation = pr.HomeLocation; SpawnPoint = DisplaySpawnPoint = 0; Handicap = pr.Handicap; } if (!spectating) { PlayerMask = new LongBitSet <PlayerBitMask>(InternalName); } // Set this property before running any Created callbacks on the player actor IsBot = BotType != null; // Special case handling is required for the Player actor: // Since Actor.Created would be called before PlayerActor is assigned here // querying player traits in INotifyCreated.Created would crash. // Therefore assign the uninitialized actor and run the Created callbacks // by calling Initialize ourselves. var playerActorType = world.Type == WorldType.Editor ? EditorPlayerActorType : PlayerActorType; PlayerActor = new Actor(world, playerActorType, new TypeDictionary { new OwnerInit(this) }); PlayerActor.Initialize(true); Shroud = PlayerActor.Trait <Shroud>(); FrozenActorLayer = PlayerActor.TraitOrDefault <FrozenActorLayer>(); // Enable the bot logic on the host if (IsBot && Game.IsHost) { var logic = PlayerActor.TraitsImplementing <IBot>().FirstOrDefault(b => b.Info.Type == BotType); if (logic == null) { Log.Write("debug", "Invalid bot type: {0}", BotType); } else { logic.Activate(this); } } stanceColors.Self = ChromeMetrics.Get <Color>("PlayerStanceColorSelf"); stanceColors.Allies = ChromeMetrics.Get <Color>("PlayerStanceColorAllies"); stanceColors.Enemies = ChromeMetrics.Get <Color>("PlayerStanceColorEnemies"); stanceColors.Neutrals = ChromeMetrics.Get <Color>("PlayerStanceColorNeutrals"); unlockRenderPlayer = PlayerActor.TraitsImplementing <IUnlocksRenderPlayer>().ToArray(); }
static FactionInfo ResolveFaction(World world, string factionName, MersenneTwister playerRandom, bool requireSelectable) { var factionInfos = world.Map.Rules.Actors["world"].TraitInfos <FactionInfo>(); return(ResolveFaction(factionName, factionInfos, playerRandom, requireSelectable)); }
static FactionInfo ResolveDisplayFaction(World world, string factionName) { var factions = world.Map.Rules.Actors["world"].TraitInfos <FactionInfo>().ToArray(); return(factions.FirstOrDefault(f => f.InternalName == factionName) ?? factions.First()); }
public void Combine(World world, IEnumerable <Actor> newSelection, bool isCombine, bool isClick) { if (isClick) { var adjNewSelection = newSelection.Take(1); /* TODO: select BEST, not FIRST */ if (isCombine) { actors.SymmetricExceptWith(adjNewSelection); } else { actors.Clear(); actors.UnionWith(adjNewSelection); } } else { if (isCombine) { actors.UnionWith(newSelection); } else { actors.Clear(); actors.UnionWith(newSelection); } } foreach (var a in newSelection) { foreach (var sel in a.TraitsImplementing <INotifySelected>()) { sel.Selected(a); } } foreach (var ns in world.WorldActor.TraitsImplementing <INotifySelection>()) { ns.SelectionChanged(); } if (world.IsGameOver) { return; } // Play the selection voice from one of the selected actors // TODO: This probably should only be considering the newly selected actors // TODO: Ship this into an INotifySelection trait to remove the engine dependency on Selectable foreach (var actor in actors) { if (actor.Owner != world.LocalPlayer || !actor.IsInWorld) { continue; } var selectable = actor.Info.TraitInfoOrDefault <SelectableInfo>(); if (selectable == null || !actor.HasVoice(selectable.Voice)) { continue; } actor.PlayVoice(selectable.Voice); break; } }