Exemple #1
0
        private void IncomingMessage(object sender, Phoenix.Server.EventArgs.PhoenixServerClientDataReceievedEventArgs e)
        {
            Output(new MercuryOutputEventArgs {
                Output = "Incoming Message from client."
            });
            if (IsLoggedIn(e.Client))
            {
                // We can do everything pretty much.
                // All but login structures will be
                // accepted.
                try
                {
                    switch (e.Data[0])
                    {
                        #region Search Request
                    case Constants.SearchRequestHeader:

                        // Define some variables
                        var sRequest = new SearchRequest();
                        var sResults = new SearchResults();

                        // Initialize the request
                        sRequest.Initialize(e.Data);

                        // Structure got.
                        Output(new MercuryOutputEventArgs {
                            Output = string.Format("Search Request from {0} ({1})", e.Client.IP, _connections[e.Client].ID)
                        });

                        // Search.
                        var resultID = Database.Buffer.Search(sRequest, _connections[e.Client].ID);

                        // Grab the results now.
                        var pkmnList = Database.SearchTracker.GetResults(resultID).ToArray();
                        sResults.Pokemon = pkmnList;
                        sResults.ID      = resultID;

                        // Ship the results back.
                        e.Client.Send(sResults.ToByteArray());

                        break;

                        #endregion
                        #region Uploading
                    case Constants.UploadHeader:

                        // Get the structure.
                        var uploadedPkmn = new Upload();
                        uploadedPkmn.Initialize(e.Data);
                        uploadedPkmn.Pokemon.ID = Guid.NewGuid();

                        // Perform some authentication...ala hack checks
                        if (uploadedPkmn.Pokemon.Requirements.MaxLevel < uploadedPkmn.Pokemon.Requirements.MinLevel)
                        {
                            e.Client.Send(new Response {
                                Resp = Responses.UploadBounced
                            }.ToByteArray());
                        }
                        else
                        {
                            // Done!
                            Output(new MercuryOutputEventArgs
                            {
                                Output =
                                    string.Format("Upload from {0} ({1})", e.Client.IP,
                                                  _connections[e.Client].ID)
                            });

                            // Make sure they don't have a pokemon already
                            if (Database.Buffer.OwnerRequest(_connections[e.Client].ID) !=
                                Database.Buffer.Request.OwnerHasNone)
                            {
                                e.Client.Send(new Response {
                                    Resp = Responses.UploadBounced
                                }.ToByteArray());
                            }
                            else
                            {
                                Database.Buffer.InsertPokemon(uploadedPkmn.Pokemon, _connections[e.Client].ID);
                                e.Client.Send(new Response {
                                    Resp = Responses.UploadGood
                                }.ToByteArray());
                            }
                        }
                        break;

                        #endregion
                        #region Client Request
                    case Constants.RequestHeader:

                        // Get the structure.
                        var request = new Request();
                        request.Initialize(e.Data);

                        // Done!
                        Output(new MercuryOutputEventArgs {
                            Output = string.Format("Request from {0} ({1})", e.Client.IP, _connections[e.Client].ID)
                        });

                        switch (request.ClientRequest)
                        {
                        case Requests.Check:
                            switch (Database.Buffer.OwnerRequest(_connections[e.Client].ID))
                            {
                            case Database.Buffer.Request.OwnerHasOriginal:
                                e.Client.Send(new Response {
                                    Resp = Responses.OldPokemon
                                }.ToByteArray());
                                break;

                            case Database.Buffer.Request.OwnerHasNew:
                                e.Client.Send(new Response {
                                    Resp = Responses.NewPokemon
                                }.ToByteArray());
                                break;

                            case Database.Buffer.Request.OwnerHasNone:
                                e.Client.Send(new Response {
                                    Resp = Responses.NoPokemon
                                }.ToByteArray());
                                break;
                            }
                            break;

                        case Requests.GetPokemon:

                            // let's search the database
                            switch (Database.Buffer.OwnerRequest(_connections[e.Client].ID))
                            {
                            case Database.Buffer.Request.OwnerHasOriginal:
                            case Database.Buffer.Request.OwnerHasNew:
                                var poke = Database.Buffer.GetPokemonByOwner(_connections[e.Client].ID);
                                e.Client.Send(new Upload {
                                    Pokemon = poke
                                }.ToByteArray());
                                Database.Buffer.RemovePokemon(poke.ID);
                                break;

                            case Database.Buffer.Request.OwnerHasNone:
                                e.Client.Send(new Response {
                                    Resp = Responses.NoPokemon
                                }.ToByteArray());
                                break;
                            }


                            break;
                        }
                        break;

                        #endregion
                        #region Additional Searching
                    case Constants.AdditionalSearchHeader:

                        var additional = new AdditionalResults();
                        additional.Initialize(e.Data);

                        // Grab the structure
                        Output(new MercuryOutputEventArgs {
                            Output = string.Format("Additional Search Results requested from {0} ({1})", e.Client.IP, _connections[e.Client].ID)
                        });

                        var results    = Database.SearchTracker.GetResults(additional.SearchID).ToArray();
                        var addResults = new SearchResults {
                            Pokemon = results, ID = additional.SearchID
                        };

                        e.Client.Send(addResults.ToByteArray());

                        break;

                        #endregion
                        #region Trading
                    case Constants.TradeHeader:

                        // Get the structure.
                        var trade = new Trade();
                        trade.Initialize(e.Data);
                        trade.Pokemon.ID = Guid.NewGuid();

                        Output(new MercuryOutputEventArgs {
                            Output = string.Format("Trade from {0} ({1})", e.Client.IP, _connections[e.Client].ID)
                        });

                        // Make sure the Pokemon to trade exists
                        if (!Database.Buffer.PokemonExists(trade.ID))
                        {
                            // Error out, f**k everything
                            e.Client.Send(new Response {
                                Resp = Responses.Error
                            }.ToByteArray());
                        }
                        else
                        {
                            // Add the new pokemon in
                            Database.Buffer.InsertPokemon(trade.Pokemon, _connections[e.Client].ID);

                            // perform the trade
                            Database.Buffer.PerformTrade(trade.ID, trade.Pokemon.ID, _connections[e.Client].ID);

                            // give it back
                            var pkmn = Database.Buffer.GetPokemon(trade.ID);
                            e.Client.Send(new Upload {
                                Pokemon = pkmn
                            }.ToByteArray());

                            // Now remove it
                            Database.Buffer.RemovePokemon(pkmn.ID);
                        }

                        break;
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    Error(new MercuryErrorEventArgs {
                        Exception = ex, Message = "Error during processing of message"
                    });
                    e.Client.Send(new Response {
                        Resp = Responses.Error
                    }.ToByteArray());
                }
            }
            else
            {
                #region Login Check

                try
                {
                    if (!e.Data[0].Equals(Constants.LoginHeader))
                    {
                        Output(new MercuryOutputEventArgs {
                            Output = "Failure to Login!"
                        });
                        e.Client.Disconnect();
                    }
                    else
                    {
                        Output(new MercuryOutputEventArgs {
                            Output = "Performing Login Check..."
                        });
                        LoginCheck(e.Client, e.Data);
                    }
                }
                catch (Exception ex)
                {
                    Error(new MercuryErrorEventArgs {
                        Exception = ex, Message = "Error during processing of login"
                    });
                    // throw;
                }


                #endregion
            }
        }