Пример #1
0
    public void AskForGame()
    {
        Debug.Log("Looking for Game");
        Schema.LookingForGame lookingForGame = new Schema.LookingForGame()
        {
            Username = this.Id,
        };

        TCPConnection.SendMessage(Any.Pack(lookingForGame)).Wait();
    }
        private async void Login(bool fromRegister) {
            if (connecting) // only allow one connection at once
                return;
            connecting = true;

            if (!ValidateUsername(username)) { // only allow valid usernames to login
                connecting = false;
                return;
            }
            if (fromRegister) // this login call was from a register
                offlineScreen.UpdateStatusText(" Logging in...");
            else
                offlineScreen.UpdateStatusText("Logging in...");
            offlineScreen.ShowResume(false);
            string postData = string.Format(postDataFormat, username, password);

            HTTPConnectionResult loginResult = await httpConn.GetPostResult(urlLogin, postData);
            if (!loginResult.error) { // not an error connecting to server
                LoginResponse response = new LoginResponse(loginResult.message);
                offlineScreen.UpdateStatusText(response.message);

                if (response.success) { // login was sucessful
                    // save the username and password in local settings so that it can load it later
                    PermanentStorage.Set("username", username);
                    PermanentStorage.Set("password", password);

                    // Initialize data structures and store in CoreApplication.Properties for the following:
                    /**
                     * Friends list
                     * Party list
                     * News
                     * Money
                     **/
                    // TODO: Retrieve and parse friend list data from HTTP response
                    // TODO: Retrieve and parse news information from HTTP response
                    // TODO: Retrieve money from SQLite

                    Storage.Set("news", response.news);
                    Storage.Set("unlocked", response.unlockedProfiles);

                    PlayerData mydata = new PlayerData(response.username, response.profile, response.lvl, 0);
                    mydata.rank = response.rank;
                    mydata.money = 2000;
                    Storage.Set("myPlayerData", mydata);

                    // set the upgrades
                    foreach (string s in response.gameUpgrades.Keys) {
                        GameData gd = GameConstants.GetGameData(s);
                        int[] upgrades = response.gameUpgrades[s];
                        gd.GameUpgrades[0].Level = upgrades[0];
                        gd.GameUpgrades[1].Level = upgrades[1];
                        gd.GameUpgrades[2].Level = upgrades[2];
                    }
                    StoreData.GameUpgradePrices = response.gameUpgradePrices.ToArray();

                    // Create a TCP connection
                    ITCPConnection conn = new TCPConnection();
                    conn.pMessageReceivedEvent += delegate(string m, bool error, ITCPConnection connection) {
                        JObject o = JObject.Parse(m);
                        // TODO: Base TCP handler handles the following
                        /**
                         * Friend list updates
                         * Party updates
                         * Store transactions
                         **/
                        if (o["conn_id"] != null) {
                            // authorize the connection using auth key recieved from http login
                            conn.SendMessage("{\"auth\": \"" + response.auth + "\"}");
                            Storage.Set("TCPSocket", conn);

                            // navigate to gamepage to start the game
                            Storage.Set("username", response.username);
                            offlineScreen.ShowControls(false);
                            offlineScreen.GoToNextScreen(typeof(HomeScreen));
                        }
                    };
                    conn.pConnectionClosedEvent += TCPConnectionClosed;
                    if (!conn.Connect()) {
                        System.Diagnostics.Debug.WriteLine("Error connecting to TCP server");
                        offlineScreen.UpdateStatusText("Error connecting to server. Please try again.");
                    }
                } else { // could not log in with the creditions. invalid username and password
                    PermanentStorage.Remove("username"); // remove any stored username and passwords
                    PermanentStorage.Remove("password");
                    ResetScreen(response.message); // reset the screen
                }

            } else {
                // there was an error when connecting to the http server
                System.Diagnostics.Debug.WriteLine("Error connecting to http server");
                offlineScreen.UpdateStatusText("Error connecting to server. Please try again.");
                if (savedInfo) { // logged in from saved information
                    offlineScreen.ShowResume(true); // show the resume button
                } else {
                    offlineScreen.ShowUserInput(true); 
                }
            }

            connecting = false;
        }