Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
T_TRIGGER_ACTION_MANUAL = "manual action"
T_TRIGGER_ACTION_SINGLE = "single action"
T_TRIGGER_ACTION_DOUBLE = "double action"
T_AMMO_9x18gzh = "9x18gzh"
T_AMMO_9x19para = "9x19para"
T_AMMO_45ACP = ".45ACP"
T_WEAPON_PISTOL = "pistol"
T_WEAPON_SMG = "SMG"
T_MAG_CAP_17 = 17
T_MAG_CAP_8 = 8
T_MAG_CAP_7 = 7
class Inventory(object):
def __init__(self):
self.inv = {}
def __getitem__(self, name):
return self.inv.get(name)
def __setitem__(self, name, value):
self.inv[name] = value
def incitem(self, name, amount):
self.inv[name] += amount
def deciterm(self, name, amount):
self.inv[name] -= amount
class Magazine(object):
ammotype = None
capacity = None
def __init__(self):
self.curr_capacity = 0
def push(self, amount):
rem = self.curr_capacity + amount - self.capacity
am = amount - rem
if am:
self.curr_capacity += am
return rem
def pop(self, amount):
if amount > self.curr_capacity:
amount = self.curr_capacity
self.curr_capacity -= amount
return amount
class Mag9x19_17(Magazine):
ammotype = T_AMMO_9x19para
capacity = 17
def __init__(self):
super(Mag9x19_17, self).__init__()
class Mag9x18_8(Magazine):
ammotype = T_AMMO_9x18gzh
capacity = 8
def __init__(self):
super(Mag9x18_8, self).__init__()
class Mag45ACP_7(Magazine):
ammotype = T_AMMO_45ACP
capacity = 7
def __init__(self):
super(Mag45ACP_7, self).__init__()
class Weapon(object):
type = None
ammotype = None
action = None
def __init__(self):
# Has cartridge in chamber
self.chamber_loaded = False
# Has magazine
self.mag = None
# Slide stop marker
self.slide_stop = False
# Depends from trigger system
# Double action guns can click
# without взведения курка, ебу я термины аааа
self.action_ready = False
@property
def total_cap(self):
if self.mag:
add = self.mag.curr_capacity
else:
add = 0
return int(self.chamber_loaded) + add
def pull_slide(self):
# Here we extract cartridge or shell or do nothing
self.chamber_loaded = False
if self.mag:
# Here we get cartridge from mag
if self.mag.pop(1):
self.chamber_loaded = True
else:
# No ammo in mag
self.slide_stop = True
else:
self.action_ready = True
def load_mag(self, mag):
if self.ammotype != mag.ammotype:
print("Unsupported mag type, bye")
return
self.mag = mag
def unload_mag(self):
if self.mag:
mag = self.mag
self.mag = False
return mag
else:
print("no mag")
def shot(self):
if self.chamber_loaded:
# DO A SHOT
self.chamber_loaded = False
if self.mag and self.mag.curr_capacity:
self.chamber_loaded = True
self.mag.pop(1)
else:
print("No mag, or mag is empty")
else:
if self.action == T_TRIGGER_ACTION_DOUBLE:
print("Click")
elif (
self.action in (
T_TRIGGER_ACTION_MANUAL,
T_TRIGGER_ACTION_SINGLE
) and self.trigger_ready
):
print("Click")
self.trigger_ready = False
class PistolP226(Weapon):
type = T_WEAPON_PISTOL
action = T_TRIGGER_ACTION_DOUBLE
ammotype = T_AMMO_9x19para
def __init__(self):
super(PistolP226, self).__init__()
class PistolMakarov(Weapon):
type = T_WEAPON_PISTOL
action = T_TRIGGER_ACTION_DOUBLE
ammotype = T_AMMO_9x18gzh
def __init__(self):
super(PistolMakarov, self).__init__()
class PistolColtM1911(Weapon):
type = T_WEAPON_PISTOL
action = T_TRIGGER_ACTION_SINGLE
ammotype = T_AMMO_45ACP
def __init__(self):
super(PistolColtM1911, self).__init__()
class SMGKrissVector45ACP(Weapon):
type = T_WEAPON_SMG
action = T_TRIGGER_ACTION_DOUBLE
ammotype = T_AMMO_45ACP
def __init__(self):
super(SMGKrissVector45ACP, self).__init__()
class SMGKrissVector9(Weapon):
type = T_WEAPON_SMG
action = T_TRIGGER_ACTION_DOUBLE
ammotype = T_AMMO_9x19para
def __init__(self):
super(SMGKrissVector9, self).__init__()
if __name__ == "__main__":
m1 = Mag9x19_17()
m1.push(17)
assert m1.curr_capacity == 17
assert m1.pop(20) == 17
assert m1.curr_capacity == 0
m1.push(20)
assert m1.curr_capacity == 17
p1 = PistolP226()
p1.load_mag(m1)
assert p1.mag.curr_capacity == 17
assert not p1.chamber_loaded
p1.pull_slide()
assert p1.chamber_loaded
assert p1.mag
assert p1.mag.curr_capacity == 16
p1.shot()
p1.shot()
p1.shot()
assert p1.mag.curr_capacity == 13
m1 = p1.unload_mag()
assert m1.curr_capacity == 13
assert p1.chamber_loaded
m2 = Mag9x19_17()
m2.push(17)
p1.load_mag(m2)
assert p1.total_cap == 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment