Пример #1
0
        private static void HandleLowestRateChange(ClientObject client)
        {
            string         newWarpMaster = null;
            PlayerWarpRate lowestRate    = null;

            if (warpMaster != null && warpList.ContainsKey(warpMaster))
            {
                newWarpMaster = warpMaster;
                lowestRate    = warpList[warpMaster];
            }
            else
            {
                newWarpMaster = client.playerName;
                lowestRate    = warpList[client.playerName];
            }

            foreach (ClientObject testClient in ClientHandler.GetClients())
            {
                if (!warpList.ContainsKey(testClient.playerName))
                {
                    newWarpMaster = null;
                    break;
                }

                PlayerWarpRate pwr = warpList[testClient.playerName];

                if (pwr.rateIndex == 0)
                {
                    newWarpMaster = null;
                    break;
                }
                if (pwr.isPhysWarp != lowestRate.isPhysWarp)
                {
                    newWarpMaster = null;
                    break;
                }
                if (pwr.rateIndex < lowestRate.rateIndex)
                {
                    newWarpMaster = testClient.playerName;
                    lowestRate    = pwr;
                }
            }

            if (newWarpMaster != warpMaster)
            {
                //No expire time
                SendSetController(newWarpMaster, long.MinValue);
                warpMaster = newWarpMaster;
            }
        }
Пример #2
0
        private static void HandleChangeWarp(ClientObject client, bool physWarp, int rateIndex, long serverClock, double planetTime)
        {
            ServerMessage newMessage = new ServerMessage();

            newMessage.type = ServerMessageType.WARP_CONTROL;
            using (MessageWriter mw = new MessageWriter())
            {
                mw.Write <int>((int)WarpMessageType.CHANGE_WARP);
                mw.Write <string>(client.playerName);
                mw.Write <bool>(physWarp);
                mw.Write <int>(rateIndex);
                mw.Write <long>(serverClock);
                mw.Write <double>(planetTime);
                newMessage.data = mw.GetMessageBytes();
            }
            if (Settings.settingsStore.warpMode == WarpMode.MCW_LOWEST)
            {
                PlayerWarpRate clientWarpRate = null;

                if (!warpList.ContainsKey(client.playerName))
                {
                    clientWarpRate = new PlayerWarpRate();
                    warpList.Add(client.playerName, clientWarpRate);
                }
                else
                {
                    clientWarpRate = warpList[client.playerName];
                }
                clientWarpRate.isPhysWarp  = physWarp;
                clientWarpRate.rateIndex   = rateIndex;
                clientWarpRate.serverClock = serverClock;
                clientWarpRate.planetTime  = planetTime;
                HandleLowestRateChange(client);
            }
            ClientHandler.SendToAll(client, newMessage, true);
        }
