コード例 #1
0
ファイル: Program.cs プロジェクト: Koneke/Farbase
 public void SendMessage(NetMessage3 message)
 {
     Console.WriteLine("-> {0}: {1}", ID, message.Format());
     byte[] buffer = Program.encoder.GetBytes(
         message.Format() + '\n'
         );
     Stream.Write(buffer, 0, buffer.Length);
     Stream.Flush();
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: Koneke/Farbase
        private void SendAll(
            NetMessage3 message,
            int except = -1
            )
        {
            Console.WriteLine(
                "-> L: {0}", message.Format()
                );

            //let our local world handle a copy of the same message
            Game.HandleNetMessage(message);

            foreach (int id in TcpPlayers.Keys)
            {
                if (id != except)
                {
                    TcpPlayers[id].SendMessage(message);
                }
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Koneke/Farbase
        private void HandleMessage(NetMessage3 message)
        {
            Console.WriteLine(
                "<- {0}: {1}",
                message.Sender,
                message.Format()
                );

            Unit attacker, target;

            switch (message.Signature.MessageType)
            {
            case NM3MessageType.message:
                Console.WriteLine(
                    message.Get <string>("message")
                    );
                break;

            case NM3MessageType.player_add_tech:
                SendAll(message);
                break;

            case NM3MessageType.player_name:
                Player p = Game.World.GetPlayer(
                    message.Get <int>("id")
                    );

                p.Name  = message.Get <string>("name");
                p.Color = ExtensionMethods.ColorFromString(
                    message.Get <string>("color")
                    );

                SendAll(message);
                break;

            case NM3MessageType.client_pass:
                Pass();
                break;

            case NM3MessageType.unit_move:
                Unit un;
                //lock necessary?
                lock (Game)
                {
                    un = Game.World.Units
                         [message.Get <int>("id")];

                    if (un.Moves > 0)
                    {
                        un.MoveTo(
                            message.Get <int>("x"),
                            message.Get <int>("y")
                            );
                        un.Moves--;
                    }
                }

                //pass along to everyone else
                SendAll(
                    message,
                    except: un.Owner
                    );

                SendAll(
                    new NetMessage3(
                        NM3MessageType.unit_status,
                        un.ID,
                        un.Moves,
                        un.Attacks,
                        un.Strength
                        ),
                    except: un.Owner
                    );
                break;

            case NM3MessageType.dev_command:
                switch (message.Get <int>("number"))
                {
                case 0:
                    BroadcastUnit(
                        new Unit(
                            Game.World,
                            UnitType.GetType(UnitTypes.Scout),
                            message.Sender,
                            Unit.IDCounter++,
                            10,
                            10
                            )
                        );

                    BroadcastUnit(
                        new Unit(
                            Game.World,
                            UnitType.GetType(UnitTypes.Catapult),
                            message.Sender,
                            Unit.IDCounter++,
                            20,
                            10
                            )
                        );

                    Game.World.GetPlayer(message.Sender).Money = 10;

                    //might make this a generic "update"
                    //so we don't have to manually resync this every
                    //time we change stuff for a player
                    SendAll(
                        new NetMessage3(
                            NM3MessageType.player_status,
                            message.Sender,
                            Game.World.GetPlayer(
                                message.Sender
                                ).Money
                            )
                        );
                    break;
                }
                break;

            case NM3MessageType.unit_bombard:
                TcpPlayers[message.Sender]
                .SendMessage(
                    new NetMessage3(NM3MessageType.client_unready)
                    );

                attacker = Game.World.Units
                           [message.Get <int>("attackerid")];

                target = Game.World.Units
                         [message.Get <int>("targetid")];

                attacker.Attacks -= 1;
                target.Strength  -= 1;

                SendAll(
                    new NetMessage3(
                        NM3MessageType.unit_status,
                        attacker.ID,
                        attacker.Moves,
                        attacker.Attacks,
                        attacker.Strength
                        )
                    );

                SendAll(
                    new NetMessage3(
                        NM3MessageType.unit_status,
                        target.ID,
                        target.Moves,
                        target.Attacks,
                        target.Strength
                        )
                    );

                TcpPlayers[message.Sender]
                .SendMessage(
                    new NetMessage3(NM3MessageType.client_ready)
                    );
                break;

            case NM3MessageType.unit_attack:
                //please don't do shit until we've resolved combat
                TcpPlayers[message.Sender]
                .SendMessage(
                    new NetMessage3(NM3MessageType.client_unready)
                    );

                attacker = Game.World.Units
                           [message.Get <int>("attackerid")];

                target = Game.World.Units
                         [message.Get <int>("targetid")];

                if (Verbose)
                {
                    Console.WriteLine(
                        "{0}:{1},{2},{3} attacking {4}:{5},{6},{7}.",
                        attacker.ID,
                        attacker.Moves,
                        attacker.Attacks,
                        attacker.Strength,
                        target.ID,
                        target.Moves,
                        target.Attacks,
                        target.Strength
                        );
                }

                int totalStrength = attacker.Strength + target.Strength;
                int roll          = random.Next(totalStrength) + 1;

                attacker.Attacks -= 1;

                Unit loser;
                if (roll <= attacker.Strength)
                {
                    loser = attacker;
                }
                else
                {
                    loser = target;
                }

                loser.Strength -= 1;

                if (attacker != loser)
                {
                    SendAll(
                        new NetMessage3(
                            NM3MessageType.unit_status,
                            attacker.ID,
                            attacker.Moves,
                            attacker.Attacks,
                            attacker.Strength
                            )
                        );
                }

                SendAll(
                    new NetMessage3(
                        NM3MessageType.unit_status,
                        loser.ID,
                        loser.Moves,
                        loser.Attacks,
                        loser.Strength
                        )
                    );

                //we done here
                TcpPlayers[message.Sender]
                .SendMessage(
                    new NetMessage3(NM3MessageType.client_ready)
                    );
                break;

            case NM3MessageType.unit_build:
                Unit unit = new Unit(
                    Game.World,
                    UnitType.GetType(
                        (UnitTypes)
                        message.Get <int>("type")
                        ),
                    message.Sender,
                    Unit.IDCounter++,
                    message.Get <int>("x"),
                    message.Get <int>("y")
                    );

                BroadcastUnit(unit);
                break;

            case NM3MessageType.station_create:
                //we don't just straight propagate the message,
                //since the client sends with an id of -1
                //(i.e. auto assign).
                SendAll(
                    new NetMessage3(
                        NM3MessageType.station_create,
                        message.Get <int>("owner"),
                        Structure.IDCounter++,
                        message.Get <int>("x"),
                        message.Get <int>("y")
                        )
                    );
                break;

            case NM3MessageType.station_set_project:
                int cost;

                switch (
                    (ProjectType)message.Get <int>("projecttype")
                    )
                {
                case ProjectType.UnitProject:
                    UnitType type = UnitType.GetType(
                        (UnitTypes)message.Get <int>("project")
                        );

                    cost = type.Cost;
                    break;

                case ProjectType.TechProject:
                    Tech tech = Tech.Techs[
                        (TechID)message.Get <int>("project")
                                ];

                    cost = tech.Cost;
                    break;

                default:
                    throw new ArgumentException();
                }

                Player projectingPlayer =
                    Game.World.Players[message.Get <int>("owner")];
                projectingPlayer.Money -= cost;

                SendAll(
                    new NetMessage3(
                        NM3MessageType.player_status,
                        message.Get <int>("owner"),
                        projectingPlayer.Money
                        )
                    );

                SendAll(message);
                break;

            case NM3MessageType.client_disconnect:
                //this should automatically remove it from our local
                //world as well, F****N' SWEET :D
                SendAll(
                    message,
                    except: message.Sender
                    );

                TcpPlayers[message.Sender].Disconnected = true;
                TcpPlayers.Remove(message.Sender);

                break;

            case NM3MessageType.world_playerstart:
                SendAll(message);
                break;

            default:
                throw new ArgumentException();
            }
        }