// Update is called once per frame void Update() { if (!hasSpawnedObjects && IsReadyToSpawn()) { InstantiateSpawner(); } if (!sentTraceToDB && (GameState.Singleton.CurrentState == State.Win || GameState.Singleton.CurrentState == State.Lose)) { // HACK (kasiu): Currently using a comma-delimeted thingy. // NetworkClient.Instance.SendServerMess(NetworkClient.MessType_ToServer.SNGSavePlayerData, ""); if (Network.isClient) { NetworkClient.Instance.SendServerMess(NetworkClient.MessType_ToServer.SNGSaveDBTrace, DBStringHelper.traceToString(GameState.Singleton.clickTrace, ':')); NetworkClient.Instance.SendServerMess(NetworkClient.MessType_ToServer.SNGSavePlayerScore, ((int)(GameState.Singleton.score)).ToString()); sentTraceToDB = true; } } }
//THIS IS THE KEY METHOD OF THIS CLASS public void MessageFromClient(NetworkPlayer player, NetworkClient.MessType_ToServer messType, string args) { DebugConsole.LogWarning("GameStateServer.MessageFromClient " + player + ": " + messType.ToString()); DebugConsole.Log("args: "); if (args == null || args.Length == 0) { DebugConsole.Log("<null or zero length>"); } else { DebugConsole.Log(args); } //if( args != null && args.Length > 0 ) DebugConsole.Log( NetworkClient.RayToString( args ) ); RunningGameData rgd = getRunningGameData(player, messType, args); if (rgd == null) { return; //this is ok. } DebugConsole.Log("Hitting big message demux."); switch (messType) { //TODO case NetworkClient.MessType_ToServer.ReadDBStr: "udid" arg case NetworkClient.MessType_ToServer.ReadDBStr: if (String.Compare("udid", args, true) == 0) { NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.UNTYPED, dbManip.getPlayerUDID(rgd.dPlayerData[player].playerid)); } break; case NetworkClient.MessType_ToServer.SaveDBStr: DebugConsole.Log("client requested save string : " + args); dbManip.dataDump(rgd.gameID, rgd.dPlayerData[player].playerid, args); DebugConsole.Log("dumped string info : " + rgd.gameID + ", " + rgd.dPlayerData[player].playerid + ", " + args); break; case NetworkClient.MessType_ToServer.DomainObjectIDsDeleted: // rgd.Mess_DomainObjectIDsDeleted( player, args ); // rgd.SendFinalListAndFirstPairWhenReady( player ); break; case NetworkClient.MessType_ToServer.DomainObjectIDsNOTDeleted: // rgd.Mess_DomainObjectIDsNOTDeleted( player, args ); // rgd.SendFinalListAndFirstPairWhenReady( player ); break; case NetworkClient.MessType_ToServer.DomainObjectNamesAdded: // rgd.Mess_DomainObjectNamesAdded( player, args ); // rgd.SendFinalListAndFirstPairWhenReady( player ); break; case NetworkClient.MessType_ToServer.JustGotToSwanScreen: //has playerScore arg; for when we're not doing add/remove objects screen // rgd.Mess_JustGotToSwanScreen( player, args ); break; case NetworkClient.MessType_ToServer.SelectedRelation: //DebugConsole.Log( "MessageFromClient: MessType.SelectedRelation" ); // rgd.Mess_SelectedRelation( player, args ); break; case NetworkClient.MessType_ToServer.SwanAtEndOfScreen: //has playerScore, millisec args // rgd.Mess_SwanAtEndOfScreen( player, args ); break; case NetworkClient.MessType_ToServer.PlayerHasNoLives: //has playerScore arg // rgd.Mess_PlayerHasNoLives( player, args ); break; case NetworkClient.MessType_ToServer.PlayAgain: //HANDLED IN getRunningGameData //PlayerConnected( player ); // NetworkClient.Instance.SendClientMess( player, NetworkClient.MessType_ToClient.RelationsList, strListRelations ); //select, send the domain // System.Collections.Generic.KeyValuePair<int,System.Text.StringBuilder> ansPair = rgd.SelectDomainid(); // if( ansPair.Equals( default(KeyValuePair<int,System.Text.StringBuilder>) ) ) // { //no more domains to work on! // rgd.EndGame( "No more domains to play!", null ); // }else{ // rgd.ResetVarsForNewGame(); // rgd.domainID = ansPair.Key; // NetworkClient.Instance.SendClientMess( player, NetworkClient.MessType_ToClient.DomainDescription, ansPair.Value.ToString() ); // NetworkClient.Instance.SendClientMess( player, NetworkClient.MessType_ToClient.PlayerPairReady ); // } break; case NetworkClient.MessType_ToServer.DeviceUniqueIdentifier: //may be HANDLED IN getRunningGameData rgd.SetUniqueDeviceID(player, args); //comes from NetworkClient.OnConnectedToServer DebugConsole.Log("Got a UDID of: " + args); break; // UNIQUE AND SPECIFIC TO SNG ONLY // Whenever we want to create a new game, we do this. case NetworkClient.MessType_ToServer.SNGRequestNewGame: int gameId = dbManip.SaveGameBasics(dbManip.getPlayerUDID(rgd.dPlayerData[player].playerid)); rgd.gameID = gameId; dbManip.SaveGameType(rgd.gameID, rgd.gameMode); List <string> objectSet = DBGWAPLoader.GenerateRandomObjectSet(7); List <string> tagSet = DBGWAPLoader.GenerateRandomTagset(); rgd.objectSet = objectSet; rgd.tagSet = tagSet; // Sends the game mode NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.SNGGameMode, rgd.gameMode.ToString()); // Construct the object and tag sets. if (objectSet != null) { NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.SNGObjectSet, DBStringHelper.listToString(objectSet, ',')); } if (tagSet != null) { if (rgd.gameMode == 3) { List <string> modifiedTagSet = new List <string>(tagSet); modifiedTagSet[1] = tagSet[1] + "-compete"; modifiedTagSet.Add(tagSet[1] + "-collab"); NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.SNGTagSet, DBStringHelper.listToString(modifiedTagSet, ',')); } else { NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.SNGTagSet, DBStringHelper.listToString(tagSet, ',')); } } int gamemode = dbManip.LookupPlayerGametype(rgd.dPlayerData[player].playerid); DebugConsole.Log("Game mode found = " + gamemode); if (gamemode == -1) { DebugConsole.Log("Need to add a new player."); dbManip.SavePlayerGameType(rgd.dPlayerData[player].playerid, rgd.gameMode); } break; case NetworkClient.MessType_ToServer.SNGRequestGameMode: if (rgd.gameMode == -1) { int gt = dbManip.LookupPlayerGametype(rgd.dPlayerData[player].playerid); if (gt != -1) { DebugConsole.Log("Found an old player. Their gametype is : " + gt); rgd.gameMode = gt; } else { rgd.gameMode = UnityEngine.Random.Range(1, 4); DebugConsole.Log("Found a new player! Assigning a random gametype of " + rgd.gameMode); dbManip.SavePlayerGameType(rgd.dPlayerData[player].playerid, rgd.gameMode); } } NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.SNGGameMode, rgd.gameMode.ToString()); break; case NetworkClient.MessType_ToServer.SNGRequestTrace: // (OH GOD BAD HARDCODED NUMBERS) string[] times = (dbManip.LookupRandomTraces(1, 1))[0].Split(','); DebugConsole.Log("Got a random timing thing from the database."); // Construct and send the partner trace. List <Triple <double, string, string> > partnerTrace; if (rgd.gameMode == 3) // BOTH MODE { List <string> opponentTrace = new List <string>(); opponentTrace.Add(rgd.tagSet[1]); opponentTrace.Add(rgd.tagSet[0] + "-compete"); opponentTrace.Add(rgd.tagSet[0] + "-collab"); partnerTrace = DBGWAPLoader.ConstructRandomPartnerTrace(times, rgd.objectSet, opponentTrace); } else { partnerTrace = DBGWAPLoader.ConstructRandomPartnerTrace(times, rgd.objectSet, rgd.tagSet); } if (partnerTrace != null) { NetworkClient.Instance.SendClientMess(player, NetworkClient.MessType_ToClient.SNGOpponentTrace, DBStringHelper.traceToString(partnerTrace, ':')); DebugConsole.Log("SENT A TRACE!"); } else { DebugConsole.Log("DIDN'T SEND TRACE!"); } break; case NetworkClient.MessType_ToServer.SNGSaveDBTrace: DebugConsole.Log("Got a trace from a player."); string[] traces = args.Split(','); // This is a really awful hack...someone just kill me please. // Basically, if I want to reuse all of my string helper functions, I have to manhandle some strings. // THIS IS WHY THE CLIENT SHOULDN'T AUTOSPLIT STRINGS WHEN READING FROM THE SERVER DAMMIT. // YOUR LOGIC IS NOT TWO-WAY...RANT...RANT...RAAAAAAAAAAAAAANT. for (int i = 0; i < traces.Length; i++) { traces[i] = traces[i].Replace(':', ','); } if (traces.Length != 3) { DebugConsole.Log("Ill-formed trace info."); break; } dbManip.SaveTraceResults(rgd.gameID, 1, traces[0]); dbManip.SaveTraceResults(rgd.gameID, 2, traces[1]); dbManip.SaveTraceResults(rgd.gameID, 3, traces[2]); string objSetStr = DBStringHelper.listToString(rgd.objectSet, ','); string tagSetStr = DBStringHelper.listToString(rgd.tagSet, ','); // XXX (kasiu): Need to save score properly dbManip.SaveGameGwapData(rgd.gameID, 0, rgd.dPlayerData[player].playerid, objSetStr, tagSetStr); // Increment player score: dbManip.IncrementPlayerGameCount(rgd.dPlayerData[player].playerid); break; case NetworkClient.MessType_ToServer.SNGSavePlayerData: // used for SNG only int gametype = dbManip.LookupPlayerGametype(rgd.dPlayerData[player].playerid); if (gametype != -1) { DebugConsole.Log("Found an old player. Their gametype is : " + gametype); rgd.gameMode = gametype; } else { rgd.gameMode = UnityEngine.Random.Range(1, 4); DebugConsole.Log("Found a new player! Assigning a random gametype of " + rgd.gameMode); dbManip.SavePlayerGameType(rgd.dPlayerData[player].playerid, rgd.gameMode); //dbManip.SavePlayerInformation(dbManip.getPlayerUDID(rgd.dPlayerData[player].playerid), rgd.gameMode); } break; case NetworkClient.MessType_ToServer.SNGSavePlayerLikertData: DebugConsole.Log("Got some survey data! We'll be saving this!"); string[] answers = args.Split(':'); dbManip.SavePlayerLikertScores(rgd.dPlayerData[player].playerid, answers); break; case NetworkClient.MessType_ToServer.SNGSavePlayerScore: dbManip.SaveGameScore(rgd.gameID, System.Convert.ToInt32(args)); break; /* * BULLETHELL CODE */ case NetworkClient.MessType_ToServer.BHSaveParamTrace: DebugConsole.Log("Got a trace from a player."); dbManip.SaveTraceResults(rgd.gameID, 4, args); // trace type 4 = bullethell parameter tuning trace // Increment player score: dbManip.IncrementPlayerBHDesignCount(rgd.dPlayerData[player].playerid); break; default: DebugConsole.Log("MessageFromClient: default"); break; } }
public void MessageFromServer(NetworkClient.MessType_ToClient messType, string[] args) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("Player ").Append(Network.player).Append(" GameStateClient.MessageFromServer: ").Append(messType.ToString()); sb.Append("\n"); if (args != null && args.Length > 0) { sb.Append("args[] contains: ").Append(NetworkClient.RayToString(args, ",")); } Debug.Log(sb.ToString()); DebugConsole.Log(sb.ToString()); /* * NOTE! any message that needs instant reaction should have a "case ..." here; * otherwise, the "case default" is to throw the message in a queue * (the queue we're using is a NetworkMailbox; by default the queue length is unbounded!) */ switch (messType) { case NetworkClient.MessType_ToClient.PlayerPairReady: // currState = StateEnum.PlayerPairReady; // bPlayerPairReady = true; break; case NetworkClient.MessType_ToClient.RelationsList: // relationList = new string[ args.Length / 2 ]; // mRelationToID = new SortedList<string, int>(); // for( int i=0; i<args.Length; i = i + 2 ) // { // mRelationToID.Add( args[i+1], Convert.ToInt32( args[i] ) ); // relationList[ i / 2 ] = args[i+1]; // } break; case NetworkClient.MessType_ToClient.DomainDescription: //toSend.Append( domainid ).Append( "|||" ).Append( domainName ).Append( "|||" ).Append( domainDescr ); // if( args != null && args.Length > 0 ) // { // string[] splitRay = Regex.Split( args[0], Regex.Escape("|||") ); // if( splitRay != null && splitRay.Length >= 2 && splitRay[ 1 ].Length > 0 ) // { // domainid = Convert.ToInt32( splitRay[0] ); // domainName = splitRay[1]; // domainDescr = splitRay[2]; // }else{ // Debug.LogError( "Did not get domain description information!" ); // NetworkClient.Instance.SendServerMess( NetworkClient.MessType_ToServer.PlayerHasNoLives ); // DoGameOver( "Did not get domain description information!" ); // } // // } // else{ // Debug.LogWarning( "GameStateClient.MessageFromServer: DomainDescription came back without ANY args." ); // NetworkClient.Instance.SendServerMess( NetworkClient.MessType_ToServer.PlayerHasNoLives ); // DoGameOver( "Did not get domain description information!" ); // } break; case NetworkClient.MessType_ToClient.DomainObjectsInitial: // currState = StateEnum.PlayerPairReady; // domainObjects = new string[ args.Length / 2 ];//domainObjects = (string[])args.Clone(); // mStoryObjectToID = new SortedList<string, int>(); // for( int i=0; i<args.Length; i = i + 2 ) // { // mStoryObjectToID.Add( args[i+1], Convert.ToInt32( args[i] ) ); // mObjectidToObjectname.Add( args[i], args[i+1] ); // domainObjects[ i / 2 ] = args[i+1]; // } // Debug.Log( "GameStateClient.DomainObjectsInitial: " + NetworkClient.RayToString( args, "," ) ); break; case NetworkClient.MessType_ToClient.DomainObjectsFinal: // if( args != null && args.Length > 0 ) // { // Debug.Log( "GameStateClient.DomainObjectsFinal" ); // string[] tmpAgreed = args[0].Split( new char[] {'|'} ); // if( tmpAgreed != null && tmpAgreed.Length > 0 && tmpAgreed[ 0 ].Length > 0 ) // { // strAgreedObjDeleted = new string[ tmpAgreed.Length ]; // for( int i=0; i<tmpAgreed.Length; i++ )//&& tmpAgreed[i] != null && tmpAgreed[i].Length > 0 // { // if( mObjectidToObjectname.ContainsKey( tmpAgreed[ i ] ) ) // strAgreedObjDeleted[ i ] = this.mObjectidToObjectname[ tmpAgreed[ i ] ]; // else // Debug.LogWarning( "Didn't find deleted object id: " + tmpAgreed[i] ); // } // }else // strAgreedObjDeleted = new string[ 0 ]; // // // Debug.Log( "Got agreed deletes: " ); // Debug.Log( NetworkClient.RayToString( strAgreedObjDeleted, "," ) ); // // tmpAgreed = args[1].Split( new char[] {'|'} ); // if( tmpAgreed != null && tmpAgreed.Length > 0 && tmpAgreed[ 0 ].Length > 0 ) // { // strAgreedObjNOTDeleted = new string[ tmpAgreed.Length ]; // for( int i=0; i<tmpAgreed.Length; i++ ) // { // if( mObjectidToObjectname.ContainsKey( tmpAgreed[ i ] ) ) // strAgreedObjNOTDeleted[ i ] = this.mObjectidToObjectname[ tmpAgreed[ i ] ]; // else // Debug.LogWarning( "Didn't find NON deleted object id: " + tmpAgreed[i] ); // } // }else // strAgreedObjNOTDeleted = new string[ 0 ]; // // Debug.Log( "Got agreed NON deletes: " ); // Debug.Log( NetworkClient.RayToString( strAgreedObjNOTDeleted, "," ) ); // // strAgreedObjAdded = args[2].Split( new char[] {'|'} ); // if( strAgreedObjAdded != null && strAgreedObjAdded.Length > 0 && strAgreedObjAdded[ 0 ].Length > 0 ) // {} // else // strAgreedObjAdded = new string[ 0 ]; // // Debug.Log( "Got agreed adds: " ); // Debug.Log( NetworkClient.RayToString( strAgreedObjAdded, "," ) ); // } // else{ // Debug.LogWarning( "GameStateClient.MessageFromServer: Domain objects final came back without ANY args." ); // } break; case NetworkClient.MessType_ToClient.NewDomainObjPair: // if( args != null && args.Length >=5 ) // { // pairDomainObj = new Pair<string, string>( args[2], args[4] ); // idPairDomainObj = Convert.ToInt32( args[0] ); // currState = StateEnum.ChoosingLocalRelation; // }else{ // Debug.LogWarning( "GameStateClient.MessageFromServer: NewDomainObjPair came back without ANY args." ); // } break; case NetworkClient.MessType_ToClient.FirstToSelectRelation: // wasFirstToSelectRelation = true; break; case NetworkClient.MessType_ToClient.SwanReadyToMove: // millisecChoosingRelation = (System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond); //record start time of selection // currState = StateEnum.SwanReadyToMove; break; case NetworkClient.MessType_ToClient.RelationMatchResult: // if( args != null && args.Length > 0 ) // { // didLastRelationPairAgree = args[ 0 ].Equals( "agree", StringComparison.OrdinalIgnoreCase ); // Debug.Log( "args[ 0 ].Equals( \"agree\", StringComparison.OrdinalIgnoreCase ) == " + didLastRelationPairAgree ); // } // else // Debug.LogWarning( "GameStateClient.MessageFromServer: RelationMatchResult came back without ANY args." ); break; case NetworkClient.MessType_ToClient.GameOver: // if( args != null && args.Length > 0 && args[0] != null) // { // DoGameOver( args[0] ); // } // else // { // Debug.LogWarning( "GameStateClient.MessageFromServer: Got a game over message without any argument XD" ); // DoGameOver( "" ); // } break; case NetworkClient.MessType_ToClient.YouWin: // bAmWinner = true; break; // BEGIN SNG-specific messages case NetworkClient.MessType_ToClient.SNGOpponentTrace: // Generate the trace here. DebugConsole.Log("Got a trace from the server"); if (args != null && args.Length > 0 && args[0] != null) { // DO THINGS DebugConsole.Log(args[0] + " is args[0]"); DBGameStateManager dsm = GameState.Singleton.GetComponent <DBGameStateManager>(); List <Triple <double, string, string> > partnerTrace = DBStringHelper.stringToTrace(args, ':'); dsm.SetPartnerTrace(partnerTrace); } break; case NetworkClient.MessType_ToClient.SNGGameMode: DebugConsole.Log("Got the game mode from the server!"); if (args != null && args.Length > 0 && args[0] != null) { DebugConsole.Log(args[0] + " is args[0]"); DBGameStateManager dsm = GameState.Singleton.GetComponent <DBGameStateManager>(); if (dsm != null) { dsm.SetMode(Convert.ToInt32(args[0])); } TutorialGWAPManager tgm = GameState.Singleton.GetComponent <TutorialGWAPManager>(); if (tgm != null) { tgm.SetMode(Convert.ToInt32(args[0])); } } break; case NetworkClient.MessType_ToClient.SNGObjectSet: DebugConsole.Log("Got the objects from the server!"); if (args != null && args.Length > 0 && args[0] != null) { DebugConsole.Log(args[0] + " is args[0] and length = " + args.Length); DBGameStateManager dsm = GameState.Singleton.GetComponent <DBGameStateManager>(); List <string> objectSet = new List <string>(args); dsm.SetObjectList(objectSet); } break; case NetworkClient.MessType_ToClient.SNGTagSet: DebugConsole.Log("Got the tags from the server!"); if (args != null && args.Length > 0 && args[0] != null) { DebugConsole.Log(args[0] + " is args[0] and length = " + args.Length); DBGameStateManager dsm = GameState.Singleton.GetComponent <DBGameStateManager>(); List <string> tagSet = new List <string>(args); dsm.SetTagList(tagSet); } break; // END SNG-specific messages default: DebugConsole.Log("MessageFromServer: default (adding to mailbox)"); mailbox.AddMessage(messType, args); break; } }