You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

backendTest.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import { Injector } from "../src/backend/Injector/Injector";
  2. import { FrontworkAdmin } from "../src/backend/Admin/Admin";
  3. import { T1 } from "../src/backend/Types/Items";
  4. import { RPCSocket } from "rpclibrary";
  5. import { FrontcraftIfc, Auth, User, FrontcraftFeatureIfc, Raid, Character, Rank, Class, Race } from "../src/backend/Types/Types";
  6. import { SpecT } from "../src/backend/Types/PlayerSpecs";
  7. type protoAccount<C extends Class = Class> = {
  8. name : string,
  9. rank : Rank,
  10. race: Race,
  11. class: C,
  12. spec: SpecT[C]
  13. }
  14. const trialsAndUp = {
  15. ADMIN: true,
  16. Guildmaster: true,
  17. Officer: true,
  18. Classleader: true,
  19. Raider: true,
  20. Trial: true,
  21. Social: false,
  22. Guest: false
  23. }
  24. const adminsOnly = {
  25. ADMIN: true,
  26. Guildmaster: true,
  27. Officer: true,
  28. Classleader: false,
  29. Raider: false,
  30. Trial: false,
  31. Social: false,
  32. Guest: false
  33. }
  34. const defaultPermissions = [
  35. { rpcname: 'signup', ...trialsAndUp
  36. },{ rpcname: 'reset', ...adminsOnly
  37. },{ rpcname: 'modifyPermissions', ...adminsOnly
  38. },{ rpcname: 'manageGuild', ...adminsOnly
  39. },{ rpcname: 'managePriorities', ...adminsOnly
  40. },{ rpcname: 'softreserveCurrency', ...adminsOnly
  41. },{ rpcname: 'manageRaid', ...adminsOnly
  42. }]
  43. const testAccounts : protoAccount[] = [
  44. {
  45. name: 'Rain',
  46. race: 'Human',
  47. class: 'Warrior',
  48. spec: 'Protection',
  49. rank: 'Guildmaster'
  50. },{
  51. name: 'Celinda',
  52. class: 'Warrior',
  53. race: 'Night Elf',
  54. spec: 'Protection',
  55. rank: 'Officer'
  56. },{
  57. name: 'Dagger',
  58. race: 'Dwarf',
  59. class: 'Rogue',
  60. spec: 'Assassination',
  61. rank: 'Classleader'
  62. },{
  63. name: 'Hope',
  64. class: 'Paladin',
  65. race: 'Human',
  66. spec: 'Holy',
  67. rank: 'Classleader'
  68. },{
  69. name: 'Shrekd',
  70. class: 'Warrior',
  71. race: 'Dwarf',
  72. spec: 'Fury',
  73. rank: 'Classleader'
  74. },{
  75. name: 'Teenieweenie',
  76. class: 'Warlock',
  77. race: 'Gnome',
  78. spec: 'Demonology',
  79. rank: 'Classleader'
  80. },{
  81. name: 'Hagibaba',
  82. class: 'Priest',
  83. race: 'Human',
  84. spec: 'Discipline',
  85. rank: 'Classleader'
  86. },{
  87. name: 'Muffinbreak',
  88. class: 'Mage',
  89. race: 'Gnome',
  90. spec: 'Arcane',
  91. rank: 'Classleader'
  92. },
  93. ]
  94. describe('Frontcraft', () => {
  95. let auth: Auth,
  96. adminUser : User,
  97. server: FrontworkAdmin,
  98. client : RPCSocket & FrontcraftIfc,
  99. adminClient : RPCSocket & FrontcraftFeatureIfc,
  100. raids: Raid[] = [],
  101. users : {[username in string] : { account: User, character: Character, auth: Auth, item?:string }} = {}
  102. const createAccount = (user: User) => {
  103. return client.UserManager.createUser(user)
  104. }
  105. const createAccountAndUser = async (acc : protoAccount) => {
  106. const account = await createAccount({
  107. pwhash: 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', //sha256("a")
  108. rank: acc.rank,
  109. username: acc.name
  110. })
  111. const auth = await client.UserManager.login(acc.name, 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb')
  112. const id = await client.CharacterManager.getSpecId(acc.class, acc.spec)
  113. const char = await client.CharacterManager.createCharacter(auth.token.value, {
  114. charactername: acc.name,
  115. specid: id,
  116. userid: account.id!,
  117. race: acc.race
  118. })
  119. return {
  120. account: account,
  121. character: char,
  122. auth: auth
  123. }
  124. }
  125. before(function (done){
  126. this.timeout(10000);
  127. server = Injector.resolve<FrontworkAdmin>(FrontworkAdmin)
  128. server.start().then((_server) => {
  129. RPCSocket.makeSocket<FrontcraftIfc>(20000, 'localhost').then(_client => {
  130. client = _client
  131. createAccount({
  132. pwhash: 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', //hash("a")
  133. rank: 'ADMIN',
  134. username: 'a',
  135. currency: 2
  136. }).then(adminUser => {
  137. client.UserManager.login(adminUser.username, 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb').then(auth => {
  138. const sock = new RPCSocket(
  139. auth.port,
  140. "localhost"
  141. )
  142. sock.connect<any>(auth.token.value).then(cli => {
  143. adminClient = cli
  144. sock.hook('kick', () => {
  145. console.log("I got kicked");
  146. })
  147. sock.hook('getUserData', () => auth)
  148. sock.hook('navigate', (where:string) => {
  149. console.log("Nagivate client to "+where);
  150. })
  151. sock.on('error', (e) => {
  152. console.log('Socket error', e)
  153. })
  154. done()
  155. })
  156. })
  157. })
  158. })
  159. }).catch(done)
  160. })
  161. after(()=>{
  162. client.destroy()
  163. adminClient.destroy()
  164. server.stop()
  165. })
  166. it('create raids', (done) => {
  167. let insertRaid = <Raid>{
  168. description: "Test raid 1",
  169. title: 'MC',
  170. start: Date.now().toString()
  171. }
  172. adminClient.manageRaid.createRaid(insertRaid).then(() => {
  173. client.RaidManager.getRaids().then((r)=>{
  174. if(r[0].title === insertRaid.title
  175. && r[0].description === insertRaid.description){
  176. raids.push(r[0])
  177. done()
  178. }
  179. }).catch(done)
  180. }).catch(done)
  181. })
  182. it('create users', (done)=>{
  183. Promise.all(testAccounts.map(acc => createAccountAndUser(acc))).then(accs => {
  184. if(accs.length === testAccounts.length){
  185. accs.forEach(acc => {
  186. users[acc.account.username] = acc
  187. })
  188. done()
  189. }
  190. }).catch(done)
  191. })
  192. it('should sign up', (done)=>{
  193. Promise.all(Object.values(users).map((user) =>
  194. adminClient.signup.sign(user.auth.token.value, user.character, raids[0], false).catch(done)
  195. )).then(x => {
  196. adminClient.signup.getSignups(raids[0]).then(s => {
  197. if(s.length == testAccounts.length)
  198. done()
  199. else{
  200. done("Unexpected number of signups: "+s.length)
  201. }
  202. })
  203. })
  204. })
  205. it('calculate priorities', (done)=>{
  206. const user = Object.values(users)[0]
  207. adminClient.managePriorities.setPriority(T1[0], {
  208. race: user.character.race,
  209. specid: user.character.specid,
  210. modifier: 1,
  211. description:'AAA'
  212. }).then(() => {
  213. adminClient.managePriorities.setPriority(T1[0], {
  214. race: user.character.race,
  215. specid: undefined,
  216. modifier: 2,
  217. description:'BBB'
  218. }).then(()=>{
  219. adminClient.managePriorities.setPriority(T1[0], {
  220. race: undefined,
  221. specid: user.character.specid,
  222. modifier: 3,
  223. description:'CCC'
  224. }).then(()=>{
  225. client.ItemManager.calculatePriorities(T1[0], user.character).then(sum => {
  226. if(sum === 6)
  227. done()
  228. })
  229. })
  230. })
  231. })
  232. })
  233. it('buy token', (done)=>{
  234. Promise.all(Object.values(users).map(async (user) =>{
  235. const itemname = T1[0]//T1[Math.floor(T1.length*Math.random())]
  236. const modifier = await client.ItemManager.calculatePriorities(itemname, user.character)
  237. const token = await client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname)
  238. users[user.account.username].item = itemname
  239. if(!token) return false
  240. return modifier+1 === token.level
  241. })).then(success => {
  242. if(success.reduce((prev, curr)=>prev&&curr, true)) done()
  243. })
  244. })
  245. it('not buy token without currency', (done)=>{
  246. const user = Object.values(users)[0]
  247. const itemname = T1[0]
  248. client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname).then(token => {
  249. if(!token)
  250. done()
  251. else {
  252. console.log("Unexpected token", token);
  253. }
  254. })
  255. })
  256. it('upgrade token', (done)=>{
  257. const user = Object.values(users)[0]
  258. const itemname = T1[0]
  259. adminClient.softreserveCurrency.incrementCurrency(user.account, 2).then(async ()=>{
  260. const item = await client.ItemManager.getItem(itemname)
  261. const before = await client.ItemManager.getToken(user.character, item)
  262. if(!before || before.level !== 7) return
  263. await client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname)
  264. const after = await client.ItemManager.buyToken(user.auth.token.value, user.character.charactername, itemname)
  265. if(!after || after.level !== 9) return
  266. done()
  267. })
  268. })
  269. it('should buy more tokens', (done) => {
  270. Promise.all(Object.values(users).map(async (user) => {
  271. await adminClient.softreserveCurrency.incrementCurrency(user.account, 1)
  272. await client.ItemManager
  273. .buyToken(
  274. user.auth.token.value,
  275. user.character.charactername,
  276. T1[Math.floor(T1.length*Math.random())]
  277. )
  278. })).then(_ => {
  279. done()
  280. })
  281. })
  282. it('can set permissions', (done) => {
  283. Promise.all(
  284. defaultPermissions.map(perm => adminClient.modifyPermissions.setPermission(perm)),
  285. ).then(_ => {
  286. done()
  287. })
  288. })
  289. it('start raid', (done) => {
  290. client.RaidManager.getRaids().then((r)=>{
  291. adminClient.manageRaid.startRaid(raids[0]).then(async data => {
  292. const dbRaids = await client.RaidManager.getRaids()
  293. if(dbRaids.length === 0){
  294. await client.UserManager.getUser(testAccounts[0].name).then(dbUser => {
  295. if(dbUser && dbUser.currency === 1){
  296. adminClient.signup.getSignups(raids[0]).then(signups => {
  297. if(signups.length === 0){
  298. done()
  299. }else{
  300. console.log(signups);
  301. }
  302. })
  303. }
  304. else{
  305. console.log("Bad user currency", dbUser);
  306. }
  307. })
  308. }
  309. })
  310. })
  311. })
  312. it('reset system', (done) => {
  313. adminClient.reset.wipeCurrencyAndItems().then(() => {
  314. client.UserManager.getUser(testAccounts[0].name).then(user => {
  315. if(user && user.currency === 0){
  316. client.ItemManager.getTokens(users[testAccounts[0].name.toLowerCase()].character).then(tokens => {
  317. if(tokens.length === 0){
  318. done()
  319. }else{
  320. console.log(tokens);
  321. }
  322. })
  323. }else{
  324. console.log(user)
  325. }
  326. })
  327. })
  328. })
  329. })