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.

views.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. from django.shortcuts import get_object_or_404, render
  2. from django.http import HttpResponseRedirect
  3. from django.urls import reverse
  4. from mygame.forms import *
  5. from django.contrib.auth.models import User
  6. from django.contrib.auth import authenticate, login
  7. from django.shortcuts import redirect
  8. import time
  9. from mygame.backend.engineeringmodel import EngineeringModel
  10. from .backend.game import Game
  11. from .backend.timer import Timer
  12. from .backend.company import Company
  13. from .backend.worker import Worker
  14. from .backend.order import Order
  15. from .backend.action import Action
  16. from .backend.game import Game
  17. from .backend.engineeringmodel import EngineeringModel
  18. from .backend.event import Event, SpecialOrder
  19. def loginView(request):
  20. if request.method == "POST":
  21. try:
  22. form = LoginForm(request.POST)
  23. if form.is_valid():
  24. username = form.cleaned_data["username"]
  25. gameid = form.cleaned_data["game_id"]
  26. password = form.cleaned_data["password"]
  27. choice = form.cleaned_data["create_join_game"]
  28. # Create oder Join angewählt?
  29. if choice == "1":
  30. lobby = Game.objects.get(id=int(gameid))
  31. else:
  32. timer = Timer.objects.create()
  33. lobby, created = Game.objects.get_or_create(
  34. name=username + "'s Game", gamemaster=username
  35. )
  36. lobby.timer = timer
  37. lobby.save()
  38. gameid = lobby.id
  39. # Checken ob username existiert:
  40. # Wenn Ja und passwort stimmt -> einloggen
  41. # Wenn Ja und passwort flasch -> Fehler
  42. # Wenn nein -> username mit passwort erstellen
  43. if User.objects.filter(username=username).exists():
  44. user = authenticate(username=username, password=password)
  45. if user is not None:
  46. login(request, user)
  47. else:
  48. raise NameError("Username already exists!")
  49. else:
  50. user = User.objects.create_user(
  51. username=username, password=password
  52. )
  53. login(request, user)
  54. company, created = Company.objects.get_or_create(
  55. name=username, user=username
  56. )
  57. lobby.participants.add(company)
  58. lobby.save()
  59. except:
  60. error_message = "FEHLER AUFGETRETEN"
  61. else:
  62. return HttpResponseRedirect(
  63. reverse(
  64. "lobby",
  65. args=(
  66. gameid,
  67. username,
  68. ),
  69. )
  70. )
  71. return render(
  72. request,
  73. "mygame/login.html",
  74. {
  75. "error_message": error_message,
  76. "form": form,
  77. },
  78. )
  79. else:
  80. form = LoginForm()
  81. return render(request, "mygame/login.html", {"form": form})
  82. def lobby(request, gameid, username):
  83. game = get_object_or_404(Game, id=gameid)
  84. if request.method == "POST":
  85. if request.POST.get("action", False) == "clearlobby":
  86. game.clearParticipants(username)
  87. if request.POST.get("action", False) == "changesettings":
  88. gamesettingsform = GamesettingsForm(
  89. request.POST,
  90. initial={
  91. "timer": game.timer.timer,
  92. "rounds": game.maxRounds,
  93. "ordermarketsize": game.ordermarketsize,
  94. },
  95. )
  96. try:
  97. if gamesettingsform.is_valid():
  98. game.maxRounds = gamesettingsform.cleaned_data["rounds"]
  99. game.ordermarketsize = gamesettingsform.cleaned_data[
  100. "ordermarketsize"
  101. ]
  102. game.timer.timer = gamesettingsform.cleaned_data["timer"]
  103. game.timer.remainingtimer = gamesettingsform.cleaned_data["timer"]
  104. game.save()
  105. game.timer.save()
  106. except (KeyError, game.DoesNotExist):
  107. return render(
  108. request,
  109. "mygame/lobby.html",
  110. {
  111. "lobby": game,
  112. "gamesettingsform": gamesettingsform,
  113. "error_message": "EIN FEHLER IST AUFGETRETEN.",
  114. },
  115. )
  116. # hier errormessages je nach fehler ausgeben
  117. else:
  118. return HttpResponseRedirect(
  119. reverse(
  120. "lobby",
  121. args=(
  122. gameid,
  123. username,
  124. ),
  125. )
  126. )
  127. gamesettingsform = GamesettingsForm(
  128. initial={
  129. "timer": game.timer.timer,
  130. "rounds": game.maxRounds,
  131. "ordermarketsize": game.ordermarketsize,
  132. }
  133. )
  134. return render(
  135. request,
  136. "mygame/lobby.html",
  137. {"game": game, "gamesettingsform": gamesettingsform},
  138. )
  139. def gamerules(request, gameid):
  140. game = get_object_or_404(Game, id=gameid)
  141. return render(
  142. request,
  143. "mygame/gamerules.html",
  144. {
  145. "game": game,
  146. },
  147. )
  148. def startGame(request, gameid, username):
  149. game = get_object_or_404(Game, id=gameid)
  150. game.startGame()
  151. return redirect("company", gameid=gameid, username=username)
  152. def company(request, gameid, username):
  153. game = get_object_or_404(Game, id=gameid)
  154. company = get_object_or_404(Company, user=username)
  155. if request.method == "POST":
  156. if request.POST["action"] == "next round":
  157. gameend = game.processGameround()
  158. if gameend:
  159. return redirect("standings", gameid=gameid, username=username)
  160. else:
  161. eventtuple = company.getEvent(0, "default")
  162. companyevent = eventtuple[0]
  163. eventnumber = eventtuple[1]
  164. return render(
  165. request,
  166. "mygame/email.html",
  167. {
  168. "game": game,
  169. "company": company,
  170. "companyevent": companyevent,
  171. "eventnumber": eventnumber,
  172. },
  173. )
  174. return render(request, "mygame/company.html", {"game": game, "company": company})
  175. def companyWorker(request, gameid, username, workerid):
  176. game = get_object_or_404(Game, id=gameid)
  177. company = get_object_or_404(Company, user=username)
  178. try:
  179. viewedworker = Worker.objects.get(id=workerid)
  180. except:
  181. return render(
  182. request,
  183. "mygame/company.html",
  184. {"game": game, "company": company},
  185. )
  186. if request.method == "POST":
  187. if request.POST["action"] == "hire":
  188. viewedaction = company.actionEmploy(viewedworker)
  189. return render(
  190. request,
  191. "mygame/action.html",
  192. {"game": game, "company": company, "viewedaction": viewedaction},
  193. )
  194. elif request.POST["action"] == "fire":
  195. viewedaction = company.actionUnemploy(viewedworker)
  196. return render(
  197. request,
  198. "mygame/action.html",
  199. {"game": game, "company": company, "viewedaction": viewedaction},
  200. )
  201. elif request.POST["action"] == "train":
  202. return companyTraining(request, gameid, username, workerid)
  203. return render(
  204. request,
  205. "mygame/worker.html",
  206. {"game": game, "company": company, "viewedworker": viewedworker},
  207. )
  208. def companyOrder(request, gameid, username, orderid):
  209. game = get_object_or_404(Game, id=gameid)
  210. company = get_object_or_404(Company, user=username)
  211. try:
  212. viewedorder = get_object_or_404(Order, id=orderid)
  213. except:
  214. return render(
  215. request,
  216. "mygame/company.html",
  217. {"game": game, "company": company},
  218. )
  219. if request.method == "POST":
  220. if request.POST["action"] == "offer":
  221. try:
  222. amount = int(request.POST["offerfield"])
  223. acceptoffer = viewedorder.checkOffer(amount)
  224. if acceptoffer == False:
  225. errormessage = "you can only offer 20 percent higher or lower than the estimated profit"
  226. return render(
  227. request,
  228. "mygame/order.html",
  229. {
  230. "game": game,
  231. "company": company,
  232. "viewedorder": viewedorder,
  233. "errormessage": errormessage,
  234. },
  235. )
  236. except:
  237. errormessage = "please enter numbers only"
  238. return render(
  239. request,
  240. "mygame/order.html",
  241. {
  242. "game": game,
  243. "company": company,
  244. "viewedorder": viewedorder,
  245. "errormessage": errormessage,
  246. },
  247. )
  248. else:
  249. viewedaction = company.actionOffer(order=viewedorder, amount=amount)
  250. return render(
  251. request,
  252. "mygame/action.html",
  253. {"game": game, "company": company, "viewedaction": viewedaction},
  254. )
  255. if request.POST["action"] == "assign resources":
  256. return render(
  257. request,
  258. "mygame/assign.html",
  259. {"game": game, "company": company, "assignorder": viewedorder},
  260. )
  261. if request.POST["action"] == "cancel":
  262. viewedaction = company.actionCancelOrder(viewedorder)
  263. return render(
  264. request,
  265. "mygame/action.html",
  266. {"game": game, "company": company, "viewedaction": viewedaction},
  267. )
  268. if request.POST["action"] == "save assignment":
  269. for worker in company.workers.all():
  270. workerid = request.POST.get("assign " + str(worker.id), 0)
  271. if workerid != 0:
  272. viewedorder.assignWorker(worker)
  273. viewedorder.save()
  274. workerid = request.POST.get("unassign " + str(worker.id), 0)
  275. if workerid != 0:
  276. viewedorder.unassignWorker(worker)
  277. viewedorder.save()
  278. return render(
  279. request,
  280. "mygame/assign.html",
  281. {"game": game, "company": company, "assignorder": viewedorder},
  282. )
  283. return render(
  284. request,
  285. "mygame/order.html",
  286. {"game": game, "company": company, "viewedorder": viewedorder},
  287. )
  288. def companyTraining(request, gameid, username, workerid):
  289. game = get_object_or_404(Game, id=gameid)
  290. company = get_object_or_404(Company, user=username)
  291. try:
  292. trainworker = get_object_or_404(Worker, id=workerid)
  293. except:
  294. return render(
  295. request,
  296. "mygame/company.html",
  297. {"game": game, "company": company},
  298. )
  299. if request.method == "POST":
  300. trainskill = request.POST.get("trainskill", False)
  301. if trainskill:
  302. viewedaction = company.actionTrain(
  303. worker=trainworker, trainskill=trainskill
  304. )
  305. return render(
  306. request,
  307. "mygame/action.html",
  308. {"game": game, "company": company, "viewedaction": viewedaction},
  309. )
  310. return render(
  311. request,
  312. "mygame/training.html",
  313. {"game": game, "company": company, "trainworker": trainworker},
  314. )
  315. def companyAction(request, gameid, username, actionid):
  316. game = get_object_or_404(Game, id=gameid)
  317. company = get_object_or_404(Company, user=username)
  318. # hier schauen welche action es ist dann aus entsprechender table holen
  319. try:
  320. viewedaction = get_object_or_404(Action, id=actionid)
  321. except:
  322. return render(
  323. request,
  324. "mygame/company.html",
  325. {"game": game, "company": company},
  326. )
  327. if request.method == "POST":
  328. if request.POST["action"] == "cancel":
  329. company.cancelAction(id=actionid)
  330. return render(
  331. request,
  332. "mygame/company.html",
  333. {"game": game, "company": company},
  334. )
  335. return render(
  336. request,
  337. "mygame/action.html",
  338. {"game": game, "company": company, "viewedaction": viewedaction},
  339. )
  340. def companyEvent(request, gameid, username, eventnumber):
  341. game = get_object_or_404(Game, id=gameid)
  342. company = get_object_or_404(Company, user=username)
  343. if request.method == "POST":
  344. if request.POST.get("action", False) == "previous":
  345. eventresult = company.getEvent(eventnumber, "previous")
  346. if request.POST.get("action", False) == "next":
  347. eventresult = company.getEvent(eventnumber, "next")
  348. else:
  349. eventresult = company.getEvent(eventnumber, "default")
  350. companyevent = eventresult[0]
  351. eventnumber = eventresult[1]
  352. return render(
  353. request,
  354. "mygame/email.html",
  355. {
  356. "game": game,
  357. "company": company,
  358. "companyevent": companyevent,
  359. "eventnumber": eventnumber,
  360. },
  361. )
  362. def companyEventSetModel(request, gameid, username, eventid, modelnumber):
  363. game = get_object_or_404(Game, id=gameid)
  364. company = get_object_or_404(Company, user=username)
  365. engineeringmodels = EngineeringModel.objects.all()
  366. if request.method == "POST":
  367. if request.POST.get("action", False) == "previous":
  368. modelnumber -= 1
  369. if modelnumber < 0:
  370. modelnumber = engineeringmodels.count() - 1
  371. engineeringmodel = engineeringmodels[modelnumber]
  372. if request.POST.get("action", False) == "next":
  373. modelnumber += 1
  374. if modelnumber > (engineeringmodels.count() - 1):
  375. modelnumber = 0
  376. if request.POST.get("action", False) == "set model":
  377. eventofferaccepted = Event.objects.get(id=eventid)
  378. setmodel = engineeringmodels.order_by("id")[modelnumber]
  379. eventofferaccepted.setModel(setmodel)
  380. return render(
  381. request,
  382. "mygame/email.html",
  383. {
  384. "game": game,
  385. "company": company,
  386. "companyevent": eventofferaccepted,
  387. "eventnumber": 0,
  388. },
  389. )
  390. engineeringmodel = engineeringmodels.order_by("id")[modelnumber]
  391. return render(
  392. request,
  393. "mygame/engineeringmodel.html",
  394. {
  395. "game": game,
  396. "company": company,
  397. "engineeringmodel": engineeringmodel,
  398. "modelnumber": modelnumber,
  399. "eventid": eventid,
  400. },
  401. )
  402. def companySpecialOrder(request, gameid, username, specialorderid):
  403. game = get_object_or_404(Game, id=gameid)
  404. company = get_object_or_404(Company, user=username)
  405. if request.method == "POST":
  406. specialorder = SpecialOrder.objects.get(id=specialorderid)
  407. for order in company.orders.all():
  408. if order.specialorder.id == specialorderid:
  409. order = order
  410. if request.POST.get("action", False) == "handle":
  411. event = specialorder.handle(order=order, company=company, handle=True)
  412. if request.POST.get("action", False) == "ignore":
  413. event = specialorder.handle(order=order, company=company, handle=False)
  414. return render(
  415. request,
  416. "mygame/email.html",
  417. {"game": game, "company": company, "companyevent": event, "eventnumber": 0},
  418. )
  419. def standings(request, gameid, username):
  420. game = get_object_or_404(Game, id=gameid)
  421. company = get_object_or_404(Company, user=username)
  422. leaderboard = game.createLeaderboard()
  423. return render(
  424. request,
  425. "mygame/standings.html",
  426. {"game": game, "company": company, "leaderboard": leaderboard},
  427. )