示例#1
0
 public void ServerMessage(DnDMessage msg)
 {
     if (msg.Action == "not_your_turn")
     {
         //deactivate what is not needed while it's not the player's turn
     }
     else if (msg.Action == "your_turn")
     {
         //activate the form controls
     }
 }
示例#2
0
        private void btnMove_Click(object sender, EventArgs e)
        {
            //the point where the player goes
            Point point = new Point(10, 10);

            //send the new location to the server
            DnDMessage msg = new DnDMessage("move_player", new Dictionary <string, string> {
                { "name", character.CharName },
                { "x", point.X.ToString() }, { "y", point.Y.ToString() }
            });

            client.sendMessage(msg);
        }
示例#3
0
        public PlayerForm(Character c, TCPClient client)
        {
            InitializeComponent();
            pbGameGrid.Width  = 900;
            pbGameGrid.Height = 495;
            this.character    = c;
            this.client       = client;
            DnDMessage msg = new DnDMessage("initialize_player", character.ToDictionary());

            client.sendMessage(msg);
            MessageBox.Show(c.CharName);
            msg = new DnDMessage("weapon", character.CharWeapon.ToDictionary());
            client.sendMessage(msg);
            this.TopMost     = true;
            this.WindowState = FormWindowState.Maximized;
            client.AddMessageDelegate(ServerMessage);
        }
示例#4
0
        private void ReceiveCallback(IAsyncResult AR)
        {
            Socket current = (Socket)AR.AsyncState;
            int    received;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                disconnect();
                return;
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(_buffer, recBuf, received);
            if (received > 0)
            {
                DnDMessage message = DnDMessage.createFromByteArray(recBuf);
                invokeMessageDelegates(messageDelegates, message);
            }
            else
            {
                byte[] data = Encoding.ASCII.GetBytes(" ");
                try
                {
                    current.Send(data);
                }
                catch (SocketException) { }
            }

            try
            {
                current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
            }
            catch (SocketException)
            {
                disconnect();
                return;
            }
        }
示例#5
0
 // Convert a byte array to an Object
 public static DnDMessage createFromByteArray(byte[] arrBytes)
 {
     using (var memStream = new MemoryStream())
     {
         var binForm = new BinaryFormatter();
         memStream.Write(arrBytes, 0, arrBytes.Length);
         memStream.Seek(0, SeekOrigin.Begin);
         DnDMessage obj = null;
         try
         {
             obj = (DnDMessage)binForm.Deserialize(memStream);
         }
         catch (SerializationException)
         {
             return(new DnDMessage("error", new Dictionary <string, string> {
                 { "value", "Invalid request" }
             }));
         }
         return(obj);
     }
 }
示例#6
0
        public void clientMessage(Socket client, DnDMessage msg)
        {
            string action = msg.Action;

            if (action == "text")
            {
                string ip   = TCPServer.GetIPAddressFromSocket(client);
                string text = msg.Properties["value"];

                DnDMessage response = DnDMessage.createWithText(text + " :)");
                client.Send(response.ToByteArray());
            }
            else if (action == "chat_message")
            {
                server.SendToAll(msg.ToByteArray());
            }
            else if (action == "time")
            {
                DnDMessage response = DnDMessage.createWithText(DateTime.Now.ToShortTimeString());
                //server.SendToAll(response.ToByteArray());
            }
            else if (action == "error")
            {
                DnDMessage response = DnDMessage.createWithText(msg.Action + ": Invalid request\n");
                client.Send(response.ToByteArray());
            }
            else if (action == "end_turn")
            {
                Character c = game.NextPlayer();
                foreach (Character cha in game.CharList.Keys)
                {
                    client = playerListIP[cha];
                    if (cha != c)
                    {
                        DnDMessage resp = new DnDMessage("not_your_turn");
                        client.Send(resp.ToByteArray());
                    }
                    else
                    {
                        DnDMessage resp = new DnDMessage("your_turn");
                        client.Send(resp.ToByteArray());
                    }
                }
            }
            else if (action == "initilize_player")
            {
                int[] statsArr = new int[6];
                for (int i = 0; i < statsArr.Length; i++)
                {
                    statsArr[i] = Convert.ToInt32(msg.Properties[i.ToString()]);
                }
                Stats     stats     = new Stats(statsArr[0], statsArr[1], statsArr[2], statsArr[3], statsArr[4], statsArr[5]);
                Character character = new Character(msg.Properties["name"], stats, Convert.ToInt32(msg.Properties["armor"]),
                                                    Convert.ToInt32(msg.Properties["maxHealth"]), Convert.ToInt32(msg.Properties["health"]));
                game.AddCharacter(character);
            }
            else if (action == "weapon")
            {
                Weapon weapon = new Weapon(msg.Properties["player"], msg.Properties["weapon"],
                                           Convert.ToInt32(msg.Properties["attack"]),
                                           Convert.ToInt32(msg.Properties["range"]), Convert.ToInt32(msg.Properties["critical"]),
                                           Convert.ToInt32(msg.Properties["effective"]));
                game.AddWeapon(weapon);
            }
            else if (action == "attack_char")
            {
                Point     point = new Point(Convert.ToInt32(msg.Properties["x"]), Convert.ToInt32(msg.Properties["y"]));
                Character c     = game.GetCharacter(point);

                /*bool result = game.Attack(game.CurrentCharacter, c, 10);
                 * if (result)
                 * {
                 *  DnDMessage resp = new DnDMessage("update_health", new Dictionary<string, string> { { "health", c.CharHealth.ToString() } });
                 *  Socket defender = playerListIP[c];
                 *  defender.Send(resp.ToByteArray());
                 * }*/
            }
            else if (action == "move_player")
            {
                Character chara = game.GetCharacter(msg.Properties["name"]);
                Point     p     = new Point(Convert.ToInt32(msg.Properties["x"]), Convert.ToInt32(msg.Properties["y"]));
                game.UpdateCharPosition(chara, p);
                foreach (Character character in playerListIP.Keys)
                {
                    client = playerListIP[character];
                    DnDMessage resp = new DnDMessage("move_player", new Dictionary <string, string> {
                        { "name", chara.CharName },
                        { "x", p.X.ToString() },
                        { "y", p.Y.ToString() }
                    });
                    client.Send(resp.ToByteArray());
                }
            }
            else if (action == "character_info")
            {
                Character chara = new Character(msg.Properties["name"], Convert.ToInt32(msg.Properties["health"]));
                game.CharList.Add(chara, new Point());
                DnDMessage resp = new DnDMessage("get_char_info", chara.ToDictionary());
                client.Send(resp.ToByteArray());
            }
        }
示例#7
0
 public void sendMessage(DnDMessage msg)
 {
     byte[] buffer = msg.ToByteArray();
     _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None);
 }