PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/freeciv/client/__init__.py

https://bitbucket.org/zielmicha/freeciv-android
Python | 295 lines | 214 code | 69 blank | 12 comment | 20 complexity | 97ad419a539a3cbb530f2c53878ef126 MD5 | raw file
  1. # Copyright (C) 2011 Michal Zielinski (michal@zielinscy.org.pl)
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2, or (at your option)
  6. # any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. import select
  13. import time
  14. import graphics
  15. import freeciv.client
  16. from freeciv import progress
  17. from freeciv import monitor
  18. from freeciv import features
  19. from freeciv.client import _freeciv as freeciv
  20. import dialogs
  21. import common
  22. import window
  23. import actions
  24. import diplomacy
  25. import city
  26. import key
  27. import misc
  28. net_socket = -1
  29. client = None
  30. main = None
  31. features.add_feature('debug.outwindow', default=False, type=bool)
  32. class ConnectionError(Exception):
  33. pass
  34. @freeciv.register
  35. def ui_main():
  36. freeciv.func.init_things()
  37. freeciv.func.init_mapcanvas_and_overview()
  38. main()
  39. def tick():
  40. pass
  41. @freeciv.register
  42. def ui_init():
  43. common.init()
  44. @freeciv.register
  45. def add_net_input(sockid):
  46. global net_socket
  47. net_socket = sockid
  48. @freeciv.register
  49. def remove_net_input():
  50. global net_socket
  51. net_socket = -1
  52. @freeciv.register
  53. def real_output_window_append(text, tags, connid):
  54. if client:
  55. client.console_line(text)
  56. else:
  57. print 'outwindow: %s\n' % text,
  58. @freeciv.register
  59. def real_meswin_dialog_update():
  60. count = freeciv.func.meswin_get_num_messages()
  61. client.update_meswin([ freeciv.func.meswin_get_message(i) for i in xrange(count) ])
  62. @freeciv.register
  63. def real_economy_report_dialog_update():
  64. client.update_taxes()
  65. @freeciv.register
  66. def update_turn_done_button(restore):
  67. client._set_turn_button_state(restore)
  68. @freeciv.register
  69. def handle_authentication_req(type, prompt):
  70. client.handle_authentication_req(prompt)
  71. @freeciv.register
  72. def popup_notify_goto_dialog(title, text, a, b):
  73. client.popup_notify(text)
  74. class Client(object):
  75. def __init__(self, no_quit=False):
  76. global client
  77. client = self
  78. self.no_quit = no_quit
  79. self.next_time = time.time()
  80. self.cursor_pos = (0, 0)
  81. self.draw_patrol_lines = False
  82. self.out_window_callback = None
  83. self.turn_button_flip = False
  84. self.meetings = {}
  85. def tick(self):
  86. if self.next_time >= time.time():
  87. sleep_time = freeciv.func.real_timer_callback()
  88. self.next_time = time.time() + sleep_time
  89. freeciv.func.call_idle_callbacks()
  90. if net_socket != -1:
  91. r, w, x = select.select([net_socket], [], [], 0.01)
  92. if r:
  93. freeciv.func.input_from_server(net_socket)
  94. else:
  95. time.sleep(0.03)
  96. #window.draw_cursor(cursor_pos)
  97. def console_line(self, text):
  98. if self.out_window_callback:
  99. self.out_window_callback(text)
  100. monitor.log('outwindow', text)
  101. if features.get('debug.outwindow'):
  102. print '[OutWindow]', text
  103. def end_turn(self):
  104. freeciv.func.key_end_turn()
  105. def chat(self, text):
  106. freeciv.func.send_chat(text)
  107. def draw_map(self, surf, pos):
  108. mapview = freeciv.func.get_mapview_store()
  109. surf.blit(mapview, pos)
  110. def draw_overview(self, surf, pos, scale):
  111. dest_surf = window.overview_surface.scale(scale)
  112. surf.blit(dest_surf, pos)
  113. surf.draw_rect((255, 255, 255), pos + scale, 1)
  114. def overview_click(self, x, y):
  115. freeciv.func.py_overview_click(x, y)
  116. def set_map_size(self, size):
  117. freeciv.func.map_canvas_resized(*size)
  118. def update_map_canvas_visible(self):
  119. freeciv.func.update_map_canvas_visible()
  120. def get_overview_size(self):
  121. return window.overview_surface.get_size()
  122. def mouse_motion(self, pos):
  123. window.mouse_pos = self.cursor_pos = pos
  124. x, y = pos
  125. freeciv.func.update_line(x, y)
  126. freeciv.func.control_mouse_cursor_pos(x, y)
  127. def popup_unit_select_dialog(self, units):
  128. print 'unit_select_dialog', units
  129. def key_event(self, type, keycode):
  130. key.key(type, keycode)
  131. def update_meswin(self, lines):
  132. print 'meswin updated'
  133. print '\t' + '\n\t'.join(map(repr, lines))
  134. def connect_to_server(self, username, host, port):
  135. bufflen = 512
  136. buff = ' ' * bufflen
  137. result = freeciv.func.connect_to_server(username, host, port, buff, bufflen)
  138. buff = buff.rstrip(' ').rstrip('\0')
  139. if result == -1:
  140. raise ConnectionError(buff)
  141. def escape(self):
  142. if self.draw_patrol_lines:
  143. self.key_event(graphics.const.KEYDOWN, graphics.const.K_ESCAPE)
  144. self.draw_patrol_lines = False
  145. else:
  146. self.quit()
  147. def disable_menus(self):
  148. print 'disable_menus'
  149. def get_unit_in_focus(self):
  150. units = freeciv.func.get_units_in_focus()
  151. if units:
  152. return actions.Unit(units[0])
  153. else:
  154. return None
  155. def disconnect(self):
  156. if not self.no_quit:
  157. self.chat('/quit')
  158. freeciv.func.disconnect_from_server()
  159. global client
  160. client = None
  161. def get_tax_values(self):
  162. lux = freeciv.func.get_tax_value(True)
  163. science = freeciv.func.get_tax_value(False)
  164. tax = 100 - lux - science
  165. return tax, lux, science
  166. def set_tax_values(self, tax, lux, science):
  167. freeciv.func.set_tax_values(tax, lux, science)
  168. def get_gold(self):
  169. return freeciv.func.get_gold_amount()
  170. def get_gold_income(self):
  171. return freeciv.func.get_gold_income()
  172. def get_current_tech(self):
  173. return freeciv.func.get_current_tech()
  174. def get_techs(self, level=11):
  175. return map(Tech, freeciv.func.get_techs(level))
  176. def get_current_year_name(self):
  177. return freeciv.func.get_current_year_name()
  178. def get_governments(self):
  179. return map(Gov, freeciv.func.get_governments())
  180. def get_cities(self):
  181. return map(city.City, freeciv.func.get_cities())
  182. def _set_turn_button_state(self, restore):
  183. if not freeciv.func.get_turn_done_button_state():
  184. return
  185. if (restore and self.turn_button_flip) or not restore:
  186. self.turn_button_flip = not self.turn_button_flip
  187. self.set_turn_button_state(not self.turn_button_flip)
  188. def set_turn_button_state(self, enabled):
  189. pass
  190. def authenticate(self, password):
  191. freeciv.func.authenticate(password)
  192. def get_players(self):
  193. return diplomacy.get_players()
  194. def get_player_with_id(self, id):
  195. return diplomacy.Player(freeciv.func.player_by_number(id))
  196. def get_playing(self):
  197. return diplomacy.Player(freeciv.func.get_playing())
  198. def toggle_full_labels(self):
  199. freeciv.func.request_toggle_city_full_bar()
  200. def get_caravan_options(self, unit, homecity, destcity):
  201. can_establish, can_trade, can_wonder = \
  202. freeciv.func.py_get_caravan_options(unit.handle, homecity.handle, destcity.handle)
  203. return can_establish, can_trade, can_wonder
  204. class Gov(object):
  205. def __init__(self, (index, name, changable)):
  206. self.name = name
  207. self.index = index
  208. self.changable = changable
  209. def change_to(self):
  210. freeciv.func.change_government(self.index)
  211. class Tech(object):
  212. def __init__(self, (index, name, steps)):
  213. self.index = index
  214. self.name = name
  215. self.steps = steps
  216. def set_as_goal(self):
  217. freeciv.func.set_tech_goal(self.index)
  218. def set_as_current(self):
  219. freeciv.func.set_tech_research(self.index)
  220. def get_nations():
  221. return [
  222. (freeciv.func.get_name_of_nation_id(i),
  223. freeciv.func.city_style_of_nation_id(i), i)
  224. for i in xrange(freeciv.func.get_playable_nation_count()) ]
  225. def get_nation_name(i):
  226. return freeciv.func.get_name_of_nation_id(i)