Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 172 additions & 38 deletions armies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from colors import *
from classes import *
import random

#======================================================================================
# ::Idea Board::
Expand Down Expand Up @@ -62,11 +64,23 @@ class Armies:
# xPos, yPos
# Adds a new battalion to the Armies battalions list
#==================================================================================
def AddBattalion(self, name, commander, numTroops, attLevel, speed, stamina, rations, xPos, yPos):
newBattalion = Battalion(name, commander, numTroops, attLevel, speed, stamina, rations, xPos, yPos)
def AddBattalion(self, name, commander, numTroops, attLevel, speed, stamina, rations, xPos, yPos, invGold, invFood, invWood, invStone, invOre):
newBattalion = Battalion(name, commander, numTroops, attLevel, speed, stamina, rations, xPos, yPos, invGold, invFood, invWood, invStone, invOre)
self.battalions.append(newBattalion)
self.write()
self.read()

#==================================================================================
# [ExistingName]
# parameters: self, name
# returns: True/False
# Compares passed name to other battalion names in battalions
#==================================================================================
def ExistingName(self, name):
# self.read()
for i in range(len(self.battalions)):
if str(self.battalions[i].name) == str(name):
return True
else:
return False

#==================================================================================
# [GetBattalions]
Expand All @@ -79,6 +93,38 @@ def GetBattalions(self):
batts.append(self.battalions[i].ListDetails())
return batts

#==================================================================================
# [GetBattalionData]
# parameters: self
# Gets a list of battalion coordinates
#==================================================================================
def GetBattalionData(self):
coords = []
for i in range(len(self.battalions)):
coords.append((self.battalions[i].yPos, self.battalions[i].xPos, self.battalions[i].commander, self.battalions[i].name))
return coords

#==================================================================================
# [GetCommanderByLocation]
# parameters: self
# Gets a list of battalions
#==================================================================================
def GetCommanderByLocation(self, y, x):
for i in range(len(self.battalions)):
if int(self.battalions[i].yPos) == int(y) and int(self.battalions[i].xPos) == int(x):
return self.battalions[i].commander

#==================================================================================
# [GetBattalionObjects]
# parameters: self
# Gets a list of battalions
#==================================================================================
def GetBattalionObjects(self):
batts = []
for i in range(len(self.battalions)):
batts.append(self.battalions[i])
return batts

#==================================================================================
# [GetBattalion]
# parameters: self, index
Expand All @@ -87,23 +133,65 @@ def GetBattalions(self):
def GetBattalion(self, index):
return self.battalions[index]

#==================================================================================
# [GetBattalionByName]
# parameters: self, name
# Returns a Battalion object at passed index
#==================================================================================
def GetBattalion(self, name):
for i in range(len(self.battalions)):
if self.battalions[i].name == name:
return self.battalions[i]

#==================================================================================
# [RemoveBattalion]
# parameters: self, index
# Removes Battalion at index from the battalions list.
#==================================================================================
def RemoveBattalion(self, index):
self.battalions.pop(index)
def RemoveBattalion(self, bat):
for i in range(len(self.battalions)):
if bat == self.battalions[i]:
self.battalions.pop(i)
self.write()

#==================================================================================
# [RemoveBattalion]
# parameters: self, index
# Removes Battalion at index from the battalions list.
#==================================================================================
def SetBattalionCoords(self, bat, direction):
for i in range(len(self.battalions)):
if bat == self.battalions[i]:
if direction == 'n':
self.battalions[i].yPos = str(int(self.battalions[i].yPos) - 1)
elif direction == 'ne':
self.battalions[i].yPos = str(int(self.battalions[i].yPos) - 1)
self.battalions[i].xPos = str(int(self.battalions[i].xPos) + 1)
elif direction == 'e':
self.battalions[i].xPos = str(int(self.battalions[i].xPos) + 1)
elif direction == 'se':
self.battalions[i].yPos = str(int(self.battalions[i].yPos) + 1)
self.battalions[i].xPos = str(int(self.battalions[i].xPos) + 1)
elif direction == 's':
self.battalions[i].yPos = str(int(self.battalions[i].yPos) + 1)
elif direction == 'sw':
self.battalions[i].yPos = str(int(self.battalions[i].yPos) + 1)
self.battalions[i].xPos = str(int(self.battalions[i].xPos) - 1)
elif direction == 'w':
self.battalions[i].xPos = str(int(self.battalions[i].xPos) - 1)
elif direction == 'nw':
self.battalions[i].yPos = str(int(self.battalions[i].yPos) - 1)
self.battalions[i].xPos = str(int(self.battalions[i].xPos) - 1)

