/// <summary> /// Send data to client. /// </summary> /// <param name="agent"></param> /// <param name="request"></param> public void Send(Agent agent, Request request) { try { string message; if (request == Common.Data.Request.DAILY_INITIALIZE || request == Common.Data.Request.INITIALIZE) { lastTalkIdxMap.Clear(); lastWhisperIdxMap.Clear(); Packet packet = new Packet(request, gameData.GetGameInfoToSend(agent), gameSetting); message = DataConverter.GetInstance().Convert(packet); } else if (request == Common.Data.Request.NAME || request == Common.Data.Request.ROLE) { Packet packet = new Packet(request); message = DataConverter.GetInstance().Convert(packet); } else if (request != Common.Data.Request.FINISH) { List<TalkToSend> talkList = gameData.GetGameInfoToSend(agent).TalkList; List<TalkToSend> whisperList = gameData.GetGameInfoToSend(agent).WhisperList; talkList = Minimize(agent, talkList, lastTalkIdxMap); whisperList = Minimize(agent, whisperList, lastWhisperIdxMap); Packet packet = new Packet(request, talkList, whisperList); message = DataConverter.GetInstance().Convert(packet); } else { Packet packet = new Packet(request, gameData.GetFinalGameInfoToSend(agent)); message = DataConverter.GetInstance().Convert(packet); } serverLogger.Info("=>" + agent + ":" + message); TcpClient client = agentConnectionMap[agent]; StreamWriter sw = new StreamWriter(client.GetStream()); sw.WriteLine(message); sw.Flush(); } catch (IOException) { // serverLogger.Fatal(e.Message); throw new LostClientException(); } }
public object Recieve(Packet packet) { GameInfo gameInfo = lastGameInfo; GameSetting gameSetting = packet.GameSetting; if (packet.GameInfo != null) { gameInfo = packet.GameInfo.ToGameInfo(); lastGameInfo = gameInfo; } if (packet.TalkHistory != null) { Talk lastTalk = null; if (gameInfo.TalkList != null && gameInfo.TalkList.Count != 0) { lastTalk = gameInfo.TalkList[gameInfo.TalkList.Count - 1]; } foreach (var talk in packet.TalkHistory) { if (IsAfter(talk, lastTalk)) { gameInfo.TalkList.Add(talk.ToTalk()); } } } if (packet.WhisperHistory != null) { Talk lastWhisper = null; if (gameInfo.WhisperList != null && gameInfo.WhisperList.Count != 0) { lastWhisper = gameInfo.WhisperList[gameInfo.WhisperList.Count - 1]; } foreach (var whisper in packet.WhisperHistory) { if (IsAfter(whisper, lastWhisper)) { gameInfo.WhisperList.Add(whisper.ToTalk()); } } } object returnObject = null; switch (packet.Request) { case Request.INITIALIZE: Running = true; player.Initialize(gameInfo, gameSetting); break; case Request.DAILY_INITIALIZE: player.Update(gameInfo); player.DayStart(); break; case Request.DAILY_FINISH: player.Update(gameInfo); break; case Request.NAME: if (PlayerName == null) { returnObject = player.Name; if (returnObject == null) { returnObject = player.GetType().Name; } } else { returnObject = PlayerName; } break; case Request.ROLE: if (RequestRole != null) { returnObject = RequestRole.ToString(); } else { returnObject = "none"; } break; case Request.ATTACK: player.Update(gameInfo); returnObject = player.Attack(); break; case Request.TALK: player.Update(gameInfo); returnObject = player.Talk(); if (returnObject == null) { returnObject = Talk.SKIP; } break; case Request.WHISPER: player.Update(gameInfo); returnObject = player.Whisper(); if (returnObject == null) { returnObject = Talk.SKIP; } break; case Request.DIVINE: player.Update(gameInfo); returnObject = player.Divine(); break; case Request.GUARD: player.Update(gameInfo); returnObject = player.Guard(); break; case Request.VOTE: player.Update(gameInfo); returnObject = player.Vote(); break; case Request.FINISH: player.Update(gameInfo); Finish(); break; default: break; } return returnObject; }