又到年终了,各种抽奖软件需求纷至沓来,现分享一个简易抽奖软件逻辑,希望对大家抽奖类的需求有所帮助;
直奔主题吧,测试程序很稳定,可以直接将界面修改为其他形式
1、奖池枚举类
public enum AwardGrade{SpecialAward=0,FirstAward=1,SencdAward=2,ThirdAward=3,FourAward=4}
2、乱序从文本文件中加载参与抽奖的人员
/// <summary>/// 加载用户乱序加载/// </summary>private static void loadUser(){try{Random random = new Random();StreamReader sr = new StreamReader("staff.txt", System.Text.Encoding.Default);while (!sr.EndOfStream){string name = sr.ReadLine();if (!string.IsNullOrEmpty(name.Trim()) && !AwardPoolUsersList.Contains(name.Trim())){AwardPoolUsersList.Insert(random.Next(AwardPoolUsersList.Count), name.Trim());}}sr.Close();logger.InfoFormat("[用户加载]用户加载完成,共加载{0}个用户", AwardPoolUsersList.Count);}catch (Exception ex){logger.ErrorFormat("加载用户异常,异常描述:{0},异常详情:{1}", ex.Message, ex.StackTrace);}}
3、获取一个中奖人(私有函数)
static object obj = new object(); /// <summary>/// 获取一个中奖人/// </summary>/// <returns></returns>private static string getCurrentAwardUser(){lock (obj){try{List<string> currentUser = new List<string>();currentUser = AwardPoolUsersList;if (AwardedUserList == null){AwardedUserList = new HashSet<string>();}int count = currentUser.Count;string name;do{int data = getNext(count);name = currentUser[data];} while (AwardedUserList.Contains(name));return name;}catch (Exception ex){logger.ErrorFormat("随机生成用户时异常,异常描述{0},异常详情:{1}", ex.Message, ex.StackTrace);return "";}}}
4、外部调用获取中奖者函数(公共的)
/// <summary>/// 请求一个中奖者/// </summary>/// <returns></returns>public string GetUser(){lock (obj){string result = getCurrentAwardUser();while (1 == 1){if (!AwardedUserList.Contains(result) && !string.IsNullOrEmpty(result)){if (msg.Contains(result)){result = getCurrentAwardUser();}break;}result = getCurrentAwardUser();}AwardedUserList.Add(result);return result;}}
5、加载的随机函数
/// <summary>/// 请求一个中奖者/// </summary>/// <returns></returns>public string GetUser(){lock (obj){string result = getCurrentAwardUser();while (1 == 1){if (!AwardedUserList.Contains(result) && !string.IsNullOrEmpty(result)){if (msg.Contains(result)){result = getCurrentAwardUser();}break;}result = getCurrentAwardUser();}AwardedUserList.Add(result);return result;}}
6、过滤中奖人员,避免重复中奖
/// <summary>/// 过滤移除已中奖用户/// </summary>private void removeAwardUser(){try{StreamReader sr = new StreamReader("Result.txt", System.Text.Encoding.Default);int i = 0;while (!sr.EndOfStream){string name = sr.ReadLine();if (!string.IsNullOrEmpty(name.Trim())){i++;AwardPoolUsersList.Remove(name.Trim());}}sr.Close();logger.InfoFormat("移除用户数:{0}", i);}catch (Exception ex){logger.ErrorFormat("移除已中奖用户时异常,异常详情:{0}-1", ex.Message, ex.StackTrace);}}
7、保存中奖信息
/// <summary>/// 保存中奖信息/// </summary>/// <param name="name"></param>public void saveAwardUserToTxt(string name){try{FileStream stream = new FileStream("Result.txt", FileMode.Append, FileAccess.Write);StreamWriter sw = new StreamWriter(stream);sw.WriteLine(name);sw.Close();stream.Close();logger.InfoFormat("中奖人:{0}", name);}catch (Exception ex){logger.ErrorFormat("将中奖者:{2}写入文件时发生户异常,异常描述:{0},异常详情:{1}", ex.Message, ex.StackTrace, name);}}
8、测试程序(
winform+ log4net+donetbar)
不好意思,不知道怎么挂rar的附件,程序挂不上去,如有需要请单独联系本人吧