Skip to content

Commit

Permalink
PreRound logic working (needs more testing), Entering Round State whe…
Browse files Browse the repository at this point in the history
…n done

difficult to test because it takes a while to reach a specific state (with 4 players)
  • Loading branch information
Luis Beaucamp committed Mar 30, 2020
1 parent de9a9c3 commit d75cd0a
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 128 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@ fabric.properties
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
.idea/caches/build_file_checksums.ser

# ignore the setup file, this will be added manually to a release
/InnoSetup/InnoCompilerOutput/gluecksschwein_setup.exe
33 changes: 2 additions & 31 deletions Assets/Scenes/SampleScene.unity → Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1399,10 +1399,10 @@ MonoBehaviour:
m_Options:
- m_Text: Sauspiel auf...
m_Image: {fileID: 0}
- m_Text: Oide
m_Image: {fileID: 21300000, guid: 85795a92179f7f44f9471f4ca80a15c7, type: 3}
- m_Text: Blaue
m_Image: {fileID: 21300000, guid: 7e0146c7891a98441ae179b851fa21bf, type: 3}
- m_Text: Oide
m_Image: {fileID: 21300000, guid: 85795a92179f7f44f9471f4ca80a15c7, type: 3}
- m_Text: Hundsgfickte
m_Image: {fileID: 21300000, guid: 2f4d6d468e1205e409f8e47a44aa7211, type: 3}
m_OnValueChanged:
Expand Down Expand Up @@ -2008,7 +2008,6 @@ GameObject:
m_Component:
- component: {fileID: 1344795749}
- component: {fileID: 1344795751}
- component: {fileID: 1344795750}
- component: {fileID: 1344795752}
m_Layer: 5
m_Name: PreRoundPanel
Expand Down Expand Up @@ -2040,34 +2039,6 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 200}
m_SizeDelta: {x: 1000, y: 40}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &1344795750
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1344795748}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 0}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &1344795751
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down
File renamed without changes.
21 changes: 18 additions & 3 deletions Assets/Scripts/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@

