示例#1
0
    public static NetworkResponse Parse(MemoryStream dataStream)
    {
        ResponseConvergeNewAttempt response = new ResponseConvergeNewAttempt();

        using (BinaryReader br = new BinaryReader(dataStream, Encoding.UTF8)) {
            int    playerId    = br.ReadInt32();
            int    ecosystemId = br.ReadInt32();
            int    attemptId   = br.ReadInt32();
            bool   allowHints  = br.ReadBoolean();
            int    hintId      = br.ReadInt32();
            short  fldSize     = br.ReadInt16();
            String config      = System.Text.Encoding.UTF8.GetString(br.ReadBytes(fldSize));
            fldSize = br.ReadInt16();
            String csv = System.Text.Encoding.UTF8.GetString(br.ReadBytes(fldSize));
            //Debug.Log ("csv = " + csv);

            ConvergeAttempt attempt = new ConvergeAttempt(playerId,
                                                          ecosystemId,
                                                          attemptId,
                                                          allowHints,
                                                          hintId,
                                                          config,
                                                          csv
                                                          //null
                                                          );

            response.attempt = attempt;
        }

        return(response);
    }
    public static NetworkResponse Parse(MemoryStream dataStream)
    {
        ResponseConvergePriorAttempt response = new ResponseConvergePriorAttempt();

        using (BinaryReader br = new BinaryReader(dataStream, Encoding.UTF8)) {
            int playerId = br.ReadInt32 ();
            int ecosystemId = br.ReadInt32 ();
            int attemptId = br.ReadInt32 ();
            bool allowHints = br.ReadBoolean ();
            int hintId = br.ReadInt32 ();
            short fldSize = br.ReadInt16 ();
            String config = System.Text.Encoding.UTF8.GetString (br.ReadBytes (fldSize));
            fldSize = br.ReadInt16 ();
            String csv = System.Text.Encoding.UTF8.GetString (br.ReadBytes (fldSize));

            ConvergeAttempt attempt = new ConvergeAttempt (playerId,
                                                           ecosystemId,
                                                           attemptId,
                                                           allowHints,
                                                           hintId,
                                                           config,
                                                           csv
                                                           //null
                                                           );

            response.attempt = attempt;
        }

        return response;
    }
    public void ProcessConvergePriorAttempt(NetworkResponse response)
    {
        ResponseConvergePriorAttempt args = response as ResponseConvergePriorAttempt;
        ConvergeAttempt attempt           = args.attempt;

        //add new attempt to list if response includes an attempt
        if (attempt.attempt_id == Constants.ID_NOT_SET)
        {
            Debug.LogError("attempt_id not valid in ProcessConvergePriorAttempt");
        }
        else
        {
            attemptList.Add(attempt);
            if (attempt.hint_id != Constants.ID_NOT_SET)
            {
                priorHintIdList.Add(attempt.hint_id);
            }
        }

        //Once all attempts have been processed, finalize.
        if (attemptList.Count == attemptCount)
        {
            FinalizeLoadPriorAttempts();
        }
    }
    //after all prior attempts have been retrieved from server, finalize
    private void FinalizeLoadPriorAttempts()
    {
        //if this is the first load, whether or not to allow hints for this player
        //has not been established; determine randomly if no prior, o/w use prior
        //attempt.
        if (!allowHintsConfigured)
        {
            if (attemptCount == 0)
            {
                allowHintsMaster = Functions.RandomBoolean();
            }
            else
            {
                allowHintsMaster = attemptList [attemptCount - 1].allow_hints;
            }
            allowHintsConfigured = true;
        }

        //if count is 0, base next attempt info on ecosystem default
        if (attemptCount == 0)
        {
            int attempt_id = 0;
            currAttempt = new ConvergeAttempt(
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                Constants.ID_NOT_SET,
                ecosystemList [ecosystem_idx].config_default,
                ecosystemList [ecosystem_idx].csv_default_string,
                manager
                );
            //otherwise, base next attempt info on immediate prior attempt
        }
        else
        {
            int attempt_id = attemptList [attemptCount - 1].attempt_id + 1;
            currAttempt = new ConvergeAttempt(
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                Constants.ID_NOT_SET,
                attemptList [attemptCount - 1].config,
                attemptList [attemptCount - 1].csv_string,
                manager
                );
        }


        FinalizeAttemptUpdate(attemptCount - 1, true);
    }
    //reset currattempt based on requested attempt (or default)
    private void ResetCurrAttempt(int attemptIdx)
    {
        //if count is 0, base next attempt info on ecosystem default
        int attempt_id = currAttempt.attempt_id;
        int hint_id    = currAttempt.hint_id;       //if player received a hint, retain history.

//		if (attemptCount > 0) {
//			attempt_id = attemptList [attemptCount - 1].attempt_id + 1;
//		} else {
//			attempt_id = 0;
//		}
        if (attemptIdx == Constants.ID_NOT_SET)
        {
            currAttempt = new ConvergeAttempt(
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                hint_id,
                ecosystemList [ecosystem_idx].config_default,
                ecosystemList [ecosystem_idx].csv_default_string,
                manager
                );
        }
        else
        {
            currAttempt = new ConvergeAttempt(
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                hint_id,
                attemptList [attemptIdx].config,
                attemptList [attemptIdx].csv_string,
                manager
                );
        }

        FinalizeAttemptUpdate(attemptIdx, false);
    }
    public void ProcessConvergeNewAttempt(NetworkResponse response)
    {
        ConvergeAttempt            attempt;
        ResponseConvergeNewAttempt args = response as ResponseConvergeNewAttempt;

        attempt = args.attempt;

        //if the submission resulted in a valid attempt, add to attempt list and reinitialize
        //currAttempt for next attempt.  Otherwise, keep current attempt
        if (attempt != null && attempt.attempt_id != Constants.ID_NOT_SET)
        {
            currAttempt.attempt_id = attempt.attempt_id;
            currAttempt.SetCSV(attempt.csv_string);
            attemptList.Add(currAttempt);
            attemptCount = attemptList.Count;

            //calculate score and send back to server.
            CSVObject target = ecosystemList[ecosystem_idx].csv_target_object;
            int       score  = currAttempt.csv_object.CalculateScore(target);
            NetworkManager.Send(
                ConvergeNewAttemptScoreProtocol.Prepare(
                    player_id,
                    ecosystem_id,
                    attempt.attempt_id,
                    score
                    ),
                ProcessConvergeNewAttemptScore
                );

            //update pertinent variables with new data
            if (currAttempt.hint_id != Constants.ID_NOT_SET)
            {
                priorHintIdList.Add(currAttempt.hint_id);
            }
            //need to recalc reset slider config due to additional attempt
            isResetSliderInitialized = false;

            if (barGraph != null)
            {
                barGraph.InputToCSVObject(currAttempt.csv_string, manager);
            }

            currAttempt = new ConvergeAttempt(
                player_id,
                ecosystem_id,
                attempt.attempt_id + 1,
                allowHintsMaster,
                Constants.ID_NOT_SET,
                attempt.config,
                null,
                manager
                );

            FinalizeAttemptUpdate(attemptCount - 1, false);
        }
        else
        {
            Debug.LogError("Submission of new attempt failed to produce results.");
            SetIsProcessing(false);
        }
    }
    void MakeWindow(int id)
    {
        Functions.DrawBackground(new Rect(0, 0, width, height), bgTexture);

        GUIStyle style = new GUIStyle(GUI.skin.label);

        style.alignment = TextAnchor.UpperCenter;
        style.font      = font;
        style.fontSize  = 16;

        GUI.Label(new Rect((windowRect.width - 100) / 2, 0, 100, 30), "Convergence Game", style);
        if (GUI.Button(new Rect(windowRect.width - 100 - bufferBorder, 0, 100, 30), "Logout"))
        {
            NetworkManager.Send(LogoutProtocol.Prepare(0), ProcessLogout);
        }

        GUI.BeginGroup(new Rect(bufferBorder, 0, windowRect.width, 100));
        style.alignment = TextAnchor.LowerLeft;
        style.fontSize  = 14;
        GUI.Label(new Rect(0, 0, 300, 30), "Select Ecosystem:", style);
        GUI.SetNextControlName("ecosystem_idx_field");
        int new_idx;

        new_idx = GUI.SelectionGrid(new Rect(0, 35, windowRect.width - 20, 30), ecosystem_idx,
                                    ecosysDescripts, ecosysDescripts.Length);
        GUI.EndGroup();
        if (!isProcessing && new_idx != ecosystem_idx)
        {
            //Debug.Log ("Updating selected ecosystem.");
            SetIsProcessing(true);
            new_ecosystem_idx = new_idx;
            new_ecosystem_id  = GetEcosystemId(new_ecosystem_idx);
            GetPriorAttempts();
        }
        if (graphs != null)
        {
            graphs.DrawGraphs();
        }

        GUIStyle styleEdit = new GUIStyle(GUI.skin.textArea);

        styleEdit.wordWrap = true;

        //data entry fields
        DrawParameterFields(style);

        if (isInitial && GUI.GetNameOfFocusedControl() == "")
        {
            GUI.FocusControl("ecosystem_idx_field");
            isInitial = false;
        }

        string buttonTitle = isProcessing ? "Processing" : "Submit";

        if (!(isProcessing && !blinkOn))
        {
            if (GUI.Button(new Rect(bufferBorder, height - 30 - bufferBorder, 100, 30), buttonTitle) &&
                !isProcessing)
            {
                //make sure new config is distinct from prior attempts and initial value
                currAttempt.UpdateConfig();                   //update config based on user data entry changes
                ConvergeAttempt prior = attemptList.Find(entry => entry.config == currAttempt.config);
                if (prior == null && currAttempt.ParamsUpdated())
                {
                    Submit();
                    SetIsProcessing(true);
                }
                else if (!showPopup)
                {
                    int prior_idx = attemptList.FindIndex(entry => entry.config == currAttempt.config);
                    if (prior_idx == Constants.ID_NOT_SET)
                    {
                        popupMessage = "Duplicate configuration to initial ecosystem.  Please try again.";
                    }
                    else
                    {
                        popupMessage = "Duplicate configuration to prior attempt (#" + (prior_idx + 1) + ").  Please try again.";
                    }
                    //Debug.Log (popupMessage);
                    showPopup = true;
                }
            }
        }

        if (GUI.Button(new Rect(bufferBorder + 110, height - 30 - bufferBorder, 110, 30), "Progress Report"))
        {
            GenerateBarGraph();
        }

        int screenOffset = 0;

        if (currAttempt != null && currAttempt.allow_hints)
        {
            screenOffset += bufferBorder + 110;
            if (GUI.Button(new Rect(bufferBorder * 2 + 110 * 2, height - 30 - bufferBorder, 110, 30), "Get Hint"))
            {
                //only give new hint if one hasn't been provided during this session.
                if (currAttempt.hint_id == Constants.ID_NOT_SET)
                {
                    int hintId = GetRandomHint(true);
                    if (hintId == Constants.ID_NOT_SET)
                    {
                        popupMessage = "Sorry, no new hints are available.";
                    }
                    else
                    {
                        popupMessage        = hintDict[hintId];
                        currAttempt.hint_id = hintId;
                    }
                }
                else
                {
                    if (hintDict.ContainsKey(currAttempt.hint_id))
                    {
                        popupMessage = hintDict[currAttempt.hint_id] + "\n[Only one hint is provided per attempt.]";
                    }
                    else
                    {
                        Debug.LogError("Invalid hint for current attempt, ID = " + currAttempt.hint_id);
                        popupMessage = "Error, hint not available.";
                    }
                }
                if (priorHintIdList.Count > 0)
                {
                    popupMessage += "\n\nPrior Hints:";
                }
                for (int i = 0; i < priorHintIdList.Count; i++)
                {
                    popupMessage += "\n\n" + hintDict[priorHintIdList[i]];
                }
                //Debug.Log (popupMessage);
                showPopup = true;
            }
        }

        DrawResetButtons(screenOffset, style);
    }
    //reset currattempt based on requested attempt (or default)
    private void ResetCurrAttempt(int attemptIdx)
    {
        //if count is 0, base next attempt info on ecosystem default
        int attempt_id = currAttempt.attempt_id;
        int hint_id = currAttempt.hint_id;  //if player received a hint, retain history.
        //		if (attemptCount > 0) {
        //			attempt_id = attemptList [attemptCount - 1].attempt_id + 1;
        //		} else {
        //			attempt_id = 0;
        //		}
        if (attemptIdx == Constants.ID_NOT_SET) {
            currAttempt = new ConvergeAttempt (
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                hint_id,
                ecosystemList [ecosystem_idx].config_default,
                ecosystemList [ecosystem_idx].csv_default_string
                //manager
            );
        } else {
            currAttempt = new ConvergeAttempt (
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                hint_id,
                attemptList [attemptIdx].config,
                attemptList [attemptIdx].csv_string
                //manager
            );
        }

        FinalizeAttemptUpdate (attemptIdx, false);
    }
    //after all prior attempts have been retrieved from server, finalize
    private void FinalizeLoadPriorAttempts()
    {
        //if this is the first load, whether or not to allow hints for this player
        //has not been established; determine randomly if no prior, o/w use prior
        //attempt.
        if (!allowHintsConfigured) {
            if (attemptCount == 0) {
                allowHintsMaster = Functions.RandomBoolean ();
            } else {
                allowHintsMaster = attemptList [attemptCount - 1].allow_hints;
            }
            allowHintsConfigured = true;
        }

        //if count is 0, base next attempt info on ecosystem default
        if (attemptCount == 0) {
            int attempt_id = 0;
            currAttempt = new ConvergeAttempt (
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                Constants.ID_NOT_SET,
                ecosystemList [ecosystem_idx].config_default,
                ecosystemList [ecosystem_idx].csv_default_string
                //manager
            );
            //otherwise, base next attempt info on immediate prior attempt
        } else {
            int attempt_id = attemptList [attemptCount - 1].attempt_id + 1;
            currAttempt = new ConvergeAttempt (
                player_id,
                ecosystem_id,
                attempt_id,
                allowHintsMaster,
                Constants.ID_NOT_SET,
                attemptList [attemptCount - 1].config,
                attemptList [attemptCount - 1].csv_string
                //manager
            );
        }

        FinalizeAttemptUpdate (attemptCount - 1, true);
    }
