1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
-
- using CommonAI.Data;
- using CommonLang;
- using CommonLang.IO;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using XmdsCommonServer.XLS.Data;
- using static XmdsCommonServer.XLS.Data.RandomName;
- namespace XmdsCommonServer.XLS
- {
- public class XLSRandomNameLoader : XLSLoader
- {
- private List<PrefixName> mPrefixName = new List<PrefixName>();
- private List<SuffixName> mSuffixName = new List<SuffixName>();
- private bool mHasData;
- public XLSRandomNameLoader(string path)
- {
- byte[] data = Resource.LoadData(path);
- if (data == null)
- {
- throw new Exception("Can not read xls file : " + path);
- }
- using (MemoryStream ms = new MemoryStream(data))
- {
- IWorkbook Workbook = null;
- if (path.Contains(".xlsx"))
- {
- Workbook = new XSSFWorkbook(ms);
- }
- else if (path.Contains(".xls"))
- {
- Workbook = new HSSFWorkbook(ms);
- }
- for (int si = 0; si < Workbook.NumberOfSheets; si++)
- {
- ISheet sheet = Workbook.GetSheetAt(si) as ISheet;
- try
- {
- if (sheet.SheetName == "Prefix")
- {
- foreach (PrefixName m in LoadSheet<PrefixName>(sheet))
- {
- mPrefixName.Add(m);
- }
- }
- else if (sheet.SheetName == "Suffix")
- {
- foreach (SuffixName m in LoadSheet<SuffixName>(sheet))
- {
- mSuffixName.Add(m);
- }
- }
- mHasData = mPrefixName.Count > 0 && mSuffixName.Count > 0;
- }
- catch (Exception error)
- {
- throw new Exception(string.Format("XLSZhanYaoDataLoader 初始化斩妖配置错误SheetName = {0},Error = {1}",
- sheet.SheetName, error.ToString()));
- }
- }
- }
- log.Info("XLSZhanYaoDataLoader 初始化怪物配置完成.");
- }
- public string GetRanomName()
- {
- if(!mHasData)
- {
- return null;
- }
- int indexPrefix = GlobalData.gRandom.Next() % this.mPrefixName.Count;
- int indexSuffix = GlobalData.gRandom.Next() % this.mSuffixName.Count;
- return this.mPrefixName[indexPrefix].prefix + this.mSuffixName[indexSuffix].suffix;
- }
- }
- }
|