1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | #-*-coding:utf-8-*- #-*-coding:cp949-*- from socket import * from thread import * from os import system import sys import time threadleft=0 lock=allocate_lock() class person: ip="none" def __init__(self,x,y): self.x = x self.y = y def add_user(self,ip): if self.ip == "none": self.ip = ip print "Add %s" %ip else: # Already set print "Already setting!\n%s try hello to %s" %(ip,self.ip) def remove_user(self,ip): if self.ip == "none": print "Not setting ip yet!" elif self.ip == ip: print "Remove %s" %self.ip self.ip = "none" else: print "%s try remove %s" %(ip,self.ip) def move(self,direction): # remove enter if "\n" == direction[len(direction)-1:]: direction = direction[:len(direction)-1] collision_detect(self.x,self.y,direction) if direction == "up": self.y=self.y-1 elif direction == "right": self.x=self.x+1 elif direction == "down": self.y=self.y+1 elif direction == "left": self.x=self.x-1 else: print "Invaild Direction" def erase_user(id_num): user[id_num].x=10 user[id_num].y=10 def collision_detect(x,y,direction): if direction == "up": result=check_user(x,y-1) elif direction == "right": result=check_user(x+1,y) elif direction == "down": result=check_user(x,y+1) elif direction == "left": result=check_user(x-1,y) if 6 != result: # 이동하려는 위치에 유저가 있으면 erase_user(result) # 유저를 지워버림. def id_character(id_num): mapping = { 0:"A", 1:"B", 2:"C", 3:"D", 4:"E", 5:"F", 6:"-" } return mapping.get(id_num) def character_id(id_char): mapping = { 'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5, '-':6 } return mapping.get(id_char) def check_user(x,y): for i in range(0,6): if(user[i].x == x and user[i].y == y): return i return 6 def draw_map(s): #for linux clear #system("clear") #for window cls system("cls") for i in range(0,10): for j in range(0,10): sys.stdout.write(id_character(check_user(j,i))) s.send(id_character(check_user(j,i))) sys.stdout.write(" ") s.send(" ") continue print "" s.send("\n") def make_user(): user = [0]*6 for i in range(0,6): user[i] = person(0,0) return user def where_user(): for i in range(0,6): print "x=" + str(user[i].x) + " y=" + str(user[i].y) def set_locate(): for i in range(0,3): user[i].y=i user[i+3].y=i for i in range(3,6): user[i].x=9 def which_command(command): # remove \n if "\n" == command[len(command)-1:]: command=command[:len(command)-1] # A right try: command=command.split(" ")[1] except: print "Invaild Command!\n" # right if command=="HELLO": print "HELLO" return 0 elif command=="BYE": print "BYE" return 1 elif command=="right" or command=="down" or command=="up" or command=="left": print command return 2 elif command=="exit": print "exit" return 4 else: # Invaild command return 3 def player_move(s,command,ip): id_char,direction=command.split(" ") id_num=character_id(id_char) # check HELLO ip and CLIENT ip if user[id_num].ip == ip: user[id_num].move(direction) draw_map(s) where_user() else: # not same ip print "Not Same IP!" s.send("Not Same IP!\n") def understand_command(s,command,ip): result = which_command(command) if result is 0: # A HELLO id_num=character_id(command.split(" ")[0]) if type(id_num) == type(None): s.send("Invaild Command!\n") print "Invaild Command!" return 0 user[id_num].add_user(ip) elif result is 1: # A BYE id_num=character_id(command.split(" ")[0]) user[id_num].remove_user(ip) elif result is 2: # A right id_num=character_id(command.split(" ")[0]) player_move(s,command,ip) elif result is 3: # A dfsd s.send("Invaild Command!") elif result is 4: # exit s.send("Exit Server") s.close() exit() return 0 def client_recv_move(conn,addr): while 1: conn.send("I'm waiting your opinion\n") try: data=conn.recv(1024) except: conn.close() break if not data: conn.close() break understand_command(conn,data,addr[0]) if __name__ == '__main__': # make user global global user # make user to object user=make_user() # set user x,y set_locate() # print user's x,y # where_user(user) # make socket HOST="192.168.1.35" PORT=10103 BUFSIZE = 1024 ADDR = (HOST,PORT) s=socket(AF_INET,SOCK_STREAM) s.bind(ADDR) s.listen(2) # Get User Command (who,where) with thread. while 1: conn,addr = s.accept() start_new_thread(client_recv_move,(conn,addr)) # print map draw_map(conn) """ 소켓으로 입력대기를 받음 HELLO 가 날아오면 무작위 포트로 소켓을 만들고 그 포트번호로 클라이언트를 다시 연결하게끔 해야 할듯. (다른 유저가 무단으로 조작하지 못하게) 유저 입력 후 캐릭터 이동은 쓰레드를 사용해서 처리. 동시에 이동하면 오류가 일어날지도 모르니 좌표 수정때는 플래그 변수를 하나 둬서 동시 수정을 막음. """ | cs |
'Python' 카테고리의 다른 글
특정 프로세스의 패킷만 캡쳐하기 (0) | 2015.09.17 |
---|---|
스택 with Linked List (0) | 2015.05.08 |
쓰레드를 이용한 다중 클라이언트 소켓 (0) | 2015.05.05 |
(Fail) 쓰레드를 이용한 소켓 (0) | 2015.05.04 |
scapy 를 이용한 ARP 패킷 (0) | 2015.05.02 |