コード例 #1
0
        private void fetchButton_Click(object sender, RoutedEventArgs e)
        {
            dfConnection.ConnectAndFetch();
            if (dfConnection.unitList != null)
            {
                UnitList.Clear();
                for (int i = 0; i < dfConnection.unitList.creature_list.Count; i++)
                {
                    RemoteFortressReader.UnitDefinition unit = dfConnection.unitList.creature_list[i];
                    Creature listedCreature = new Creature();
                    listedCreature.Index          = i;
                    listedCreature.unitDefinition = unit;
                    if ((listedCreature.flags1 & UnitFlags.UnitFlags1.dead) == UnitFlags.UnitFlags1.dead)
                    {
                        continue;
                    }
                    if ((listedCreature.flags1 & UnitFlags.UnitFlags1.forest) == UnitFlags.UnitFlags1.forest)
                    {
                        continue;
                    }
                    UnitList.Add(listedCreature);
                }

                CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(unitListView.ItemsSource);
                view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
                view.SortDescriptions.Add(new SortDescription("Race", ListSortDirection.Ascending));
                view.Filter = UnitlistFilter;
            }
            if (DFConnection.creatureRawList != null)
            {
                RaceList.Clear();
                for (int raceIndex = 0; raceIndex < DFConnection.creatureRawList.creature_raws.Count; raceIndex++)
                {
                    var raceRaw = DFConnection.creatureRawList.creature_raws[raceIndex];
                    for (int casteIndex = 0; casteIndex < raceRaw.caste.Count; casteIndex++)
                    {
                        var casteRaw = raceRaw.caste[casteIndex];
                        RemoteFortressReader.UnitDefinition fakeUnit = new RemoteFortressReader.UnitDefinition();
                        fakeUnit.race = new RemoteFortressReader.MatPair()
                        {
                            mat_type = raceIndex, mat_index = casteIndex
                        };
                        string name = casteRaw.caste_name[0];
                        switch (casteRaw.gender)
                        {
                        case 0:
                            name += " ♀";
                            break;

                        case 1:
                            name += " ♂";
                            break;

                        //case -1:
                        //    name += " ⚪";
                        //    break;
                        default:
                            break;
                        }
                        name          = TI.ToTitleCase(name);
                        fakeUnit.name = name;
                        Creature fakeCreature = new Creature()
                        {
                            unitDefinition = fakeUnit
                        };
                        RaceList.Add(fakeCreature);
                    }
                }

                CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(raceListView.ItemsSource);
                view.SortDescriptions.Add(new SortDescription("Race", ListSortDirection.Ascending));
                view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
                view.Filter = UnitlistFilter;
            }
        }
コード例 #2
0
        private void BtnInsert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ScreenMode.Location == screenmode)
                {
                    if (string.IsNullOrEmpty(txtDescription.Text))
                    {
                        throw new Exception("Please enter a location");
                    }

                    Location location = new Location();

                    if (locations.Any(l => l.Description == txtDescription.Text))
                    {
                        throw new Exception("This location has already exists.");
                    }
                    else
                    {
                        location.Description = txtDescription.Text;
                    }

                    //Send it to the API
                    HttpClient client             = InitializeClient();
                    string     serializedLocation = JsonConvert.SerializeObject(location);
                    var        content            = new StringContent(serializedLocation);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Location", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new location Id
                        response = client.GetAsync("Location?description=" + location.Description).Result;
                        string   result            = response.Content.ReadAsStringAsync().Result;
                        Location retrievedLocation = JsonConvert.DeserializeObject <Location>(result);

                        //Save the Id so that we can update it.
                        location.Id = retrievedLocation.Id;

                        locations.Add(location);
                        Rebind();

                        //Select the inserted location
                        cboAttribute.SelectedIndex = locations.FindIndex(p => p == location);
                    }
                    else
                    {
                        throw new Exception("Location could not be inserted");
                    }
                }
                else if (ScreenMode.Gender == screenmode)
                {
                    if (string.IsNullOrEmpty(txtDescription.Text))
                    {
                        throw new Exception("Please enter a gender");
                    }

                    Gender gender = new Gender();

                    if (genders.Any(l => l.Description == txtDescription.Text))
                    {
                        throw new Exception("This gender has already exists.");
                    }
                    else
                    {
                        gender.Description = txtDescription.Text;
                    }

                    //Send it to the API
                    HttpClient client           = InitializeClient();
                    string     serializedGender = JsonConvert.SerializeObject(gender);
                    var        content          = new StringContent(serializedGender);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Gender", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new location Id
                        response = client.GetAsync("Gender?description=" + gender.Description).Result;
                        string result          = response.Content.ReadAsStringAsync().Result;
                        Gender retrievedGender = JsonConvert.DeserializeObject <Gender>(result);

                        //Save the Id so that we can update it.
                        gender.Id = retrievedGender.Id;

                        genders.Add(gender);
                        Rebind();

                        //Select the inserted location
                        cboAttribute.SelectedIndex = genders.FindIndex(p => p == gender);
                    }
                    else
                    {
                        throw new Exception("Gender could not be inserted");
                    }
                }
                else if (ScreenMode.Race == screenmode)
                {
                    if (string.IsNullOrEmpty(txtDescription.Text))
                    {
                        throw new Exception("Please enter a race");
                    }

                    Race race = new Race();

                    if (races.Any(l => l.Description == txtDescription.Text))
                    {
                        throw new Exception("This race has already exists.");
                    }
                    else
                    {
                        race.Description = txtDescription.Text;
                    }

                    //Send it to the API
                    HttpClient client         = InitializeClient();
                    string     serializedRace = JsonConvert.SerializeObject(race);
                    var        content        = new StringContent(serializedRace);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    HttpResponseMessage response = client.PostAsync("Race", content).Result;

                    if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NoContent)
                    {
                        //Get the new location Id
                        response = client.GetAsync("Race?description=" + race.Description).Result;
                        string result        = response.Content.ReadAsStringAsync().Result;
                        Race   retrievedRace = JsonConvert.DeserializeObject <Race>(result);

                        //Save the Id so that we can update it.
                        race.Id = retrievedRace.Id;


                        races.Add(race);
                        Rebind();


                        //Select the inserted location
                        cboAttribute.SelectedIndex = races.FindIndex(p => p == race);
                    }
                    else
                    {
                        throw new Exception("Race could not be inserted");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!");
            }
        }