Beispiel #1
0
        public void OnIDRequest(object sender, IDRequestEventArgs e)
        {
            ClientHost clientHost = (ClientHost)sender;
            GameData   gameData   = _model.Update();
            int        id         = e.ID;
            string     password   = e.Password;

            //
            //	Permission is checked at client level, here we just check to see
            //   if the player ID has already been assigned to another client
            //

            //Cycle through Client Hosts, if any have requested ID,
            //do nothing - no update, probably should send a rejection notice at
            // some point, we'll see if its necessary
            foreach (ClientHost tempClient in _clientHostArray)
            {
                if (tempClient != null && tempClient.PlayerID == id)
                {
                    return;
                }
            }
            this._playerIDArray[id]          = clientHost;
            gameData.PlayerList[id].Name     = clientHost.UserName;
            gameData.PlayerList[id].Password = password;
            clientHost.PlayerID = id;
            Console.WriteLine(clientHost.UserName + " is assigned slot " + id);
            foreach (ClientHost tempClient in _clientHostArray)
            {
                if (tempClient != null)
                {
                    tempClient.SendID(id, clientHost.UserName);
                }
            }
        }
        private void ProcessMessage(byte [] data)
        {
            //Deserialize message
            IFormatter   fmt       = new BinaryFormatter();
            MemoryStream memHolder = new MemoryStream(data, 0, data.Length);

            memHolder.Position = 0;
            switch (_msgType)
            {
            case MsgType.Ping:
            {
                Console.WriteLine("Pong");
                break;
            }

            case MsgType.Text:
            {
                MsgText msg = (MsgText)fmt.Deserialize(memHolder);
                //if this is the first message assign it to client name
                if (_firstTime)
                {
                    _userName  = msg.Text;
                    _firstTime = false;
                    _connected = true;
                    if (eConnected != null)
                    {
                        EventArgs e = new EventArgs();
                        eConnected(this, e);
                    }
                }
                else if (eMessageReceived != null && _connected)
                {
                    //Else raise the MessageReceived Event
                    //and pass the message along
                    MessageEventArgs e = new MessageEventArgs(msg);
                    eMessageReceived(this, e);
                }
                break;
            }

            case MsgType.Orders:
            {
                //
                //	TODO: make sure it isn't possible to get more
                //     than one set of orders in the list for a
                //		single player
                //

                //accept fleet data from player and add to
                //arraylist of fleetdata
                if (_fleet != null && _orders.Contains(_fleet))
                {
                    Console.WriteLine("Old fleet orders removed for player " + this._clientID);
                    _orders.Remove(_fleet);
                }
                Console.WriteLine("Attempting to Deserialize Orders from player");
                this._fleet = (Fleet)fmt.Deserialize(memHolder);
                ClientHost._orders.Add(_fleet);
                Console.WriteLine("Fleet added to server orders");
                this._ready = true;
                if (eReady != null)
                {
                    EventArgs e = new EventArgs();
                    eReady(this, e);
                }
                break;
            }

            case MsgType.Cancel:
            {
                ClientHost._orders.Remove(this._fleet);
                Console.WriteLine("Fleet orders removed for player " + this._clientID);
                this._ready = false;
                break;
            }

            case MsgType.PlayerIDRequest:
            {
                NameAndID nameAndID = (NameAndID)fmt.Deserialize(memHolder);
                int       id        = nameAndID.ID;
                string    password  = nameAndID.Password;
                if (eIDRequest != null)
                {
                    Console.WriteLine(_userName + " requests player ID(event fired)");
                    IDRequestEventArgs e = new IDRequestEventArgs(id, password);
                    eIDRequest(this, e);
                }
                break;
            }

            case MsgType.SystemName:
            {
                MsgSystemName sysName = (MsgSystemName)fmt.Deserialize(memHolder);
                if (this.eRenameSystem != null)
                {
                    SystemNameEventArgs mea = new SystemNameEventArgs(sysName);
                    this.eRenameSystem(this, mea);
                }
                break;
            }

            case MsgType.Trade:
            {
                MsgTrade tradeMsg = (MsgTrade)fmt.Deserialize(memHolder);
                if (this.eTradeMessage != null)
                {
                    TradeMessageEventArgs ea = new TradeMessageEventArgs(tradeMsg);
                    this.eTradeMessage(this, ea);
                }
                break;
            }
            }
            memHolder.Close();
            //Determine message type
            //Carry out message instructions
        }