lobby_functions.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. -- lobby/functions.lua
  2. TestLobbyFunctions = {}
  3. function TestLobbyFunctions:setUp()
  4. mtmock.privs = {
  5. ['p1'] = {},
  6. ['p2'] = {creative=true, fly=true, fast=true},
  7. ['p3'] = {builder=false, creative=true, fly=true, fast=true},
  8. ['p4'] = {builder=false, creative=false, fly=true, fast=true},
  9. ['b1'] = {builder=true, creative=true, fly=true, fast=true},
  10. ['b2'] = {builder=true, creative=false, fly=true, fast=true},
  11. }
  12. end
  13. function TestLobbyFunctions:testIsBuilder()
  14. local p1 = mtmock.add_player('p1')
  15. local p2 = mtmock.add_player('p2')
  16. local p3 = mtmock.add_player('p3')
  17. local p4 = mtmock.add_player('p4')
  18. local b1 = mtmock.add_player('b1', true)
  19. local b2 = mtmock.add_player('b2', true)
  20. lu.assertIsNil(lobby.is_builder(nil))
  21. lu.assertIsFalse(lobby.is_builder(p1))
  22. lu.assertIsFalse(lobby.is_builder(p2))
  23. lu.assertIsFalse(lobby.is_builder(p3))
  24. lu.assertIsFalse(lobby.is_builder(p4))
  25. lu.assertIsTrue(lobby.is_builder(b1))
  26. lu.assertIsTrue(lobby.is_builder(b2))
  27. end
  28. -- does lobby.builder_to_player() reset the player privileges?
  29. function TestLobbyFunctions:testBuilderToPlayerResetsPrivs()
  30. local b1 = mtmock.add_player('b1', true)
  31. lobby.builder_to_player(b1)
  32. b1_privs = minetest.get_player_privs('b1')
  33. lu.assertIsNil(b1_privs.creative)
  34. lu.assertIsNil(b1_privs.fly)
  35. lu.assertIsNil(b1_privs.fast)
  36. local b2 = mtmock.add_player('b2', true)
  37. lobby.builder_to_player(b2)
  38. b2_privs = minetest.get_player_privs('b2')
  39. lu.assertIsNil(b2_privs.creative)
  40. lu.assertIsNil(b2_privs.fly)
  41. lu.assertIsNil(b2_privs.fast)
  42. end
  43. -- does lobby.builder_to_player() set the player mode to 'solo'?
  44. function TestLobbyFunctions:testBuilderToPlayerSetsSoloMode()
  45. local b1 = mtmock.add_player('b1', true)
  46. lobby.builder_to_player(b1)
  47. b1_attrs = b1:get_meta()
  48. lu.assertEquals(b1_attrs:get_string('mode'), 'solo')
  49. local b2 = mtmock.add_player('b2', true)
  50. lobby.builder_to_player(b2)
  51. b2_attrs = b2:get_meta()
  52. lu.assertEquals(b2_attrs:get_string('mode'), 'solo')
  53. end
  54. -- does lobby.builder_to_player() save the main inventory list?
  55. function TestLobbyFunctions:testBuilderToPlayerSavesMainInventory()
  56. local b1 = mtmock.add_player('b1', true)
  57. b1_inv = b1:get_inventory()
  58. local b1_main = {'old_main'}
  59. b1_inv:set_list('main', b1_main)
  60. lobby.builder_to_player(b1)
  61. local new_bldr = b1_inv:get_list('builder')
  62. lu.assertEquals(new_bldr, b1_main)
  63. end
  64. -- does lobby.builder_to_player() respect the player mode?
  65. function TestLobbyFunctions:testBuilderToPlayerRespectsMode()
  66. local p1 = mtmock.add_player('p1')
  67. p1_attr = p1:get_meta()
  68. p1_inv = p1:get_inventory()
  69. local exp_bldr = {'builder_invlist'}
  70. local exp_main = {'player_invlist'}
  71. local exp_mode = p1_attr:get_string('mode')
  72. p1_inv:set_list('builder', exp_bldr)
  73. p1_inv:set_list('main', exp_main)
  74. lobby.builder_to_player(p1)
  75. local act_bldr = p1_inv:get_list('builder')
  76. local act_main = p1_inv:get_list('main')
  77. local act_mode = p1_attr:get_string('mode')
  78. lu.assertEquals(act_bldr, exp_bldr)
  79. lu.assertEquals(act_main, exp_main)
  80. lu.assertEquals(act_mode, exp_mode)
  81. end
  82. -- does lobby.player_to_builder() restore builder privs?
  83. function TestLobbyFunctions:testPlayerToBuilderRestoresPrivs()
  84. local p1 = mtmock.add_player('p1')
  85. lobby.player_to_builder(p1)
  86. p1_privs = minetest.get_player_privs('p1')
  87. lu.assertIsTrue(p1_privs.creative)
  88. lu.assertIsTrue(p1_privs.fly)
  89. lu.assertIsTrue(p1_privs.fast)
  90. local p2 = mtmock.add_player('p2')
  91. lobby.player_to_builder(p2)
  92. p2_privs = minetest.get_player_privs('p2')
  93. lu.assertIsTrue(p2_privs.creative)
  94. lu.assertIsTrue(p2_privs.fly)
  95. lu.assertIsTrue(p2_privs.fast)
  96. local p3 = mtmock.add_player('p3')
  97. lobby.player_to_builder(p3)
  98. p3_privs = minetest.get_player_privs('p3')
  99. lu.assertIsTrue(p3_privs.creative)
  100. lu.assertIsTrue(p3_privs.fly)
  101. lu.assertIsTrue(p3_privs.fast)
  102. local p4 = mtmock.add_player('p4')
  103. lobby.player_to_builder(p4)
  104. p4_privs = minetest.get_player_privs('p4')
  105. lu.assertIsTrue(p4_privs.creative)
  106. lu.assertIsTrue(p4_privs.fly)
  107. lu.assertIsTrue(p4_privs.fast)
  108. end
  109. -- does lobby.player_to_builder() set the player mode to 'builder'?
  110. function TestLobbyFunctions:testPlayerToBuilderSetsBuilderMode()
  111. local p1 = mtmock.add_player('p1')
  112. lobby.player_to_builder(p1)
  113. p1_attrs = p1:get_meta()
  114. lu.assertEquals(p1_attrs:get_string('mode'), 'builder')
  115. local p2 = mtmock.add_player('p2')
  116. lobby.player_to_builder(p2)
  117. p2_attrs = p2:get_meta()
  118. lu.assertEquals(p2_attrs:get_string('mode'), 'builder')
  119. local p3 = mtmock.add_player('p3')
  120. lobby.player_to_builder(p3)
  121. p3_attrs = p3:get_meta()
  122. lu.assertEquals(p3_attrs:get_string('mode'), 'builder')
  123. local p4 = mtmock.add_player('p4')
  124. lobby.player_to_builder(p4)
  125. p4_attrs = p4:get_meta()
  126. lu.assertEquals(p4_attrs:get_string('mode'), 'builder')
  127. end
  128. -- does lobby.player_to_builder() restore the builder inventory?
  129. function TestLobbyFunctions:testPlayerToBuilderRestoresBuilderInventory()
  130. local p1 = mtmock.add_player('p1')
  131. p1_inv = p1:get_inventory()
  132. local p1_builder = {'p1_builder'}
  133. local p1_main = {'p1_player'}
  134. p1_inv:set_list('builder', p1_builder)
  135. p1_inv:set_list('main', p1_main)
  136. lobby.player_to_builder(p1)
  137. local act_maininv = p1_inv:get_list('main')
  138. lu.assertEquals(act_maininv, p1_builder)
  139. end
  140. -- does lobby.player_to_builder() respect the player mode?
  141. -- (i.e., inventories do not get changed)
  142. function TestLobbyFunctions:testPlayerToBuilderRespectsMode()
  143. local b1 = mtmock.add_player('b1', true)
  144. b1_attr = b1:get_meta()
  145. b1_inv = b1:get_inventory()
  146. local exp_bldr = {'invlist:builder'}
  147. local exp_main = {'invlist:main'}
  148. local exp_mode = b1_attr:get_string('mode')
  149. b1_inv:set_list('builder', exp_bldr)
  150. b1_inv:set_list('main', exp_main)
  151. lobby.player_to_builder(b1)
  152. local act_bldr = b1_inv:get_list('builder')
  153. local act_main = b1_inv:get_list('main')
  154. local act_mode = b1_attr:get_string('mode')
  155. lu.assertEquals(act_bldr, exp_bldr)
  156. lu.assertEquals(act_main, exp_main)
  157. lu.assertEquals(act_mode, exp_mode)
  158. end
  159. --[[ TestLobbyFunctions ]]--