Skip to content

Commit

Permalink
Refactor Player Scoring to be on Player and not Game
Browse files Browse the repository at this point in the history
Resolve #126 Add General Ability for Point Scoring to Spawn Manager
Fixes #117 Tracking Lowest Primary Score Modes
Removed AsteroidMiner example (game still exists)
  • Loading branch information
hawkerm committed May 27, 2016
1 parent fb7c5df commit 293cd37
Show file tree
Hide file tree
Showing 24 changed files with 119 additions and 118 deletions.
49 changes: 0 additions & 49 deletions SBA_Serv/Game/AsteroidMiner.py

This file was deleted.

2 changes: 1 addition & 1 deletion SBA_Serv/Game/BaubleHunt.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def collectBaubles(self, ship, bauble):
def depositBaubles(self, ship, home):
logging.info("Player Depositing Baubles #%d", ship.id)
for b in ship.player.carrying:
self.player_update_score(ship.player, b.value)
ship.player.update_score(b.value)
home.stored += b.value
ship.player.totalcollected += len(ship.player.carrying)

Expand Down
8 changes: 4 additions & 4 deletions SBA_Serv/Game/CombatExercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def player_died(self, player, gone):
player.object.killedby != None and \
player.object.killedby.owner != None and \
player.object.killedby.owner.player != None:
self.player_update_score(player.object.killedby.owner.player, player.object.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by"))
player.object.killedby.owner.player.update_score(player.object.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by"))
super(CombatExerciseGame, self).player_died(player, gone)
"""
Expand All @@ -53,7 +53,7 @@ def world_add_remove_object(self, wobj, added):
if not added and isinstance(wobj, Asteroid) and hasattr(wobj, "killedby") and wobj.killedby != None:
obj = wobj.killedby
if isinstance(obj, Torpedo):
self.player_update_score(obj.owner.player, self.cfg.getint("CombatExercise", "points_shoot_asteroid"))
obj.owner.player.update_score(self.cfg.getint("CombatExercise", "points_shoot_asteroid"))

logging.info("Torpedo Owner (#%d) Destroyed Asteroid", obj.owner.id)
"""
Expand All @@ -65,11 +65,11 @@ def world_add_remove_object(self, wobj, added):
elif not added and isinstance(wobj, Ship) and hasattr(wobj, "killedby") and wobj.killedby != None:
obj = wobj.killedby
if isinstance(obj, Torpedo):
self.player_update_score(obj.owner.player, wobj.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by"))
obj.owner.player.update_score(wobj.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by"))

logging.info("Torpedo Owner (#%d) Destroyed Ship (#%d)", obj.owner.id, wobj.id)
elif isinstance(obj, Ship) and obj.health.value > 0:
self.player_update_score(obj.player, wobj.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by"))
obj.player.update_score(wobj.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by"))

logging.info("Ship (#%d) Destroyed Ship (#%d)", obj.id, wobj.id)

Expand Down
6 changes: 3 additions & 3 deletions SBA_Serv/Game/DiscoveryQuest.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def dq_finished_scan(self, ship, obj, success):
# establish as ship's Outpost
ship.player.outpost = obj
obj.home_for.append(ship.player)
self.player_update_score(ship.player, ship.player.buffervalue + obj.value * 2) # Score initial points + 2*outpost value for establishing base
ship.player.update_score(ship.player.buffervalue + obj.value * 2) # Score initial points + 2*outpost value for establishing base
ship.player.buffervalue = 0
ship.player.sound = "SUCCESS"

Expand All @@ -216,7 +216,7 @@ def dq_finished_scan(self, ship, obj, success):
for obj in ship.player.scanned:
points += obj.value
ship.player.sound = "MISSION"
self.player_update_score(ship.player, points * self.cfg.getfloat("DiscoveryQuest", "mission_bonus_multiplier"))
ship.player.update_score(points * self.cfg.getfloat("DiscoveryQuest", "mission_bonus_multiplier"))

self.player_reset_mission(ship.player)

Expand All @@ -234,7 +234,7 @@ def dq_finished_scan(self, ship, obj, success):
obj.scanned_by.append(ship.player)
ship.player.sound = "SUCCESS"
if ship.player.outpost != None:
self.player_update_score(ship.player, obj.value)
ship.player.update_score(obj.value)
else: #haven't found outpost, need to buffer points
ship.player.buffervalue += obj.value
#end dq_finished_scan
Expand Down
2 changes: 1 addition & 1 deletion SBA_Serv/Game/FindTheMiddle.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def game_update(self, t):
break
x += 1
ship.body.position = self.player_get_start_position()
self.player_update_score(ship.player, self.__objective_points[x])
ship.player.update_score(self.__objective_points[x])
ship.player.time = 0
elif self.__reset_timer:
ship.player.time = 0
Expand Down
19 changes: 2 additions & 17 deletions SBA_Serv/Game/Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def server_register_player(self, name, color, imgindex, netid, aiship=None):
if (self.__started and self.__allowafterstart) or not self.__started:
# create new player
#
p = Player(name, color, imgindex, netid, None)
p = Player(name, color, imgindex, netid, self._primary_victory_high)
self.player_added(p, BasicGame._ADD_REASON_REGISTER_)
self._players[netid] = p

Expand Down Expand Up @@ -467,21 +467,6 @@ def player_get_by_network_id(self, netid):
#endregion

#region Player Scoring / Leaderboard Functions
def player_update_score(self, player, amount):
"""
Should be called to manipulate a player's score, will do extra bookkeeping and sanity for you.
"""
player.score += amount

# scores can't be negative
if player.score < 0:
player.score = 0

# TODO: #118 should probably check if primary highest flag to see if we want to keep track of lowest or highest score here
# update if this is a new personal best
if player.score > player.bestscore:
player.bestscore = player.score

def player_died(self, player, gone):
"""
Will be called when a player dies, will adjust score appropriately based on game rules.
Expand All @@ -495,7 +480,7 @@ def player_died(self, player, gone):
logging.info("Player %s Died", player.name)
player.deaths += 1
if self._points_lost_on_death > 0:
self.player_update_score(player, -self._points_lost_on_death)
player.update_score(-self._points_lost_on_death)

if self._reset_score_on_death:
self._player_reset_score(player)
Expand Down
4 changes: 2 additions & 2 deletions SBA_Serv/Game/HungryHungryBaubles.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def collectBaubles(self, ship, bauble):
# collect own Bauble?
if self.__assign_specific_bauble and bauble == self.__baubles[ship.player.netid]:
logging.info("Collected Own Bauble #%d", ship.id)
self.player_update_score(ship.player, self.__points_extra)
ship.player.update_score(self.__points_extra)
ship.player.sound = "COLLECT"
# add new bauble
self.__addBauble(ship.player, True)
Expand All @@ -82,7 +82,7 @@ def collectBaubles(self, ship, bauble):
ship.player.sound = "BAUBLE"
Bauble.spawn(self.world, self.cfg)
#eif
self.player_update_score(ship.player, bauble.value)
ship.player.update_score(bauble.value)
bauble.destroyed = True

logging.info("Done Collecting Baubles #%d", ship.id)
Expand Down
5 changes: 2 additions & 3 deletions SBA_Serv/Game/KingOfTheBubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def player_died(self, player, gone):
# calculate points to lose
addyum = player.score * (self.__pointpercent / 100.0) > self.__pointatleast
stealpoints = max(player.score * (self.__pointpercent / 100.0), self.__pointatleast)
self.player_update_score(player, -stealpoints)
player.update_score(-stealpoints)

# see if a Bubble is near us
added = False
Expand Down Expand Up @@ -148,8 +148,7 @@ def update(self, t):
ships.append(obj)

for ship in ships:
#HACK: Need to refigure out how game objects can get game reference with new spawn paradigm, is it the spawn manager passing game instead of world?
self.__world._GameWorld__game.player_update_score(ship.player, t * self.pointspeed)
ship.player.update_score(t * self.pointspeed)
#logging.info("Player %s getting points %d", ship.player.name, ship.player.score)

self.size -= (t * self.pointspeed * len(ships))
Expand Down
21 changes: 19 additions & 2 deletions SBA_Serv/Game/Players.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
class Player(object):
"""Controller of an object in the world"""

def __init__(self, name, color, imageindex, netid, wobj):
def __init__(self, name, color, imageindex, netid, highscore=True):
self.name = name
self.color = color
self.image = imageindex
self.netid = netid
self.object = wobj
self.object = None # Assigned Later
self.roundover = False
self.waiting = False
self.sound = None
Expand All @@ -16,6 +16,23 @@ def __init__(self, name, color, imageindex, netid, wobj):
self.deaths = 0
self.disconnected = False
self.lastkilledby = None
self.wanthighscore = highscore

def update_score(self, amount):
"""
Should be called to manipulate a player's score, will do extra bookkeeping and sanity for you.
"""
self.score += amount

# scores can't be negative
if self.score < 0:
self.score = 0

# update personal best
if self.wanthighscore and self.score > self.bestscore:
self.bestscore = self.score
elif not self.wanthighscore and self.score < self.bestscore:
self.bestscore = self.score

#TODO: Put alliegiences in here?

Expand Down
2 changes: 1 addition & 1 deletion SBA_Serv/Game/Survivor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def game_update(self, t):
for player in self.game_get_current_player_list():
# Update a player's score only when they're moving
if player.object != None and player.object.body.velocity.length > self._min_vel:
self.player_update_score(player, t)
player.update_score(t)

super(SurvivorGame, self).game_update(t)

Expand Down
38 changes: 37 additions & 1 deletion SBA_Serv/Game/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import World.WorldEntities
from World.WorldMath import intpos, friendly_type, cfg_rand_min_max, getPositionAwayFromOtherObjects
from World.Entities import Entity
from World.WorldEntities import Ship, Torpedo

class CallbackTimer(Thread):
"""
Expand Down Expand Up @@ -65,6 +66,7 @@ def __init__(self, name, ctype, initialnum):
self._max = False
self._player = False
self._shortlife = False
self._points = False

def add_timed_spawn(self, time_num, time_min, time_max): # int, int, int
if time_num > 0 and time_min > 0 and time_max >= time_min:
Expand Down Expand Up @@ -127,6 +129,20 @@ def add_shortlife(self, min, max):
def get_shortlife(self):
return random.randint(self._life_min, self._life_max)

def add_points(self, torpedo, ram):
self._points = True
self._points_torpedo = torpedo
self._points_ram = ram

def is_points(self):
return self._points

def get_points_torpedo(self):
return self._points_torpedo

def get_points_ram(self):
return self._points_ram

class SpawnManager:
ENTITY_TYPES = None
"""
Expand Down Expand Up @@ -185,6 +201,10 @@ def __init__(self, cfg, world):
if cfg.has_option(entity, "spawn_alive_time_min"):
sc.add_shortlife(cfg.getint(entity, "spawn_alive_time_min"), cfg.getint(entity, "spawn_alive_time_max"))

# do we want to give points on destruction of this object
if cfg.has_option(entity, "points_torpedo"):
sc.add_points(cfg.getint(entity, "points_torpedo"), cfg.getint(entity, "points_ram"))

self._spawns[entity] = sc

self._timers = {}
Expand Down Expand Up @@ -271,11 +291,27 @@ def stop(self):
def check_number(self, wobj):
"""
Checks the number of objects in the world to see if more need to be added over minimum.
Called by game when object removed from the world.
"""
name = friendly_type(wobj)
if self._running and self._spawns.has_key(name):
#logging.debug("Checking Number in Spawn Manager for %s - Running: %s Config: %s", name, repr(self._running), repr(self._spawns.has_key(name)))
if self._running and self._spawns.has_key(name):
sc = self._spawns[name]

#logging.debug("Configured for Points: %s and Has Info %s", repr(sc.is_points()), repr(hasattr(wobj, "killedby")))
if sc.is_points() and hasattr(wobj, "killedby") and wobj.killedby != None:
obj = wobj.killedby
#logging.debug("Killed By: %s", repr(obj))
if isinstance(obj, Torpedo) and hasattr(obj, "owner") and obj.owner != None and isinstance(obj.owner, Ship):
obj.owner.player.update_score(sc.get_points_torpedo())

logging.info("Torpedo Owner (#%d) Destroyed %s for %d Points", obj.owner.id, name, sc.get_points_torpedo())
elif isinstance(obj, Ship) and obj.health.value > 0:
obj.player.update_score(sc.get_points_ram())

logging.info("Ship (#%d) Destroyed %s for %d Points", obj.id, name, sc.get_points_ram())

if sc.is_min():
count = self._world.get_count_of_objects(type(wobj))
logging.info("Found %d %s Entities", count, name)
Expand Down
1 change: 0 additions & 1 deletion SBA_Serv/SBA_Serv.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Game\HungryHungryBaubles.py" />
<Compile Include="Game\AsteroidMiner.py" />
<Compile Include="Game\CombatExercise.py" />
<Compile Include="Game\Survivor.py" />
<Compile Include="Game\Game.py" />
Expand Down
7 changes: 1 addition & 6 deletions SBA_Serv/Tests/test_asteroidminer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ spawn_time_max = 10
spawn_on_player_num = 1
spawn_on_player_start = true
spawn_on_player_respawn = false

[Game]
game = AsteroidMiner

[AsteroidMiner]
points_torpedo = 2
points_ship = 1
points_ram = 1
7 changes: 1 addition & 6 deletions SBA_Serv/basicgame_asteroid.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,5 @@ spawn_time_max = 20
spawn_on_player_num = 1
spawn_on_player_start = true
spawn_on_player_respawn = false

[Game]
game = AsteroidMiner

[AsteroidMiner]
points_torpedo = 2
points_ship = 1
points_ram = 1
4 changes: 4 additions & 0 deletions SBA_Serv/basicgame_dragonslair.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ spawn_keep_min = 12
spawn_keep_max = 12
move_speed_min = 10
move_speed_max = 50
points_torpedo = 2
points_ram = 0

[Dragon]
number = 6
Expand All @@ -26,6 +28,8 @@ attack_time_min = 0.5
attack_time_max = 1.5
attack_amount_min = 15
attack_amount_max = 25
points_torpedo = 8
points_ram = 0

[Game]
game = Survivor
Expand Down
2 changes: 1 addition & 1 deletion SBA_Serv/buildnum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1119
1120
Loading

0 comments on commit 293cd37

Please sign in to comment.