Пример #3
0
        private void HandleWarpMessage(byte[] messageData)
        {
            using (MessageReader mr = new MessageReader(messageData, false))
            {
                WarpMessageType messageType = (WarpMessageType)mr.Read<int>();
                string fromPlayer = mr.Read<string>();
                switch (messageType)
                {
                    case WarpMessageType.REQUEST_VOTE:
                        {
                            if (warpMode == WarpMode.MCW_VOTE)
                            {
                                if (voteMaster == "")
                                {
                                    voteMaster = fromPlayer;
                                }
                                else
                                {
                                    //Freak out and tell everyone to reset their warp votes - This can happen if 2 clients start a vote at the exact same time.
                                    ReleaseWarpMaster();
                                }
                            }
                        }
                        break;
                    case WarpMessageType.REPLY_VOTE:
                        {
                            if (warpMode == WarpMode.MCW_VOTE)
                            {
                                if (voteMaster == Settings.fetch.playerName && warpMaster == "")
                                {
                                    if (!voteList.ContainsKey(fromPlayer))
                                    {
                                        bool vote = mr.Read<bool>();
                                        DarkLog.Debug(fromPlayer + " voted " + vote);
                                        voteList.Add(fromPlayer, vote);
                                    }
                                    voteNoCount = 0;
                                    voteYesCount = 0;
                                    foreach (KeyValuePair<string,bool> vote in voteList)
                                    {
                                        if (vote.Value)
                                        {
                                            voteYesCount++;
                                        }
                                        else
                                        {
                                            voteNoCount++;
                                        }
                                    }

                                    //We have enough votes
                                    if (voteYesCount >= voteNeededCount)
                                    {
                                        //Vote has passed.
                                        warpMasterOwnerTime = UnityEngine.Time.realtimeSinceStartup;
                                        warpMaster = Settings.fetch.playerName;
                                        using (MessageWriter mw = new MessageWriter())
                                        {
                                            mw.Write<int>((int)WarpMessageType.SET_CONTROLLER);
                                            mw.Write<string>(Settings.fetch.playerName);
                                            mw.Write<string>(Settings.fetch.playerName);
                                            NetworkWorker.fetch.SendWarpMessage(mw.GetMessageBytes());
                                        }
                                    }
                                    //We have enough votes
                                    if (voteNoCount >= voteFailedCount)
                                    {
                                        //Vote has failed.
                                        ReleaseWarpMaster();
                                        DisplayMessage("Vote failed!", 5f);
                                    }
                                }
                            }
                        }
                        break;
                    case WarpMessageType.SET_CONTROLLER:
                        {
                            if (warpMode == WarpMode.MCW_FORCE || warpMode == WarpMode.MCW_VOTE || warpMode == WarpMode.MCW_LOWEST)
                            {
                                string newController = mr.Read<string>();
                                warpMaster = newController;
                                if (warpMode == WarpMode.MCW_FORCE && newController == "")
                                {
                                    warpMasterOwnerTime = 0f;
                                }
                                if (warpMode == WarpMode.MCW_VOTE && newController == "")
                                {
                                    TimeWarp.SetRate(0, true);
                                    CancelVote();
                                }
                            }
                        }
                        break;
                    case WarpMessageType.CHANGE_WARP:
                        {
                            if (warpMode == WarpMode.MCW_FORCE || warpMode == WarpMode.MCW_VOTE || warpMode == WarpMode.MCW_LOWEST || warpMode == WarpMode.SUBSPACE)
                            {
                                bool newPhysWarp = mr.Read<bool>();
                                int newRateIndex = mr.Read<int>();
                                if (clientWarpList.ContainsKey(fromPlayer))
                                {
                                    clientWarpList[fromPlayer].isPhysWarp = newPhysWarp;
                                    clientWarpList[fromPlayer].rateIndex = newRateIndex;
                                }
                                else
                                {
                                    PlayerWarpRate newPlayerWarpRate = new PlayerWarpRate();
                                    newPlayerWarpRate.isPhysWarp = newPhysWarp;
                                    newPlayerWarpRate.rateIndex = newRateIndex;
                                    clientWarpList.Add(fromPlayer, newPlayerWarpRate);
                                }
                                //DarkLog.Debug(fromPlayer + " warp rate changed, Physwarp: " + newPhysWarp + ", Index: " + newRateIndex);
                            }
                        }
                        break;
                    case WarpMessageType.NEW_SUBSPACE:
                        {
                            int newSubspaceID = mr.Read<int>();
                            long serverTime = mr.Read<long>();
                            double planetariumTime = mr.Read<double>();
                            float gameSpeed = mr.Read<float>();
                            TimeSyncer.fetch.LockNewSubspace(newSubspaceID, serverTime, planetariumTime, gameSpeed);
                            if (((warpMode == WarpMode.MCW_VOTE) || (warpMode == WarpMode.MCW_FORCE)) && (warpMaster == fromPlayer))
                            {
                                TimeSyncer.fetch.LockSubspace(newSubspaceID);
                            }
                            if (!TimeSyncer.fetch.locked && TimeSyncer.fetch.currentSubspace == newSubspaceID)
                            {
                                TimeSyncer.fetch.LockSubspace(newSubspaceID);
                            }
                        }
                        break;
                    case WarpMessageType.CHANGE_SUBSPACE:
                        {
                            int changeSubspaceID = mr.Read<int>();
                            clientSubspaceList[fromPlayer] = changeSubspaceID;
                        }
                        break;
                    case WarpMessageType.RELOCK_SUBSPACE:
                        {
                            if (fromPlayer == "Server")
                            {
                                int subspaceID = mr.Read<int>();
                                long serverTime = mr.Read<long>();
                                double planetariumTime = mr.Read<double>();
                                float gameSpeed = mr.Read<float>();
                                TimeSyncer.fetch.RelockSubspace(subspaceID, serverTime, planetariumTime, gameSpeed);
                            }
                        }
                        break;
                    case WarpMessageType.REPORT_RATE:
                        {
                            //Not interested in subspace.
                            mr.Read<int>();
                            clientSkewList[fromPlayer] = mr.Read<float>();
                        }
                        break;
                    default:
                        {
                            DarkLog.Debug("Unhandled WARP_MESSAGE type: " + messageType);
                            break;
                        }
                }
            }
        }
