Beispiel #1
0
        private static void Networker_Disconnected(object sender, ClientDisconnectedEventArgs e)
        {
            string reason = e.Reason;

            if (string.IsNullOrWhiteSpace(reason))
            {
                reason = ". No reason given.";
            }
            else
            {
                reason = ": " + reason;
            }

            if (CupboardApp.Networker.Hosting)
            {
                OldContext.ShowNotification("Info", e.RemoteEndPoint + " left the server.", duration: 3);
            }
            else
            {
                string type = reason == "Quit" ? "Info" : "Error";

                OldContext.ShowNotification(type, "Disconnected from server" + reason, duration: 3, channel: "Main");
                Scene.SwitchTo(_menuScene);
            }
        }
        public static void Sync(INetworker networker)
        {
            // For now, allowing both sides to push. This behaviour needs to be replaced in general anyways
            if (networker.Hosting || true)
            {
                var save = Scene.Current.FindComponent <GamePieces>()?.GetSave();

                if (!save.HasValue || save.Value.Res == null)
                {
                    Logs.Game.WriteWarning("Attempted to sync, but no gave save found.");
                    OldContext.ShowNotification("Error", "Failed to start sync.", 3);
                }
                else
                {
                    SendSave(networker, save.Value.Res);
                    OldContext.ShowNotification("Info", "Sync started.", 3);
                }
            }

            else if (networker.Connected)
            {
                networker.SendData(CommandChannel, "Sync");
                OldContext.ShowNotification("Info", "Sync requested.", 3);
            }

            else
            {
                Logs.Game.WriteWarning($"Cannot try to sync while {networker.Status.ToString().ToLower()}");
            }
        }
        private static void ParseIncomingDragMessage(DataRecievedEventArgs e)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.Data)))
            {
                var message = Serializer.TryReadObject <DragMessage>(stream);

                bool success = false;

                if (message == null || message.Name == null)
                {
                    Logs.Game.WriteWarning("Failed to read drag message sent by " + e.RemoteEndPoint);
                }
                else
                {
                    var draggable = Scene.Current.FindGameObject(message.Name)?.GetComponent <Draggable>();

                    if (draggable == null)
                    {
                        Logs.Game.WriteWarning("Recieved dragmessage, but could not find a draggable for it.");
                    }

                    else
                    {
                        switch (message.Action)
                        {
                        case DragAction.Start:
                            draggable.StartDrag();
                            Logs.Game.Write("Recieved drag message: Start");
                            break;

                        case DragAction.Move:
                            if (message.Pos.HasValue)
                            {
                                draggable.ContinueDrag(ConvertPosition(draggable, message.Pos.Value));
                            }
                            Logs.Game.Write($"Recieved drag message: Continue [{message.Pos.Value}]");
                            break;

                        case DragAction.End:
                            if (message.Pos.HasValue)
                            {
                                draggable.ContinueDrag(ConvertPosition(draggable, message.Pos.Value));
                            }
                            draggable.EndDrag();
                            Logs.Game.Write($"Recieved drag message: End [{message.Pos.Value}]");
                            break;
                        }

                        success = true;
                    }
                }

                if (!success)
                {
                    OldContext.ShowNotification("Error", "Failed to load incoming drag message", 3);
                }
            }
        }
Beispiel #4
0
        private static void Networker_Joined(object sender, ServerJoinedEventArgs e)
        {
            if (CupboardApp.Networker.Hosting)
            {
                OldContext.ShowNotification("Success", $"{e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port} has joined.", duration: 3);
                GameNetworking.Sync(CupboardApp.Networker);
            }
            else
            {
                OldContext.ShowNotification("Success", $"Joined server {e.RemoteEndPoint.Address}:{e.RemoteEndPoint.Port}", duration: 3);

                var joinButton = Scene.Current.FindComponents <Button>().Where(x => x.Command == "Cancel").FirstOrDefault();
                joinButton.Command = "Join";

                Scene.SwitchTo(_gameScene);
            }
        }
        private static void ParseIncomingDiceMessage(DataRecievedEventArgs e)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.Data)))
            {
                var message = Serializer.TryReadObject <DiceMessage>(stream);

                bool success = false;

                if (message == null || message.DiceID == null)
                {
                    Logs.Game.WriteWarning("Failed to read dice message sent by " + e.RemoteEndPoint);
                }
                else
                {
                    var dice = Scene.Current
                               .FindComponents <Dice>()
                               .Where(x => x.DiceID == message.DiceID)
                               .FirstOrDefault();

                    if (Warnings.NullOrDisposed(dice))
                    {
                        Logs.Game.WriteWarning("Recieved dice message, but could not find a pawn for it.");
                    }

                    else
                    {
                        if (!dice.Rolling)
                        {
                            dice.Roll(message.Time, message.Side);
                        }

                        else
                        {
                            OldContext.ShowNotification("Error", "Simultaneous dice rolls.", 3);
                        }

                        success = true;
                    }
                }

                if (!success)
                {
                    OldContext.ShowNotification("Error", "Failed to load incoming dice message.", 3);
                }
            }
        }
        private static void ParseIncomingSpinMessage(DataRecievedEventArgs e)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.Data)))
            {
                var message = Serializer.TryReadObject <SpinMessage>(stream);

                bool success = false;

                if (message == null || message.Name == null)
                {
                    Logs.Game.WriteWarning("Failed to read spin message sent by " + e.RemoteEndPoint);
                }
                else
                {
                    var target  = Scene.Current.FindGameObject(message.Name);
                    var spinner = Scene.Current.FindComponent <Spinner>();

                    if (Warnings.NullOrDisposed(target))
                    {
                        Logs.Game.WriteWarning("Recieved spin message, but could not find a target for it.");
                    }

                    else if (Warnings.NullOrDisposed(spinner))
                    {
                        Logs.Game.WriteWarning("Recieved spin message, but could not find a spinner for it.");
                    }

                    else
                    {
                        spinner.Spin(target, message.Angle);
                        success = true;
                    }
                }

                if (!success)
                {
                    OldContext.ShowNotification("Error", "Failed to load incoming spin message.", 3);
                }
            }
        }
        private static void ParseIncomingSave(DataRecievedEventArgs e)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.Data)))
            {
                var save = Serializer.TryReadObject <Save>(stream);

                bool success = false;

                if (save == null)
                {
                    Logs.Game.WriteWarning("Failed to read save sent by " + e.RemoteEndPoint);
                }
                else
                {
                    var game = Scene.Current.FindComponent <GamePieces>();

                    if (game == null)
                    {
                        Logs.Game.WriteWarning("Recieved save, but could not find a game for it.");
                    }

                    else
                    {
                        game.Load(save, instant: true);
                        game.Save();
                        success = true;
                    }
                }

                if (success)
                {
                    OldContext.ShowNotification("Success", "Recieved save", 3);
                }
                else
                {
                    OldContext.ShowNotification("Error", "Failed to load incoming save", 3);
                }
            }
        }
Beispiel #8
0
 private static void Networker_UnexpectedError(object sender, NetErrorEventArgs e)
 {
     OldContext.ShowNotification("Error", e.UserMessage, channel: "Error");
 }