File: CFItemBroker.py

package info (click to toggle)
crossfire-maps 1.71.0-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 211,128 kB
  • sloc: python: 7,656; ansic: 2,143; perl: 744; sh: 73; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#CFItemBroker.py
#An often used bit of code to add or remove a number of objects
#Useful for removing items (like in payment or as part of
#an inventory check)  This is also useful for setting the number
#of a newly created item(s) as it will check for existing item(s) and add
#the appropriate number of new items avoiding such silliness as the
#number of the existing items being reset.
#This will not check for the existence of an item as that would better
#be done in the calling script so you can be flexible.
#
#ToddMitchell

import Crossfire

class Item:

    def __init__(self, object):
        self.object = object
        self.numberof = self.object.Quantity
        # 0 for non merging items
        if self.numberof == 0:
            self.numberof = 1

    def add(self, number):
        tmp = (self.numberof + number)-1
        self.object.Quantity=tmp
        return 1

    def subtract(self, number):
        remainder = self.numberof - number
        if remainder >= number:
            self.object.Quantity=remainder
            return 1
        elif remainder == 0:
            self.object.Remove()
            return 1
        else:
            return 0

    def quantity(self):
        return self.numberof