Skip to content

Commit

Permalink
Merge pull request #107 from mfld-fr/pull_82
Browse files Browse the repository at this point in the history
Scoreboard example 'reloaded'
  • Loading branch information
macfreek committed Feb 25, 2019
2 parents db1190b + 49b8317 commit 3c57d99
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python

"""
Prints out raw scoreboard data.
"""

import os,sys

# local module
try:
import nbt
except ImportError:
# nbt not in search path. Let's see if it can be found in the parent folder
extrasearchpath = os.path.realpath(os.path.join(__file__,os.pardir,os.pardir))
if not os.path.exists(os.path.join(extrasearchpath,'nbt')):
raise
sys.path.append(extrasearchpath)
from nbt.nbt import NBTFile, TAG_Long, TAG_Int, TAG_String, TAG_List, TAG_Compound

def main(world_folder, show=True):
scorefile = world_folder + '/data/scoreboard.dat'
scores = NBTFile(scorefile,'rb')

for player in scores["data"]["PlayerScores"]:
print("%s: %d %s" % (player["Name"], player["Score"].value, player["Objective"].value))

if __name__ == '__main__':
if (len(sys.argv) == 1):
print("No world folder specified!")
sys.exit(64) # EX_USAGE
if sys.argv[1] == '--noshow' and len(sys.argv) > 2:
show = False
world_folder = sys.argv[2]
else:
show = True
world_folder = sys.argv[1]
# clean path name, eliminate trailing slashes. required for os.path.basename()
world_folder = os.path.normpath(world_folder)
if (not os.path.exists(world_folder)):
print("No such folder as "+world_folder)
sys.exit(72) # EX_IOERR

sys.exit(main(world_folder, show))
31 changes: 31 additions & 0 deletions tests/examplestests.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,37 @@ def testNBTGeneration(self):
self.assertEqualString(output[11], self.expected[11])
self.assertEqualString(output[13], self.expected[13])


class ScoreboardScriptTest(ScriptTestCase):
"""Scoreboard unit test"""

expected = [
'soulthps: 0 Diamond',
'soulthps: 1 Deaths',
'soulthps: 19238 Time',
'fwaggle: 77 Diamond',
'fwaggle: 9 Deaths',
'fwaggle: 26 Level',
'fwaggle: 2471784 Time',
'fwaggle: 1035 Kills',
'Sabriena: 60 Diamond',
'Sabriena: 2 Deaths',
'Sabriena: 4 Level',
'Sabriena: 1029806 Time',
'Sabriena: 187 Kills',
'DuncanDonutz: 22 Diamond',
'DuncanDonutz: 7 Deaths',
'DuncanDonutz: 15 Level',
'DuncanDonutz: 1313860 Time',
'DuncanDonutz: 30 Kills',
]

def testScoreboard(self):
output = self.runScript('scoreboard.py', ['tests/world_test',])
self.assertTrue(len(output) == 18, "Expected output of 18 lines long")
self.assertEqualOutput(output, self.expected)


# Load resources once.
# Implement as setUpModule/tearDownModule, since it only needs to be called
# once for the ScriptTestCase class. By default, a setUpClass/tearDownClass
Expand Down
Binary file added tests/world_test/data/scoreboard.dat
Binary file not shown.

0 comments on commit 3c57d99

Please sign in to comment.