public static BeamCoreState FromApianSerialized(long seqNum, long timeStamp, string stateHash, string serializedData) { BeamCoreState newState = new BeamCoreState(null); JArray sData = JArray.Parse(serializedData); long newSeq = (long)sData[0]; Dictionary <string, BeamPlayer> newPlayers = (sData[1] as JArray) .Select(s => BeamPlayer.FromApianJson((string)s)) .ToDictionary(p => p.PeerId); List <string> peerIds = newPlayers.Values.OrderBy(p => p.PeerId).Select((p) => p.PeerId).ToList(); // to replace array indices in bikes Dictionary <string, IBike> newBikes = (sData[2] as JArray) .Select(s => (IBike)BaseBike.FromApianJson((string)s, newState, peerIds, timeStamp)) .ToDictionary(p => p.bikeId); List <string> bikeIds = newBikes.Values.OrderBy(p => p.bikeId).Select((p) => p.bikeId).ToList(); // to replace array indices in places Dictionary <int, BeamPlace> newPlaces = (sData[3] as JArray) .Select(s => BeamPlace.FromApianJson((string)s, bikeIds, newBikes)) .ToDictionary(p => p.PosHash); newState.Players = newPlayers; newState.Bikes = newBikes; newState.activePlaces = newPlaces; newState.UpdateCommandSequenceNumber(seqNum); return(newState); }
public static BaseBike FromApianJson(string jsonData, BeamCoreState gData, List <string> peerIdList, long timeStamp) { object[] data = JsonConvert.DeserializeObject <object[]>(jsonData); Vector2 lastGridPos = new Vector2((long)data[5] * .001f, (long)data[6] * .001f); long lastGridTime = (long)data[7]; Heading head = (Heading)(long)data[8]; float speed = (float)(double)data[9]; int score = (int)(long)data[10]; TurnDir pendingTurn = (TurnDir)(long)data[11]; float secsSinceGrid = (timeStamp - lastGridTime) * .001f; gData.Logger.Info($"BaseBike FromApianJson() - Id: {(string)data[0]} curTime: {timeStamp} timeAtPos: {lastGridTime} lastGridPos: {lastGridPos.ToString()}"); BaseBike bb = new BaseBike( gData, (string)data[0], // bikeId peerIdList[(int)(long)data[1]], // peerId (string)data[2], // _name Team.teamData[(int)(long)data[3]], // Team (string)data[4], // ctrl, lastGridTime, // timeAtPosition lastGridPos, // current pos head); // head) bb.speed = speed; bb.score = score; bb.basePendingTurn = pendingTurn; return(bb); }
public void ApplyCheckpointStateData(long seqNum, long timeStamp, string stateHash, string serializedData) { logger.Debug($"ApplyStateData() Seq#: seqNum ApianTime: {timeStamp}"); UpdateFrameTime(timeStamp); CoreData = BeamCoreState.FromApianSerialized(seqNum, timeStamp, stateHash, serializedData); OnNewCoreState(); // send NewCoreStateEvt foreach (BeamPlayer p in CoreData.Players.Values) { if (p.PeerId == LocalPeerId) { LocalPlayer = p; } PlayerJoinedEvt.Invoke(this, new PlayerJoinedArgs(CurrentGameId, p)); } foreach (IBike ib in CoreData.Bikes.Values) { NewBikeEvt?.Invoke(this, ib); } foreach (BeamPlace p in CoreData.activePlaces.Values) { CoreData.AnnounceNewPlace(p); // } }
public static MoveNode BuildMoveTree(BeamCoreState g, Vector2 curPos, Heading curHead, int depth, List <Vector2> otherBadPos = null) { Vector2 nextPos = UpcomingGridPoint(curPos, curHead); MoveNode root = MoveNode.GenerateTree(g, nextPos, curHead, 1, otherBadPos); return(root); }
public BaseBike(BeamCoreState gData, string _id, string _peerId, string _name, Team _team, string ctrl, long initialTime, Vector2 initialPos, Heading head) { gameData = gData; bikeId = _id; peerId = _peerId; name = _name; team = _team; basePosition = initialPos; baseTime = initialTime; baseHeading = head; ctrlType = ctrl; score = kStartScore; logger = UniLogger.GetLogger("BaseBike"); }
public List <MoveNode> next; // length 3 public MoveNode(BeamCoreState gData, Vector2 p, Heading head, TurnDir d, int depth, List <Vector2> otherClaimedPos) { pos = p; dir = d; // for later lookup place = gData.GetPlace(p); score = ScoreForPoint(gData.Ground, pos, place); if (score == 0 && otherClaimedPos.Any(op => op.Equals(pos))) // TODO: make prettier { score = 1; // TODO: use named scoring constants } next = depth < 1 ? null : BikeUtils.PossiblePointsForPointAndHeading(pos, head) .Select((pt, childTurnDir) => new MoveNode(gData, pos + GameConstants.UnitOffset2ForHeading(GameConstants.NewHeadForTurn(head, (TurnDir)childTurnDir)) * Ground.gridSize, head, (TurnDir)childTurnDir, depth - 1, otherClaimedPos)) .ToList(); }
public BeamAppCore(IBeamFrontend fep) { logger = UniLogger.GetLogger("GameInstance"); frontend = fep; CoreData = new BeamCoreState(frontend); OnNewCoreState(); commandHandlers = new Dictionary <string, Action <BeamMessage> >() { [BeamMessage.kNewPlayer] = (msg) => OnNewPlayerCmd(msg as NewPlayerMsg), [BeamMessage.kPlayerLeft] = (msg) => OnPlayerLeftCmd(msg as PlayerLeftMsg), [BeamMessage.kBikeCreateData] = (msg) => this.OnCreateBikeCmd(msg as BikeCreateDataMsg), [BeamMessage.kRemoveBikeMsg] = (msg) => this.OnRemoveBikeCmd(msg as RemoveBikeMsg), [BeamMessage.kBikeTurnMsg] = (msg) => this.OnBikeTurnCmd(msg as BikeTurnMsg), [BeamMessage.kBikeCommandMsg] = (msg) => this.OnBikeCommandCmd(msg as BikeCommandMsg), [BeamMessage.kPlaceClaimMsg] = (msg) => this.OnPlaceClaimCmd(msg as PlaceClaimMsg), [BeamMessage.kPlaceHitMsg] = (msg) => this.OnPlaceHitCmd(msg as PlaceHitMsg), [BeamMessage.kPlaceRemovedMsg] = (msg) => this.OnPlaceRemovedCmd(msg as PlaceRemovedMsg), }; }
public static MoveNode GenerateTree(BeamCoreState gd, Vector2 rootPos, Heading initialHead, int depth, List <Vector2> otherBadPos) { return(new MoveNode(gd, rootPos, initialHead, TurnDir.kStraight, depth, otherBadPos)); }