/// <summary> /// 单击用户列事件,主要内容是路径的绘制 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UsersList_SelectedIndexChanged(object sender, EventArgs e) { // 清空画板 MapPicture.Refresh(); g = MapPicture.CreateGraphics(); // 创建画笔,设置箭头 Pen pen_Blue = new Pen(Color.Blue, 4); Pen pen_Red = new Pen(Color.Red, 4); Pen pen_Green = new Pen(Color.Lime, 4); Pen pen_Orange = new Pen(Color.Orange, 6); pen_Blue.EndCap = LineCap.ArrowAnchor; pen_Red.EndCap = LineCap.ArrowAnchor; pen_Green.EndCap = LineCap.ArrowAnchor; // 当刚刚删除用户时,应当加入该判断以防出错 if (UsersList.SelectedIndex == -1) { return; } _selectedIndex = UsersList.SelectedIndex; User selectedUser = GlobleVariable.users[_selectedIndex]; for (int i = 0; selectedUser.RecommandPath[i + 1] != null; i++) { if (selectedUser.IsArrive) { g.DrawLine(pen_Green, selectedUser.RecommandPath[i].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i].mapPos_y - MapPicture.Location.Y, selectedUser.RecommandPath[i + 1].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i + 1].mapPos_y - MapPicture.Location.Y); } else { g.DrawLine(pen_Blue, selectedUser.RecommandPath[i].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i].mapPos_y - MapPicture.Location.Y, selectedUser.RecommandPath[i + 1].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i + 1].mapPos_y - MapPicture.Location.Y); } } if (selectedUser.State == 1) { string[] strArr = selectedUser.Location.Split(); int index_1 = Array.IndexOf(GlobleVariable.cityMapping, strArr[0]); int index_2 = Array.IndexOf(GlobleVariable.cityMapping, strArr[2]); g.DrawLine(pen_Red, GlobleVariable.cityPos[index_1, 0] - MapPicture.Location.X, GlobleVariable.cityPos[index_1, 1] - MapPicture.Location.Y, GlobleVariable.cityPos[index_2, 0] - MapPicture.Location.X, GlobleVariable.cityPos[index_2, 1] - MapPicture.Location.Y); } else if (selectedUser.IsArrive == false) { int index_tmp = Array.IndexOf(GlobleVariable.cityMapping, selectedUser.Location); g.DrawRectangle(pen_Orange, GlobleVariable.cityPos[index_tmp, 0] - MapPicture.Location.X, GlobleVariable.cityPos[index_tmp, 1] - MapPicture.Location.Y, 6, 6); } }
/// <summary> /// 每10秒一个周期,更新主视窗 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateMainForm_Tick_1(object sender, EventArgs e) { GlobleVariable.dt = GlobleVariable.dt.AddHours(1); CurTime.Text = GlobleVariable.dt.ToString("yyyy-MM-dd HH") + "时"; int index = 0; UsersList.Items.Clear(); while (GlobleVariable.users.Count > index) { UsersList.Items.Add("用户" + index.ToString()); index++; } using (FileStream logWriter = new FileStream(GlobleVariable.logPath, FileMode.OpenOrCreate, FileAccess.Write)) { logWriter.Seek(0, SeekOrigin.End); string content = "\n\n\n\n进入程序内时间:" + GlobleVariable.dt.ToString() + "\n\n"; byte[] buffer = Encoding.Default.GetBytes(content); logWriter.Write(buffer, 0, buffer.Length); } // 更新每一个用户的状态 for (int i = 0; i < GlobleVariable.users.Count; i++) { int timeElapse = (int)GlobleVariable.dt.Subtract(GlobleVariable.users[i].OriginTime).TotalHours; int timeTotal = 0; int vehicle; if (timeElapse >= PathFinding.CountPathTime(GlobleVariable.users[i].RecommandPath)) { GlobleVariable.users[i].IsArrive = true; GlobleVariable.users[i].State = 0; GlobleVariable.users[i].Location = GlobleVariable.cityMapping[GlobleVariable.users[i].Dest] + "(已到达)"; } #region 更新用户位置数据 if (!GlobleVariable.users[i].IsArrive) { for (int j = 0; GlobleVariable.users[i].RecommandPath[j + 1] != null && j < GlobleVariable.users[i].RecommandPath.Length - 1; j++) { int nextCityTime = GlobleVariable.users[i].RecommandPath[j].TimeInfo[GlobleVariable.users[i].RecommandPath[j + 1].CityCode]; timeTotal += nextCityTime; vehicle = GlobleVariable.users[i].RecommandPath[j].VehicleInfo[GlobleVariable.users[i].RecommandPath[j + 1].CityCode]; if (timeTotal > timeElapse) { int stayTime; switch (vehicle) { case 1: stayTime = GlobleVariable.users[i].RecommandPath[j].MinPlaneTime; break; case 2: stayTime = GlobleVariable.users[i].RecommandPath[j].MinTrainTime; break; case 3: stayTime = GlobleVariable.users[i].RecommandPath[j].MinCarTime; break; default: throw new NotImplementedException("路径时间处理失败"); } if (timeTotal - nextCityTime + stayTime < timeElapse) { GlobleVariable.users[i].State = 1; GlobleVariable.users[i].Location = GlobleVariable.users[i].RecommandPath[j].Name + " 到 " + GlobleVariable.users[i].RecommandPath[j + 1].Name; GlobleVariable.users[i].NextCityReachTime = GlobleVariable.users[i].CityReachTime[j + 1]; GlobleVariable.users[i].Vehicle = vehicle; } else { GlobleVariable.users[i].State = 0; GlobleVariable.users[i].Location = GlobleVariable.users[i].RecommandPath[j].Name; } break; } } // 每个单位时间的用户状态改变写入日志 using (FileStream logWriter = new FileStream(GlobleVariable.logPath, FileMode.OpenOrCreate, FileAccess.Write)) { logWriter.Seek(0, SeekOrigin.End); string content = DateTime.Now.ToString() + "(程序内时间:" + GlobleVariable.dt.ToString() + "): "; content += "用户" + i.ToString(); if (GlobleVariable.users[i].State == 0) { content += "正逗留在" + GlobleVariable.users[i].Location + "。"; } else { content += "正在从" + GlobleVariable.users[i].Location + ",使用交通工具为" + GlobleVariable.vehicleMapping[GlobleVariable.users[i].Vehicle] + "。"; } content += "已经旅行了" + (int)(GlobleVariable.dt - GlobleVariable.users[i].OriginTime).TotalHours + "小时(" + GlobleVariable.users[i].OriginTime.ToShortDateString() + " " + GlobleVariable.users[i].OriginTime.Hour.ToString() + "时 出发)。 \n\n"; byte[] buffer = Encoding.Default.GetBytes(content); logWriter.Write(buffer, 0, buffer.Length); } } else { using (FileStream logWriter = new FileStream(GlobleVariable.logPath, FileMode.OpenOrCreate, FileAccess.Write)) { logWriter.Seek(0, SeekOrigin.End); string content = DateTime.Now.ToString() + "(程序内时间:" + GlobleVariable.dt.ToString() + "): "; content += "用户" + i.ToString() + "已到达目的地\n\n"; byte[] buffer = Encoding.Default.GetBytes(content); logWriter.Write(buffer, 0, buffer.Length); } } #endregion } // 保持选中路径的绘图更新 if (UsersList.SelectedIndex != -1 || _selectedIndex != -1) { // 清空画板 MapPicture.Refresh(); g = MapPicture.CreateGraphics(); // 创建画笔,设置箭头 Pen pen_Blue = new Pen(Color.Blue, 4); Pen pen_Red = new Pen(Color.Red, 4); Pen pen_Green = new Pen(Color.Lime, 4); Pen pen_Black = new Pen(Color.Orange, 6); pen_Blue.EndCap = LineCap.ArrowAnchor; pen_Red.EndCap = LineCap.ArrowAnchor; pen_Green.EndCap = LineCap.ArrowAnchor; User selectedUser = GlobleVariable.users[_selectedIndex]; for (int i = 0; selectedUser.RecommandPath[i + 1] != null; i++) { if (selectedUser.IsArrive) { g.DrawLine(pen_Green, selectedUser.RecommandPath[i].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i].mapPos_y - MapPicture.Location.Y, selectedUser.RecommandPath[i + 1].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i + 1].mapPos_y - MapPicture.Location.Y); } else { g.DrawLine(pen_Blue, selectedUser.RecommandPath[i].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i].mapPos_y - MapPicture.Location.Y, selectedUser.RecommandPath[i + 1].mapPos_x - MapPicture.Location.X, selectedUser.RecommandPath[i + 1].mapPos_y - MapPicture.Location.Y); } } if (selectedUser.State == 1) { string[] strArr = selectedUser.Location.Split(); int index_1 = Array.IndexOf(GlobleVariable.cityMapping, strArr[0]); int index_2 = Array.IndexOf(GlobleVariable.cityMapping, strArr[2]); g.DrawLine(pen_Red, GlobleVariable.cityPos[index_1, 0] - MapPicture.Location.X, GlobleVariable.cityPos[index_1, 1] - MapPicture.Location.Y, GlobleVariable.cityPos[index_2, 0] - MapPicture.Location.X, GlobleVariable.cityPos[index_2, 1] - MapPicture.Location.Y); } else if (selectedUser.IsArrive == false) { int index_tmp = Array.IndexOf(GlobleVariable.cityMapping, selectedUser.Location); g.DrawRectangle(pen_Black, GlobleVariable.cityPos[index_tmp, 0] - MapPicture.Location.X, GlobleVariable.cityPos[index_tmp, 1] - MapPicture.Location.Y, 6, 6); } } }