public void addLine(ChatLine chatLine, CPacheStream cPacheStream) { if (cpacheStreams.ContainsKey(cPacheStream.StreamId)) { cpacheStreams.Remove(cPacheStream.StreamId); cpacheStreams.Add(chatLine.userName, cPacheStream); if (players.Count == 0) { players.Add(chatLine.userName, new Player() { myTurn = true, Score = 0, Username = chatLine.userName }); } else { players.Add(chatLine.userName, new Player() { myTurn = false, Score = 0, Username = chatLine.userName }); } } lock (locker) { //add it to the legder chatContent.Add(chatLine); foreach (KeyValuePair <String, CPacheStream> client in cpacheStreams) { var ret = JsonConvert.SerializeObject(chatLine); try { client.Value.Broadcast(ret); } catch (Exception) { disposedRunningList.Add(client.Value); } } } }
protected override void MessageReceived(string Message) { ChatLine chat = JsonConvert.DeserializeObject <ChatLine>(Message); chat.dateTime = System.DateTime.Now; if (chat.content.Length <= 7) { ChatRoom.addLine(chat, myStream); return; } if (chat.content.Substring(0, 7) == "SideCar") { var ret = ChatRoom.handleClientAppMessage(chat); //if(ret != "") //{ // myStream.Broadcast(ret); //} } else { ChatRoom.addLine(chat, myStream); return; } }
public string handleClientAppMessage(ChatLine chat) { switch (chat.content.Substring(7, 3)) { case "STG": GameUpdate gameUpdate = new GameUpdate() { players = new List <Player>() }; foreach (KeyValuePair <String, Player> keyValuePair in players) { gameUpdate.players.Add(keyValuePair.Value); } gameUpdate.BlackCard = getBlackCard(); var retval = Newtonsoft.Json.JsonConvert.SerializeObject(gameUpdate); foreach (KeyValuePair <String, CPacheStream> streams in cpacheStreams) { streams.Value.Broadcast("SideCarSTG" + retval); // becuase we are starting the game everyone gets their white cards for (int q = 0; q < 6; q++) { Card card1 = getWhiteCard(); Card bcard = getBlackCard(); var retString = Newtonsoft.Json.JsonConvert.SerializeObject(card1); streams.Value.Broadcast("SideCarHWC" + retString); } } return("SideCarSTG" + retval); //whitecarddelivery case "WCD": //todo add card to correct round. var card = new Card() { content = chat.content.Substring(10, chat.content.Length - 10) }; currentWhiteCards.Add(chat.userName, card); var ret = Newtonsoft.Json.JsonConvert.SerializeObject(getWhiteCard()); foreach (KeyValuePair <String, CPacheStream> stream in cpacheStreams) { if (chat.userName == stream.Key) { stream.Value.Broadcast("SideCarHWC" + ret); } } if (players.Count - 1 == currentWhiteCards.Count) { foreach (KeyValuePair <String, CPacheStream> stream in cpacheStreams) { foreach (var pWhiteCard in currentWhiteCards) { stream.Value.Broadcast("SideCarPWC" + Newtonsoft.Json.JsonConvert.SerializeObject(pWhiteCard.Value)); } } } return(""); //whitecardselectforwinner case "WCS": var winner = ""; var cardString = chat.content.Substring(10, chat.content.Length - 10); Card winnerCard = new Card(); foreach (KeyValuePair <String, Card> whitecard in currentWhiteCards) { if (whitecard.Value.content == cardString) { winner = whitecard.Key; winnerCard = whitecard.Value; } } players[winner].Score = players[winner].Score + 1; var winnerCardJson = Newtonsoft.Json.JsonConvert.SerializeObject(winnerCard); //TODO update the clients of the winning Card. foreach (KeyValuePair <String, CPacheStream> c in cpacheStreams) { c.Value.Broadcast("SideCarWWC" + winnerCardJson); } System.Threading.Thread.Sleep(3000); GameUpdate gameUpdate2 = new GameUpdate() { players = new List <Player>() }; var i = 1; var lastTurn = 0; var nextTurn = 0; foreach (KeyValuePair <String, Player> keyValuePair in players) { if (keyValuePair.Value.myTurn) { lastTurn = i; } else { i = i + 1; } } if (lastTurn == players.Count) { nextTurn = 1; } else { nextTurn = lastTurn + 1; } i = 1; foreach (KeyValuePair <String, Player> keyValuePair in players) { if (nextTurn == i) { keyValuePair.Value.myTurn = true; i = i + 1; } else { keyValuePair.Value.myTurn = false; i = i + 1; } gameUpdate2.players.Add(keyValuePair.Value); } gameUpdate2.BlackCard = getBlackCard(); var JsonValue = Newtonsoft.Json.JsonConvert.SerializeObject(gameUpdate2); currentWhiteCards.Clear(); foreach (KeyValuePair <String, CPacheStream> c in cpacheStreams) { c.Value.Broadcast("SideCarSTG" + JsonValue); } return(""); //getNewCard TODO case "GNC": return(""); default: return(""); } }