public static class Extensions
{
private static Random rng = new Random();
private static readonly Random Rng = new Random();

public static void Shuffle<T>(this IList<T> list)
{
{
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
int k = Rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}

public static T CycleNext<T>(this IList<T> list, T current) => list.Cycle(current, true);

public static T CyclePrev<T>(this IList<T> list, T current) => list.Cycle(current, false);

private static T Cycle<T>(this IList<T> list, T current, bool positive)
{
int elemIndex = list.IndexOf(current);
if (elemIndex == -1)
{
throw new InvalidOperationException($"{nameof(current)} not in list");
}
int amount = positive ? 1 : -1;
return list[(elemIndex + list.Count + amount) % list.Count];
}
}
127 changes: 61 additions & 66 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Networking;
Expand All @@ -14,17 +13,17 @@ public class GameManager : NetworkBehaviour
public enum GameState
{
Waiting, // Waiting until 4 players are connected
GameRunning, // 4 players are connected, host can click button to deal cards
GameRunning, // 4 players are connected, players can enter names, host can click button to deal cards
PreRound, // Players take turns deciding the game mode, i.e. Sauspiel, Solo, etc.
RoundSau, // Active during a round of Sauspiel
RoundSolo, // Active during a round of Solo
RoundWenz, // Active during a round of Wenz
RoundRamsch, // Active during a round of Ramsch
Round, // Active during a round (use in combination with CurrentRoundMode)
RoundFinished // We enter this state after the last "Stich". Here we can show/count scores
// TODO maybe we don't need RoundFinished and can just enter GameRunning instead
}

[field: SyncVar] public GameState CurrentGameState { get; set; }
[field: SyncVar] public GameState CurrentGameState { get; private set; }
[field: SyncVar] public RoundMode CurrentRoundMode { get; private set; }

private Dictionary<NetworkInstanceId, PreRoundChoice> CurrentPreRoundChoices { get; } =
new Dictionary<NetworkInstanceId, PreRoundChoice>();

[SyncVar(hook = nameof(OnGameStateTextChanged))] [SerializeField]
private string gameStateText;
Expand All @@ -33,22 +32,22 @@ public enum GameState

public enum RoundMode
{
Ramsch,
SauspielBlatt,
SauspielEichel,
SauspielSchelln,
Solo,
Wenz,
Ramsch
}

public enum PreRoundChoice
{
Weiter,
SauspielBlatt,
SauspielEichel,
SauspielSchelln,
Solo,
Wenz,
Weiter
}

#endregion
Expand Down Expand Up @@ -82,7 +81,8 @@ public class SyncListPlayingCard : SyncListStruct<PlayingCard.PlayingCardInfo> {

[SerializeField] private List<GameObject> playedCardSlots;
private readonly SyncListPlayingCard _playedCards = new SyncListPlayingCard();

private Player _currentPreRoundDecider;

#endregion


Expand Down Expand Up @@ -134,6 +134,7 @@ private void EnterStatePreRound()
{
// update the game state
CurrentGameState = GameState.PreRound;
gameStateText = "Runde vorbereiten...";

// deal the cards to the players
DealCards();
Expand All @@ -142,46 +143,33 @@ private void EnterStatePreRound()
dealCardsButton.gameObject.SetActive(false);

// update the starting player
_startingPlayer = SelectNextStartingPlayer();

Player currentPreRoundDecider = _startingPlayer;

_startingPlayer = players.CycleNext(_startingPlayer);

// set the current pre round decider to be the starting player
_currentPreRoundDecider = _startingPlayer;

// reset the Round Mode
CurrentRoundMode = RoundMode.Ramsch;

// reset the player choices
CurrentPreRoundChoices.Clear();

// Display the Pre Round Buttons for the currently deciding player
currentPreRoundDecider.RpcDisplayPreRoundButtons();
}

/// <summary>
/// Used to enter the "RoundSau" state
/// </summary>
private void EnterStateRoundSau()
{
CurrentGameState = GameState.RoundSau;
}

/// <summary>
/// Used to enter the "RoundSolo" state
/// </summary>
private void EnterStateRoundSolo()
{
CurrentGameState = GameState.RoundSolo;
}

/// <summary>
/// Used to enter the "RoundWenz" state
/// </summary>
private void EnterStateRoundWenz()
{
CurrentGameState = GameState.RoundWenz;
_currentPreRoundDecider.RpcDisplayPreRoundButtons(CurrentRoundMode);
}

/// <summary>
/// Used to enter the "RoundRamsch" state
/// Used to enter the "Round" state
/// </summary>
private void EnterStateRoundRamsch()
private void EnterStateRound()
{
CurrentGameState = GameState.RoundRamsch;
CurrentGameState = GameState.Round;
gameStateText = $"Runde läuft ({CurrentRoundMode})";

Debug.Log($"{MethodBase.GetCurrentMethod().DeclaringType}::{MethodBase.GetCurrentMethod().Name}: " +
$"entering round with {nameof(CurrentRoundMode)}={CurrentRoundMode}.");
}

/// <summary>
/// Used to enter the "Round Finished" state
/// </summary>
Expand All @@ -190,31 +178,38 @@ private void EnterStateRoundFinished()
CurrentGameState = GameState.RoundFinished;
}

private Player SelectNextStartingPlayer()
{
// gib mir den index des aktuellen _startingPlayers
int lastStartingPlayerIndex = players.IndexOf(_startingPlayer);

int newStartingPlayerIndex = lastStartingPlayerIndex + 1;
if (newStartingPlayerIndex == 4)
{
newStartingPlayerIndex = 0;
}

return players[newStartingPlayerIndex];
}

[Command]
public void CmdSelectPreRoundChoice(NetworkInstanceId playerId, PreRoundChoice playerChoice)
public void CmdHandlePreRoundChoice(NetworkInstanceId playerId, PreRoundChoice playerChoice)
{
Debug.Log($"{MethodBase.GetCurrentMethod().DeclaringType}::{MethodBase.GetCurrentMethod().Name}: " +
$"player {playerId} chose {playerChoice}");

// TODO compute remaining options for other players

// Now we compute the remaining options for next player
///////////////////////////////////////////////////////

// we can essentially set the round mode to the player's choice, we only chooses from the remaining (legal) choices
// BUT... this does not apply for "Weiter": if the player chose "Weiter", we don't change the round mode
if (playerChoice != PreRoundChoice.Weiter)
{
CurrentRoundMode = (RoundMode) playerChoice;
}

// maybe we want to show the choices to all players at some point
CurrentPreRoundChoices[playerId] = playerChoice;

bool preRoundFinished = playerChoice == PreRoundChoice.Wenz || CurrentPreRoundChoices.Count == players.Count;

// TODO display buttons to the next player
throw new NotImplementedException();
if (preRoundFinished)
{
// we don't have to do anything else here: the CurrentRoundMode is already set correctly (including Ramsch)
EnterStateRound();
}
else
{
// otherwise display buttons to the next player
_currentPreRoundDecider = players.CycleNext(_currentPreRoundDecider);
_currentPreRoundDecider.RpcDisplayPreRoundButtons(CurrentRoundMode);
}
}

#endregion
Expand Down Expand Up @@ -300,7 +295,7 @@ private void OnCardPlayed(SyncList<PlayingCard.PlayingCardInfo>.Operation op, in
playedCardSlots[i].GetComponent<Image>().sprite = PlayingCard.SpriteDict[_playedCards[i]];
}

void OnGameStateTextChanged(string newText)
private void OnGameStateTextChanged(string newText)
{
Debug.Log($"{MethodBase.GetCurrentMethod().DeclaringType}::{MethodBase.GetCurrentMethod().Name}: " +
$"new text = \"{gameStateText}\"");
Expand Down
Loading

0 comments on commit d75cd0a

Please sign in to comment.