public void UserApp_RollConversation_Test() { TestAppWorker testAppWorker = new TestAppWorker(); testAppWorker.StartTest(); var fakeGameManager = new UdpCommunicator() { MinPort = 10000, MaxPort = 10999, Timeout = 1000, EnvelopeHandler = ProcessEnvelope1 }; fakeGameManager.Start(); IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Loopback, fakeGameManager.Port); Message msg1 = new RollMessage(1, 6); Envelope env = new Envelope(msg1, targetEndPoint); Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); testAppWorker.commFacility.Process(env); Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId)); Thread.Sleep(100); Assert.AreNotSame(msg1, _lastIncomingEnvelope1); Assert.IsNotNull(_lastIncomingEnvelope1); Assert.IsNotNull(_lastIncomingEnvelope1.message); Assert.AreEqual(msg1.msgId, _lastIncomingEnvelope1.message.msgId); Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId); }
public void Init() { // 声明消息发送循环方法(有消息就发) var action = new Action(() => { while (_isSending) { RollMessage[] msgs; lock (_sync_sendWhispers) { msgs = new RollMessage[_sendWhispers.Count]; _sendWhispers.CopyTo(msgs, 0); _sendWhispers.Clear(); } foreach (var msg in msgs) { // todo: 改为异步调用?? ContactCenterProxy.Whisper(msg.ID, new byte[][] { BitConverter.GetBytes((int)msg.RollAction), msg.Data }); } msgs = null; Thread.Sleep(1); } }); // 开始执行 action.BeginInvoke(null, null); }
/// <summary> /// 获取当前已接收的消息数组,并清空接收容器 /// </summary> public RollMessage[] GetMessages() { RollMessage[] msgs; lock (_sync_receivedWhispers) { msgs = new RollMessage[_receivedWhispers.Count]; _receivedWhispers.CopyTo(msgs, 0); _receivedWhispers.Clear(); } return(msgs); }
private void RollButton_Click(object sender, RoutedEventArgs e) { VantageType vantageType = GetVantage(); var diceTray = new DiceTray(diceSelector.Text, diceCount.Text, modifier.Text, vantageType); var rollMessage = new RollMessage(diceTray); string message = $"You Rolled {rollMessage.RollMade}\n" + $"Your rolls were {rollMessage.Rolls}\n" + $"Result = {rollMessage.Result}"; MessageBox.Show(this, message, rollMessage.CritMessage); }
public void TestRollMessage() { RollMessage origMessage = new RollMessage(1, 2); byte[] bytes = origMessage.Encode(); RollMessage decodedMessage = RollMessage.Decode(bytes); Assert.AreEqual(origMessage.MessageType, 5, "Incorrect MessageType"); Assert.AreEqual(origMessage.MessageType, decodedMessage.MessageType, "MessageType did not match"); Assert.AreEqual(origMessage.GameId, decodedMessage.GameId, "GameId did not match"); Assert.AreEqual(origMessage.Roll, decodedMessage.Roll, "Roll did not match"); }
/// <summary> /// 根据当前游戏的进展,玩家状态来判断当前消息是否“合法” /// </summary> public bool CheckReceiveMessageIsValid(RollMessage m) { // 服务刚开始运行 if (_state.当前阶段 == 0) { return(false); } // todo return(true); }
private void ReceiveRollMessage(RollMessage pMessage) { if (pMessage.Action == "add") { if (fRollers == null) { fRollers = new ObservableCollection <Roller>(); } App.Current.Dispatcher.Invoke((Action) delegate { AddRoller(pMessage.Roller); }); RaisePropertyChanged("Rollers"); } if (pMessage.Action == "clear") { App.Current.Dispatcher.Invoke((Action) delegate { ClearRollers(); }); RaisePropertyChanged("Rollers"); } if (pMessage.Action == "refresh") { if (fRollers != null) { App.Current.Dispatcher.Invoke((Action) delegate { RefreshRollers(); }); RaisePropertyChanged("Rollers"); } } }
/// <summary> /// 过滤并接收消息,存入消息接收队列 /// </summary> public void ReceiveWhisper(int id, byte[][] data) { // 将收到的数据转为 RollMessage 对象实例 var msg = new RollMessage { ID = id, RollAction = (RollActions)BitConverter.ToInt32(data[0], 0), Data = data.Length > 1 ? data[1] : null }; // 如果消息“合法” if (CheckReceiveMessageIsValid(msg)) { // 将 RollMessage 对象实例 插入到接收队列中 lock (_sync_receivedWhispers) { _receivedWhispers.Enqueue(msg); } } else { // todo: 攻击判定 } }
/// <summary> /// 获取当前已接收的消息数组,并清空接收容器 /// </summary> public RollMessage[] GetMessages() { RollMessage[] msgs; lock (_sync_receivedWhispers) { msgs = new RollMessage[_receivedWhispers.Count]; _receivedWhispers.CopyTo(msgs, 0); _receivedWhispers.Clear(); } return msgs; }
/// <summary> /// 根据当前游戏的进展,玩家状态来判断当前消息是否“合法” /// </summary> public bool CheckReceiveMessageIsValid(RollMessage m) { // 服务刚开始运行 if (_state.当前阶段 == 0) return false; // todo return true; }