using Common.system; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows; using System.Windows.Input; using System.Windows.Threading; using XHZB.Desktop.Utils; using XHZB.Desktop.WebSocket; using XHZB.Model; namespace XHZB.Desktop { /// /// 点名抢答 /// public partial class RollCallWindow : Window, SocketCallback { #region 值初始化 /// /// 服务返回 0成功 /// private readonly int serverReturnCode = 0; internal RollCallPageData pageData = new RollCallPageData(); private readonly RegisterController registerController = new RegisterController(); internal string backgroundOpacity = "0.5"; private readonly Random rd = new Random(); private readonly int[] iArrty = new int[3] { 201314, 201314, 201314 }; private static string userids = string.Empty; private static string useridss = string.Empty; private static string useridsss = string.Empty; private static int ranNum = 201314; private readonly int[] iArray = new int[1]; private static int returnNum = 0; //private bool isSjdm = false; internal double pwidth = SystemParameters.PrimaryScreenWidth; internal double pHeight = SystemParameters.PrimaryScreenHeight; private RollCallPerfectionWindow rollCallPerfectionWindow; /// /// 随机点名 /// private bool isRollCall = false; /// /// 抢答 /// private bool isRushToAnswer = false; public string BackgroundOpacity { get => backgroundOpacity; set => backgroundOpacity = value; } /// /// 随机几率 1高 2中 3低 /// string flag = "1"; int rNum = 999; #endregion 值初始化 /// /// 课堂点名 /// public RollCallWindow() { InitializeComponent(); WindowStartupLocation = WindowStartupLocation.CenterScreen; if (!File.Exists(APP.dataPath + "randomProbability.txt")) //若为空 { flag = "1"; } else { flag = System.IO.File.ReadAllText(APP.dataPath + "randomProbability.txt", Encoding.Default); } Initialize(); WSocketClient.getInstance().addWin(this); } /// /// 初始化 /// public void Initialize() { userids = string.Empty; useridss = string.Empty; useridsss = string.Empty; returnNum = 0; rNum = 999; APP.RollCallList = new System.Collections.Generic.List(); imgRandomRollCall.Visibility = Visibility.Visible; imgRandomRollCallTwo.Visibility = Visibility.Collapsed; imgAnswer.Visibility = Visibility.Visible; imgAnswerTwo.Visibility = Visibility.Collapsed; stpAnswer.Visibility = Visibility.Collapsed; //ToolbarWindow.IsNotOperation = true; IsRandomRollCallButton = false; //初始化配置文件 //ZJConfigUtil.init(); initPageData(); //ZSocketServer.getInstance().addWin(this); } public void userListChange() { Console.WriteLine("本节考勤-渲染用户列表"); Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { receiveWsMsg(null); })); } public void receiveWsMsg(SocketModel msg) { if (!isRollCall && !isRushToAnswer) { initPageData(); } if (msg != null && msg.c == 2021) { Console.WriteLine(msg); #region 去重 for (int i = 0; i < pageData.userList.Count; i++) { if (msg.b.stid.ToString().Equals(pageData.userList[i].ID)) { return; } } #endregion //Askstudent askstudent = new Askstudent //{ // roomid = ZCommonData.roomid, // studentid = msg.body.userid //}; //askstudent.asktid = ZCommonData.askid; //askstudent.schoolid = ZCommonData.loginUser.schoolid; //askstudent.createtime = DataProvider.TimestampTotalSeconds(); //askstudent.studentselected = 0; //askstudent.radiocorrect = 0; //askstudent.classid = msg.body.classid; //ZSqliteManger.insertClassData(new ClassData() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // api = "askstudent", // json = ZJsonHelper.ToJson(askstudent), // askid = ZCommonData.askid //}); System.Collections.Generic.List callList = APP.RollCallList; bool isUserid = true; //if (TeachingDataStatic.RollCallStatisticsList.Count > 0)//统计抢答 //{ // for (int i = 0; i < TeachingDataStatic.RollCallStatisticsList.Count; i++) // { // if (TeachingDataStatic.RollCallStatisticsList[i].userid == msg.body.userid) // { // TeachingDataStatic.RollCallStatisticsList[i].replies += 1; // isUserid = false; // break; // } // } //} if (isUserid) {//统计 //Shared.TeachingData.rollCall = new RollCall //{ // usertype = msg.usertype, // userid = msg.body.userid, // username = msg.body.username, // userpic = msg.body.userpic, // replies = 1 //}; //TeachingDataStatic.RollCallStatisticsList.Add(Shared.TeachingData.rollCall); //Shared.TeachingData.rollCall = new RollCall(); } Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { //try //{ // if (pageData.userList.Count > 0) // { // if (pageData.userList[pageData.userList.Count - 1].askid != msg.body.asktid) // { // pageData.userList.Clear(); // } // } //} //catch (Exception ex) //{ // LogHelper.WriteErrLog("【抢答】(receiveWsMsg)socket移除askid不一致用户," + ex.Message, ex); //} //renderView(); returnNum++; if (returnNum <= 18) { string medal = "../Images/RollCall/02log-1.png"; string attendance = "../Images/RollCall/attendance_3.png"; if (returnNum == 1) { medal = "../Images/RollCall/rollCall_7.png"; attendance = "../Images/RollCall/attendance_33.png"; } else if (returnNum == 2) { medal = "../Images/RollCall/rollCall_5.png"; attendance = "../Images/RollCall/attendance_33.png"; } else if (returnNum == 3) { medal = "../Images/RollCall/rollCall_6.png"; attendance = "../Images/RollCall/attendance_33.png"; } string userpic = msg.b.stpic; if (ZCache.headDic.ContainsKey(userpic.Replace("/", ""))) { userpic = ZCache.headDic[userpic.Replace("/", "")]; } else { userpic = "../Images/RollCall/attendance_1.png"; } pageData.userList.Add(new RollCallPageData() { Name = msg.b.stname, Attendance_33 = "Collapsed", Attendance_3 = "Visible", Pic = userpic, ID = msg.b.stid.ToString(), Attendance = attendance, Medal = medal, }); stpAnswer.Visibility = Visibility.Visible; } txbNumberOfReplies.Text = returnNum.ToString(); })); } } System.Collections.Generic.List raduser = new System.Collections.Generic.List(); System.Collections.Generic.List userListNew = new System.Collections.Generic.List(); private void initPageData() { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { if (pageData.userList.Count > 0) { pageData.userList.Clear(); } List userList = APP.OnlineUserList; userListNew = new List(); string userlistStr = JsonHelper.ToJson(userList); userListNew = JsonToList(userlistStr); List classStudentList = APP.ClassStudentList; for (int i = 0; i < classStudentList.Count; i++) { classStudentList[i].status = 0; for (int j = 0; j < userListNew.Count; j++) { if (classStudentList[i].studentid == userListNew[j].userid) { userListNew[j].num = classStudentList[i].num; } } } if (userListNew.Count > 0) { //OrderbyDescending 倒叙 OrderBy 顺序 raduser = userListNew.OrderBy(x => x.num).ToList(); } foreach (OnlineUserModel user in userListNew) { string userpic = user.userpic; if (ZCache.headDic.ContainsKey(userpic.Replace("/", ""))) { userpic = ZCache.headDic[userpic.Replace("/", "")]; } else { userpic = "../Images/RollCall/attendance_1.png"; } pageData.userList.Add(new RollCallPageData() { Name = user.username, Attendance_33 = "Collapsed", Attendance_3 = "Visible", Pic = userpic, ID = user.userid.ToString(), Attendance = "../Images/RollCall/attendance_3.png" }); foreach (ClassStudentListModel cl in classStudentList) { if (cl.studentid == user.userid) { cl.status = 1; } } } string notOnline = string.Empty; foreach (ClassStudentListModel cl in classStudentList) { if (cl.status == 0) { notOnline += cl.studentname + " "; } } if (!string.IsNullOrWhiteSpace(notOnline)) { txbNotOnline.Text = notOnline; } else { txbNotOnline.Text = "暂无"; } toolbar_list.DataContext = pageData; })); } internal static List JsonToList(string respstr) { JsonSerializer serializer = new JsonSerializer(); StringReader sr = new StringReader(respstr); object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List)); List list = o as List; return list; } /// /// 是否已经按下按钮 /// public bool IsRandomRollCallButton = false; /// /// 随机点名 /// /// /// private void btnRandomRollCall_Click(object sender, RoutedEventArgs e) { if (!IsRandomRollCallButton) { IsRandomRollCallButton = true; userids = string.Empty; useridss = string.Empty; useridsss = string.Empty; returnNum = 0; APP.RollCallList = new System.Collections.Generic.List(); stpAnswer.Visibility = Visibility.Collapsed; imgRandomRollCall.Visibility = Visibility.Collapsed; imgRandomRollCallTwo.Visibility = Visibility.Visible; imgAnswer.Visibility = Visibility.Visible; imgAnswerTwo.Visibility = Visibility.Collapsed; initPageData(); if (isRushToAnswer)//如果抢答没有结束 先结束抢答 { //ZSocketServer.getInstance().SendMessage(ZSocketMsgManger.qiangdaEnd()); isRushToAnswer = false; } if (APP.OnlineUserList.Count > 0)//判断当前在线人数大于0 点击才有效 ceshi= { isRollCall = true; Thread.Sleep(500); APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeRandomRollCallServering, InvokeRandomRollCallServerCompate); } else { IsRandomRollCallButton = false; } } } /// /// 点名抽奖效果 /// /// private object InvokeRandomRollCallServering() { try { if (ranNum != 201314 && ranNum != 0) { if (ranNum < pageData.userList.Count) { pageData.userList[ranNum].Attendance_33 = "Collapsed"; pageData.userList[ranNum].Attendance_3 = "Visible"; Thread.Sleep(500); } } int randomTimes = 0;//随机次数 ranNum = 0;//随机到用户的下标 if (pageData.userList.Count == 1)//当前在线1人直接选中 { pageData.userList[0].Attendance_33 = "Collapsed"; pageData.userList[0].Attendance_3 = "Visible"; Thread.Sleep(500); pageData.userList[0].Attendance_33 = "Visible"; pageData.userList[0].Attendance_3 = "Collapsed"; } else { randomTimes = 10; //if (TeachingDataStatic.OnlineUserList.Count > 0) //{ // //OrderbyDescending 倒叙 OrderBy 顺序 // raduser = raduser.OrderBy(x => x.num).ToList(); //} int ranNumberOfPeople = 999; if ("1".Equals(flag) && raduser.Count > 5) { do { long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); ranNumberOfPeople = ran.Next(5); for (int i = 0; i < userListNew.Count; i++) { if (userListNew[i].userid == raduser[ranNumberOfPeople].userid) { ranNumberOfPeople = i; break; } } } while (ranNumberOfPeople == rNum); raduser[ranNumberOfPeople].num += 1; } else if ("2".Equals(flag) && raduser.Count > 15) { do { long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); ranNumberOfPeople = ran.Next(15); for (int i = 0; i < userListNew.Count; i++) { if (userListNew[i].userid == raduser[ranNumberOfPeople].userid) { ranNumberOfPeople = i; break; } } } while (ranNumberOfPeople == rNum); raduser[ranNumberOfPeople].num += 1; } else if (raduser.Count > 30) { do { long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); ranNumberOfPeople = ran.Next(30); for (int i = 0; i < userListNew.Count; i++) { if (userListNew[i].userid == raduser[ranNumberOfPeople].userid) { ranNumberOfPeople = i; break; } } raduser[ranNumberOfPeople].num += 1; } while (ranNumberOfPeople == rNum); } else { ranNumberOfPeople = 999; } //if (APP.outputInforLog) //{ // LogHelper.WriteInfoLog("ranNum:" + TeachingDataStatic.OnlineUserList[ranNum].username + "&" + raduser[0].username + raduser[1].username + raduser[2].username + raduser[3].username + raduser[4].username + raduser[5].username); //} int ranNums = 0;//记录上一次随机的下标 for (int i = 0; i < randomTimes; i++) { Thread.Sleep(200); if (i > 0) { pageData.userList[ranNum].Attendance_33 = "Collapsed"; pageData.userList[ranNum].Attendance_3 = "Visible"; } while (ranNums == ranNum)//保证这次随机的下标和上次不相同 { long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); ranNum = ran.Next(pageData.userList.Count); if (i == 9 && ranNumberOfPeople != 999) { ranNum = ranNumberOfPeople; break; } if (pageData.userList.Count < 3) { break; } } ranNums = ranNum; rNum = ranNum; pageData.userList[ranNum].Attendance_33 = "Visible"; pageData.userList[ranNum].Attendance_3 = "Collapsed"; } } } catch (Exception ex) { LogHelper.WriteErrLog("【抢答点名(RollCallWindow)" + ex.Message, ex); } return APP.ErrorMessage; } /// /// 点名抽奖效果-返回结果 /// /// public void InvokeRandomRollCallServerCompate(object obj) { APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeInteractiveLaunchsServering, InvokeInteractiveLaunchsServerCompate); } private void frm_ChangeTextEvent(string text) { if ("重新点名".Equals(text)) { returnNum = 0; WSocketClient.getInstance().SendMessage(SocketMsgManger.RandomRollCallEndMsg()); //ZSocketServer.getInstance().SendMessage(ZSocketMsgManger.qiangdaEnd()); initPageData(); IsRandomRollCallButton = true; if (APP.OnlineUserList.Count > 0)//判断当前在线人数大于0 点击才有效 { isRollCall = true; APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeRandomRollCallServering, InvokeRandomRollCallServerCompate); } } else if ("关闭窗口".Equals(text)) { returnNum = 0; userids = string.Empty; useridss = string.Empty; useridsss = string.Empty; isRollCall = false; isRushToAnswer = false; WSocketClient.getInstance().SendMessage(SocketMsgManger.RandomRollCallEndMsg()); IsRandomRollCallButton = false; //ToolbarWindow.IsOpenRollCallWindow = false; Hide(); } } private void Window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e) { DragMove(); } /// /// 关闭 /// /// /// private void btnDown_Click(object sender, RoutedEventArgs e) { WSocketClient.getInstance().SendMessage(SocketMsgManger.RushToAnswerEndMsg()); //ToolbarWindow.IsNotOperation = false; APP.myloading.Show(); new Thread(o => { returnNum = 0; userids = string.Empty; useridss = string.Empty; useridsss = string.Empty; isRollCall = false; isRushToAnswer = false; APP.RollCallList = new System.Collections.Generic.List(); //string msg = ZSocketMsgManger.qiangdaEnd(); //ZSocketServer.getInstance().SendMessage(msg); //ZSqliteManger.insertClassAction(new ClassAction() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // type = 403, // json = msg, // askid = ZCommonData.askid //}); Dispatcher.Invoke(new Action(() => { //ToolbarWindow.IsOpenRollCallWindow = false; APP.myloading.Hide(); Hide(); })); }).Start(); } /// /// 抢答 /// /// /// private void btnAnswer_Click(object sender, RoutedEventArgs e) { WSocketClient.getInstance().SendMessage(SocketMsgManger.RushToAnswerMsg()); if (!IsRandomRollCallButton) { returnNum = 0; userids = string.Empty; useridss = string.Empty; useridsss = string.Empty; APP.RollCallList = new System.Collections.Generic.List(); imgRandomRollCall.Visibility = Visibility.Visible; imgRandomRollCallTwo.Visibility = Visibility.Collapsed; imgAnswer.Visibility = Visibility.Collapsed; imgAnswerTwo.Visibility = Visibility.Visible; //initPageData(); if (isRollCall)//如果随机点名没有结束 发送结束随机点名消息 { //ZSocketServer.getInstance().SendMessage(ZSocketMsgManger.qiangdaEnd()); isRollCall = false; } if (APP.OnlineUserList.Count > 0)//判断当前在线人数大于0 点击才有效 { if (pageData.userList.Count > 0) { pageData.userList.Clear(); } //TeachingDataStatic.rollCallNum += 1; txbNumberOfReplies.Text = "0"; isRushToAnswer = true; APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeInteractiveLaunchServering, InvokeInteractiveLaunchServerCompate); } else { isRushToAnswer = false; } } } /// /// 互动 抢答服务-调用 /// /// private object InvokeInteractiveLaunchServering() { //Askteacher askteacher = new Askteacher //{ // roomid = ZCommonData.roomid, // asktype = 20, // schoolid = ZCommonData.loginUser.schoolid, // asktid = DataProvider.TimestampAddRandomNumber(), // createtime = DataProvider.TimestampTotalSeconds(), // asktnum = 0 //}; //ZCommonData.askid = askteacher.asktid; //ZSqliteManger.insertClassData(new ClassData() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // api = "askteacher", // json = ZJsonHelper.ToJson(askteacher), // askid = askteacher.asktid //}); //string msg = ZSocketMsgManger.qiangdaBegin(ZCommonData.askid); //ZSocketServer.getInstance().SendMessage(msg); //ZSqliteManger.insertClassAction(new ClassAction() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // type = 401, // json = msg, // askid = ZCommonData.askid //}); return APP.ErrorMessage; } /// /// 互动 抢答服务-返回结果 /// /// public void InvokeInteractiveLaunchServerCompate(object obj) { } /// /// 互动 随机点名 服务-调用 /// /// private object InvokeInteractiveLaunchsServering() { //Askteacher askteacher = new Askteacher //{ // roomid = ZCommonData.roomid, // asktype = 21, // schoolid = ZCommonData.loginUser.schoolid, // asktid = DataProvider.TimestampAddRandomNumber(), // createtime = DataProvider.TimestampTotalSeconds(), // asktnum = 0 //}; //ZCommonData.askid = askteacher.asktid; //ZSqliteManger.insertClassData(new ClassData() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // api = "askteacher", // json = ZJsonHelper.ToJson(askteacher), // askid = askteacher.asktid //}); OnlineUserModel user = APP.OnlineUserList[ranNum]; Console.WriteLine("ranNum:" + ranNum.ToString() + @user.username + "Ci" + user.num.ToString()); //Askstudent askstudent = new Askstudent //{ // roomid = ZCommonData.roomid, // studentid = user.userid, // asktid = ZCommonData.askid, // classid = ZCommonData.classid, // schoolid = ZCommonData.loginUser.schoolid, // createtime = DataProvider.TimestampTotalSeconds(), // studentselected = 1, // radiocorrect = 0 //}; //ZSqliteManger.insertClassData(new ClassData() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // api = "askstudent", // json = ZJsonHelper.ToJson(askstudent), // askid = askteacher.asktid //}); WSocketClient.getInstance().SendMessage(SocketMsgManger.RandomRollCallMsg(user.userid)); //ZSqliteManger.insertClassAction(new ClassAction() //{ // unix = DataProvider.TimestampTotalSeconds(), // roomid = ZCommonData.roomid, // type = 411, // json = msg, // askid = ZCommonData.askid //}); return APP.ErrorMessage; } /// /// 互动 随机点名服务-返回结果 /// /// public void InvokeInteractiveLaunchsServerCompate(object obj) { if (serverReturnCode == APP.ServerScuessCode) { OnlineUserModel user = APP.OnlineUserList[ranNum]; string userpic = user.userpic; string userpicPath = "../Images/RollCall/attendance_1.png"; if (ZCache.headDic.ContainsKey(userpic.Replace("/", ""))) { userpicPath = ZCache.headDic[userpic.Replace("/", "")]; } try { #region 随机次数增加 for (int i = 0; i < APP.ClassStudentList.Count; i++) { if (APP.ClassStudentList[i].studentid == user.userid) { APP.ClassStudentList[i].num += 1; break; } } #endregion bool isUserid = true; //if (TeachingDataStatic.RandomRollCallStatisticsList.Count > 0) //{ // for (int i = 0; i < TeachingDataStatic.RandomRollCallStatisticsList.Count; i++) // { // if (TeachingDataStatic.RandomRollCallStatisticsList[i].userid == user.userid) // { // TeachingDataStatic.RandomRollCallStatisticsList[i].replies += 1; // isUserid = false; // break; // } // } //} if (isUserid) { //RandomRollCallStatistics randomRollCallStatistics = new RandomRollCallStatistics //{ // username = user.username, // userid = user.userid, // userpicPath = userpicPath, // replies = 1 //}; //TeachingDataStatic.RandomRollCallStatisticsList.Add(randomRollCallStatistics); //randomRollCallStatistics = new RandomRollCallStatistics(); } } catch (Exception ex) { LogHelper.WriteErrLog("【课堂点名(RollCallWindow)】错误日志:" + ex.Message, ex); } try { if (rollCallPerfectionWindow == null) { rollCallPerfectionWindow = new RollCallPerfectionWindow( user.username, userpicPath ) { Topmost = true, Width = pwidth, Height = pHeight, Left = 0, Top = 0 }; //订阅事件 rollCallPerfectionWindow.ChangeTextEvent += new ChangeTextHandler(frm_ChangeTextEvent); rollCallPerfectionWindow.Owner = this; } else { rollCallPerfectionWindow.Name(user.username); } rollCallPerfectionWindow.Show(); } catch (Exception ex) { LogHelper.WriteErrLog("【课堂点名(RollCallWindow)】错误日志:" + ex.Message, ex); } } } /// /// 页面关闭 /// /// /// private void Window_Closed(object sender, EventArgs e) { //ToolbarWindow.IsNotOperation = false; click_closeClick(); //ZSocketServer.getInstance().removedWin(this); } #region 关闭委托 /// /// 关闭委托 结束页面 创建人:赵耀 创建时间:2020年8月13日 /// /// /// public delegate void CloseClick(); /// /// 关闭委托 结束页面 创建人:赵耀 创建时间:2020年8月13日 /// public event CloseClick click_closeClick; #endregion 关闭委托 } public class RollCallPageData : NotifyModel { public ObservableCollection userList { get; set; } internal string _name; public string Name { get => _name; set { _name = value; OnPropertyChanged("Name"); } } internal string _id; public string ID { get => _id; set { _id = value; OnPropertyChanged("ID"); } } internal string _Pic; public string Pic { get => _Pic; set { _Pic = value; OnPropertyChanged("Pic"); } } internal string _attendance = ""; public string Attendance { get => _attendance; set { _attendance = value; OnPropertyChanged("Attendance"); } } internal string _attendance_3 = "Visible"; public string Attendance_3 { get => _attendance_3; set { _attendance_3 = value; OnPropertyChanged("Attendance_3"); } } internal string _attendance_33 = "Visible"; public string Attendance_33 { get => _attendance_33; set { _attendance_33 = value; OnPropertyChanged("Attendance_33"); } } internal string _medal = "../Images/noting.png"; public string Medal { get => _medal; set { _medal = value; OnPropertyChanged("Medal"); } } public long askid { get; set; } public RollCallPageData() { userList = new ObservableCollection(); } } }