示例#10
0
    public void ProcessConvergeNewAttempt(NetworkResponse response)
    {
        ConvergeAttempt attempt;
        ResponseConvergeNewAttempt args = response as ResponseConvergeNewAttempt;
        attempt = args.attempt;

        //if the submission resulted in a valid attempt, add to attempt list and reinitialize
        //currAttempt for next attempt.  Otherwise, keep current attempt
        if (attempt != null && attempt.attempt_id != Constants.ID_NOT_SET) {
            currAttempt.attempt_id = attempt.attempt_id;
            currAttempt.SetCSV (attempt.csv_string);
            attemptList.Add (currAttempt);
            attemptCount = attemptList.Count;

            //calculate score and send back to server.
            CSVObject target = ecosystemList[ecosystem_idx].csv_target_object;
            int score = currAttempt.csv_object.CalculateScore (target);
            NetworkManager.Send (
                ConvergeNewAttemptScoreProtocol.Prepare (
                player_id,
                ecosystem_id,
                attempt.attempt_id,
                score
                ),
                ProcessConvergeNewAttemptScore
                );

            //update pertinent variables with new data
            if (currAttempt.hint_id != Constants.ID_NOT_SET) {
                priorHintIdList.Add (currAttempt.hint_id);
            }
            //need to recalc reset slider config due to additional attempt
            isResetSliderInitialized = false;

            if (barGraph != null) {
                barGraph.InputToCSVObject (currAttempt.csv_string, manager);
            }

            currAttempt = new ConvergeAttempt (
                player_id,
                ecosystem_id,
                attempt.attempt_id + 1,
                allowHintsMaster,
                Constants.ID_NOT_SET,
                attempt.config,
                null
                //manager
            );

            FinalizeAttemptUpdate (attemptCount - 1, false);
        } else {
            Debug.LogError ("Submission of new attempt failed to produce results.");
            SetIsProcessing (false);
        }
    }
