Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

company.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. from django.db import models
  2. from django.core.validators import MaxValueValidator, MinValueValidator
  3. import random
  4. from mygame.backend.event import SpecialOrder
  5. from .worker import Worker
  6. from .order import Order
  7. from .event import Event
  8. from .action import *
  9. # ACTION NOCH IMPORTIEREN?
  10. # company die jeder spieler steuert
  11. class Company(models.Model):
  12. name = models.CharField(default="A company", max_length=20)
  13. user = models.CharField(default="default User", max_length=20)
  14. customersatisfaction = models.IntegerField(
  15. default=100, validators=[MinValueValidator(0), MaxValueValidator(100)]
  16. )
  17. workersatisfaction = models.IntegerField(
  18. default=100, validators=[MinValueValidator(0), MaxValueValidator(100)]
  19. )
  20. money = models.IntegerField(default=100000)
  21. workermarket = models.ManyToManyField(Worker, related_name="workermarket")
  22. actions = models.ManyToManyField("Action", blank=True)
  23. workers = models.ManyToManyField(Worker, blank=True, related_name="workers")
  24. orders = models.ManyToManyField(Order, blank=True)
  25. events = models.ManyToManyField(Event, blank=True)
  26. def __str__(self):
  27. return self.name
  28. def addProfit(self, profit):
  29. self.money += profit
  30. self.save()
  31. def clearEvents(self):
  32. for event in self.events.all():
  33. # wenn specialorder ignoriert wurde -> penalty und removen (nicht löschen)
  34. if isinstance(event, SpecialOrder):
  35. for order in self.orders.all():
  36. if order.specialorder == event:
  37. event.handle(order=order, company=self, handle=False)
  38. self.events.remove(event)
  39. else:
  40. event.delete()
  41. def addEvent(self, event):
  42. self.events.add(event)
  43. self.save()
  44. def getEvent(self, eventnumber, direction):
  45. # wenn keine events gib leeres event
  46. if not self.events.exists():
  47. event = Event.createEmpty()
  48. return (event, eventnumber)
  49. # bei klick auf eventbutton immer erstes event zeigen
  50. if eventnumber == 0 and direction == "default":
  51. event = self.events.order_by("id")[eventnumber]
  52. elif direction == "previous":
  53. # wenn bei erstem element zurück dann letztes element zeigen
  54. if eventnumber <= 0:
  55. eventnumber = self.events.count() - 1
  56. event = self.events.order_by("id")[eventnumber]
  57. else:
  58. eventnumber -= 1
  59. event = self.events.order_by("id")[eventnumber]
  60. elif direction == "next":
  61. # wenn bei letztem element weiter dann erstes anzeigen
  62. if eventnumber >= (self.events.count() - 1):
  63. eventnumber = 0
  64. event = self.events.order_by("id")[eventnumber]
  65. else:
  66. eventnumber += 1
  67. event = self.events.order_by("id")[eventnumber]
  68. return (event, eventnumber)
  69. def addAction(self, action):
  70. for existingaction in self.actions.all():
  71. if action.name == existingaction.name:
  72. self.cancelAction(existingaction.id)
  73. self.actions.add(action)
  74. def deleteAction(self, action):
  75. self.actions.delete(action)
  76. def calculateCustomerSatisfaction(self, number):
  77. calc = self.customersatisfaction
  78. calc += number
  79. if calc < 0:
  80. calc = 0
  81. if calc > 100:
  82. calc = 100
  83. self.customersatisfaction = calc
  84. self.save()
  85. def calculateWorkerSatisfaction(self, number):
  86. if (self.workersatisfaction + number) > 100:
  87. self.workersatisfaction = 100
  88. elif (self.workersatisfaction + number) < 0:
  89. self.workersatisfaction = 0
  90. else:
  91. self.workersatisfaction += number
  92. self.save()
  93. def calculateAllWorkersHappyness(self, addhappyness):
  94. for worker in self.workers.all():
  95. worker.calculateHappyness(addhappyness)
  96. def paySalary(self):
  97. salarysum = 0
  98. for worker in self.workers.all():
  99. if self.money < salarysum:
  100. worker.calculateHappyness(-50)
  101. else:
  102. salarysum += worker.salary
  103. self.money -= salarysum
  104. self.save()
  105. def checkWorkerQuit(self):
  106. threshhold = 55
  107. for worker in self.workers.all():
  108. if worker.happyness <= threshhold:
  109. diff = threshhold - worker.happyness
  110. randnum = random.randint(1, 100)
  111. if 2 * diff > randnum:
  112. event = Event.createWorkerQuit(
  113. threshhold=threshhold,
  114. workername=worker.name,
  115. workerhappyness=worker.happyness,
  116. )
  117. self.addEvent(event)
  118. worker.quitJob()
  119. def calculateOrderWorkload(self):
  120. for order in self.orders.all():
  121. finished = order.calculateWorkload(self.workersatisfaction)
  122. if finished:
  123. order.finishOrder(self)
  124. event = Event.createOrderFinished(
  125. ordername=order.name, profit=str(order.profit)
  126. )
  127. self.events.add(event)
  128. order.delete()
  129. elif order.maxRounds == 0:
  130. order.cancelOrder()
  131. self.calculateCustomerSatisfaction(-30)
  132. event = Event.createOrderFailed(ordername=order.name)
  133. self.events.add(event)
  134. order.delete()
  135. def addOrder(self, order):
  136. self.orders.add(order)
  137. def deleteOrder(self, order):
  138. self.orders.remove(order)
  139. def addToWorkermarket(self, worker):
  140. self.workermarket.add(worker)
  141. def removeFromWorkermarket(self, worker):
  142. self.workermarket.remove(worker)
  143. def generateWorkermarket(self):
  144. self.clearWorkermarket()
  145. for x in range(5):
  146. worker = Worker.createRandom()
  147. self.addToWorkermarket(worker)
  148. def clearWorkermarket(self):
  149. for worker in self.workermarket.all():
  150. if worker.status == 0:
  151. worker.delete()
  152. self.workermarket.clear()
  153. # actions:
  154. def cancelAction(self, id):
  155. action = Action.objects.get(id=id)
  156. action.cancel()
  157. if action:
  158. action.delete()
  159. def actionEmploy(self, worker):
  160. act = ActionEmploy.create(worker=worker, actionmaker=self)
  161. return act
  162. def actionUnemploy(self, worker):
  163. act = ActionUnemploy.create(worker=worker, actionmaker=self)
  164. return act
  165. def actionOffer(self, order, amount):
  166. act = ActionOffer.create(order=order, amount=amount, actionmaker=self)
  167. return act
  168. def actionCancelOrder(self, order):
  169. act = ActionCancelOrder.create(order=order, actionmaker=self)
  170. return act
  171. def actionTrain(self, worker, trainskill):
  172. act = ActionTrain.create(worker=worker, trainskill=trainskill, actionmaker=self)
  173. return act