Beispiel #1
0
        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);
        }
Beispiel #2
0
        public void OnPlaceClaimCmd(PlaceClaimMsg msg)
        {
            // Apian has said this message is authoritative
            BaseBike b = CoreData.GetBaseBike(msg.bikeId);

            if (CoreData.Ground.IndicesAreOnMap(msg.xIdx, msg.zIdx))
            {
                if (b == null)
                {
                    logger.Warn($"OnPlaceClaimCmd() Bike:{msg.bikeId} not found!"); // can happen if RemoveCmd and ClaimObs interleave
                    return;
                }

                b.UpdatePosFromCommand(msg.TimeStamp, FrameApianTime, BeamPlace.PlacePos(msg.xIdx, msg.zIdx), msg.exitHead);

                // Claim it
                BeamPlace p = CoreData.ClaimPlace(b, msg.xIdx, msg.zIdx, msg.TimeStamp + BeamPlace.kLifeTimeMs);
                if (p != null)
                {
                    ApplyScoreUpdate(msg.scoreUpdates);
                    logger.Verbose($"OnPlaceClaimCmd() Bike: {b.bikeId} claimed {BeamPlace.PlacePos( msg.xIdx, msg.zIdx).ToString()} at {msg.TimeStamp}");
                    //logger.Verbose($"                  BikePos: {b.position.ToString()}, FrameApianTime: {FrameApianTime} ");
                    //logger.Verbose($"   at Timestamp:  BikePos: {b.PosAtTime(msg.TimeStamp, FrameApianTime).ToString()}, Time: {msg.TimeStamp} ");
                    PlaceClaimedEvt?.Invoke(this, p);
                }
                else
                {
                    logger.Warn($"OnPlaceClaimCmd()) failed. Place already claimed.");
                }
            }
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        public void OnBikeCommandCmd(BikeCommandMsg msg)
        {
            BaseBike bb = CoreData.GetBaseBike(msg.bikeId);

            logger.Verbose($"OnBikeCommandCmd({msg.cmd}) Now: {FrameApianTime} Ts: {msg.TimeStamp} Bike:{msg.bikeId}");
            bb.ApplyCommand(msg.cmd, new Vector2(msg.nextPtX, msg.nextPtZ), msg.TimeStamp);
        }
Beispiel #5
0
        // Gameplay control

        protected string SpawnAiBike()
        {
            BaseBike bb = game.CreateBaseBike(BikeFactory.AiCtrl, game.LocalPeerId, BikeDemoData.RandomName(), BikeDemoData.RandomTeam());

            game.PostBikeCreateData(bb); // will result in OnBikeInfo()
            logger.Debug($"{this.ModeName()}: SpawnAiBike({ bb.bikeId})");
            return(bb.bikeId);           // the bike hasn't been added yet, so this id is not valid yet.
        }
Beispiel #6
0
        protected string SpawnPlayerBike()
        {
            // Create one the first time
            string scrName = game.frontend.GetUserSettings().screenName;

            BaseBike bb = game.CreateBaseBike(BikeFactory.LocalPlayerCtrl, game.LocalPeerId, scrName, BikeDemoData.RandomTeam());

            game.PostBikeCreateData(bb); // will result in OnBikeInfo()
            logger.Debug($"{this.ModeName()}: SpawnAiBike({ bb.bikeId})");
            return(bb.bikeId);           // the bike hasn't been added yet, so this id is not valid yet.
        }
Beispiel #7
0
 protected string SpawnPlayerBike()
 {
     if (settings.localPlayerCtrlType != "none")
     {
         BaseBike bb = game.CreateBaseBike(settings.localPlayerCtrlType, game.LocalPeerId, game.LocalPlayer.Name, BikeDemoData.RandomTeam());
         game.PostBikeCreateData(bb); // will result in OnBikeInfo()
         logger.Debug($"{this.ModeName()}: SpawnPlayerBike({ bb.bikeId})");
         return(bb.bikeId);           // the bike hasn't been added yet, so this id is not valid yet.
     }
     return(null);
 }
Beispiel #8
0
        public void OnBikeTurnCmd(BikeTurnMsg msg)
        {
            BaseBike bb = CoreData.GetBaseBike(msg.bikeId);

            logger.Verbose($"OnBikeTurnCmd({msg.dir}) Now: {FrameApianTime} Ts: {msg.TimeStamp} Bike:{msg.bikeId}");
            if (bb == null)
            {
                logger.Warn($"OnBikeTurnCmd() Bike:{msg.bikeId} not found!");
            }
            bb?.ApplyTurn(msg.dir, msg.entryHead, new Vector2(msg.nextPtX, msg.nextPtZ), msg.TimeStamp, msg.bikeState);
        }
Beispiel #9
0
 protected void _CreateLocalBike(string bikeCtrlType)
 {
     if (bikeCtrlType == "none")
     {
         logger.Info($"No LOCAL PLAYER BIKE created.");
     }
     else
     {
         _localBikesToCreate++;
         string   scrName = game.frontend.GetUserSettings().screenName;
         BaseBike bb      = game.CreateBaseBike(bikeCtrlType, game.LocalPeerId, game.LocalPlayer.Name, BikeDemoData.RandomTeam());
         game.PostBikeCreateData(bb); // will result in OnBikeInfo()
     }
 }
Beispiel #10
0
        protected string SpawnAIBike(string name = null, Team team = null)
        {
            if (name == null)
            {
                name = BikeDemoData.RandomName();
            }

            if (team == null)
            {
                team = BikeDemoData.RandomTeam();
            }

            BaseBike bb = game.CreateBaseBike(BikeFactory.AiCtrl, game.LocalPeerId, name, team);

            game.PostBikeCreateData(bb); // will result in OnBikeInfo()
            logger.Debug($"{this.ModeName()}: SpawnAiBike({ bb.bikeId})");
            return(bb.bikeId);           // the bike hasn't been added yet, so this id is not valid yet.
        }
Beispiel #11
0
        public void OnPlaceHitCmd(PlaceHitMsg msg)
        {
            // Apian has already checked the the place is claimed and the bike exists
            Vector2   pos         = BeamPlace.PlacePos(msg.xIdx, msg.zIdx);
            BeamPlace p           = CoreData.GetPlace(pos);
            BaseBike  hittingBike = CoreData.GetBaseBike(msg.bikeId);

            if (p != null && hittingBike != null)
            {
                hittingBike.UpdatePosFromCommand(msg.TimeStamp, FrameApianTime, p.GetPos(), msg.exitHead);
                logger.Info($"OnPlaceHitCmd{p?.GetPos().ToString()} Now: {FrameApianTime} Ts: {msg.TimeStamp} Bike: {hittingBike?.bikeId} Pos: {p?.GetPos().ToString()}");
                ApplyScoreUpdate(msg.scoreUpdates);
                PlaceHitEvt?.Invoke(this, new PlaceHitArgs(p, hittingBike));
            }
            else
            {
                logger.Info($"OnPlaceHitCmd{p?.GetPos().ToString()} Now: {FrameApianTime} Ts: {msg.TimeStamp} Bike: {hittingBike?.bikeId} Pos: {p?.GetPos().ToString()}");
            }
        }
Beispiel #12
0
        protected void xOnScoreEvent(BaseBike bike, ScoreEvent evt, BeamPlace place)
        {
            // Score events:
            //
            // TODO: as with above: This is coming from the backend (BaseBike, mostly) and should
            // be comming from the Net/event/whatever layer
            // NOTE: I'm not so sure about above comment. It;'s not clear that score changes constitute "events"
            int scoreDelta = GameConstants.eventScores[(int)evt];

            bike.AddScore(scoreDelta);

            if (evt == ScoreEvent.kHitEnemyPlace || evt == ScoreEvent.kHitFriendPlace)
            {
                logger.Debug($"OnScoreEvent(). Bike: {bike.bikeId} Event: {evt}");

                // half of the deduction goes to the owner of the place, the rest is divded
                // among the owner's team
                // UNLESS: the bike doing the hitting IS the owner - then the rest of the team just splits it
                if (bike != place.bike)
                {
                    scoreDelta /= 2;
                    place.bike.AddScore(-scoreDelta); // adds
                }

                IEnumerable <IBike> rewardedOtherBikes =
                    CoreData.Bikes.Values.Where(b => b != bike && b.team == place.bike.team);   // Bikes other the "bike" on affected team
                if (rewardedOtherBikes.Count() > 0)
                {
                    foreach (BaseBike b  in rewardedOtherBikes)
                    {
                        b.AddScore(-scoreDelta / rewardedOtherBikes.Count());
                    }
                }
            }

            if (evt == ScoreEvent.kOffMap || bike.score <= 0)
            {
                bike.score = 0;
                logger.Info($"OnScoreEvent(). Sending RemoveBikeObs: {bike.bikeId}");
                apian.SendRemoveBikeObs(FrameApianTime, bike.bikeId);
            }
        }