combined start and lobby frame
This commit is contained in:
115
client.py
115
client.py
@@ -14,7 +14,6 @@ class ClientVCKO(wx.App):
|
||||
self.game_id = ""
|
||||
self.game = None
|
||||
self.debug_frame = DebugFrame(self)
|
||||
self.start_frame = StartFrame(self)
|
||||
self.lobby_frame = LobbyFrame(self)
|
||||
self.game_frame = GameFrame(self)
|
||||
self.last_lobby_state = ""
|
||||
@@ -34,7 +33,6 @@ class ClientVCKO(wx.App):
|
||||
if full_command[1] == "joined" and len(full_command) == 3:
|
||||
self.player_id = full_command[2]
|
||||
self.in_lobby = True
|
||||
self.start_frame.enter_lobby(None)
|
||||
elif full_command[1] == "state":
|
||||
json_response = ' '.join(full_command[2:])
|
||||
new_lobby_state = json.loads(json_response)
|
||||
@@ -114,23 +112,55 @@ class LobbyFrame(wx.Frame):
|
||||
def __init__(self, app):
|
||||
super().__init__(parent=None, title='VCK Online Lobby', size=Constants.medium_window_size)
|
||||
self.app = app
|
||||
|
||||
self.timer_interval = 500
|
||||
self.timer = wx.Timer(self)
|
||||
self.Bind(wx.EVT_TIMER, self.get_lobby_status, self.timer)
|
||||
self.timer.Start(self.timer_interval)
|
||||
|
||||
self.panel = wx.Panel(self)
|
||||
self.vertical_sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
|
||||
splitter = wx.SplitterWindow(self.panel)
|
||||
|
||||
left_panel = wx.Panel(splitter)
|
||||
left_sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
text = wx.StaticText(left_panel, label='Enter name:')
|
||||
self.name_field = wx.TextCtrl(left_panel, style=wx.TE_PROCESS_ENTER, value='')
|
||||
submit_button = wx.Button(left_panel, label='Submit')
|
||||
submit_button.Bind(wx.EVT_BUTTON, self.on_submit)
|
||||
self.name_field.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter)
|
||||
left_sizer.Add(text, 0, wx.ALL, 5)
|
||||
left_sizer.Add(self.name_field, 0, wx.EXPAND | wx.ALL, 5)
|
||||
left_sizer.Add(submit_button, 0, wx.ALL | wx.CENTER, 5)
|
||||
left_panel.SetSizer(left_sizer)
|
||||
|
||||
self.last_lobby_state = []
|
||||
self.current_player_index = None
|
||||
|
||||
# Create the list control and columns
|
||||
self.list_ctrl = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
|
||||
right_panel = wx.Panel(splitter)
|
||||
self.list_ctrl = wx.ListCtrl(right_panel, style=wx.LC_REPORT)
|
||||
self.list_ctrl.InsertColumn(0, "Player Name")
|
||||
self.list_ctrl.InsertColumn(1, "Ready Status", format=wx.LIST_FORMAT_RIGHT)
|
||||
self.get_lobby_status()
|
||||
# Create the ready button
|
||||
ready_button = wx.Button(self.panel, label="Ready Up")
|
||||
ready_button = wx.Button(right_panel, label="Ready Up")
|
||||
ready_button.Bind(wx.EVT_BUTTON, self.on_ready_up)
|
||||
self.list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.highlight_current_player)
|
||||
# Add the list control and ready button to the vertical sizer
|
||||
self.vertical_sizer.Add(self.list_ctrl, 1, wx.ALL | wx.EXPAND, 5)
|
||||
self.vertical_sizer.Add(ready_button, 0, wx.ALL | wx.CENTER, 5)
|
||||
right_sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
right_sizer.Add(self.list_ctrl, 1, wx.ALL | wx.EXPAND, 5)
|
||||
right_sizer.Add(ready_button, 0, wx.ALL | wx.CENTER, 5)
|
||||
right_panel.SetSizer(right_sizer)
|
||||
|
||||
splitter.SplitVertically(left_panel, right_panel)
|
||||
splitter.SetMinimumPaneSize(250)
|
||||
splitter.SetSashGravity(0.0)
|
||||
|
||||
self.vertical_sizer.Add(splitter, 1, wx.EXPAND)
|
||||
self.panel.SetSizer(self.vertical_sizer)
|
||||
|
||||
self.SetMinSize(Constants.small_window_size)
|
||||
|
||||
# Bind the size event to adjust the column widths
|
||||
@@ -141,6 +171,7 @@ class LobbyFrame(wx.Frame):
|
||||
self.timer = wx.Timer(self)
|
||||
self.Bind(wx.EVT_TIMER, self.get_lobby_status, self.timer)
|
||||
self.timer.Start(self.timer_interval)
|
||||
self.Show()
|
||||
|
||||
def on_size(self, event):
|
||||
# Calculate the width of each column based on the width of the list control
|
||||
@@ -150,8 +181,35 @@ class LobbyFrame(wx.Frame):
|
||||
self.list_ctrl.SetColumnWidth(1, col_width)
|
||||
event.Skip()
|
||||
|
||||
def on_submit(self, event):
|
||||
name = self.name_field.GetValue()
|
||||
if not name:
|
||||
print("You didn't enter anything!")
|
||||
else:
|
||||
# Check if the player has already joined the lobby
|
||||
player_exists = False
|
||||
for player in self.last_lobby_state:
|
||||
if player['player_id'] == self.app.player_id:
|
||||
player_exists = True
|
||||
break
|
||||
if player_exists:
|
||||
# If the player already exists, rename them
|
||||
self.app.parse_response(send(f"lobby rename {self.app.player_id} {name}"))
|
||||
else:
|
||||
# If the player doesn't exist, join the lobby
|
||||
self.app.parse_response(send(f"lobby join {name}"))
|
||||
self.name_field.SetValue("")
|
||||
|
||||
def on_text_enter(self, event):
|
||||
self.on_submit(event)
|
||||
|
||||
def api_call(self, message):
|
||||
if connection_check():
|
||||
self.app.parse_response(send(message))
|
||||
self.name_field.SetValue("")
|
||||
|
||||
def get_lobby_status(self, event=None):
|
||||
if self.app.in_lobby and connection_check():
|
||||
if connection_check():
|
||||
self.app.parse_response(send(f"lobby get_status {self.app.player_id}"))
|
||||
if self.app.lobby == self.last_lobby_state:
|
||||
# If the current lobby state is the same as the last one, don't update the list control
|
||||
@@ -197,49 +255,6 @@ class LobbyFrame(wx.Frame):
|
||||
self.Destroy()
|
||||
|
||||
|
||||
class StartFrame(wx.Frame):
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent=None, title='Enter Name', size=Constants.small_window_size)
|
||||
self.panel = wx.Panel(self)
|
||||
self.app = parent
|
||||
# Create text field with suggestion text
|
||||
text = wx.StaticText(self.panel, label='Enter name:')
|
||||
self.name_field = wx.TextCtrl(self.panel, style=wx.TE_PROCESS_ENTER, value='')
|
||||
|
||||
# Create submit button
|
||||
submit_button = wx.Button(self.panel, label='Submit')
|
||||
submit_button.Bind(wx.EVT_BUTTON, self.on_submit)
|
||||
self.name_field.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter)
|
||||
|
||||
# Add text field and submit button to vertical sizer
|
||||
vertical_sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
vertical_sizer.Add(text, 0, wx.ALL, 5)
|
||||
vertical_sizer.Add(self.name_field, 0, wx.EXPAND | wx.ALL, 5)
|
||||
vertical_sizer.Add(submit_button, 0, wx.ALL | wx.CENTER, 5)
|
||||
|
||||
self.panel.SetSizer(vertical_sizer)
|
||||
self.Show()
|
||||
|
||||
def on_submit(self, event):
|
||||
message = self.name_field.GetValue()
|
||||
if not message:
|
||||
print("You didn't enter anything!")
|
||||
else:
|
||||
self.api_call(f"lobby join {message}")
|
||||
|
||||
def on_text_enter(self, event):
|
||||
self.on_submit(event)
|
||||
|
||||
def enter_lobby(self, event):
|
||||
self.app.lobby_frame.Show()
|
||||
self.Hide()
|
||||
|
||||
def api_call(self, message):
|
||||
if connection_check():
|
||||
self.app.parse_response(send(message))
|
||||
self.name_field.SetValue("")
|
||||
|
||||
|
||||
class DebugFrame(wx.Frame):
|
||||
def __init__(self, app):
|
||||
super().__init__(parent=None, title='VCKO Debug Console', size=Constants.small_window_size)
|
||||
|
||||
@@ -601,7 +601,7 @@ class Game:
|
||||
case 'choose 1':
|
||||
choice = [second_word, third_word]
|
||||
case 'choose 2':
|
||||
choice = [fourth_word, fifth_word]
|
||||
choice = [fourth_word, split_command[4]]
|
||||
case 'choose 3':
|
||||
choice = [split_command[5], split_command[6]] # [sixth_word, seventh_word]
|
||||
case _:
|
||||
@@ -675,6 +675,7 @@ class Game:
|
||||
player.magic_score = player.magic_score - mp
|
||||
player.owned_domains.append(domain_stack.pop(-1))
|
||||
domain_stack[-1].toggle_accessibility(True)
|
||||
|
||||
def action_phase(self):
|
||||
return
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ class Constants:
|
||||
port = 8328
|
||||
text_format = "utf-8"
|
||||
small_window_size = (300, 150)
|
||||
medium_window_size = (300, 500)
|
||||
medium_window_size = (600, 400)
|
||||
large_window_size = (1440, 900)
|
||||
header_size = 512
|
||||
buffer_size = 4096
|
||||
|
||||
35
server.py
35
server.py
@@ -58,6 +58,14 @@ class ServerVCKO:
|
||||
self.lobby.append(joining_player)
|
||||
message = f"lobby joined {joining_player_id}"
|
||||
send_data(conn, message.encode(Constants.encoding))
|
||||
elif full_command[1] == "rename" and len(full_command) > 3:
|
||||
for player in self.lobby:
|
||||
if player.player_id == full_command[2]:
|
||||
player.name = ' '.join(full_command[3:])
|
||||
message = f"lobby renamed {player.player_id}"
|
||||
send_data(conn, message.encode(Constants.encoding))
|
||||
else:
|
||||
send_data(conn, "invalid message".encode(Constants.encoding))
|
||||
elif full_command[1] == "leave" and len(full_command) > 2:
|
||||
temp_lobby = []
|
||||
for player in self.lobby:
|
||||
@@ -65,22 +73,19 @@ class ServerVCKO:
|
||||
temp_lobby.append(player)
|
||||
self.lobby = temp_lobby
|
||||
self.send_lobby_state(conn)
|
||||
elif full_command[1] == "get_status" and len(full_command) == 3:
|
||||
elif full_command[1] == "get_status" and len(full_command) >= 2:
|
||||
found = False
|
||||
for player in self.lobby:
|
||||
if full_command[2] == player.player_id:
|
||||
player.last_active_time = time.time() # update last active time
|
||||
self.send_lobby_state(conn)
|
||||
found = True
|
||||
for player in self.gamers:
|
||||
if full_command[2] == player.player_id:
|
||||
message = f"game joined {player.game_id}"
|
||||
send_data(conn, message.encode(Constants.encoding))
|
||||
found = True
|
||||
# this only runs if we somehow receive an invalid player id
|
||||
if not found:
|
||||
message = f"Unable to find any player with player id: {full_command[2]}"
|
||||
send_data(conn, message.encode(Constants.encoding))
|
||||
if len(full_command) == 3:
|
||||
for player in self.lobby:
|
||||
if full_command[2] == player.player_id:
|
||||
player.last_active_time = time.time() # update last active time
|
||||
self.send_lobby_state(conn)
|
||||
for player in self.gamers:
|
||||
if full_command[2] == player.player_id:
|
||||
message = f"game joined {player.game_id}"
|
||||
send_data(conn, message.encode(Constants.encoding))
|
||||
else:
|
||||
self.send_lobby_state(conn)
|
||||
elif full_command[1] == "ready" and len(full_command) > 2:
|
||||
ready_check = 0
|
||||
for player in self.lobby:
|
||||
|
||||
18
vckonline.py
18
vckonline.py
@@ -8,24 +8,6 @@ player_list = [player1, player2]
|
||||
try:
|
||||
base1_new_game_state = load_game_data(str(uuid.uuid4()), "base1", player_list)
|
||||
game = Game(base1_new_game_state)
|
||||
game.hire_citizen(player1_id, 1, 0, 0)
|
||||
game.hire_citizen(player1_id, 3, 0, 0)
|
||||
game.hire_citizen(player1_id, 4, 0, 0)
|
||||
game.hire_citizen(player1_id, 5, 0, 0)
|
||||
game.hire_citizen(player1_id, 6, 0, 0)
|
||||
game.hire_citizen(player1_id, 7, 0, 0)
|
||||
game.hire_citizen(player1_id, 8, 0, 0)
|
||||
game.hire_citizen(player1_id, 9, 0, 0)
|
||||
game.hire_citizen(player1_id, 10, 0, 0)
|
||||
game.hire_citizen(player2_id, 1, 0, 0)
|
||||
game.hire_citizen(player2_id, 3, 0, 0)
|
||||
game.hire_citizen(player2_id, 4, 0, 0)
|
||||
game.hire_citizen(player2_id, 5, 0, 0)
|
||||
game.hire_citizen(player2_id, 6, 0, 0)
|
||||
game.hire_citizen(player2_id, 7, 0, 0)
|
||||
game.hire_citizen(player2_id, 8, 0, 0)
|
||||
game.hire_citizen(player2_id, 9, 0, 0)
|
||||
game.hire_citizen(player2_id, 10, 0, 0)
|
||||
game.play_turn()
|
||||
game_json = json.dumps(game, cls=GameObjectEncoder, indent=2)
|
||||
with open("game_state.txt", "w") as dump:
|
||||
|
||||
Reference in New Issue
Block a user