/// <summary> /// Saves team that is currently being edited and switches to team select mode. /// </summary> public void EndTeamEdit() { FridgeFruitonTeam team = teams[selectedTeamIndex]; FruitonTeam kTeam = teams[selectedTeamIndex].KernelTeam; team.Valid = !TeamContainsMissingFruitons(kTeam); var newName = InputTeamName.text; if (kTeam.Name != newName) { string oldName = kTeam.Name; kTeam.Name = newName; team.gameObject.GetComponentInChildren <Text>().text = GetTeamDescription(kTeam); PlayerHelper.RemoveFruitonTeam(oldName, (r) => { PlayerHelper.UploadFruitonTeam(team.KernelTeam, Debug.Log, Debug.Log); }, Debug.Log); } else { team.gameObject.GetComponentInChildren <Text>().text = GetTeamDescription(kTeam); PlayerHelper.UploadFruitonTeam(team.KernelTeam, Debug.Log, Debug.Log); } Serializer.SerializeFruitonTeams(); SwitchViewMode(ViewMode.TeamSelect); }
/// <param name="a">1st team to compare</param> /// <param name="b">2nd team to compare</param> /// <returns>true if given teams have same fruitons on same positions</returns> private static bool AreTeamsEqual(FruitonTeam a, FruitonTeam b) { if (a.FruitonIDs.Count != b.FruitonIDs.Count || a.Positions.Count != b.Positions.Count) { return(false); } for (var i = 0; i < a.FruitonIDs.Count; i++) { if (a.FruitonIDs[i] != b.FruitonIDs[i]) { return(false); } } for (var i = 0; i < a.Positions.Count; i++) { if (a.Positions[i].X != b.Positions[i].X || a.Positions[i].Y != b.Positions[i].Y) { return(false); } } return(true); }
/// <summary> /// Checks if the team is missing any fruiton. /// </summary> /// <param name="team">team to check</param> /// <returns>true if the team is complete</returns> private bool IsTeamComplete(FruitonTeam team) { int[] fruitonIDsArray = new int[team.FruitonIDs.Count]; team.FruitonIDs.CopyTo(fruitonIDsArray, 0); return(FruitonTeamValidator .validateFruitonTeam(new haxe.root.Array <int>(fruitonIDsArray), GameManager.Instance.FruitonDatabase).complete); }
/// <summary> /// Creates new team, switches to team edit mode and selects it. /// </summary> public void CreateNewTeam() { var newFruitonTeam = new FruitonTeam { Name = GetNextAvailableTeamName() }; GameManager.Instance.FruitonTeamList.FruitonTeams.Add(newFruitonTeam); AddTeamToScene(newFruitonTeam, true); SelectTeam(teams.Count - 1); SwitchViewMode(ViewMode.TeamEdit); ButtonNewTeam.interactable = teams.Count < MAX_TEAM_COUNT; }
private void AddFruitonToTeam(KFruiton fruiton, Position position, FruitonTeam team) { if (isMyTurnToDraft) { FridgeFruiton fridgeFruiton = dbFridgeMapping[fruiton.dbId]; fridgeFruiton.Count--; team.FruitonIDs.Add(fruiton.dbId); team.Positions.Add(position); MyTeamGrid.AddFruitonAt(fruiton, position); draftHandler.SendDraftResponse(fruiton.dbId); } }
/// <summary> /// Send response to incoming challenge request to the server. /// </summary> /// <param name="enemyLogin">username of challenging player</param> /// <param name="accepted">true if challenge was accepted</param> /// <param name="fruitonTeam">team to use in challenge, null if pick mode is draft</param> private void SendChallengeResult(string enemyLogin, bool accepted, FruitonTeam fruitonTeam = null) { var challengeResult = new ChallengeResult { ChallengeFrom = enemyLogin, ChallengeAccepted = accepted, Team = fruitonTeam }; var wsMessage = new WrapperMessage { ChallengeResult = challengeResult }; ConnectionHandler.Instance.SendWebsocketMessage(wsMessage); }
/// <param name="team">team to get description of</param> /// <returns>text to be displayed on team game object</returns> private string GetTeamDescription(FruitonTeam team) { if (TeamContainsMissingFruitons(team)) { return(String.Format( "{0}\n\n(INVALID)", team.Name )); } return(String.Format( "{0}\n\n({1}/10)", team.Name, team.FruitonIDs.Count )); }
/// <summary> /// Creates http request to save logged player's team on the server. /// </summary> /// <param name="fruitonTeam">team to be saved</param> /// <param name="success">action to invoke when the request succeeds</param> /// <param name="error">action to invoke when the request fails</param> public static void UploadFruitonTeam(FruitonTeam fruitonTeam, Action <string> success, Action <string> error) { if (GameManager.Instance.IsInTrial) { return; } byte[] body = Serializer.GetBinaryData(fruitonTeam); ConnectionHandler.Instance.StartCoroutine( ConnectionHandler.Instance.Post( "secured/addFruitonTeam", success, error, body, NetworkUtils.CreateRequestHeaders(true) ) ); }
protected override void Start() { base.Start(); myTeam = new FruitonTeam(); enemyTeam = new FruitonTeam(); InitializeAllFruitons(); MyTeamGrid.LoadTeam(myTeam, dbFridgeMapping); EnemyTeamGrid.LoadTeam(enemyTeam, null); SetupView(); InitializeTeamGridListeners(); InitializeFruitonDetailListeners(); DragAndDropFruiton.gameObject.SetActive(false); TurnOffDrafting(); if (!ChallengeController.Instance.IsChallengeActive) { FindGame(); } }
/// <summary> /// Checks if the team contains any fruitons that are no longer owned by the player. /// </summary> /// <param name="team">team to check</param> /// <returns>true if the team contains fruitons that are no longer owned by the player</returns> private bool TeamContainsMissingFruitons(FruitonTeam team) { Dictionary <int, int> teamCounts = new Dictionary <int, int>(); foreach (var id in team.FruitonIDs) { if (!teamCounts.ContainsKey(id)) { teamCounts[id] = 0; } teamCounts[id]++; } foreach (var r in teamCounts) { var teamFruitonId = r.Key; if (r.Value > GameManager.Instance.AvailableFruitons.Count(id => id == teamFruitonId)) { return(true); } } return(false); }
/// <summary> /// Loads fruiton team and displays it on the grid. /// </summary> /// <param name="team">team to load</param> /// <param name="dbFridgeMapping">fruiton id to fridge fruiton game object map</param> public void LoadTeam(FruitonTeam team, Dictionary <int, FridgeFruiton> dbFridgeMapping) { ClearFruitons(); if (dbFridgeMapping != null) { List <int> availableFruitons = GameManager.Instance.AvailableFruitons; foreach (var fruiton in GameManager.Instance.AllPlayableFruitons) { var dbId = fruiton.dbId; dbFridgeMapping[dbId].Count = availableFruitons.Count(id => id == dbId); } } // reset background color of every square for (var x = 0; x < gridSquares.GetLength(0); x++) { for (var y = 0; y < gridSquares.GetLength(1); y++) { gridSquares[x, y].ResetDefaultBgColor(); gridSquares[x, y].CancelHighlight(); } } for (int i = 0; i < team.FruitonIDs.Count; i++) { var fruitonId = team.FruitonIDs[i]; var pos = team.Positions[i]; var kernelFruiton = FruitonFactory.makeFruiton(fruitonId, GameManager.Instance.FruitonDatabase); var x = IsMirrored ? 1 - pos.Y : pos.Y; var y = pos.X - 2; if (dbFridgeMapping != null && --dbFridgeMapping[fruitonId].Count < 0) { // set background color of squares containing not owned fruitons to red gridSquares[x, y].SetSecondaryBgColorAsDefault(); gridSquares[x, y].CancelHighlight(); } gridSquares[x, y].SetFruiton(kernelFruiton); } }
/// <summary> /// Creates team game object in a scene. /// </summary> /// <param name="team">team object</param> /// <param name="valid">true if team is valid</param> private void AddTeamToScene(FruitonTeam team, bool valid) { GameObject fruitonTeamObject = Instantiate(FridgeTeamTemplate); fruitonTeamObject.transform.SetParent(WrapperTeams.transform); var teamIndex = teams.Count; var fridgeFruitonTeam = fruitonTeamObject.GetComponent <FridgeFruitonTeam>(); fridgeFruitonTeam.Valid = valid; fridgeFruitonTeam.FridgeIndex = teamIndex; fridgeFruitonTeam.KernelTeam = team; teams.Add(fridgeFruitonTeam); fruitonTeamObject.name = team.Name; fruitonTeamObject.transform.localScale = FridgeFruitonTemplate.gameObject.GetComponent <RectTransform>() .localScale; fruitonTeamObject.transform.localPosition = GetPositionOnScrollViewGrid(teamIndex); fruitonTeamObject.GetComponentInChildren <Text>().text = GetTeamDescription(team); fruitonTeamObject.GetComponent <Button>().onClick.AddListener(() => SelectTeam(fridgeFruitonTeam.FridgeIndex)); fruitonTeamObject.GetComponent <Image>().color = valid ? FridgeFruitonTeam.COLOR_DEFAULT : FridgeFruitonTeam.COLOR_INVALID; fruitonTeamObject.SetActive(true); }
/// <summary> /// Selects and loads fruiton team to team grid. /// </summary> /// <param name="index">index of selected team</param> private void SelectTeam(int index) { var isValidTeamIndex = IsValidTeamIndex(index); ButtonPlay.interactable = isValidTeamIndex || canPlayWithoutTeamSelected; ButtonEdit.interactable = isValidTeamIndex; ButtonDelete.interactable = isValidTeamIndex; if (!isValidTeamIndex) { selectedTeamIndex = -1; CurrentFruitonTeam = null; MyTeamGrid.ResetTeam(); return; } if (selectedTeamIndex >= 0) { var lastSelectedTeam = teams[selectedTeamIndex]; lastSelectedTeam.gameObject.GetComponent <Image>().color = lastSelectedTeam.Valid ? FridgeFruitonTeam.COLOR_DEFAULT : FridgeFruitonTeam.COLOR_INVALID; } teams[index].gameObject.GetComponent <Image>().color = FridgeFruitonTeam.COLOR_SELECTED; selectedTeamIndex = index; var newTeam = teams[selectedTeamIndex].KernelTeam; InputTeamName.text = newTeam.Name; CurrentFruitonTeam = newTeam; Dictionary <int, FridgeFruiton> passedDictionary = dbFridgeMapping; if (state != TeamManagementState.TEAM_MANAGEMENT) { passedDictionary = null; } MyTeamGrid.LoadTeam(newTeam, passedDictionary); }