private void btnAddAnimalsSelected_Click(object sender, EventArgs e) { try { int amount = int.Parse(txtAnimalAmount.Text); Species specie = (Species)cbxSpecies.SelectedItem; int counter = animals.Where(ani => ani.Species.Equals(specie)).Count(); if (counter % 10 == 0) { AnimalsSelected assd = new AnimalsSelected(); assd.Animaal = specie; assd.AnimalAmount = amount; AddingAnimal adding = new AddingAnimal(new List <AnimalsSelected>() { assd }, ID); MessageObject message = new MessageObject(assd.BinarySerialization(), 4, 3, 2); } else { //Cages available var cageIds = from loc in locations where !(loc.Cage.Equals(null)) select loc; foreach (Location item in cageIds) { tempLoc.Add(item); } int counters = 0; Species[] space = new Species[tempLoc.Count]; foreach (Location item in tempLoc) { foreach (Animal item2 in animals) { if (item.ID == item2.LocationID) { space[counters] = item2.Species; } } counters++; } for (int i = 0; i < space.Length; i++) { if (space[i].Equals(specie)) { int animalAmount = 0; foreach (Animal item in animals) { if (item.LocationID == tempLoc[i].ID) { animalAmount++; } } for (int j = 0; j < amount + 1; j++) { if (animalAmount < 10) { //Insert Singular Animal Here ArrayList AnimalToAdd = new ArrayList(); AnimalToAdd.Add(specie.AnimalName); //Generate Gender Random rnd = new Random(); int genderChance = rnd.Next(0, 7); string gender = ""; if (genderChance <= 3) { gender = "Female"; AnimalToAdd.Add(gender); } else { gender = "Male"; AnimalToAdd.Add(gender); } int age = rnd.Next(0, 2556); string mate = ""; if ((gender.Equals("Male") || gender.Equals("Female")) && age > 474) { mate = "Ready"; } else { mate = "Not Ready"; } AnimalToAdd.Add(mate); AnimalToAdd.Add(age); double eatTime = 2; if (age < 365 || age > 2920) { eatTime += 1.5; } AnimalToAdd.Add(eatTime); Animal ani = new Animal(gender, mate, eatTime, specie, age, tempLoc[i].ID); animals.Add(ani); MessageObject message = new MessageObject(ani.BinarySerialization(), 6, 3, 2); co.SendData(message); animalAmount++; amount--; } } } } } } catch (Exception) { MessageBox.Show("Please enter a number for Amount!"); } }
//Go to next Screen And add the animals selected private void pbxNext_Click(object sender, EventArgs e) { try { int predatorsNum = 0; int preysNum = 0; //Counting predator and prey foreach (AnimalsSelected item in animalsSelected) { if (item.Animaal.Animaltype == "Predator") { predatorsNum++; } else if (item.Animaal.Animaltype == "Prey") { preysNum++; } } //Checking to see that there is at least 1 prey and 1 predator if (preysNum > 0 && predatorsNum > 0) { //Making sure there is more than 3 types of animals selected if (animalsSelected.Count >= 3) { if (maxCages >= CagesNeeded) { //adding the animals Animal animal = new Animal(); // animal.AddAnimal(animalsSelected); MessageObject AnimalsToAdd = new MessageObject(); AddingAnimal addingAnimal = new AddingAnimal(animalsSelected, ID); AnimalsToAdd.Data = addingAnimal.BinarySerialization(); AnimalsToAdd.FormIdentifier = 4; AnimalsToAdd.ObjectIdentifier = 3; AnimalsToAdd.ActionIdentifier = 2; Client.SendData(AnimalsToAdd); //Task.Delay(1000); // System.Timers.Timer timer = new System.Timers.Timer(); timerNew.Interval = 8000; timerNew.Start(); while (true) { Cursor.Current = Cursors.WaitCursor; // MessageBox.Show("Creating Farm"); timerNew.Tick += TimerNew_Tick; } } else { throw new CageAmountExceededException("You can only have " + maxCages + " please remove animals before continuing"); } } else { throw new AnimalCriteriaNotMeetException("At least 3 different animals need to be selected"); } } else { throw new AnimalCriteriaNotMeetException("At least 1 Prey and 1 Predator needs to be selected"); } } catch (CageAmountExceededException ex) { MessageBox.Show(ex.Message.ToString()); } catch (AnimalCriteriaNotMeetException ex) { MessageBox.Show(ex.Message.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
public void ServerActions(MessageObject message, Socket client) { switch (message.FormIdentifier) { //From Loading Screen case 0: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Select case 1: { Farmer farmer = new Farmer(); Farmer[] AllFarmers = farmer.FarmerSelection(); message.Data = AllFarmers.BinarySerialization(); SendData(message, client); break; } default: break; } break; } default: break; } break; } //From Farmer Selection case 1: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Select case 1: { Farmer farmer = new Farmer(); Farmer[] AllFarmers = farmer.FarmerSelection(); message.Data = AllFarmers.BinarySerialization(); SendData(message, client); break; } case 5: { Farmer farmer = new Farmer(); Farmer[] AllFarmers = farmer.FarmerSelection(); message.Data = AllFarmers.BinarySerialization(); SendData(message, client); break; } //Delete case 3: { Farmer farmer = (Farmer)message.Data.BinaryDeserialization(); farmer.DeleteFarmer(); break; } default: break; } break; } default: break; } break; } //From Farmer Creation case 2: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Insert case 2: { Farmer farmer = (Farmer)message.Data.BinaryDeserialization(); farmer.InsertFarmer(); break; } default: break; } break; } default: break; } break; } //From Farm Creation case 3: { switch (message.ObjectIdentifier) { //Farm case 2: { switch (message.ActionIdentifier) { //Insert case 2: { Farm farm = (Farm)message.Data.BinaryDeserialization(); farm.insertFarm(); break; } default: break; } break; } default: break; } break; } //From Animal Selection case 4: { switch (message.ObjectIdentifier) { //Animal case 3: { switch (message.ActionIdentifier) { //Insert case 2: { lock (semePhore) { Animal animal = new Animal(); AddingAnimal animalstoadd = (AddingAnimal)message.Data.BinaryDeserialization(); // List<AnimalsSelected> animalsSelected = (List<AnimalsSelected>)message.Data.BinaryDeserialization(); animal.AddAnimal(animalstoadd.AnimalstoAdd, animalstoadd.FarmerId); } break; } default: break; } break; } //Species case 4: { switch (message.ActionIdentifier) { //Select case 1: { AnimalsSelected animalsSelected = new AnimalsSelected(); List <Species> species = animalsSelected.getAnimalName(); message.Data = species.BinarySerialization(); SendData(message, client); break; } default: break; } break; } default: break; } break; } //From Add Species case 5: { switch (message.ObjectIdentifier) { //Species case 4: { switch (message.ActionIdentifier) { //Insert case 2: { Species species = (Species)message.Data.BinaryDeserialization(); species.writeSpecies(); break; } default: break; } break; } default: break; } break; } //From Farm View case 6: { switch (message.ObjectIdentifier) { //Farmer case 1: { switch (message.ActionIdentifier) { //Update case 4: { //Update farmer details Farmer farmer = (Farmer)message.Data.BinaryDeserialization(); farmer.UpdateFarmer(); break; } default: break; } break; } //Farm, Animal and Location case 2: { switch (message.ActionIdentifier) { //Select case 1: { ArrayList farmViewData = new ArrayList(); int id = (int)message.Data.BinaryDeserialization(); Farm farm = new Farm(); List <Farm> farms = farm.selectFarm(id); farmViewData.Add(farms); Animal animal = new Animal(); List <Animal> animals = animal.selectAnimals(id); farmViewData.Add(animals); Location location = new Location(); List <Location> locations = location.selectLocation(id); farmViewData.Add(locations); message.Data = farmViewData.BinarySerialization(); SendData(message, client); break; } //Update case 4: { //Update farm details Farm farm = (Farm)message.Data.BinaryDeserialization(); farm.UpdateFarm(); break; } default: break; } break; } //Animal case 3: { switch (message.ActionIdentifier) { //Select case 1: { break; } //Insert case 2: { Animal animal = (Animal)message.Data.BinaryDeserialization(); Console.WriteLine("INSERT IS STILL COMMENTED OU!!!!!!!!!!!!"); animal.InsertAnimal(); break; } //Delete case 3: { Animal animal = (Animal)message.Data.BinaryDeserialization(); Console.WriteLine("DELETE IS STILL COMMENTED OU!!!!!!!!!!!!"); animal.DeleteAnimal(); break; } default: break; } break; } //Location case 5: { switch (message.ActionIdentifier) { //Select case 1: { break; } default: break; } break; } default: break; } break; } default: break; } }