def _add_friend_line(self, username): ''' Add a line to the friend scroll list. ''' cadre = Cadre(DIM_CADRE_LINE, (0, 0)) text_username = TextBox(Spec.DIM_MEDIUM_TEXT, (20, 10), text=username, font=Font.f(35)) color = self._get_color_connected(username) form_connected = Form(DIM_CONN, POS_CONN, color) button_profil = Button(Spec.DIM_SMALL_BUTTON, POS_PROFIL, color=C.LIGHT_BLUE, text="Profil", font=Font.f(30)) button_play = Button(Spec.DIM_SMALL_BUTTON, POS_PLAY, color=C.LIGHT_GREEN, text="Play", font=Font.f(30)) notif = TextBox(Spec.DIM_NOTIF, POS_NOTIF, color=C.LIGHT_RED, text_color=C.WHITE, text='0', font=Font.f(30)) # add line scroll = self.get_component('s frs') scroll.add_line([ cadre, text_username, form_connected, button_profil, button_play, notif ]) # set buttons logic line = scroll.get_line(-1) button_profil.set_logic(self._get_profil_logic(line)) button_play.set_logic(self._get_play_logic(line)) # set notif state scroll.set_active_state(False, line=line, element=notif)
def _add_fr_line(self, username): ''' Add a line to the friend scroll list. ''' cadre = Cadre(DIM_FRL_CADRE, (0, 0)) text_username = TextBox(DIM_L_USERNAME, DX_L + DY_L, text=username, font=Font.f(45)) button_requests = Button(DIM_L_BUTTON, POS_L_BUTTON, color=C.LIGHT_BLUE, text='Add', font=Font.f(30)) scroll = self.get_component('s frs') scroll.add_line([cadre, text_username, button_requests]) # set buttons logic line = scroll.get_line(-1) button_requests.set_logic(self._get_request_logic(line))
def _create_msg(self, username, msg, user_color): ''' Create the TextBoxs that composed a message (ui), return two elements to be use with `ScrollList.add_line`. ''' text_username = TextBox(DIM_USER, (10, 0), font=Font.f(40), text=username, text_color=user_color, centered=False) text_msg = TextBox(DIM_MSG, (DIM_USER[0], MSG_Y), font=Font.f(30), text=msg, centered=False) return [text_username, text_msg]
def _add_game_demand_line(self, username): ''' Add a line to the game demand scroll list. ''' cadre = Cadre(DIM_CADRE_LINE_DFR, (0, 0), color=C.XLIGHT_GREY) icon = Form(DIM_IMG, POS_IMG, color=C.XLIGHT_GREY, surface=img_game, with_font=True) text_username = TextBox(Spec.DIM_MEDIUM_TEXT, POS_DFR_TEXT, color=C.XLIGHT_GREY, text=username, font=Font.f(35), centered=False) button_yes = Button(DIM_DFR_BUTT, POS_YES, color=C.LIGHT_GREEN, surface=vu, with_font=True) button_no = Button(DIM_DFR_BUTT, POS_NO, color=C.LIGHT_RED, surface=cross, with_font=True) # add line scroll = self.get_component('s dfrs') scroll.add_line([cadre, icon, text_username, button_yes, button_no]) line = scroll.get_line(-1) # set buttons logic button_yes.set_logic(self._get_demand_game_logic(line, True)) button_no.set_logic(self._get_demand_game_logic(line, False))
DIM_ICON = np.array([512, 512]) POS_ICON = (15 * DIM_DFR_BUTT - DIM_ICON) // 2 POS_IMG = np.array([10, 5]) POS_DFR_TEXT = np.array([100, 10]) POS_YES = np.array([400, 20]) POS_NO = np.array([480, 20]) DIM_CADRE_LINE_DFR = np.array([560, 80]) ### Components ### ### base ### title = TextBox(Spec.DIM_TITLE, Spec.POS_TITLE, text="Friends", font=Font.f(80)) button_back = Button(Spec.DIM_MEDIUM_BUTTON, POS_BACK, color=C.LIGHT_BLUE, text="Back", font=Font.f(35)) button_add_fr = Button(Spec.DIM_MEDIUM_BUTTON, POS_ADD_FR, color=C.LIGHT_BLUE, text="Add friend", font=Font.f(35)) scroll_frs = ScrollList(DIM_SCR_FRS, POS_SCR_FRS, [])
X_TEXT = Spec.CENTER_X - Spec.DIM_MEDIUM_TEXT[0] - X_SPACE // 2 POS_USER = np.array([X_TEXT, 600]) POS_PASS = np.array([X_TEXT, 800]) POS_INP_USER = np.array([Spec.CENTER_X + X_SPACE // 2, 600]) POS_INP_PASS = np.array([Spec.CENTER_X + X_SPACE // 2, 800]) POS_DONE = np.array([2200, 1200]) POS_BACK = np.array([100, 100]) POS_ERROR = np.array([2120, 1140]) ### Components ### title = TextBox(Spec.DIM_TITLE, Spec.POS_TITLE, text="Connection", font=Font.f(80)) button_back = Button(Spec.DIM_MEDIUM_BUTTON, POS_BACK, color=C.LIGHT_BLUE, text="Back", font=Font.f(35)) ### base ### button_login = Button(Spec.DIM_BIG_BUTTON, POS_LOG, color=C.LIGHT_BLUE, text="Login", font=Font.f(45))
DIM_CADRE_ACTIONS = np.array([400, 400]) DIM_TITLE_ACTIONS = np.array([390, 50]) POS_CADRE_ACTS1 = np.array([600, 50]) POS_CADRE_ACTS2 = np.array([2150, 50]) POS_TITLE_ACTS = np.array([5, 5]) POS_TEXT_ACTS = np.array([5, 60]) DIM_TEXT_ACTS = np.array([390, 390]) titles = ['HP', 'Shield', 'Engine', 'Speed', 'Orientation', 'Script errors'] ### components ### text_time = TextBox(Spec.DIM_MEDIUM_TEXT, POS_TIME, marge=True, color=C.XLIGHT_GREY, font=Font.f(40)) cadre_1 = Cadre(DIM_CADRE, POS_CADRE1) cadre_2 = Cadre(DIM_CADRE, POS_CADRE2) cadre_1.MARGE_WIDTH = 8 cadre_2.MARGE_WIDTH = 8 cadre_acts_1 = Cadre(DIM_CADRE_ACTIONS, POS_CADRE_ACTS1, color=C.XLIGHT_BLUE) cadre_acts_2 = Cadre(DIM_CADRE_ACTIONS, POS_CADRE_ACTS2, color=C.XLIGHT_BLUE) text_username1 = TextBox(DIM_USER, POS_CADRE1 + POS_USER, text_color=Spec.DCOLOR_P1, font=Font.f(60))
## fr line ## DIM_FRL_CADRE = np.array([600, 80]) DIM_L_USERNAME = np.array([300, 60]) DIM_L_BUTTON = np.array([160, 40]) POS_L_BUTTON = np.array([340, 20]) DX_L = np.array([20, 0]) DY_L = np.array([0, 10]) ### Components ### img_question = pygame.image.load("imgs/question_mark.png") chat = Chat(POS_CHAT, general_chat=False) title = TextBox(Spec.DIM_TITLE, Spec.POS_TITLE, font=Font.f(80)) button_back = Button(Spec.DIM_MEDIUM_BUTTON, POS_BACK, color=C.LIGHT_BLUE, text="Back", font=Font.f(35)) cadre = Cadre(DIM_CADRE, POS_CADRE) form_ship = Cadre(DIM_SHIP, POS_SHIP) title_games = TextBox(DIM_CASE, POS_TABLE, text='Games', marge=True,
def __init__(self, client, game): self.client = client self.game = game self.username = None self.opponent = None self.version = TextBox((100, 60), (50, 1700), font=Font.f(30), text_color=C.GREY, text=str(Spec.JSON_DATA['version'])) pages = [(Spec.PAGE_MENU, Menu(client)), (Spec.PAGE_CONN, Connection(client)), (Spec.PAGE_FRIENDS, Friends(client)), (Spec.PAGE_EDITOR, Editor(client)), (Spec.PAGE_PROFIL, Profil(client)), (Spec.PAGE_OFFLINE, Offline(client))] super().__init__(pages) self.add_frame_function( self.look_friends, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function( self.look_demand_friends, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function( self.look_game_demands, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function( self.manage_notif, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function(self.look_comm_login, active_pages=Spec.PAGE_CONN) self.add_frame_function(self.look_general_chat_msg, active_pages=Spec.PAGE_MENU) self.add_frame_function( self.look_private_chat_msg, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS, Spec.PAGE_PROFIL]) self.add_frame_function( self.look_rdfr, active_pages=[Spec.PAGE_FRIENDS, Spec.PAGE_PROFIL]) self.add_frame_function(self.look_rdg, active_pages=[Spec.PAGE_FRIENDS]) self.add_frame_function(self.look_game_notif, active_pages=None) self.add_frame_function(self.look_profil_infos, active_pages=Spec.PAGE_FRIENDS) self.add_frame_function(self.manage_updater, active_pages=Spec.PAGE_MENU) # set log out logic page_menu = self.get_page(Spec.PAGE_MENU) page_menu.add_button_logic('b logout', self.logic_logout) # set game reference in offline page self.get_page(Spec.PAGE_OFFLINE).game = self.game # set up updater - start download self.updater = self.get_page(Spec.PAGE_MENU).get_component('updater') self.updater.load_server_data()
class App(Application): def __init__(self, client, game): self.client = client self.game = game self.username = None self.opponent = None self.version = TextBox((100, 60), (50, 1700), font=Font.f(30), text_color=C.GREY, text=str(Spec.JSON_DATA['version'])) pages = [(Spec.PAGE_MENU, Menu(client)), (Spec.PAGE_CONN, Connection(client)), (Spec.PAGE_FRIENDS, Friends(client)), (Spec.PAGE_EDITOR, Editor(client)), (Spec.PAGE_PROFIL, Profil(client)), (Spec.PAGE_OFFLINE, Offline(client))] super().__init__(pages) self.add_frame_function( self.look_friends, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function( self.look_demand_friends, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function( self.look_game_demands, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function( self.manage_notif, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS]) self.add_frame_function(self.look_comm_login, active_pages=Spec.PAGE_CONN) self.add_frame_function(self.look_general_chat_msg, active_pages=Spec.PAGE_MENU) self.add_frame_function( self.look_private_chat_msg, active_pages=[Spec.PAGE_MENU, Spec.PAGE_FRIENDS, Spec.PAGE_PROFIL]) self.add_frame_function( self.look_rdfr, active_pages=[Spec.PAGE_FRIENDS, Spec.PAGE_PROFIL]) self.add_frame_function(self.look_rdg, active_pages=[Spec.PAGE_FRIENDS]) self.add_frame_function(self.look_game_notif, active_pages=None) self.add_frame_function(self.look_profil_infos, active_pages=Spec.PAGE_FRIENDS) self.add_frame_function(self.manage_updater, active_pages=Spec.PAGE_MENU) # set log out logic page_menu = self.get_page(Spec.PAGE_MENU) page_menu.add_button_logic('b logout', self.logic_logout) # set game reference in offline page self.get_page(Spec.PAGE_OFFLINE).game = self.game # set up updater - start download self.updater = self.get_page(Spec.PAGE_MENU).get_component('updater') self.updater.load_server_data() def manage_updater(self): ''' Manage updater downloadings. ''' self.updater.run() # check if game can be launch if self.updater.ready: # set connection button page_menu = self.get_page(Spec.PAGE_MENU) page_menu.set_states_components('unlogged', 'b conn') # stop run this function, no need to self.set_frame_function_state(self.manage_updater, False) def manage_notif(self): ''' Manage the notifs of page Menu & page friends that indicates the number of active friend demands and unread messages. ''' page_fr = self.get_page(Spec.PAGE_FRIENDS) page_menu = self.get_page(Spec.PAGE_MENU) page_profil = self.get_page(Spec.PAGE_PROFIL) # update friends page notifs page_fr.update_notifs(page_profil.unreads) notif = page_menu.get_component('notif') n_notif = page_fr.get_n_requests() n_notif += page_profil.get_n_unreads() if n_notif == 0: page_menu.change_display_state('notif', False) else: page_menu.change_display_state('notif', True) notif.set_text(str(n_notif)) def logic_logout(self): ''' Function executed when button logout is pushed. ''' page_menu = self.get_page(Spec.PAGE_MENU) page_fr = self.get_page(Spec.PAGE_FRIENDS) page_conn = self.get_page(Spec.PAGE_CONN) page_profil = self.get_page(Spec.PAGE_PROFIL) self.client.send_logout() # store user infos on local Spec.store_local_profil(self.username, self.client) Spec.set_json_variable('active account', self.username) # stop game client self.game.game_client.stop() page_conn.change_state('base') page_menu.change_state('unlogged') page_menu.get_component('chat').reset() page_fr.reset() page_profil.reset() def look_comm_login(self): ''' Check if the server sent a login response ''' # look for login/signup response with self.client.get_data(['rlg', 'rsg']) as (rlg, rsg): if rlg == 1 or rsg == 1: # activate game client self.game.start_udp() # get username self.username = self.get_page(Spec.PAGE_CONN).username # set username attr in client self.client.username = self.username self.change_page(Spec.PAGE_MENU, state='logged') # manage profil infos on local self.get_page(Spec.PAGE_MENU).check_user_data() elif rlg == 0 or rsg == 0: # set error msg conn = self.get_page(Spec.PAGE_CONN) conn.set_negative_response() def look_general_chat_msg(self): ''' Check if the server sent a message on the general chat. ''' with self.client.get_data('gc') as contents: menu = self.get_page(Spec.PAGE_MENU) chat = menu.get_component('chat') for username, msg in contents: chat.add_msg(username, msg) def look_private_chat_msg(self): ''' Check if the server sent a message on the private chat. ''' with self.client.get_data('pc') as contents: profil = self.get_page(Spec.PAGE_PROFIL) for username, msg in contents: profil.add_message(username, msg) def look_rdfr(self): ''' Check if the server send a response on a friend demand. ''' with self.client.get_data('rdfr') as content: if content == None: return page = self.get_active_page() # check that page has set_rdfr method if hasattr(page, 'set_rdfr'): page.set_rdfr(content) def look_rdg(self): ''' Check if the server send a response on a friend demand. ''' with self.client.get_data('rdg') as content: if content == None: return page = self.get_active_page() # check that page has set_rdg method if hasattr(page, 'set_rdg'): page.set_rdg(content) def look_friends(self): ''' Check if the server sent info about friends ''' with self.client.get_data('frs') as contents: if contents == None: return page_fr = self.get_page(Spec.PAGE_FRIENDS) for username, connected in contents: page_fr.set_friend(username, connected) def look_demand_friends(self): ''' Check if the server sent a friend demand. ''' with self.client.get_data('dfr') as contents: page_fr = self.get_page(Spec.PAGE_FRIENDS) for username in contents: page_fr.set_demand_friend(username) def look_game_demands(self): ''' Check if the server sent a game demand. ''' with self.client.get_data('dg') as contents: page_fr = self.get_page(Spec.PAGE_FRIENDS) for username in contents: page_fr.set_game_demand(username) def look_game_notif(self): ''' Check if the user is entering in a game ''' # look for notification with self.client.get_data('ign') as contents: if contents == None: return username, team = contents self.in_game = True self.opponent = username # wait to be sure to receive every info time.sleep(.1) # if notified -> get opp's grid with self.client.get_data('igsh') as grid: opp_grid = grid own_grid = self.client.in_data['sh'] # load & store script script = self.client.in_data['sc'] with open('script.py', 'w') as file: file.write(script) # set game self.game.setup(int(team), own_grid, opp_grid, self.username, self.opponent) # go to the menu self.change_page(Spec.PAGE_MENU) # set menu's play button to normal state page_menu = self.get_page(Spec.PAGE_MENU) page_menu.reset_play() def look_profil_infos(self): ''' Check if receiving profil data. ''' with self.client.get_data('rpd') as content: if content == None: return page_profil = self.get_page(Spec.PAGE_PROFIL) page_profil.setup_page(**content) self.change_page(Spec.PAGE_PROFIL) def display(self): self.version.display() super().display()
from data.spec import Spec from lib.perfeval import Counter import numpy as np DIM_CADRE = np.array([600, 200]) POS_TEXT = np.array([100, 20]) DIM_INFO = np.array([560, 80]) POS_INFO = np.array([20, 100]) # components cadre = Cadre(DIM_CADRE, (0, 0)) text_state = TextBox(Spec.DIM_MEDIUM_TEXT, POS_TEXT, text='Looking for update...', font=Font.f(45), text_color=C.GREY) text_info = TextBox(DIM_INFO, POS_INFO, font=Font.f(35)) components = [('cadre', cadre), ('t state', text_state), ('t info', text_info)] states = ['base'] class Updater(SubPage): root_link = "https://raw.githubusercontent.com/Plouc314/CodeShip/master/CodeShip/" def __init__(self, pos):
from ui.script_analyser import ScriptAnalyser from ui.ship_editor import ShipEditor from data.spec import Spec import numpy as np ### Components ### Y_TB = 100 X_TB1 = 100 POS_SHIP_EDITOR = np.array([300, 400]) POS_SCRIPT = np.array([1920, 400]) ### base ### title = TextBox(Spec.DIM_TITLE, Spec.POS_TITLE, text="Editor", font=Font.f(80)) button_back = Button(Spec.DIM_MEDIUM_BUTTON, (X_TB1, Y_TB), color=C.LIGHT_BLUE, text="Back", font=Font.f(35)) script_analyser = ScriptAnalyser(POS_SCRIPT) ship_editor = ShipEditor(POS_SHIP_EDITOR) states = ['base'] components = [('title', title), ('b back', button_back), ('script analyser', script_analyser), ('ship editor', ship_editor)]