During the synchronization of the server and the client in Unity PUN2, the movement on the server was successful in synchronizing with the movement of the client. However, the client's movements are not synchronized to the server.
enter image description here
The blue cursor in the image moves on the client, but the blue cursor does not move on the server.
Below is the code I wrote.
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UIElements;
using static UnityEngine.RuleTile.TilingRuleOutput;
using Photon.Pun;
using Photon.Realtime;
using ExitGames.Client.Photon;
public class blueCursor : MonoBehaviour
{
public GameObject currentCharacter;
public GameObject Mech_01;
public GameObject Mech_02;
public GameObject Mech_03;
public GameObject Mech_04;
public GameObject Mech_05;
public GameObject Mech_06;
public GameObject Mech_07;
public GameObject Mech_08;
public PhotonView PV;
public GameObject BlueReady;
public bool isReady;
void Start()
{
currentCharacter = Mech_04;
isReady = false;
}
[PunRPC]
void currentCharacterRPC()
{
if (PV.IsMine == false)
this.gameObject.transform.position = currentCharacter.transform.position;
}
// Update is called once per frame
void Update()
{
if (PV.IsMine == false)
{
if (currentCharacter == Mech_01)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_02;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_04;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_05;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_05;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_01").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_02)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_03;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_01;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_06;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_06;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_02").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_03)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_04;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_02;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_07;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_07;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_03").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
// this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_04)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_01;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_03;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_08;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_08;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_04").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_05)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_06;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_04;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_01;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_01;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_05").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_06)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_07;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_05;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_02;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_02;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_06").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_07)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_08;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_06;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_03;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_03;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_07").gameObject.SetActive(true);
isReady = true;
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
else if (currentCharacter == Mech_08)
{
if (Input.GetKeyDown(KeyCode.Keypad6) == true && isReady == false)
{
currentCharacter = Mech_05;
}
else if (Input.GetKeyDown(KeyCode.Keypad4) == true && isReady == false)
{
currentCharacter = Mech_07;
}
else if (Input.GetKeyDown(KeyCode.Keypad8) == true && isReady == false)
{
currentCharacter = Mech_04;
}
else if (Input.GetKeyDown(KeyCode.Keypad5) == true && isReady == false)
{
currentCharacter = Mech_04;
}
if (Input.GetKeyDown(KeyCode.A) == true && isReady == false && isReady == false)
{
isReady = true;
BlueReady = currentCharacter;
BlueReady.transform.Find("BlueReady_08").gameObject.SetActive(true);
GameObject.Find("DataSend").GetComponent<SendData>().BlueReady = BlueReady;
}
//this.gameObject.transform.position = currentCharacter.transform.position;
}
PV.RPC("currentCharacterRPC", RpcTarget.AllBuffered);
}
}
}
Through YouTube or CHATGPT, I tried to find ways to link servers and clients, but none of them helped.
Related
I'm new using C# and I'm building a remote control. I got stuck and my code is not compiling. Please I need suggestions and how can I improve my code.
I think its Console.Readline() but I'm not sure how to use it. Also how can change this to 2 primary classes?
public class testRemote
{
public static bool PowerOn(bool powerStatus)
{
if (powerStatus == true)
{
testRemote.DisplayMessage("TV already on");
}
else
{
powerStatus = true;
testRemote.DisplayMessage("TV On, Your Channel is 3 and the Volume is 5");
}
return powerStatus;
}
public static bool PowerOff(bool powerStatus)
{
if (powerStatus == false)
{
testRemote.DisplayMessage("TV already off");
}
else
{
powerStatus = false;
testRemote.DisplayMessage("TV is now off");
}
return powerStatus;
}
public static int VolumeUp(bool powerStat, int vol)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (vol >= 10)
{
testRemote.DisplayMessage("TV is already on Maximum Volume.");
}
else
{
vol++;
if (vol == 10)
{
testRemote.DisplayMessage("Maximum Volume.");
}
}
}
return vol;
}
public static int VolumeDown(bool powerStat, int vol)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (vol <= 0)
{
testRemote.DisplayMessage("Sound Muted");
}
else
{
vol--;
if (vol == 0)
{
testRemote.DisplayMessage("Sound Muted");
}
}
}
return vol;
}
public static int ChannelUp(bool powerStat, int channelNo)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (channelNo < 99)
{
channelNo++;
}
else
{
channelNo = 2;
}
Console.WriteLine("Channel " + channelNo.ToString());
}
return channelNo;
}
public static int ChannelDown(bool powerStat, int channelNo)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (channelNo == 2)
{
channelNo = 99;
}
else
{
channelNo--;
}
Console.WriteLine("Channel " + channelNo.ToString());
}
return channelNo;
}
public static String SmartMenu(bool powerStat, String md)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
testRemote.DisplayMessage("Smart Menu On");
md = "TV";
}
return md;
}
public static String SetSettings(bool powerStat, String md)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
testRemote.DisplayMessage("Settings On");
md = "Settings";
}
return md;
}
public static void DisplayMessage(String msg)
{
Console.WriteLine(msg);
}
public static void Banner()
{
Console.WriteLine("Welcome to TV Remote Control");
Console.WriteLine("Please enter your selection");
Console.WriteLine("Power On - turns on your TV");
Console.WriteLine("Power Off - turns off your TV");
Console.WriteLine("Increase volume - turns up the volume");
Console.WriteLine("Decrease volume - turn down the volume");
Console.WriteLine("Channel Up - increments the channel");
Console.WriteLine("Channel Down - decrements the channel");
Console.WriteLine("Smart Menu");
Console.WriteLine("Settings");
Console.WriteLine();
Console.WriteLine("Please enter your selection");
}
public static void Main(String[] args)
{
bool powerOn = false;
int channel = 3;
int volume = 5;
string mode = "TV";
string choice = "Turn On";
string c = Console.ReadLine();
while (choice.ToLower().Equals("Exit".ToLower()) != true)
{
if (choice.ToLower().Equals("Power On".ToLower()) == true)
{
powerOn = testRemote.PowerOn(powerOn);
}
if (choice.ToLower().Equals("Power Off".ToLower()) == true)
{
powerOn = testRemote.PowerOff(powerOn);
}
if (choice.ToLower().Equals("Increase volume".ToLower()) == true)
{
volume = testRemote.VolumeUp(powerOn, volume);
}
if (choice.ToLower().Equals("Decrease volume".ToLower()) == true)
{
volume = testRemote.VolumeDown(powerOn, volume);
}
if (choice.ToLower().Equals("Channel Up".ToLower()) == true)
{
channel = testRemote.ChannelUp(powerOn, channel);
}
if (choice.ToLower().Equals("Channel Down".ToLower()) == true)
{
channel = testRemote.ChannelDown(powerOn, channel);
}
if (choice.ToLower().Equals("Mode TV".ToLower()) == true)
{
mode = testRemote.SmartMenu(powerOn, mode);
}
if (choice.ToLower().Equals("Mode DVD".ToLower()) == true)
{
mode = testRemote.SetSettings(powerOn, mode);
}
Console.WriteLine();
Console.WriteLine();
choice = Console.ReadLine();
}
Console.WriteLine("Thank you for the Remote Controller");
Console.ReadLine();
}
}
Since the question is not clear i slightly modified your code, it compiles and runs BUT needs a lot of refactor.
Here is the working code.
using System;
namespace TvRemote
{
public class testRemote
{
public static bool PowerOn(bool powerStatus)
{
if (powerStatus == true)
{
testRemote.DisplayMessage("TV already on");
}
else
{
powerStatus = true;
testRemote.DisplayMessage("TV On, Your Channel is 3 and the Volume is 5");
}
return powerStatus;
}
public static bool PowerOff(bool powerStatus)
{
if (powerStatus == false)
{
testRemote.DisplayMessage("TV already off");
}
else
{
powerStatus = false;
testRemote.DisplayMessage("TV is now off");
}
return powerStatus;
}
public static int VolumeUp(bool powerStat, int vol)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (vol >= 10)
{
testRemote.DisplayMessage("TV is already on Maximum Volume.");
}
else
{
vol++;
if (vol == 10)
{
testRemote.DisplayMessage("Maximum Volume.");
}
}
}
return vol;
}
public static int VolumeDown(bool powerStat, int vol)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (vol <= 0)
{
testRemote.DisplayMessage("Sound Muted");
}
else
{
vol--;
if (vol == 0)
{
testRemote.DisplayMessage("Sound Muted");
}
}
}
return vol;
}
public static int ChannelUp(bool powerStat, int channelNo)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (channelNo < 99)
{
channelNo++;
}
else
{
channelNo = 2;
}
Console.WriteLine("Channel " + channelNo.ToString());
}
return channelNo;
}
public static int ChannelDown(bool powerStat, int channelNo)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
if (channelNo == 2)
{
channelNo = 99;
}
else
{
channelNo--;
}
Console.WriteLine("Channel " + channelNo.ToString());
}
return channelNo;
}
public static String SmartMenu(bool powerStat, String md)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
testRemote.DisplayMessage("Smart Menu On");
md = "TV";
}
return md;
}
public static String SetSettings(bool powerStat, String md)
{
if (powerStat == false)
{
testRemote.DisplayMessage("TV is off");
}
else
{
testRemote.DisplayMessage("Settings On");
md = "Settings";
}
return md;
}
public static void DisplayMessage(String msg)
{
Console.WriteLine(msg);
}
public static void Banner()
{
Console.WriteLine("Welcome to TV Remote Control");
Console.WriteLine("Please enter your selection");
Console.WriteLine("Power On - turns on your TV");
Console.WriteLine("Power Off - turns off your TV");
Console.WriteLine("Increase volume - turns up the volume");
Console.WriteLine("Decrease volume - turn down the volume");
Console.WriteLine("Channel Up - increments the channel");
Console.WriteLine("Channel Down - decrements the channel");
Console.WriteLine("Smart Menu");
Console.WriteLine("Settings");
Console.WriteLine();
Console.WriteLine("Please enter your selection");
}
public static void Main(String[] args)
{
bool powerOn = false;
int channel = 3;
int volume = 5;
string mode = "TV";
//string choice = "Turn On"; //THIS MUST BE Power On if you want to power on the tv as soon as the app is launched
Banner();
string choice = Console.ReadLine();
while (!choice.ToLower().Equals("Exit".ToLower()))
{
if (choice.ToLower().Equals("Power On".ToLower()) == true)
{
powerOn = testRemote.PowerOn(powerOn);
}
if (choice.ToLower().Equals("Power Off".ToLower()) == true)
{
powerOn = testRemote.PowerOff(powerOn);
}
if (choice.ToLower().Equals("Increase volume".ToLower()) == true)
{
volume = testRemote.VolumeUp(powerOn, volume);
}
if (choice.ToLower().Equals("Decrease volume".ToLower()) == true)
{
volume = testRemote.VolumeDown(powerOn, volume);
}
if (choice.ToLower().Equals("Channel Up".ToLower()) == true)
{
channel = testRemote.ChannelUp(powerOn, channel);
}
if (choice.ToLower().Equals("Channel Down".ToLower()) == true)
{
channel = testRemote.ChannelDown(powerOn, channel);
}
if (choice.ToLower().Equals("Mode TV".ToLower()) == true)
{
mode = testRemote.SmartMenu(powerOn, mode);
}
if (choice.ToLower().Equals("Mode DVD".ToLower()) == true)
{
mode = testRemote.SetSettings(powerOn, mode);
}
Console.WriteLine();
Console.WriteLine();
choice = Console.ReadLine();
}
Console.WriteLine("Thank you for the Remote Controller");
Console.ReadLine();
}
}
}
So I am using Unity c# and this asset for this question -> https://assetstore.unity.com/packages/tools/gui/inventory-system-full-126053.
The asset does not have a feature for using an item thought, so I want to use these lines of prewritten code.
public bool FindMetaData(string type, string metaData)
{
foreach (Slot slot in inventory)
if (slot.type == type && slot.metaData == metaData)
return true;
foreach (Slot slot in hotbar)
if (slot.type == type && slot.metaData == metaData)
return true;
return false;
}
My problem is that I haveno idea how I could combine this with anything, or if there is even a better solution to creating a better system.
Hence I want to ask you if you guys could give me an d example, if I would want to look for the metadata "Example". Here is the rest of the code from the inventory system:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuantumInventory : MonoBehaviour
{
[System.NonSerialized] public List<Slot> inventory, hotbar, slots;
public int maxSlots;
public KeyCode interact, action;
public AudioClip open, close, pickUp, moveSlot, drop;
public float distance;
public bool inventoryToggle = false;
GameObject inventoryObj;
GameObject[] go;
// [0 SLOT][1 INVENTORY][2 HOTBAR][3 ITEM]
Transform[] t;
// [0 CONTAINER][1 INVENTORY][2 HOTBAR][3 SLOTS][4 OPTIONS][5 DOC VIEW]
Sprite error;
bool o;
Transform playerCamera, canvas, invSlots;
PlayerMove pm;
PlayerLook pl;
CanvasScaler cs;
Dropdown sort;
Text info;
QuantumContainer quantumContainer;
private void Start()
{
o = !o;
inventory = new List<Slot>();
hotbar = new List<Slot>();
slots = new List<Slot>();
foreach (Transform child in transform)
if (child.GetComponent<Camera>() != null)
playerCamera = child;
foreach (Transform child in transform)
if (child.GetComponent<PlayerLook>() != null)
pl = child.GetComponent<PlayerLook>();
if (GetComponent<PlayerMove>() != null)
pm = GetComponent<PlayerMove>();
if (maxSlots > 40)
maxSlots = 40;
canvas = GameObject.Find("Canvas").transform;
cs = canvas.GetComponent<CanvasScaler>();
cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
cs.referenceResolution = new Vector2(1280, 720);
cs.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
cs.matchWidthOrHeight = 0.5f;
go = new GameObject[5];
t = new Transform[7];
go[0] = Resources.Load<GameObject>("Core/QIS/_slot");
go[1] = Resources.Load<GameObject>("Core/QIS/_inventory");
go[2] = Resources.Load<GameObject>("Core/QIS/_hotbar");
go[3] = Resources.Load<GameObject>("Core/QIS/_erit");
error = Resources.Load<Sprite>("Core/QIS/_ertex");
t[1] = Instantiate<GameObject>(go[1], canvas).transform;
invSlots = t[1].Find("_slots");
t[0] = t[1].Find("_container");
t[4] = t[1].Find("_options");
t[3] = t[4].Find("_moreSlots");
t[5] = t[1].Find("_docViewer");
t[2] = Instantiate<GameObject>(go[2], canvas).transform;
t[6] = t[4].Find("_info");
sort = t[4].Find("_sort").GetComponent<Dropdown>();
sort.GetComponent<Dropdown>().onValueChanged.AddListener(delegate { RefreshInventory(); });
info = t[6].Find("Text").GetComponent<Text>();
t[1].gameObject.SetActive(false);
}
private void Update()
{
if (Input.GetKeyDown(interact))
{
RaycastHit hit;
int layer = gameObject.layer;
gameObject.layer = 2;
if (Physics.Raycast(playerCamera.position, playerCamera.forward, out hit, distance))
{
if (hit.collider.GetComponent<QuantumItem>() != null)
Gather(hit.collider.GetComponent<QuantumItem>());
else if (hit.collider.GetComponent<QuantumContainer>() != null)
Container(hit.collider.GetComponent<QuantumContainer>());
}
gameObject.layer = layer;
}
if (Input.GetKeyDown(action))
{
Freeze();
ActionInventory();
SetActive(false, true, false);
}
// links function to update
UseItems();
}
public void Freeze()
{
o = !o;
if (pm != null && pl != null)
{ pm.enabled = o; pl.enabled = o; }
if (o)
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
Time.timeScale = 1;
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
Time.timeScale = 0f;
}
}
public void Container(QuantumContainer container)
{
if (container.locked != "")
{
if (!FindMetaData("Key", container.locked))
{
container.PlayFX(container.lockState);
return;
}
}
Freeze();
ActionInventory();
SetActive(true, false, false);
quantumContainer = container;
quantumContainer.PlayFX(quantumContainer.open);
RefreshInventory();
}
public void ActionInventory()
{
t[1].gameObject.SetActive(!o);
if (t[1].gameObject.activeSelf)
PlayFX(open);
else
PlayFX(close);
if (t[0].gameObject.activeSelf)
quantumContainer.PlayFX(quantumContainer.close);
SetActive(false, true, false);
RefreshInventory();
}
private void DeNuller ()
{
foreach (Transform child in invSlots)
Destroy(child.gameObject);
foreach (Transform child in t[0])
Destroy(child.gameObject);
foreach (Transform child in t[2])
Destroy(child.gameObject);
foreach (Transform child in t[3])
Destroy(child.gameObject);
}
private void RefreshInventory()
{
DeNuller();
if (sort.value == 0) //NORMAL
{
foreach (Slot slot in inventory)
{
GameObject x = Instantiate(go[0], invSlots);
if (slot.icon != null)
x.transform.Find("Icon").GetComponent<Image>().sprite = slot.icon;
else
x.transform.Find("Icon").GetComponent<Image>().sprite = error;
x.transform.Find("Type").GetComponent<Image>().sprite = Resources.Load<Sprite>("Core/QIS/" + slot.type);
if (slot.quantity > 1)
x.transform.Find("Text").GetComponent<Text>().text = slot.quantity.ToString("");
else
x.transform.Find("Text").GetComponent<Text>().text = "";
x.GetComponent<Button>().onClick.AddListener(delegate { Action(slot); });
}
}
else // TYPE [ITEM-DOCUMENT-KEY-CONSUMABLE-SLOT-CUSTOM]
{
InstantiateSlot("Item");
InstantiateSlot("Document");
InstantiateSlot("Key");
InstantiateSlot("Consumable");
InstantiateSlot("Slot");
InstantiateSlot("Custom");
}
foreach (Slot slot in hotbar)
{
GameObject x = Instantiate(go[0], t[2]);
if (slot.icon != null)
x.transform.Find("Icon").GetComponent<Image>().sprite = slot.icon;
else
x.transform.Find("Icon").GetComponent<Image>().sprite = error;
x.transform.Find("Type").GetComponent<Image>().sprite = Resources.Load<Sprite>("Core/QIS/" + slot.type);
if (slot.quantity != 1)
x.transform.Find("Text").GetComponent<Text>().text = slot.quantity.ToString("");
else
x.transform.Find("Text").GetComponent<Text>().text = "";
x.GetComponent<Button>().onClick.AddListener(delegate { HotbarAction(slot); });
}
foreach (Slot slot in slots)
{
GameObject x = Instantiate(go[0], t[3]);
if (slot.icon != null)
x.transform.Find("Icon").GetComponent<Image>().sprite = slot.icon;
else
x.transform.Find("Icon").GetComponent<Image>().sprite = error;
x.transform.Find("Type").GetComponent<Image>().sprite = Resources.Load<Sprite>("Core/QIS/" + slot.type);
if (slot.quantity != 1)
x.transform.Find("Text").GetComponent<Text>().text = slot.quantity.ToString("");
else
x.transform.Find("Text").GetComponent<Text>().text = "";
x.GetComponent<Button>().onClick.AddListener(delegate { SlotAction(slot); });
}
if (t[0].gameObject.activeSelf)
{
foreach (Slot slot in quantumContainer.inventory)
{
GameObject x = Instantiate(go[0], t[0]);
if (slot.icon != null)
x.transform.Find("Icon").GetComponent<Image>().sprite = slot.icon;
else
x.transform.Find("Icon").GetComponent<Image>().sprite = error;
x.transform.Find("Type").GetComponent<Image>().sprite = Resources.Load<Sprite>("Core/QIS/" + slot.type);
if (slot.quantity != 1)
x.transform.Find("Text").GetComponent<Text>().text = slot.quantity.ToString("");
else
x.transform.Find("Text").GetComponent<Text>().text = "";
x.GetComponent<Button>().onClick.AddListener(delegate { ContainerAction(slot); });
}
}
info.text = "Max Slots: " + maxSlots;
info.text += "\nUsed Slots: " + inventory.Count;
info.text += "\nFree Slots: " + (maxSlots - inventory.Count);
info.text += "\n\nHotbar Max Slots: 9";
info.text += "\nHotbar Used Space: " + hotbar.Count;
info.text += "\nHotbar Free Space: " + (9 - hotbar.Count);
int additionalSlots = 0;
foreach (Slot slot in slots)
additionalSlots += int.Parse(slot.metaData);
info.text += "\n\nAdditional Slots: " + additionalSlots;
}
private void InstantiateSlot(string type)
{
foreach (Slot slot in inventory)
{
if (slot.type == type)
{
GameObject x = Instantiate(go[0], invSlots);
if (slot.icon != null)
x.transform.Find("Icon").GetComponent<Image>().sprite = slot.icon;
else
x.transform.Find("Icon").GetComponent<Image>().sprite = error;
x.transform.Find("Type").GetComponent<Image>().sprite = Resources.Load<Sprite>("Core/QIS/" + slot.type);
if (slot.quantity != 1)
x.transform.Find("Text").GetComponent<Text>().text = slot.quantity.ToString("");
else
x.transform.Find("Text").GetComponent<Text>().text = "";
x.GetComponent<Button>().onClick.AddListener(delegate { Action(slot); });
}
}
}
private void Action(Slot slot)
{
if (Input.GetKey(KeyCode.LeftControl))
{
Drop(slot);
inventory.Remove(slot);
PlayFX(moveSlot);
}
else
{
if (t[0].gameObject.activeSelf)
{
if (quantumContainer.inventory.Count >= quantumContainer.maxSlots)
return;
quantumContainer.Gather(slot);
inventory.Remove(slot);
PlayFX(moveSlot);
}
else
{
switch (slot.type)
{
case "Document":
SetActive(false, false, true);
t[5].Find("Text").GetComponent<Text>().text = slot.metaData;
break;
case "Slot":
SetActive(false, true, false);
ChangeMaxSlots(int.Parse(slot.metaData));
slots.Add(slot);
inventory.Remove(slot);
PlayFX(moveSlot);
break;
default:
if (hotbar.Count >= 9)
return;
GatherHotbar(slot);
inventory.Remove(slot);
PlayFX(moveSlot);
break;
}
}
}
RefreshInventory();
}
private void HotbarAction(Slot slot)
{
if (Input.GetKey(KeyCode.LeftControl))
{
Drop(slot);
hotbar.Remove(slot);
PlayFX(drop);
}
else
{
if (t[0].gameObject.activeSelf)
{
if (quantumContainer.inventory.Count >= quantumContainer.maxSlots)
return;
quantumContainer.Gather(slot);
hotbar.Remove(slot);
PlayFX(moveSlot);
}
else
{
if (inventory.Count >= maxSlots)
return;
Gather(slot);
hotbar.Remove(slot);
PlayFX(moveSlot);
}
}
RefreshInventory();
}
private void ContainerAction(Slot slot)
{
if (Input.GetKey(KeyCode.LeftControl))
{
Drop(slot);
quantumContainer.inventory.Remove(slot);
PlayFX(drop);
RefreshInventory();
}
else if (!Input.GetKey(KeyCode.LeftControl) && inventory.Count < maxSlots)
{
Gather(slot);
quantumContainer.inventory.Remove(slot);
PlayFX(moveSlot);
RefreshInventory();
}
}
private void SlotAction(Slot slot)
{
int i = int.Parse(slot.metaData);
ChangeMaxSlots(-i);
if (Input.GetKey(KeyCode.LeftControl))
{
Drop(slot);
slots.Remove(slot);
PlayFX(drop);
RefreshInventory();
}
else if (!Input.GetKey(KeyCode.LeftControl) && inventory.Count < maxSlots)
{
Gather(slot);
slots.Remove(slot);
PlayFX(moveSlot);
RefreshInventory();
}
}
private void ChangeMaxSlots(int quantity)
{
maxSlots += quantity;
if (maxSlots > 40)
maxSlots = 40;
else if (maxSlots < 0)
maxSlots = 0;
}
private void Drop(Slot slot)
{
GameObject x = Resources.Load<GameObject>("Core/QIS/" + slot.item);
if (x == null)
{ x = Instantiate(go[3]); x.GetComponent<Renderer>().material = Resources.Load<Material>("Core/QIS/Materials/_" + slot.type.ToUpper()); }
else
{ Instantiate(x); }
x.transform.position = transform.position;
x.GetComponent<QuantumItem>().item = slot.item;
x.GetComponent<QuantumItem>().type = slot.type;
x.GetComponent<QuantumItem>().quantity = slot.quantity;
if (slot.icon == null)
x.GetComponent<QuantumItem>().icon = error;
else
x.GetComponent<QuantumItem>().icon = slot.icon;
x.GetComponent<QuantumItem>().stackable = slot.stackable;
x.GetComponent<QuantumItem>().metaData = slot.metaData;
}
private void SetActive(bool container, bool options, bool viewer)
{
t[0].gameObject.SetActive(container);
t[4].gameObject.SetActive(options);
t[5].gameObject.SetActive(viewer);
}
public void Gather(QuantumItem item)
{
if (inventory.Count >= maxSlots)
return;
Slot slot = FindSlot(item.item);
if (slot == null || !slot.stackable)
inventory.Add(new Slot(item));
else if (slot != null && slot.stackable && slot.type == item.type)
slot.quantity += item.quantity;
PlayFX(pickUp);
Destroy(item.gameObject);
}
public void Gather(Slot item)
{
if (inventory.Count >= maxSlots)
return;
Slot slot = FindSlot(item.item);
if (slot == null || !slot.stackable)
inventory.Add(item);
else if (slot != null && slot.stackable && slot.type == item.type)
slot.quantity += item.quantity;
PlayFX(pickUp);
}
public void GatherHotbar(Slot item)
{
if (hotbar.Count >= 9)
return;
Slot slot = FindHotbarSlot(item.item);
if (slot == null || !slot.stackable)
hotbar.Add(item);
else if (slot != null && slot.stackable && slot.type == item.type)
slot.quantity += item.quantity;
PlayFX(pickUp);
}
public bool FindItem(string item)
{
foreach (Slot slot in inventory)
if (slot.item == item)
return true;
return false;
}
public bool FindItem(string item, int quantity)
{
foreach (Slot slot in inventory)
if (slot.item == item && slot.quantity >= quantity)
return true;
return false;
}
public Slot FindSlot(string item)
{
foreach (Slot slot in inventory)
if (slot.item == item)
return slot;
return null;
}
public Slot FindHotbarSlot(string item)
{
foreach (Slot slot in hotbar)
if (slot.item == item)
return slot;
return null;
}
public bool FindMetaData(string type, string metaData)
{
foreach (Slot slot in inventory)
if (slot.type == type && slot.metaData == metaData)
return true;
foreach (Slot slot in hotbar)
if (slot.type == type && slot.metaData == metaData)
return true;
return false;
}
public void FindItemAndRemove(string item)
{
foreach (Slot slot in inventory)
if (slot.item == item)
inventory.Remove(slot);
}
public void FindItemAndRemove(string item, int quantity)
{
foreach (Slot slot in inventory)
if (slot.item == item && slot.quantity >= quantity)
{
slot.quantity -= quantity;
if (slot.quantity <= 0)
inventory.Remove(slot);
}
}
public Slot GetHotbarID (int i)
{
return hotbar[i];
}
private void PlayFX(AudioClip fx)
{
if (fx == null)
return;
GameObject obj = new GameObject();
obj.transform.position = transform.position;
AudioSource source = obj.AddComponent<AudioSource>();
source.clip = fx;
source.Play();
Destroy(obj, fx.length);
}
[System.Serializable]
public class Slot
{
public string item, type;
public int quantity;
public Sprite icon;
public bool stackable;
[TextArea(3, 5)]
public string metaData;
public Slot(string item, string type, int quantity, Sprite icon, bool stackable, string metaData)
{
this.item = item;
this.type = type;
this.quantity = quantity;
this.icon = icon;
this.stackable = stackable;
this.metaData = metaData;
}
public Slot(QuantumItem quantum)
{
this.item = quantum.item;
this.type = quantum.type;
this.quantity = quantum.quantity;
this.icon = quantum.icon;
this.stackable = quantum.stackable;
this.metaData = quantum.metaData;
}
}
}
I have List<Book> Books
public class Book
{
public string Name { get; set; }
public string Author { get; set; }
public string Category { get; set; }
public string Language { get; set; }
public DateTime Publication_date { get; set; }
public string ISBN { get; set; }
}
tried this, but it only work if all parameters matched
foreach (var item in Books)
{
if ((!string.IsNullOrEmpty(name) && item.Name == name)
&& (!string.IsNullOrEmpty(author) && item.Author == author)
&& (!string.IsNullOrEmpty(category) && item.Category == category)
&& (!string.IsNullOrEmpty(language) && item.Language == language)
&& (Publication_date.HasValue && item.Publication_date == Publication_date)
&& (!string.IsNullOrEmpty(ISBN) && item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
how to make filter by one or more parameters? like if i enter only "name" and "Language" it will print me the book where both parameters a matched (if string is null or empty then parameter should be skipped)
Thanks)
The logic is a touch complicated, but made simpler by use of continue.
foreach (var item in Books)
{
if (!string.IsNullOrEmpty(name) && item.Name != name) continue;
if (!string.IsNullOrEmpty(author) && item.Author != author) continue;
if (!string.IsNullOrEmpty(category) && item.Category != category) continue;
if (!string.IsNullOrEmpty(language) && item.Language != language) continue;
if (Publication_date.HasValue && item.Publication_Date != Publication_Date) continue;
if (!string.IsNullOrEmpty(ISBN) && item.ISBN!= ISBN) continue;
Console.WriteLine(item);
}
you want it in a form where each condition returns true if null, or true if match. Basically null/empty means anything is a match
like
(string.IsNullOrEmpty(name) || item.Name == name)
then && them altogether
foreach (var item in Books)
{
if ((string.IsNullOrEmpty(name) || item.Name == name)
&& (string.IsNullOrEmpty(author) || item.Author == author)
&& (string.IsNullOrEmpty(category) || item.Category == category)
&& (string.IsNullOrEmpty(language) || item.Language == language)
&& (!Publication_date.HasValue || item.Publication_date == Publication_date)
&& (string.IsNullOrEmpty(ISBN) || item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
to be a little clearer you can do
bool IsMatchForFilter(string filter, string value)
=> string.IsNullOrEmpty(filter) || filter == value;
then do
if(
IsMatchForFilter(name, item.Name)
&& IsMatchForFilter(author, item.Author)
...
Try it like this, use the OR (||) operator
foreach (var item in Books)
{
if ((!string.IsNullOrEmpty(name) && item.Name == name)
|| (!string.IsNullOrEmpty(author) && item.Author == author)
|| (!string.IsNullOrEmpty(category) && item.Category == category)
|| (!string.IsNullOrEmpty(language) && item.Language == language)
|| (Publication_date.HasValue && item.Publication_date == Publication_date)
|| (!string.IsNullOrEmpty(ISBN) && item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
Hello.I am currently working on a VS form app with pictureboxes gerating random images.Down below i have posted a simplified version of my programme, where error occurs.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BackgroundImageLayout = ImageLayout.Stretch;
}
int[] random = new int[16];
public void LoadImages()
{
List<Image> images = new List<Image>();
Random rand = new Random();
images.Add(Properties.strings.firsthero);
images.Add(Properties.strings.secondhero);
images.Add(Properties.strings.thirdhero);
images.Add(Properties.strings.fourthhero);
images.Add(Properties.strings.fifthhero);
images.Add(Properties.strings.sixthhero);
images.Add(Properties.strings.seventhhero);
images.Add(Properties.strings.eighthero);
images.Add(Properties.strings.ninethhero);
images.Add(Properties.strings.tenthhero);
images.Add(Properties.strings.eleventhhero);
images.Add(Properties.strings.twelevethhero);
images.Add(Properties.strings.thirteenthhero);
images.Add(Properties.strings.fourteenthhero);
images.Add(Properties.strings.fifteenthhero);
random[0] = rand.Next(images.Count);
pictureBox1.Image = images[random[0]];
random[1] = rand.Next(images.Count);
pictureBox2.Image = images[random[1]];
random[2] = rand.Next(images.Count);
pictureBox3.Image = images[random[2]];
random[3] = rand.Next(images.Count);
pictureBox4.Image = images[random[3]];
random[4] = rand.Next(images.Count);
pictureBox5.Image = images[random[4]];
random[5] = rand.Next(images.Count);
pictureBox6.Image = images[random[5]];
random[6] = rand.Next(images.Count);
pictureBox7.Image = images[random[6]];
random[7] = rand.Next(images.Count);
pictureBox8.Image = images[random[7]];
random[8] = rand.Next(images.Count);
pictureBox9.Image = images[random[8]];
random[9] = rand.Next(images.Count);
pictureBox10.Image = images[random[9]];
random[10] = rand.Next(images.Count);
pictureBox11.Image = images[random[10]];
random[11] = rand.Next(images.Count);
pictureBox12.Image = images[random[11]];
random[12] = rand.Next(images.Count);
pictureBox13.Image = images[random[12]];
random[13] = rand.Next(images.Count);
pictureBox14.Image = images[random[13]];
random[14] = rand.Next(images.Count);
pictureBox15.Image = images[random[14]];
random[15] = rand.Next(images.Count);
pictureBox16.Image = images[random[15]];
}
private void btnStart_Click(object sender, EventArgs e)
{
LoadImages();
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
button2WasClicked = true;
button1Match();
//the same for other buttons
}
public void button1Match()
{
if(button1WasClicked == true)
{
if(button2WasClicked == true)
{
if (random[0] == random[1])
{
winsNumber++;
}
}
else if(button3WasClicked == true)
{
if (random[0] == random[2])
{
winsNumber++;
}
}
else if(button4WasClicked == true)
{
if (random[0] == random[3])
{
winsNumber++;
}
}
else if(button5WasClicked == true)
{
if (random[0] == random[4])
{
winsNumber++;
}
}
else if(button6WasClicked == true)
{
if (random[0] == random[5])
{
winsNumber++;
}
}
else if(button7WasClicked == true)
{
if (random[0] == random[6])
{
winsNumber++;
}
}
else if(button8WasClicked == true)
{
if (random[0] == random[7])
{
winsNumber++;
}
}
else if(button9WasClicked == true)
{
if (random[0] == random[8])
{
winsNumber++;
}
}
else if(button10WasClicked == true)
{
if(random[0] == random[9])
{
winsNumber++;
}
}
else if(button11WasClicked == true)
{
if (random[0] == random[10])
{
winsNumber++;
}
}
else if(button12WasClicked == true)
{
if (random[0] == random[11])
{
winsNumber++;
}
}
else if(button13WasClicked == true)
{
if (random[0] == random[12])
{
winsNumber++;
}
}
else if(button14WasClicked == true)
{
if (random[0] == random[13])
{
winsNumber++;
}
}
else if(button15WasClicked == true)
{
if (random[0] == random[14])
{
winsNumber++;
}
}
else if(button16WasClicked == true)
{
if (random[0] == random[15])
{
winsNumber++;
}
}
}
wins.Text = winsNumber.ToString();
}
So basically I am generatinf random indexes from image list and via them show random images in picture boxes.To find out whether a match between two pictures occurs, I check the indexes of the images in the buttons.
However the problem is that the random[0] = rand.Next(images.Count); only genrates number in the void, and does not assign random[0] value to the global array. Possible solutins?
I've built a program that creates employee schedules and assigns employees to those schedules. I can assign the schedule with no issues, but I'm having problems figuring out how to make that schedule disappear from the dropdown list if it's already been assigned to an employee so that the same schedule isn't given to two employees.
#Html.DropDownListFor(m => m.ApplicationUser.ScheduleSum, new SelectList(Model.ScheduleBuilderList, "ScheduleSummary", "ScheduleSummary"), "Choose a schedule", new {#class = "form-control"}
How can I get a schedule ID to disappear when assigned and reappeared if not assigned?
Let me know if you need any additional information.
Schedule Model
public int Id { get; set; }
public int ScheduleId { get; set; }
public string DaysMatching { get; set; }
public string Days { get; set; }
public string ScheduleSummary { get; set; }
public string MondaySchedule { get; set; }
public string TuesdaySchedule { get; set; }
public string WednesdaySchedule { get; set; }
public string ThursdaySchedule { get; set; }
public string FridaySchedule { get; set; }
public string SaturdaySchedule { get; set; }
public string SundaySchedule { get; set; }
public string MondayStartTime { get; set; }
public string TuesdayStartTime { get; set; }
public string WednesdayStartTime { get; set; }
public string ThursdayStartTime { get; set; }
public string FridayStartTime { get; set; }
public string SaturdayStartTime { get; set; }
public string SundayStartTime { get; set; }
public string MondayEndTime { get; set; }
public string TuesdayEndTime { get; set; }
public string WednesdayEndTime { get; set; }
public string ThursdayEndTime { get; set; }
public string FridayEndTime { get; set; }
public string SaturdayEndTime { get; set; }
public string SundayEndTime { get; set; }
public string MondayFirstBreak { get; set; }
public string TuesdayFirstBreak { get; set; }
public string WednesdayFirstBreak { get; set; }
public string ThursdayFirstBreak { get; set; }
public string FridayFirstBreak { get; set; }
public string SaturdayFirstBreak { get; set; }
public string SundayFirstBreak { get; set; }
public string MondaySecondBreak { get; set; }
public string TuesdaySecondBreak { get; set; }
public string WednesdaySecondBreak { get; set; }
public string ThursdaySecondBreak { get; set; }
public string FridaySecondBreak { get; set; }
public string SaturdaySecondBreak { get; set; }
public string SundaySecondBreak { get; set; }
public string MondayLunch { get; set; }
public string TuesdayLunch { get; set; }
public string WednesdayLunch { get; set; }
public string ThursdayLunch { get; set; }
public string FridayLunch { get; set; }
public string SaturdayLunch { get; set; }
public string SundayLunch { get; set; }
Employee Model
public class ApplicationUser : IdentityUser
{
public int ScheduleId { get; set; }
public ScheduleBuilder ScheduleBuilder { get; set; }
public string ScheduleSum { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
[RegularExpression(#"^\(?([0-9]{3})\)?[-]([0-9]{3})[-]([0-9]{4})$", ErrorMessage = "Please enter correct format with hyphens. i.e. 407-200-2000")]
[Display(Name = "Phone Number")]
public string PhoneNumbers { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessage = "You must provide a valid Email")]
public string EmailAccount => Email;
}
Schedule Controller Save ActionResult
[HttpPost]
public ActionResult Save(ScheduleBuilder scheduleBuilder)
{
if (!ModelState.IsValid)
{
var viewModel = new TierViewModel()
{
ScheduleBuilder = scheduleBuilder
};
return View("ScheduleBuilder", viewModel);
}
if (scheduleBuilder.Id == 0)
{
_context.ScheduleBuilder.Add(scheduleBuilder);
if (scheduleBuilder.MondayStartTime != null)
{
scheduleBuilder.MondaySchedule = "Mon";
}
if (scheduleBuilder.TuesdayStartTime != null)
{
scheduleBuilder.TuesdaySchedule = "Tue";
}
if (scheduleBuilder.WednesdayStartTime != null)
{
scheduleBuilder.WednesdaySchedule = "Wed";
}
if (scheduleBuilder.ThursdayStartTime != null)
{
scheduleBuilder.ThursdaySchedule = "Thur";
}
if (scheduleBuilder.FridayStartTime != null)
{
scheduleBuilder.FridaySchedule = "Fri";
}
if (scheduleBuilder.SaturdayStartTime != null)
{
scheduleBuilder.SaturdaySchedule = "Sat";
}
if (scheduleBuilder.SundayStartTime != null)
{
scheduleBuilder.SundaySchedule = "Sun";
}
scheduleBuilder.Days = scheduleBuilder.SundaySchedule + " " +
scheduleBuilder.MondaySchedule + " " +
scheduleBuilder.TuesdaySchedule + " " +
scheduleBuilder.WednesdaySchedule + " " +
scheduleBuilder.ThursdaySchedule + " " +
scheduleBuilder.FridaySchedule + " " +
scheduleBuilder.SaturdaySchedule;
scheduleBuilder.DaysMatching = "Yes";
var startTime = scheduleBuilder.MondayStartTime + "-" + scheduleBuilder.MondayEndTime;
if (scheduleBuilder.MondayStartTime == null)
{
startTime = scheduleBuilder.TuesdayStartTime + "-" + scheduleBuilder.TuesdayEndTime;
}
if (scheduleBuilder.MondayStartTime == null && scheduleBuilder.TuesdayStartTime == null)
{
startTime = scheduleBuilder.WednesdayStartTime + "-" + scheduleBuilder.WednesdayEndTime;
}
if (scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.MondayStartTime == null && scheduleBuilder.TuesdayStartTime == null)
{
startTime = scheduleBuilder.ThursdayStartTime + "-" + scheduleBuilder.ThursdayEndTime; ;
}
if (scheduleBuilder.ThursdayStartTime == null && scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.MondayStartTime == null && scheduleBuilder.TuesdayStartTime == null)
{
startTime = scheduleBuilder.FridayStartTime + "-" + scheduleBuilder.FridayEndTime; ;
}
if (scheduleBuilder.FridayStartTime == null && scheduleBuilder.ThursdayStartTime == null && scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.TuesdayStartTime == null && scheduleBuilder.MondayStartTime == null)
{
startTime = scheduleBuilder.SaturdayStartTime + "-" + scheduleBuilder.SaturdayEndTime; ;
}
if (scheduleBuilder.SaturdayStartTime == null && scheduleBuilder.FridayStartTime == null && scheduleBuilder.ThursdayStartTime == null && scheduleBuilder.WednesdayStartTime == null && scheduleBuilder.TuesdayStartTime == null && scheduleBuilder.MondayStartTime == null)
{
startTime = scheduleBuilder.SundayStartTime + "-" + scheduleBuilder.SundayEndTime; ;
}
System.Diagnostics.Debug.Write("Monday End Time: " + scheduleBuilder.MondayEndTime);
System.Diagnostics.Debug.Write("Sunday End Time: " + scheduleBuilder.SundayEndTime);
if (scheduleBuilder.SundayStartTime != scheduleBuilder.MondayStartTime && scheduleBuilder.MondayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.MondayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.TuesdayStartTime && scheduleBuilder.TuesdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.TuesdayEndTime && scheduleBuilder.TuesdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.WednesdayStartTime && scheduleBuilder.WednesdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.WednesdayEndTime && scheduleBuilder.WednesdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.SundayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.SundayStartTime != null || scheduleBuilder.SundayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.SaturdayEndTime != null && scheduleBuilder.SundayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.TuesdayStartTime && scheduleBuilder.TuesdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.TuesdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.WednesdayStartTime && scheduleBuilder.WednesdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.WednesdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.ThursdayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.FridayEndTime != null
|| scheduleBuilder.MondayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.MondayStartTime != null || scheduleBuilder.MondayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.MondayEndTime != null && scheduleBuilder.SaturdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.WednesdayStartTime && scheduleBuilder.WednesdayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.WednesdayEndTime && scheduleBuilder.WednesdayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.TuesdayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.TuesdayStartTime != null || scheduleBuilder.TuesdayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.SaturdayEndTime != null && scheduleBuilder.TuesdayEndTime != null
|| scheduleBuilder.WednesdayStartTime != scheduleBuilder.ThursdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.WednesdayStartTime != null || scheduleBuilder.WednesdayEndTime != scheduleBuilder.ThursdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.WednesdayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.WednesdayStartTime != null || scheduleBuilder.WednesdayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.WednesdayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.SaturdayStartTime != null && scheduleBuilder.WednesdayStartTime != null || scheduleBuilder.WednesdayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.SaturdayEndTime != null && scheduleBuilder.WednesdayEndTime != null
|| scheduleBuilder.ThursdayStartTime != scheduleBuilder.FridayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.FridayStartTime != null || scheduleBuilder.ThursdayEndTime != scheduleBuilder.FridayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.FridayEndTime != null
|| scheduleBuilder.ThursdayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.ThursdayStartTime != null && scheduleBuilder.SaturdayStartTime != null || scheduleBuilder.ThursdayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.ThursdayEndTime != null && scheduleBuilder.SaturdayEndTime != null
|| scheduleBuilder.FridayStartTime != scheduleBuilder.SaturdayStartTime && scheduleBuilder.FridayStartTime != null && scheduleBuilder.SaturdayStartTime != null || scheduleBuilder.FridayEndTime != scheduleBuilder.SaturdayEndTime && scheduleBuilder.FridayEndTime != null && scheduleBuilder.SaturdayEndTime != null
)
{
System.Diagnostics.Debug.Write("Tuesday and Sunday Don't Match!");
scheduleBuilder.DaysMatching = "No";
}
scheduleBuilder.ScheduleSummary = scheduleBuilder.Days + " " + startTime + " | Scheduled Days Match: " + scheduleBuilder.DaysMatching;
}
else
{
var scheduleInDb = _context.ScheduleBuilder.Single(a => a.Id == scheduleBuilder.Id);
scheduleInDb.MondayStartTime = scheduleBuilder.MondayStartTime;
scheduleInDb.TuesdayStartTime = scheduleBuilder.TuesdayStartTime;
scheduleInDb.WednesdayStartTime = scheduleBuilder.WednesdayStartTime;
scheduleInDb.ThursdayStartTime = scheduleBuilder.ThursdayStartTime;
scheduleInDb.FridayStartTime = scheduleBuilder.FridayStartTime;
scheduleInDb.SaturdayStartTime = scheduleBuilder.SaturdayStartTime;
scheduleInDb.SundayStartTime = scheduleBuilder.SundayStartTime;
scheduleInDb.MondayEndTime = scheduleBuilder.MondayEndTime;
scheduleInDb.TuesdayEndTime = scheduleBuilder.TuesdayEndTime;
scheduleInDb.WednesdayEndTime = scheduleBuilder.WednesdayEndTime;
scheduleInDb.ThursdayEndTime = scheduleBuilder.ThursdayEndTime;
scheduleInDb.FridayEndTime = scheduleBuilder.FridayEndTime;
scheduleInDb.SaturdayEndTime = scheduleBuilder.SaturdayEndTime;
scheduleInDb.SundayEndTime = scheduleBuilder.SundayEndTime;
scheduleInDb.MondayFirstBreak = scheduleBuilder.MondayFirstBreak;
scheduleInDb.TuesdayFirstBreak = scheduleBuilder.TuesdayFirstBreak;
scheduleInDb.WednesdayFirstBreak = scheduleBuilder.WednesdayFirstBreak;
scheduleInDb.ThursdayFirstBreak = scheduleBuilder.ThursdayFirstBreak;
scheduleInDb.FridayFirstBreak = scheduleBuilder.FridayFirstBreak;
scheduleInDb.SaturdayFirstBreak = scheduleBuilder.SaturdayFirstBreak;
scheduleInDb.SundayFirstBreak = scheduleBuilder.SundayFirstBreak;
scheduleInDb.MondaySecondBreak = scheduleBuilder.MondaySecondBreak;
scheduleInDb.TuesdaySecondBreak = scheduleBuilder.TuesdaySecondBreak;
scheduleInDb.WednesdaySecondBreak = scheduleBuilder.WednesdaySecondBreak;
scheduleInDb.ThursdaySecondBreak = scheduleBuilder.ThursdaySecondBreak;
scheduleInDb.FridaySecondBreak = scheduleBuilder.FridayFirstBreak;
scheduleInDb.SaturdaySecondBreak = scheduleBuilder.SaturdaySecondBreak;
scheduleInDb.SundaySecondBreak = scheduleBuilder.SundaySecondBreak;
scheduleInDb.MondayLunch = scheduleBuilder.MondayLunch;
scheduleInDb.TuesdayLunch = scheduleBuilder.TuesdayLunch;
scheduleInDb.WednesdayLunch = scheduleBuilder.WednesdayLunch;
scheduleInDb.ThursdayLunch = scheduleBuilder.ThursdayLunch;
scheduleInDb.FridayLunch = scheduleBuilder.FridayLunch;
scheduleInDb.SaturdayLunch = scheduleBuilder.SaturdayLunch;
scheduleInDb.SundayLunch = scheduleBuilder.SundayLunch;
}
_context.SaveChanges();
return RedirectToAction("ScheduleBuilder", "Schedule");
}
ViewModel
public class TierViewModel
{
public IEnumerable<Tier> TierLevel { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public IEnumerable<ShiftLevel> ShiftLevel { get; set; }
public ScheduleBuilder ScheduleBuilder { get; set; }
public IEnumerable<ScheduleBuilder> ScheduleBuilderList { get; set; }
public ScheduleBuilderTier2 ScheduleBuilderTwo { get; set; }
}