示例#11
0
	void MakeWindow (int id)
	{
		Functions.DrawBackground (new Rect (0, 0, width, height), bgTexture);
		
		GUIStyle style = new GUIStyle (GUI.skin.label);
		style.alignment = TextAnchor.UpperCenter;
		style.font = font;
		style.fontSize = 16;

		GUI.Label (new Rect ((windowRect.width - 200) / 2, 0, 200, 30), "Multiplayer Convergence", style);
		if (GUI.Button (new Rect (windowRect.width - 100 - bufferBorder, 0, 100, 30), "Return to Lobby")) {
			Destroy (this);
			Destroy (foodWeb);
			GameState gs = GameObject.Find ("Global Object").GetComponent<GameState> ();
			Species[] s = gs.GetComponents<Species>();
			foreach (Species sp in s) Destroy (sp); //destroy the "species" objects
			Game.SwitchScene("World");
		}

		/* Player does not select Ecosystem here. Host selects at beginning. 
		GUI.BeginGroup (new Rect (bufferBorder, 0, windowRect.width, 100));
		style.alignment = TextAnchor.LowerLeft;
		style.fontSize = 14;
		GUI.Label (new Rect (0, 0, 300, 30), "Select Ecosystem:", style);
		GUI.SetNextControlName ("ecosystem_idx_field");
		int new_idx;
		new_idx = GUI.SelectionGrid (new Rect (0, 35, windowRect.width - 20, 30), ecosystem_idx, 
                        ecosysDescripts, ecosysDescripts.Length);
		GUI.EndGroup ();
		if (!isProcessing && new_idx != ecosystem_idx) {
			//Debug.Log ("Updating selected ecosystem.");
			SetIsProcessing (true);
			new_ecosystem_idx = new_idx;
			new_ecosystem_id = GetEcosystemId (new_ecosystem_idx);
			GetPriorAttempts ();
		}
		*/

		if (graphs != null) {
			graphs.DrawGraphs ();
		}

		GUIStyle styleEdit = new GUIStyle (GUI.skin.textArea);
		styleEdit.wordWrap = true;

		//data entry fields
		DrawParameterFields (style);

		/* DH change. Not using ecosystem selection by player
		if (isInitial && GUI.GetNameOfFocusedControl () == "") {
			GUI.FocusControl ("ecosystem_idx_field");
			isInitial = false;
		}
		*/

		// DH change
		// Add in time remaining label
		if (timeRemain < 0)
			timeRemain = 0;
		remainLabel = "Bidding Time Remaining:  ";
		if (windowClosed) {
			remainLabel += "Bidding Now Closed";
		} else if (simRunning) {
			remainLabel += "Simulation Running";
		} else {
			remainLabel = remainLabel + timeRemain + " seconds";
		}
		GUI.Label (new Rect (bufferBorder, height - 70 - bufferBorder, 400, 30), remainLabel, style);

		// Add in money balance and bid amount
		GUI.Label (new Rect (bufferBorder + 450, height/2 + bufferBorder + 110, 200, 30), "Balance: $" + balance, style);
		GUI.Label (new Rect (bufferBorder + 450, height/2 + bufferBorder + 150, 200, 30), "Bet:      $" + bet, style);
		if (betAccepted) {
			GUI.Label (new Rect (bufferBorder + 450, height/2 + bufferBorder + 190, 300, 30), "Please wait for results of betting.", style);
		} else if (results) {
			if (won == 1) {
				GUI.Label (new Rect (bufferBorder + 450, height / 2 + bufferBorder + 190, 300, 30), "Congratulations - you won last round!", style);
			} else if (won == 0) {
				GUI.Label (new Rect (bufferBorder + 450, height / 2 + bufferBorder + 190, 300, 30), "Sorry - you lost last round.", style);
			} else {
				GUI.Label (new Rect (bufferBorder + 450, height / 2 + bufferBorder + 190, 300, 30), "You did not play last round.", style);
			}
		}

		if (betAccepted) {
			buttonTitle = "Bet Entered";
		} else {
			buttonTitle = windowClosed ? "Closed" : "Accept";
		}
		if (true) {
			if (GUI.Button (new Rect (bufferBorder, height - 30 - bufferBorder, 100, 30), buttonTitle) &&
				!betAccepted && !windowClosed) {
				//make sure new config is distinct from prior attempts and initial value
				currAttempt.UpdateConfig ();  //update config based on user data entry changes
				ConvergeAttempt prior = attemptList.Find (entry => entry.config == currAttempt.config);
				if (prior == null && currAttempt.ParamsUpdated ()) {
					betAccepted = true;
					Submit ();
					// SetIsProcessing (true);
				} else if (!showPopup) {
					int prior_idx = attemptList.FindIndex (entry => entry.config == currAttempt.config);
					if (prior_idx == Constants.ID_NOT_SET) {
						popupMessage = "Duplicate configuration to initial ecosystem.  Please try again.";
					} else {
						popupMessage = "Duplicate configuration to prior attempt (#" + (prior_idx + 1) + ").  Please try again.";
					}
					//Debug.Log (popupMessage);
					showPopup = true;
				}
			}
		}

		if (GUI.Button (new Rect (bufferBorder + 110, height - 30 - bufferBorder, 110, 30), "Progress Report")) {
			GenerateBarGraph ();
		}

		// Display buttons with opponent bet status 
		// ******

		Color savedColor2 = GUI.color;
		float topLeft = topGraph;

		int id2;
		string buttonText;
		Debug.Log ("Other player button routine");
		foreach (DictionaryEntry entry in playerNames) {
			// do something with entry.Value or entry.Key
			id2 = (int) entry.Key;
			if ((id2 > 0 ) && (betStatusList.Contains(id2))) {
				if (((short) betStatusList [id2]) == 1) {  // bet placed
					GUI.color = Color.green; 
					buttonText = ((string)entry.Value) + " Entered Bet";
				} else {  // bet not placed
					GUI.color = Color.red;
					buttonText = ((string)entry.Value) + " No Bet";
				}
				Debug.Log ("other player button: " + (bufferBorder + width - 170) + " " + topLeft + " " + buttonWidth);
				Debug.Log ("Button text: " + buttonText);
				GUI.Button (new Rect (bufferBorder + width - 150, topLeft, buttonWidth, 30), buttonText);
				topLeft += 45;
			}
		}
		GUI.color = savedColor2;



		int screenOffset = 0;
		/* Not doing hints in multiplayer game - initially
		if (currAttempt != null && currAttempt.allow_hints) {
			screenOffset += bufferBorder + 110;
			if (GUI.Button (new Rect (bufferBorder * 2 + 110 * 2, height - 30 - bufferBorder, 110, 30), "Get Hint")) {
				//only give new hint if one hasn't been provided during this session.
				if (currAttempt.hint_id == Constants.ID_NOT_SET) {
					int hintId = GetRandomHint (true);
					if (hintId == Constants.ID_NOT_SET) {
						popupMessage = "Sorry, no new hints are available.";
					} else {
						popupMessage = hintDict[hintId];
						currAttempt.hint_id = hintId;
					}
				} else {
					if (hintDict.ContainsKey (currAttempt.hint_id)) {
						popupMessage = hintDict[currAttempt.hint_id] + "\n[Only one hint is provided per attempt.]";
					} else {
						Debug.LogError ("Invalid hint for current attempt, ID = " + currAttempt.hint_id);
						popupMessage = "Error, hint not available.";
					}
				}
				if (priorHintIdList.Count > 0) {
					popupMessage += "\n\nPrior Hints:";
				}
				for (int i = 0; i < priorHintIdList.Count; i++) {
					popupMessage += "\n\n" + hintDict[priorHintIdList[i]];
				}
				//Debug.Log (popupMessage);
				showPopup = true;
			}
		}
		*/

		DrawResetButtons (screenOffset, style);
	}