self.write()

#==================================================================================
# [write]
#==================================================================================
def write(self):
armiesFile = 'armies/serverArmies.txt'
#If no file has been made yet:
try:
with open(armiesFile, 'x') as f:
with open(armiesFile, 'w') as f:
f.write(str("["))
for i in range(len(self.battalions)):
f.write(str("["))
Expand All @@ -115,32 +203,12 @@ def write(self):
f.write("('" + str(self.battalions[i].stamina) + "'), ")
f.write("('" + str(self.battalions[i].rations) + "'), ")
f.write("('" + str(self.battalions[i].xPos) + "'), ")
f.write("('" + str(self.battalions[i].yPos) + "')")
if i < len(self.battalions) - 1:
f.write(str("], "))
else:
f.write(str("]"))
f.write(str("]"))
except:
print('Could not write armies file!')
pass
#If file has already been made:
try:
print("Trying to Open")
with open(armiesFile, 'x') as f:
print("About to loop")
f.write(str("["))
for i in range(len(self.battalions)):
f.write(str("["))
f.write("('" + str(self.battalions[i].name) + "'), ")
f.write("('" + str(self.battalions[i].commander) + "'), ")
f.write("('" + str(self.battalions[i].numTroops) + "'), ")
f.write("('" + str(self.battalions[i].attLevel) + "'), ")
f.write("('" + str(self.battalions[i].speed) + "'), ")
f.write("('" + str(self.battalions[i].stamina) + "'), ")
f.write("('" + str(self.battalions[i].rations) + "'), ")
f.write("('" + str(self.battalions[i].xPos) + "'), ")
f.write("('" + str(self.battalions[i].yPos) + "')")
f.write("('" + str(self.battalions[i].yPos) + "'),")
f.write("('" + str(self.battalions[i].invGold) + "'),")
f.write("('" + str(self.battalions[i].invFood) + "'),")
f.write("('" + str(self.battalions[i].invWood) + "'),")
f.write("('" + str(self.battalions[i].invStone) + "'),")
f.write("('" + str(self.battalions[i].invOre) + "')")
if i < len(self.battalions) - 1:
f.write(str("], "))
else:
Expand All @@ -162,10 +230,10 @@ def read(self):
readArmiesFile.close()

for i in range(len(rL)):
self.AddBattalion(str(rL[i][0]), str(rL[i][1]), str(rL[i][2]), str(rL[i][3]), str(rL[i][4]), str(rL[i][5]), str(rL[i][6]), str(rL[i][7]), str(rL[i][8]))
self.AddBattalion(str(rL[i][0]), str(rL[i][1]), str(rL[i][2]), str(rL[i][3]), str(rL[i][4]), str(rL[i][5]), str(rL[i][6]), str(rL[i][7]), str(rL[i][8]), str(rL[i][9]), str(rL[i][10]), str(rL[i][11]), str(rL[i][12]), str(rL[i][13]))

except:
print('Could not read armies file!')
# print('Could not read armies file!')
pass

class Battalion:
Expand All @@ -178,8 +246,13 @@ class Battalion:
rations = "0"
xPos = "0"
yPos = "0"
invGold = "0"
invFood = "0"
invWood = "0"
invStone = "0"
invOre = "0"