Пример #4
0
 private void HandleChangeWarp(string fromPlayer, bool isPhysWarp, int newRate, long serverClock, double planetTime)
 {
     if (warpMode != WarpMode.NONE)
     {
         if (clientWarpList.ContainsKey(fromPlayer))
         {
             clientWarpList[fromPlayer].isPhysWarp = isPhysWarp;
             clientWarpList[fromPlayer].rateIndex = newRate;
             clientWarpList[fromPlayer].serverClock = serverClock;
             clientWarpList[fromPlayer].planetTime = planetTime;
         }
         else
         {
             PlayerWarpRate newPlayerWarpRate = new PlayerWarpRate();
             newPlayerWarpRate.isPhysWarp = isPhysWarp;
             newPlayerWarpRate.rateIndex = newRate;
             newPlayerWarpRate.serverClock = serverClock;
             newPlayerWarpRate.planetTime = planetTime;
             clientWarpList.Add(fromPlayer, newPlayerWarpRate);
         }
     }
 }
Пример #5
0
 private void HandleMCWLowestInput(bool startWarpKey, bool stopWarpKey)
 {
     int newRequestIndex = requestIndex;
     bool newRequestPhysWarp = requestPhysWarp;
     if (startWarpKey)
     {
         if (requestIndex == 0)
         {
             newRequestPhysWarp = GameSettings.MODIFIER_KEY.GetKey();
         }
         float[] physRates = TimeWarp.fetch.physicsWarpRates;
         float[] warpRates = TimeWarp.fetch.warpRates;
         newRequestIndex++;
         if (newRequestPhysWarp && newRequestIndex >= physRates.Length)
         {
             newRequestIndex = physRates.Length - 1;
         }
         if (!newRequestPhysWarp && newRequestIndex >= warpRates.Length)
         {
             newRequestIndex = warpRates.Length - 1;
         }
     }
     if (stopWarpKey)
     {
         newRequestIndex--;
         if (newRequestIndex < 0)
         {
             newRequestIndex = 0;
         }
         if (newRequestIndex == 0)
         {
             newRequestPhysWarp = false;
         }
     }
     if ((newRequestIndex != requestIndex) || (newRequestPhysWarp != requestPhysWarp))
     {
         requestIndex = newRequestIndex;
         requestPhysWarp = newRequestPhysWarp;
         PlayerWarpRate newWarpRate = new PlayerWarpRate();
         newWarpRate.isPhysWarp = requestPhysWarp;
         newWarpRate.rateIndex = requestIndex;
         newWarpRate.planetTime = Planetarium.GetUniversalTime();
         newWarpRate.serverClock = TimeSyncer.fetch.GetServerClock();
         clientWarpList[Settings.fetch.playerName] = newWarpRate;
         DarkLog.Debug("Warp request change: " + requestIndex + ", physwarp: " + requestPhysWarp);
         using (MessageWriter mw = new MessageWriter())
         {
             mw.Write<int>((int)WarpMessageType.CHANGE_WARP);
             mw.Write<bool>(requestPhysWarp);
             mw.Write<int>(requestIndex);
             mw.Write<long>(TimeSyncer.fetch.GetServerClock());
             mw.Write<double>(Planetarium.GetUniversalTime());
             NetworkWorker.fetch.SendWarpMessage(mw.GetMessageBytes());
         }
     }
 }
Пример #6
0
        private void SetTimeFromWarpEntry(PlayerWarpRate masterWarpRate)
        {
            float[] warpRates = null;
            if (masterWarpRate.isPhysWarp)
            {
                TimeWarp.fetch.Mode = TimeWarp.Modes.LOW;
                warpRates = TimeWarp.fetch.physicsWarpRates;
            }
            else
            {
                TimeWarp.fetch.Mode = TimeWarp.Modes.HIGH;
                warpRates = TimeWarp.fetch.warpRates;
            }

            if (TimeWarp.WarpMode != TimeWarp.Modes.LOW && masterWarpRate.isPhysWarp)
            {
                TimeWarp.fetch.Mode = TimeWarp.Modes.LOW;
            }

            if (TimeWarp.WarpMode != TimeWarp.Modes.HIGH && !masterWarpRate.isPhysWarp)
            {
                TimeWarp.fetch.Mode = TimeWarp.Modes.HIGH;
            }

            //Set warp
            if (TimeWarp.CurrentRateIndex != masterWarpRate.rateIndex)
            {
                TimeWarp.SetRate(masterWarpRate.rateIndex, true);
            }

            //Set clock
            long serverClockDiff = TimeSyncer.fetch.GetServerClock() - masterWarpRate.serverClock;
            double secondsDiff = serverClockDiff / 10000000d;
            double newTime = masterWarpRate.planetTime + (warpRates[masterWarpRate.rateIndex] * secondsDiff);
            Planetarium.SetUniversalTime(newTime);
        }