def __init__(self, name, commander, numTroops, attLevel, speed, stamina, rations, xPos, yPos):
def __init__(self, name, commander, numTroops, attLevel, speed, stamina, rations, xPos, yPos, invGold, invFood, invWood, invStone, invOre):
self.name = name
self.commander = commander
self.numTroops = numTroops
Expand All @@ -189,6 +262,11 @@ def __init__(self, name, commander, numTroops, attLevel, speed, stamina, rations
self.rations = rations
self.xPos = xPos
self.yPos = yPos
self.invGold = invGold
self.invFood = invFood
self.invWood = invWood
self.invStone = invStone
self.invOre = invOre

def ListDetails(self):
name = str(" name: " + str(self.name))
Expand All @@ -200,6 +278,62 @@ def ListDetails(self):
rations = str(" rations: " + str(self.rations))
xPos = str(" xPos: " + str(self.xPos))
yPos = str(" yPos: " + str(self.yPos))
invGold = str(" Gold: " + str(self.invGold))
invFood = str(" Food: " + str(self.invFood))
invWood = str(" Wood: " + str(self.invWood))
invStone = str(" Stone: " + str(self.invStone))
invOre = str(" Ore: " + str(self.invOre))

return str(name + commander + numTroops + attLevel + speed + stamina + rations + xPos + yPos + invGold + invFood + invWood + invStone + invOre)

def MenuBar(self, userStronghold):
SHC = StrongholdColor(userStronghold.color)
name = str("{ " + SHC + str(self.name) + RESET + " }")
# commander = str(" commander: " + str(self.commander))
numTroops = str(" | Warriors: " + COLOR_WARRIOR + str(self.numTroops) + RESET)
attLevel = str(" | Attack: " + MAGENTA + str(self.attLevel) + RESET)
speed = str(" | Speed: " + LIME + str(self.speed) + RESET)
stamina = str(" | Stamina: " + YELLOW + str(self.stamina) + RESET)
# rations = str(" | Rations: " + C_FOOD + str(self.rations) + RESET)
xPos = str(" | X: " + RED_GRAY + str(self.xPos) + RESET)
yPos = str(" | Y: " + RED_GRAY + str(self.yPos) + RESET)

return str(name + numTroops + attLevel + speed + stamina + xPos + yPos)

def MenuBarWithLocation(self, userStronghold, location):
SHC = StrongholdColor(userStronghold.color)
name = str("{ " + SHC + str(self.name) + RESET + " }")
# commander = str(" commander: " + str(self.commander))
numTroops = str(" | Warriors: " + COLOR_WARRIOR + str(self.numTroops) + RESET)
attLevel = str(" | Attack: " + MAGENTA + str(self.attLevel) + RESET)
speed = str(" | Speed: " + LIME + str(self.speed) + RESET)
stamina = str(" | Stamina: " + YELLOW + str(self.stamina) + RESET)
# rations = str(" | Rations: " + C_FOOD + str(self.rations) + RESET)
# xPos = str(" | X: " + RED_GRAY + str(self.xPos) + RESET)
# yPos = str(" | Y: " + RED_GRAY + str(self.yPos) + RESET)
# loc = str(" | Location: " + RED_GRAY + location)
loc = str(" | " + location)

return str(name + numTroops + attLevel + speed + stamina + loc)

def Inventory(self):
invGold = str("| Gold: " + C_GOLD + str(self.invGold) + RESET)
invFood = str(" | Food: " + C_FOOD + str(self.invFood) + RESET)
invWood = str(" | Wood: " + C_WOOD + str(self.invWood) + RESET)
invStone = str(" | Stone: " + C_STONE + str(self.invStone) + RESET)
invOre = str(" | Ore: " + C_ORE + str(self.invOre) + RESET + " |")

return str(invGold + invFood + invWood + invStone + invOre)

def PrintGold(self):
return str(C_GOLD + str(self.invGold) + RESET)
def PrintFood(self):
return str(C_FOOD + str(self.invFood) + RESET)
def PrintWood(self):
return str(C_WOOD + str(self.invWood) + RESET)
def PrintStone(self):
return str(C_STONE + str(self.invStone) + RESET)
def PrintOre(self):
return str(C_ORE + str(self.invOre) + RESET)

return str(name + commander + numTroops + attLevel + speed + stamina + rations + xPos + yPos)

Loading